-
Notifications
You must be signed in to change notification settings - Fork 0
/
newsplinemod.f90
1236 lines (796 loc) · 30.5 KB
/
newsplinemod.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module newsplinemod
! This is newspline, a fast, means-preserving spline for interval data
! Leo O Lai and Jed O. Kaplan
! 2021
use parametersmod, only : i4,sp
use utilitiesmod, only : matsol,findloc
implicit none
public :: newspline ! Newspline subroutine with all optional bounded adjustment schemes (version 2.0)
private :: alim_adjust ! Bounded interpolation adjustment scheme for absolute limit (i.e., bound tolerance)
private :: plim_adjust ! Bounded interpolation adjustmnet scheme for percentage limit (relative to each interval individually)
private :: ulim_adjust ! Bounded interpolation adjustmnet scheme for maximum limit
private :: llim_adjust ! Bounded interpolation adjustmnet scheme for minimum limit
contains
!------------------------------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------------------------------
subroutine newspline(monthdata,nk,bcond,daydata,alim,llim,ulim,plim)
implicit none
! Input variables
real(sp), dimension(:), intent(in) :: monthdata ! Array of (monthly) interval data
integer(i4), dimension(:), intent(in) :: nk ! Array of number of small time-steps for each interval (can be variable)
real(sp), dimension(2), intent(in) :: bcond ! Boundary condition array, bcond(1) = bcond of first interval and bcond(2) = bcond of last interval
real(sp), dimension(:), intent(out) :: daydata ! Array of interpolated values (dimension must be equal to sum of nk)
real(sp), optional , intent(in) :: alim ! OPTIONAL :: Absolute limit for bounded interpolation
real(sp), optional , intent(in) :: llim ! OPTIONAL :: Minimum limit for bounded interpolation
real(sp), optional , intent(in) :: ulim ! OPTIONAL :: Maximum limit for bounded interpolation
real(sp), optional , intent(in) :: plim ! OPTIONAL :: Percetnage limit for bounded interpolation
! Local variables for wall control points
real(sp), dimension(:), allocatable :: wcp
! Local variables for linear system of mid control adjustments
real(sp), dimension(:,:), allocatable :: mat
real(sp), dimension(:), allocatable :: solution
! Final vector of all control points
real(sp), dimension(:), allocatable :: all_cont
! Local variables for generating daily values
real(sp), dimension(:), allocatable :: m_cont
! Hermite cubic and quartic spline basis functions
real(sp) :: H_00, H_10, H_01, H_11
real(sp) :: G_00, G_10, G_01, G_11
real(sp) :: u
real(sp) :: del
integer :: len
integer :: len_cont
integer :: i
integer :: j
integer :: k
integer :: n
!-------------------------------------------------------------------------------
! Start of the spline routine
len = size(monthdata)
!-------------------------------------------------------------------------------
! Approximate first wall control points using linear interpolation by connecting yi with yi+1
! and calculating the interception with interval wall
!
! New possible simplified method upon revision by Leo O Lai (Sep 2021)
! Since we assume the interval width to be arbitary 1 unit, the wall-CPs are simply the average of yi and yi+1
! First and last interval wall-CP (i.e. wcp(1) and wcp(N+1)) are determined by user specified boundary conditions
allocate(wcp(len+1))
wcp(1) = (monthdata(1) + bcond(1)) / 2.
wcp(len+1) = (monthdata(len) + bcond(2)) / 2.
do i = 2, len
wcp(i) = (monthdata(i-1) + monthdata(i)) / 2
end do
!-------------------------------------------------------------------------------
! Generate matrix for solution to mid-control points
allocate(mat(len,len))
allocate(solution(len))
! ====================
! Version 3 amendements - fixed G_00 integral u^4 term sign from '-' to '+'
! ====================
u = 1.
G_00 = u - (u**3) + (u**4) / 2.
G_10 = (u**2) * (3.*(u**2) - 8.*u + 6.) / 12.
G_01 = (u**3) * (1. - u / 2.)
G_11 = (u**3) * (3.*u - 4.) / 12.
!---
mat = 0.
! Construct matrix coefficients and solution array
! ====================
! Version 3 amendements - elimited '+ 1.' terms in tridiagonal coefficients and solution array after new equation derivation
! ====================
! Consider two "buffer mid-CPs" outside of first and last interval
mat(1,1) = 0.5 * G_10 + G_01 + G_00 - 0.5 * G_11
mat(1,2) = 0.5 * G_11
mat(len,len-1) = -0.5 * G_10
mat(len,len) = 0.5 * G_10 + G_01 + G_00 - 0.5 * G_11
n = 1
do i = 2, (len-1)
mat(i,n) = -0.5 * G_10
mat(i,n+1) = G_00 + G_01 + 0.5 * G_10 - 0.5 * G_11
mat(i,n+2) = 0.5 * G_11
n = n + 1
end do
!---
solution(1) = 2 * monthdata(1) - &
(G_00 - 0.5 * G_10 - 0.5 * G_11) * wcp(1) - &
(G_01 + 0.5 * G_10 + 0.5 * G_11) * wcp(2)
solution(1) = solution(1) + 0.5 * G_10 * bcond(1)
solution(len) = 2 * monthdata(len) - &
(G_00 - 0.5 * G_10 - 0.5 * G_11) * wcp(len) - &
(G_01 + 0.5 * G_10 + 0.5 * G_11) * wcp(len+1)
solution(len) = solution(len) - 0.5 * G_11 * bcond(2)
do i = 2, (len-1)
solution(i) = 2 * monthdata(i) - &
(G_00 - 0.5 * G_10 - 0.5 * G_11) * wcp(i) - &
(G_01 + 0.5 * G_10 + 0.5 * G_11) * wcp(i+1)
end do
!-------------------------------------------------------------------------------
! Solve linear system to get final mid control points
call matsol(mat, solution)
!-------------------------------------------------------------------------------
! Compile wall control with newly adjusted mid control points (all_cont)
allocate(all_cont(size(wcp)+size(solution)))
n = 1
do i = 1, len
all_cont(n) = wcp(i)
all_cont(n+1) = solution(i)
n = n + 2
end do
all_cont(2*len+1) = wcp(len+1)
!-------------------------------------------------------------------------------
! Construct the spline for daily values based on all_cont
len_cont = size(all_cont)
!---
! Calculate the slope for each control point
allocate(m_cont(len_cont))
m_cont(1) = all_cont(2) - bcond(1)
m_cont(len_cont) = bcond(2) - all_cont(len_cont-1)
do i = 2, len_cont-1
m_cont(i) = all_cont(i+1) - all_cont(i-1)
end do
!-------------------------------------------------------------------------------
! Find discrete daily values on the continuous function (i.e., series of u values in Hermite functions)
! ====================
! Version 3 amendements - include delta = 1/2 here instead of m_cont above to be consistent with equation in manuscript
! ====================
del = 0.5
n = 1
k = 1
do i = 1, len ! Outer loop start, for all monthly intervals N
if(mod(nk(i),2) == 0) then ! Seperate into even or odd months, starting with EVEN
u = 1. / nk(i)
do j = 1, (nk(i) / 2)
H_00 = 1. + (u**2) * (2*u - 3)
H_01 = (u**2) * (3 - 2*u)
H_10 = u * (u - 1) * (u - 1)
H_11 = (u**2) * (u - 1)
daydata(n) = all_cont(k) * H_00 + del * m_cont(k) * H_10 + all_cont(k+1) * H_01 + del * m_cont(k+1) * H_11
u = u + (2. / nk(i))
n = n + 1
end do
!---
u = 1. / nk(i)
do j = 1, (nk(i) / 2)
H_00 = 1. + (u**2) * (2*u - 3)
H_01 = (u**2) * (3 - 2*u)
H_10 = u * (u - 1) * (u - 1)
H_11 = (u**2) * (u - 1)
daydata(n) = all_cont(k+1) * H_00 + del * m_cont(k+1) * H_10 + all_cont(k+2) * H_01 + del * m_cont(k+2) * H_11
u = u + (2. / nk(i))
n = n + 1
end do
else ! if odd months (or odd number of smaller time step)
u = 1. / nk(i)
do j = 1, ((nk(i)+1) / 2)
H_00 = 1. + (u**2) * (2*u - 3)
H_01 = (u**2) * (3 - 2*u)
H_10 = u * (u - 1) * (u - 1)
H_11 = (u**2) * (u - 1)
daydata(n) = all_cont(k) * H_00 + del * m_cont(k) * H_10 + all_cont(k+1) * H_01 + del * m_cont(k+1) * H_11
u = u + (2. / nk(i))
n = n + 1
end do
!---
u = 2. / nk(i)
do j = 1, ((nk(i)-1) / 2)
H_00 = 1. + (u**2) * (2*u - 3)
H_01 = (u**2) * (3 - 2*u)
H_10 = u * (u - 1) * (u - 1)
H_11 = (u**2) * (u - 1)
daydata(n) = all_cont(k+1) * H_00 + del * m_cont(k+1) * H_10 + all_cont(k+2) * H_01 + del * m_cont(k+2) * H_11
u = u + (2. / nk(i))
n = n + 1
end do
end if
k = k + 2
end do ! End of outer loop
!-------------------------------------------------------------------------------
! Call bounded interpolation adjustment scheme if optional arguments are present
if (present(llim)) call llim_adjust(llim,monthdata,nk,bcond,all_cont,daydata)
if (present(ulim)) call ulim_adjust(ulim,monthdata,nk,bcond,all_cont,daydata)
if (present(alim)) call alim_adjust(alim,monthdata,nk,bcond,all_cont,daydata)
if (present(plim)) call plim_adjust(plim,monthdata,nk,bcond,all_cont,daydata)
end subroutine newspline
!------------------------------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------------------------------
subroutine alim_adjust(alim,monthdata,nk,bcond,all_cont,daydata)
real(sp), intent(in) :: alim ! Absolute limit (e.g., no interpolated value can exceed +/- 0.05 of original interval mean input)
real(sp), dimension(:), intent(in) :: monthdata ! Array of monthly (interval) input data
integer(i4), dimension(:), intent(in) :: nk ! Array of number of small time-steps for each interval (can be variable)
real(sp), dimension(:), intent(in) :: bcond ! Boundary condition array
real(sp), dimension(:), intent(in) :: all_cont ! Array of all control points (wall-CPs and mid-CPs)
real(sp), dimension(:), intent(inout) :: daydata ! Array of daily intepolated values
real(sp), allocatable, dimension(:) :: d_orig ! Slope direction of the current interval (1 = positive, -1 = negative, 0 = local maxima/minima)
logical, allocatable, dimension(:) :: osc_check ! TRUE if interval require adjustment (dim = input data length)
real(sp), allocatable, dimension(:) :: c2 ! Array to store the amount of adjustment required
real(sp), allocatable, dimension(:) :: c_mon ! Array to store current month (or interval) of values for bounded adjustment (dim = day in month)
real(sp) :: perc
real(sp) :: int_n
real(sp) :: int_nm1
real(sp) :: int_np1
real(sp) :: sip12
integer :: len
integer :: i
integer :: j
integer :: k
integer :: srt
integer :: end
!-----
len = size(monthdata)
allocate(d_orig(len))
allocate(osc_check(len))
allocate(c2(len))
d_orig = -9999.
osc_check = .FALSE.
c2 = -9999.
!------
! Assign -1 for negative slope, 1 for postive and 0 for turning point
do i = 1, len
! Assign intervals values to local variables
int_n = monthdata(i) ! Current interval
if (i == 1) then ! If first interval, apply bcond(1)
int_nm1 = bcond(1)
int_np1 = monthdata(i+1)
else if (i == len) then ! If last interval, apply bcond(2)
int_nm1 = monthdata(i-1)
int_np1 = bcond(2)
else
int_nm1 = monthdata(i-1)
int_np1 = monthdata(i+1)
end if
! Determine the slope direction of the current interval relative to previous and next interval
if (int_np1 - int_n < 0) then
d_orig(i) = -1
else if (int_np1 - int_n > 0) then
d_orig(i) = 1
end if
!---
if (int_n > int_nm1 .and. int_n > int_np1) then
d_orig(i) = 0
else if (int_n < int_nm1 .and. int_n < int_np1) then
d_orig(i) = 0
end if
end do
!------
! Assign TRUE if oscillation of turning point exceeds the predetermined threshold
do i = 1, len
! Assign intervals values to variables
int_n = monthdata(i) ! Current interval
sip12 = all_cont(2*i) ! Position of interval mid-control point (i.e. si+1/2)
if (d_orig(i) == 0) then
if (sip12 > int_n + alim .OR. sip12 < int_n - alim) then
osc_check(i) = .TRUE.
end if
else if (d_orig(i) == 0 .and. int_n < 0) then
if (sip12 < (1.0+perc) * int_n .OR. sip12 > (1.0-perc) * int_n) then
osc_check(i) = .TRUE.
end if
end if
end do
!------
! Calculate the amount of adjustment required and insert into the c2 variable
c2 = 0.
do i = 1, len
! Assign intervals values to local variables
int_n = monthdata(i) ! Current interval
if (i == 1) then ! If first interval, apply bcond(1)
int_nm1 = bcond(1)
int_np1 = monthdata(i+1)
else if (i == len) then ! If last interval, apply bcond(2)
int_nm1 = monthdata(i-1)
int_np1 = bcond(2)
else
int_nm1 = monthdata(i-1)
int_np1 = monthdata(i+1)
end if
!---
if (osc_check(i) .and. int_n > 0) then
!---
if (int_n > int_nm1 .and. int_n > int_np1) then
c2(i) = alim
else if (int_n < int_nm1 .and. int_n < int_np1) then
c2(i) = -alim
end if
!---
else if (osc_check(i) .and. int_n < 0) then
!---
if (int_n > int_nm1 .and. int_n > int_np1) then
c2(i) = alim
else if (int_n < int_nm1 .and. int_n < int_np1) then
c2(i) = -alim
end if
!---
end if
end do
!------
! Construct the spline for daily values based on all_cont
do i = 1, len
if (osc_check(i)) then
! Assign intervals values to local variables
int_n = monthdata(i) ! Current interval
if (i == 1) then ! If first interval, apply bcond(1)
int_nm1 = bcond(1)
int_np1 = monthdata(i+1)
else if (i == len) then ! If last interval, apply bcond(2)
int_nm1 = monthdata(i-1)
int_np1 = bcond(2)
else
int_nm1 = monthdata(i-1)
int_np1 = monthdata(i+1)
end if
!---
if (i == 1) then
srt = 1
end = srt + nk(1) - 1
else
srt = sum(nk(1:(i-1))) + 1
end = srt + nk(i) - 1
end if
!---
! Create seperate array of all the daily values within the interval being adjusted
allocate(c_mon(nk(i)+2))
if (i == 1) then
c_mon(1) = all_cont(1)
c_mon(2:nk(i)+1) = int_n + c2(i)
c_mon(nk(i)+2) = daydata(end+1)
else if (i == len) then
c_mon(1) = daydata(srt-1)
c_mon(2:nk(i)+1) = int_n + c2(i)
c_mon(nk(i)+2) = all_cont(2*len+1)
else
c_mon(1) = daydata(srt-1)
c_mon(2:nk(i)+1) = int_n + c2(i)
c_mon(nk(i)+2) = daydata(end+1)
end if
!---
do j = 1, 1000
if ((int_n > int_nm1 .and. int_n > int_np1) .and. &
sum(c_mon(2:nk(i)+1)) / nk(i) - int_n < 0.01) exit
if ((int_n < int_nm1 .and. int_n < int_np1) .and. &
sum(c_mon(2:nk(i)+1)) / nk(i) - int_n > 0.01) exit
do k = 2, nk(i)+1
c_mon(k) = (c_mon(k-1) + c_mon(k) + c_mon(k+1)) / 3.
end do
end do
!---
daydata(srt:end) = c_mon(2:nk(i)+1)
deallocate(c_mon)
end if
end do
! n_adjust = count(osc_check)
end subroutine alim_adjust
!------------------------------------------------------------------------------------------------------------------
subroutine plim_adjust(plim,monthdata,nk,bcond,all_cont,daydata)
real(sp), intent(in) :: plim ! Percentage limit (e.g. no interpolated value can exceed 5% of original interval mean input)
real(sp), dimension(:), intent(in) :: monthdata ! Array of monthly (interval) input data
integer(i4), dimension(:), intent(in) :: nk ! Array of number of small time-steps for each interval (can be variable)
real(sp), dimension(:), intent(in) :: bcond ! Boundary condition array
real(sp), dimension(:), intent(in) :: all_cont ! Array of all control points (wall-CPs and mid-CPs)
real(sp), dimension(:), intent(inout) :: daydata ! Array of daily intepolated values
real(sp), allocatable, dimension(:) :: d_orig ! Slope direction of the current interval (1 = positive, -1 = negative, 0 = local maxima/minima)
logical, allocatable, dimension(:) :: osc_check ! TRUE if interval require adjustment (dim = input data length)
real(sp), allocatable, dimension(:) :: c2 ! Array to store the amount of adjustment required
real(sp), allocatable, dimension(:) :: c_mon ! Array to store current month (or interval) of values for bounded adjustment (dim = day in month)
real(sp) :: perc
real(sp) :: int_n
real(sp) :: int_nm1
real(sp) :: int_np1
real(sp) :: sip12
integer :: len
integer :: i
integer :: j
integer :: k
integer :: srt
integer :: end
!-----
len = size(monthdata)
allocate(d_orig(len))
allocate(osc_check(len))
allocate(c2(len))
d_orig = -9999.
osc_check = .FALSE.
c2 = -9999.
!------
! Assign -1 for negative slope, 1 for postive and 0 for turning point
do i = 1, len
! Assign intervals values to local variables
int_n = monthdata(i) ! Current interval
if (i == 1) then ! If first interval, apply bcond(1)
int_nm1 = bcond(1)
int_np1 = monthdata(i+1)
else if (i == len) then ! If last interval, apply bcond(2)
int_nm1 = monthdata(i-1)
int_np1 = bcond(2)
else
int_nm1 = monthdata(i-1)
int_np1 = monthdata(i+1)
end if
! Determine the slope direction of the current interval relative to previous and next interval
if (int_np1 - int_n < 0) then
d_orig(i) = -1
else if (int_np1 - int_n > 0) then
d_orig(i) = 1
end if
!---
if (int_n > int_nm1 .and. int_n > int_np1) then
d_orig(i) = 0
else if (int_n < int_nm1 .and. int_n < int_np1) then
d_orig(i) = 0
end if
end do
!------
! Assign TRUE if oscillation of turning point exceeds the predetermined threshold
perc = plim / 100.
do i = 1, len
! Assign intervals values to variables
int_n = monthdata(i) ! Current interval
sip12 = all_cont(2*i) ! Position of interval mid-control point (i.e. si+1/2)
!---
if (d_orig(i) == 0 .and. monthdata(i) > 0) then
if (sip12 > (1.0+perc) * int_n .OR. sip12 < (1.0-perc) * int_n) then
osc_check(i) = .TRUE.
end if
else if (d_orig(i) == 0 .and. int_n < 0) then
if (sip12 < (1.0+perc) * int_n .OR. sip12 > (1.0-perc) * int_n) then
osc_check(i) = .TRUE.
end if
end if
end do
!------
! Calculate the amount of adjustment required and insert into the c2 variable
c2 = 0.
do i = 1, len
! Assign intervals values to local variables
int_n = monthdata(i) ! Current interval
if (i == 1) then ! If first interval, apply bcond(1)
int_nm1 = bcond(1)
int_np1 = monthdata(i+1)
else if (i == len) then ! If last interval, apply bcond(2)
int_nm1 = monthdata(i-1)
int_np1 = bcond(2)
else
int_nm1 = monthdata(i-1)
int_np1 = monthdata(i+1)
end if
!---
if (osc_check(i) .and. int_n > 0) then
!---
if (int_n > int_nm1 .and. int_n > int_np1) then
c2(i) = perc * int_n
else if (int_n < int_nm1 .and. int_n < int_np1) then
c2(i) = -perc * int_n
end if
!---
else if (osc_check(i) .and. int_n < 0) then
!---
if (int_n > int_nm1 .and. int_n > int_np1) then
c2(i) = -perc * int_n
else if (int_n < int_nm1 .and. int_n < int_np1) then
c2(i) = perc * int_n
end if
!---
end if
end do
!------
! Construct the spline for daily values based on all_cont
do i = 1, len
if (osc_check(i)) then
! Assign intervals values to local variables
int_n = monthdata(i) ! Current interval
if (i == 1) then ! If first interval, apply bcond(1)
int_nm1 = bcond(1)
int_np1 = monthdata(i+1)
else if (i == len) then ! If last interval, apply bcond(2)
int_nm1 = monthdata(i-1)
int_np1 = bcond(2)
else
int_nm1 = monthdata(i-1)
int_np1 = monthdata(i+1)
end if
!---
if (i == 1) then
srt = 1
end = srt + nk(1) - 1
else
srt = sum(nk(1:(i-1))) + 1
end = srt + nk(i) - 1
end if
!---
! Create seperate array of all the daily values within the interval being adjusted
allocate(c_mon(nk(i)+2))
if (i == 1) then
c_mon(1) = all_cont(1)
c_mon(2:nk(i)+1) = int_n + c2(i)
c_mon(nk(i)+2) = daydata(end+1)
else if (i == len) then
c_mon(1) = daydata(srt-1)
c_mon(2:nk(i)+1) = int_n + c2(i)
c_mon(nk(i)+2) = all_cont(2*len+1)
else
c_mon(1) = daydata(srt-1)
c_mon(2:nk(i)+1) = int_n + c2(i)
c_mon(nk(i)+2) = daydata(end+1)
end if
!---
do j = 1, 1000
if ((int_n > int_nm1 .and. int_n > int_np1) .and. &
sum(c_mon(2:nk(i)+1)) / nk(i) - int_n < 0.01) exit
if ((int_n < int_nm1 .and. int_n < int_np1) .and. &
sum(c_mon(2:nk(i)+1)) / nk(i) - int_n > 0.01) exit
do k = 2, nk(i)+1
c_mon(k) = (c_mon(k-1) + c_mon(k) + c_mon(k+1)) / 3.
end do
end do
!---
daydata(srt:end) = c_mon(2:nk(i)+1)
deallocate(c_mon)
end if
end do
end subroutine plim_adjust
!------------------------------------------------------------------------------------------------------------------
subroutine ulim_adjust(ulim,monthdata,nk,bcond,all_cont,daydata)
real(sp), intent(in) :: ulim ! Maximum limit (e.g. no interpolated value can exceed 1.0 in the ENTIRE interpolated series)
real(sp), dimension(:), intent(in) :: monthdata ! Array of monthly (interval) input data
integer(i4), dimension(:), intent(in) :: nk ! Array of number of small time-steps for each interval (can be variable)
real(sp), dimension(:), intent(in) :: bcond ! Boundary condition array
real(sp), dimension(:), intent(in) :: all_cont ! Array of all control points (wall-CPs and mid-CPs)
real(sp), dimension(:), intent(inout) :: daydata ! Array of daily intepolated values
real(sp), allocatable, dimension(:) :: d_orig ! Slope direction of the current interval (1 = positive, -1 = negative, 0 = local maxima/minima)
logical, allocatable, dimension(:) :: osc_check ! TRUE if interval require adjustment (dim = input data length)
real(sp), allocatable, dimension(:) :: c2 ! Array to store the amount of adjustment required
real(sp), allocatable, dimension(:) :: c_mon ! Array to store current month (or interval) of values for bounded adjustment (dim = day in month)
real(sp) :: int_n
real(sp) :: int_nm1
real(sp) :: int_np1
real(sp) :: sip12
integer :: len
integer :: i
integer :: j
integer :: k
integer :: srt
integer :: end
!-----
len = size(monthdata)
allocate(d_orig(len))
allocate(osc_check(len))
allocate(c2(len))
d_orig = -9999.
osc_check = .FALSE.
c2 = -9999.
!------
! Assign -1 for negative slope, 1 for postive and 0 for turning point
do i = 1, len
! Assign intervals values to local variables
int_n = monthdata(i) ! Current interval
if (i == 1) then ! If first interval, apply bcond(1)
int_nm1 = bcond(1)
int_np1 = monthdata(i+1)
else if (i == len) then ! If last interval, apply bcond(2)
int_nm1 = monthdata(i-1)
int_np1 = bcond(2)
else
int_nm1 = monthdata(i-1)
int_np1 = monthdata(i+1)
end if
! Determine the slope direction of the current interval relative to previous and next interval
if (int_np1 - int_n < 0) then
d_orig(i) = -1
else if (int_np1 - int_n > 0) then
d_orig(i) = 1
end if
!---
if (int_n > int_nm1 .and. int_n > int_np1) then
d_orig(i) = 0
else if (int_n < int_nm1 .and. int_n < int_np1) then
d_orig(i) = 0
end if
end do
!------
! Assign TRUE if oscillation of turning point exceeds the predetermined threshold
do i = 1, len
sip12 = all_cont(2*i)
if (sip12 > ulim .and. d_orig(i) == 0) then
osc_check(i) = .TRUE.
end if
end do
!------
! Calculate the amount of adjustment required and insert into the c2 variable
c2 = 0.
do i = 1, len
if (osc_check(i)) then
c2(i) = ulim - monthdata(i)
end if
end do
!------
! Construct the spline for daily values based on all_cont
do i = 1, len
if (osc_check(i)) then
! Assign intervals values to local variables
int_n = monthdata(i) ! Current interval
if (i == 1) then ! If first interval, apply bcond(1)
int_nm1 = bcond(1)
int_np1 = monthdata(i+1)
else if (i == len) then ! If last interval, apply bcond(2)
int_nm1 = monthdata(i-1)
int_np1 = bcond(2)
else
int_nm1 = monthdata(i-1)
int_np1 = monthdata(i+1)
end if
!---
if (i == 1) then
srt = 1
end = srt + nk(1) - 1
else
srt = sum(nk(1:(i-1))) + 1
end = srt + nk(i) - 1
end if
!---
! Create seperate array of all the daily values within the interval being adjusted
allocate(c_mon(nk(i)+2))
if (i == 1) then
c_mon(1) = all_cont(1)
c_mon(2:nk(i)+1) = int_n + c2(i)
c_mon(nk(i)+2) = daydata(end+1)
else if (i == len) then
c_mon(1) = daydata(srt-1)
c_mon(2:nk(i)+1) = int_n + c2(i)
c_mon(nk(i)+2) = all_cont(2*len+1)
else
c_mon(1) = daydata(srt-1)
c_mon(2:nk(i)+1) = int_n + c2(i)
c_mon(nk(i)+2) = daydata(end+1)