-
Notifications
You must be signed in to change notification settings - Fork 1
/
S3M_Module_Tools_Generic.f90
1189 lines (922 loc) · 57.1 KB
/
S3M_Module_Tools_Generic.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
!------------------------------------------------------------------------------------------
! File: S3M_Module_Tools_Generic.f90
! Author: Fabio Delogu, Francesco Avanzi.
!
! Created on March 24, 2014, 1:25 PM
! Last update on Dec 15, 2023 09:30 AM
!
! Module to define generic tools
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Module header
module S3M_Module_Tools_Generic
!------------------------------------------------------------------------------------------
! External module(s) and implicit none
use S3M_Module_Tools_Debug
use S3M_Module_Tools_Interp, only: nearest_interp_1d, linspace
implicit none
!------------------------------------------------------------------------------------------
contains
!------------------------------------------------------------------------------------
! Method to get process PID
function getProcessID() result(str)
implicit none
character(len=256) :: str
integer(kind = 4) :: pid
! Get Process ID
pid = getpid()
write (str, "(I10)") pid !length of PID number to be checked!!if different from I5 does not work
endfunction getProcessID
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Method to filter data and return only the unique values from vec
subroutine filter_array_condition(vec, vec_filter, cond_value_arg, cond_less_arg, cond_eq_arg, cond_greater_arg)
integer(kind = 4) :: i
real(kind = 4), dimension(:) :: vec
real(kind = 4), dimension(:), allocatable :: vec_filter
logical, optional :: cond_less_arg, cond_eq_arg, cond_greater_arg
logical :: cond_less_select, cond_eq_select, cond_greater_select
real(kind = 4), optional :: cond_value_arg
real(kind = 4) :: cond_value_select
logical,dimension(size(vec)) :: mask
if( present(cond_value_arg)) then
cond_value_select = cond_value_arg
else
cond_value_select = -9999.0
end if
if( present(cond_less_arg)) then
cond_less_select = cond_less_arg
else
cond_less_select = .false.
end if
if( present(cond_eq_arg)) then
cond_eq_select = cond_eq_arg
else
cond_eq_select = .false.
end if
if( present(cond_greater_arg)) then
cond_greater_select = cond_greater_arg
else
cond_greater_select = .false.
end if
mask = .false.
do i=1,size(vec)
if (cond_less_select .eqv. .true.) then
if (vec(i) .lt. cond_value_select) mask(i) = .true.
end if
if (cond_eq_select .eqv. .true.) then
if (vec(i) .eq. cond_value_select) mask(i) = .true.
end if
if (cond_greater_select .eqv. .true.) then
if (vec(i) .gt. cond_value_select) mask(i) = .true.
end if
end do
!return only flagged elements:
allocate( vec_filter(count(mask)) )
vec_filter = pack( vec, mask )
end subroutine filter_array_condition
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Method to filter data and return only the unique values from vec
subroutine filter_array_unique(vec, vec_filter)
integer,dimension(:) :: vec
integer,dimension(:), allocatable :: vec_filter
integer :: i,num
logical,dimension(size(vec)) :: mask
mask = .false.
do i=1,size(vec)
if (vec(i) > -9999.0) then
!count the number of occurrences of this element:
num = count( vec(i)==vec )
if (num==1) then
!there is only one, flag it:
mask(i) = .true.
else
!flag this value only if it hasn't already been flagged:
if (.not. any(vec(i)==vec .and. mask) ) mask(i) = .true.
end if
endif
end do
!return only flagged elements:
allocate( vec_filter(count(mask)) )
vec_filter = pack( vec, mask )
end subroutine filter_array_unique
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Function to reshape 2D variable
function reshape2DVar(a2dVarValue) result(a1dVarValue)
!------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: iRows, iCols
real(kind = 4), dimension(:,:) :: a2dVarValue
real(kind = 4), dimension( (ubound(a2dVarValue, dim=2) - lbound(a2dVarValue, dim=2) + 1) * &
(ubound(a2dVarValue, dim=1) - lbound(a2dVarValue, dim=1) + 1) ) :: a1dVarValue
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Init variable(s)
a1dVarValue = 0.0;
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Define dimension(s)
iCols = ubound(a2dVarValue, dim=2) - lbound(a2dVarValue, dim=2) + 1
iRows = ubound(a2dVarValue, dim=1) - lbound(a2dVarValue, dim=1) + 1
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Reshape from 2d to 1d
a1dVarValue = reshape(a2dVarValue, (/iCols * iRows/))
!------------------------------------------------------------------------------------
end function reshape2DVar
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Subroutine to get integration boundaries
subroutine getIntRange(a1dDemStep, a1dIntStep, dDEMStepMean, dDemDelta, iIndexStart, iIndexEnd)
!------------------------------------------------------------------------------------
! Variable(s) declaration
real(kind = 4) :: dDEMStepMean
real(kind = 4) :: dDemDelta, dIntDelta
integer(kind = 4) :: iIndexStart, iIndexEnd
real(kind = 4), dimension(4) :: a1dDemStep, a1dIntStep
real(kind = 4), dimension(1) :: a1dDemStepMean, a1dDemStepMin
integer(kind = 4), dimension(1) :: a1iIndexStepMin
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Compute array indexes to get integration and dem upper and lower boundaries
a1dDemStepMean = dDEMStepMean
call nearest_interp_1d( size(a1dIntStep), a1dIntStep, a1dDemStep, 1, a1dDemStepMean, a1dDemStepMin, a1iIndexStepMin)
if (a1dDemStep(a1iIndexStepMin(1)) .lt. dDEMStepMean) then
iIndexStart = a1iIndexStepMin(1)
iIndexEnd = a1iIndexStepMin(1) + 1
else
iIndexStart = a1iIndexStepMin(1) - 1
iIndexEnd = a1iIndexStepMin(1)
endif
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Compute dem delta
dDemDelta = a1dDemStep(iIndexEnd) - a1dDemStep(iIndexStart) + 1
dIntDelta = a1dIntStep(iIndexEnd) - a1dIntStep(iIndexStart) + 1
!------------------------------------------------------------------------------------
end subroutine
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Subroutine to get integration values
subroutine getIntValue(a1dDemStep, a1dIntStep, a1dDtStep, a1dDtRatioStep, &
dDemMean, dDemDelta, iIndexStart, iIndexEnd, &
dIntSel, dDtSel, dDtRatioSel)
!------------------------------------------------------------------------------------
! Variable(s) declaration
real(kind = 4), dimension(4) :: a1dDemStep, a1dIntStep, a1dDtStep, a1dDtRatioStep
real(kind = 4) :: dDEMMean, dDemDelta
integer(kind = 4) :: iIndexStart, iIndexEnd
real(kind = 4), dimension(int(dDemDelta)) :: a1dIntDef, a1dDemDef, a1dDtDef, a1dDtRatioDef
real(kind = 4), dimension(1) :: a1dDemMean, a1dIntSel
integer(kind = 4), dimension(1) :: a1iIndexSel
real(kind = 4) :: dIntSel, dDtSel, dDtRatioSel
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Compute integration values (using average dem step)
a1dDemMean = dDEMMean
call linspace(a1dDemStep(iIndexStart),a1dDemStep(iIndexEnd), a1dDemDef)
call linspace(a1dIntStep(iIndexStart),a1dIntStep(iIndexEnd), a1dIntDef)
call linspace(a1dDtStep(iIndexStart),a1dDtStep(iIndexEnd), a1dDtDef)
call linspace(a1dDtRatioStep(iIndexStart),a1dDtRatioStep(iIndexEnd), a1dDtRatioDef)
call nearest_interp_1d( size(a1dDemDef), a1dDemDef, a1dIntDef, 1, a1dDemMean, a1dIntSel, a1iIndexSel)
! Select integration values
dIntSel = real(nint(a1dIntDef(a1iIndexSel(1))))
dDtSel = real(nint(a1dDtDef(a1iIndexSel(1))))
dDtRatioSel = real(nint(a1dDtRatioDef(a1iIndexSel(1))))
!------------------------------------------------------------------------------------
end subroutine getIntValue
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Subroutine to use nudging assimilation
subroutine assimNudging(a2iVarMask, a2dVarModel, a2dVarObs, a2dVarKernel, &
a2dVarModelAssim, a2dVarCorr, iFlagAssOnlyPos)
!------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: iCols, iRows
integer(kind = 4), dimension(:,:) :: a2iVarMask
integer(kind = 4) :: iFlagAssOnlyPos
real(kind = 4), dimension(:,:) :: a2dVarModel, a2dVarObs
real(kind = 4), dimension(:,:) :: a2dVarKernel
real(kind = 4), dimension(lbound(a2dVarModel, dim=1):ubound(a2dVarModel, dim=1), &
lbound(a2dVarModel, dim=2):ubound(a2dVarModel, dim=2)) :: a2dVarModelAssim
real(kind = 4), dimension(lbound(a2dVarModel, dim=1):ubound(a2dVarModel, dim=1), &
lbound(a2dVarModel, dim=2):ubound(a2dVarModel, dim=2)) :: a2dVarCorr
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Get iCols and iRows
iCols = ubound(a2dVarModel, dim=2) - lbound(a2dVarModel, dim=2) + 1
iRows = ubound(a2dVarModel, dim=1) - lbound(a2dVarModel, dim=1) + 1
! Initialize variable
a2dVarModelAssim = 0.0; a2dVarCorr = 0.0
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Apply nudging method
where( (a2iVarMask.gt.0.0) .and. (a2dVarObs.ge.0.0) .and. (a2dVarKernel.ge.0.0) )
a2dVarCorr = a2dVarKernel*(a2dVarObs - a2dVarModel)
endwhere
if (iFlagAssOnlyPos.eq.1) then
!If iFlagAssOnlyPos is activated, then we only correct where a2dVarCorr is positive
where( (a2iVarMask.gt.0.0) .and. (a2dVarObs.ge.0.0) .and. (a2dVarKernel.ge.0.0) .and. (a2dVarCorr .gt.0.0))
a2dVarModel = a2dVarModel + a2dVarCorr
endwhere
elseif (iFlagAssOnlyPos.eq.0) then
!If iFlagAssOnlyPos is not activated, we assimilate everywhere.
where( (a2iVarMask.gt.0.0) .and. (a2dVarObs.ge.0.0) .and. (a2dVarKernel.ge.0.0))
a2dVarModel = a2dVarModel + a2dVarCorr
endwhere
endif
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Store result(s) in output variable
a2dVarModelAssim = a2dVarModel
!------------------------------------------------------------------------------------
end subroutine assimNudging
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Function to transpose 3D variable
function transpose3Dvar(a3dVar) result(a3dVarT)
!------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: iT
real(kind = 4), dimension(:,:,:) :: a3dVar
real(kind = 4), dimension(lbound(a3dVar, dim=2):ubound(a3dVar, dim=2), &
lbound(a3dVar, dim=1):ubound(a3dVar, dim=1), &
lbound(a3dVar, dim=3):ubound(a3dVar, dim=3)) :: a3dVarT
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Initialize variable
!a3dVarT = -9999.0
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Transpose array step by step
do iT = 1, ubound(a3dVar, dim=3)
a3dVarT(:,:,iT) = transpose(a3dVar(:,:,iT))
enddo
!------------------------------------------------------------------------------------
end function transpose3Dvar
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Function to nullify 2D variable borders
function nullborder2DVar(a2dVarValue, dNoData) result(a2dVarValueUpd)
!------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: iRows, iCols
real(kind = 4) :: dNoData
real(kind = 4), dimension(:,:) :: a2dVarValue
real(kind = 4), dimension(lbound(a2dVarValue, dim=1):ubound(a2dVarValue, dim=1), &
lbound(a2dVarValue, dim=2):ubound(a2dVarValue, dim=2)) :: a2dVarValueUpd
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Init variable(s)
a2dVarValueUpd = 0.0;
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Define dimension(s)
iCols = ubound(a2dVarValue, dim=2) - lbound(a2dVarValue, dim=2) + 1
iRows = ubound(a2dVarValue, dim=1) - lbound(a2dVarValue, dim=1) + 1
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Pass source map to outcome map
a2dVarValueUpd = a2dVarValue
! Nullify map boundary
a2dVarValueUpd(1,:) = dNoData
a2dVarValueUpd(:,1) = dNoData
a2dVarValueUpd(:,iCols) = dNoData
a2dVarValueUpd(iRows,:) = dNoData
!------------------------------------------------------------------------------------
end function nullborder2DVar
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Function to compute minimum 2D variable value
function min2Dvar(a2dVarValue, a2iVarMask) result(dVarValue)
!------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4), dimension(:,:) :: a2iVarMask
real(kind = 4), dimension(:,:) :: a2dVarValue
integer(kind=4) :: iRows, iCols
real(kind = 4) :: dVarValue
character(len = 20) :: sVarValue
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Compute max value
dVarValue = -9999.0
dVarValue = minval(a2dVarValue, mask=a2iVarMask.gt.0.0)
! Info max value
write(sVarValue, *) dVarValue
call mprintf(.true., iINFO_Extra, ' Min variable value: '//trim(sVarValue))
!------------------------------------------------------------------------------------
end function min2Dvar
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Function to compute maximum 2D variable value
function max2Dvar(a2dVarValue, a2iVarMask) result(dVarValue)
!------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4), dimension(:,:) :: a2iVarMask
real(kind = 4), dimension(:,:) :: a2dVarValue
real(kind = 4) :: dVarValue
character(len = 20) :: sVarValue
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Compute max value
dVarValue = -9999.0
dVarValue = maxval(a2dVarValue, mask=a2iVarMask.gt.0.0)
! Info max value
write(sVarValue, *) dVarValue
call mprintf(.true., iINFO_Extra, ' Max variable value: '//trim(sVarValue))
!------------------------------------------------------------------------------------
end function max2Dvar
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Function to compute average 2D variable value
function mean2Dvar(a2dVarValue, a2iVarMask) result(dVarValue)
!------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4), dimension(:,:) :: a2iVarMask
real(kind = 4), dimension(:,:) :: a2dVarValue
real(kind = 4) :: dVarValue
character(len = 20) :: sVarValue
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Compute average value
dVarValue = -9999.0
dVarValue = sum(a2dVarValue, mask=a2iVarMask.gt.0.0)/max(1,count(a2iVarMask.gt.0.0))
! Info mean value
write(sVarValue, *) dVarValue
call mprintf(.true., iINFO_Extra, ' Mean variable value: '//trim(sVarValue))
!------------------------------------------------------------------------------------
end function mean2Dvar
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! function to check domain variable
function checkdomainvar(a2dVarValue, a2iVarMask, dVarNoData) result(a2dVarValueUpd)
!------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4), dimension(:,:) :: a2iVarMask
real(kind = 4), dimension(:,:) :: a2dVarValue
real(kind = 4), optional :: dVarNoData
real(kind = 4), dimension(lbound(a2dVarValue, dim=1):ubound(a2dVarValue, dim=1), &
lbound(a2dVarValue, dim=2):ubound(a2dVarValue, dim=2)) :: a2dVarValueUpd
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Initialize variable
a2dVarValueUpd = -9999.0
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Info
call mprintf(.true., iINFO_Extra, ' Check variable domain ... ')
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Check variable on mask domain
if (present(dVarNoData)) then
!------------------------------------------------------------------------------------
! Check value on mask
where(a2iVarMask.lt.0.0)
a2dVarValue = dVarNoData
endwhere
!------------------------------------------------------------------------------------
else
!------------------------------------------------------------------------------------
! Check value on mask
where(a2iVarMask.lt.0.0)
a2dVarValue = -9999.0
endwhere
!------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Update variable
a2dVarValueUpd = a2dVarValue
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Info
call mprintf(.true., iINFO_Extra, ' Check variable domain ... OK')
!------------------------------------------------------------------------------------
end function checkdomainvar
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! function to check 2D variable limit(s)
function check2Dvar(a2dVarValue, a2iVarMask, &
dVarValueMin, dVarValueMax, dVarNoData) result(a2dVarValueUpd)
!------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4), dimension(:,:) :: a2iVarMask
real(kind = 4), dimension(:,:) :: a2dVarValue
real(kind = 4) :: dVarValueMin, dVarValueMax
real(kind = 4), optional :: dVarNoData
real(kind = 4), dimension(lbound(a2dVarValue, dim=1):ubound(a2dVarValue, dim=1), &
lbound(a2dVarValue, dim=2):ubound(a2dVarValue, dim=2)) :: a2dVarValueUpd
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Initialize variable
a2dVarValueUpd = -9999.0
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Info
call mprintf(.true., iINFO_Extra, ' Check variable limit (max and min) ... ')
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Check variable on mask domain
if (present(dVarNoData)) then
!------------------------------------------------------------------------------------
! Check value on mask
where(a2iVarMask.ge.0.0)
!------------------------------------------------------------------------------------
! Lower limit
where(a2dVarValue.lt.dVarValueMin)
a2dVarValue = dVarNoData
endwhere
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Upper limit
where(a2dVarValue.gt.dVarValueMax)
a2dVarValue = dVarNoData
endwhere
!------------------------------------------------------------------------------------
elsewhere
!------------------------------------------------------------------------------------
! Mask undefined cell(s)
a2dVarValue = dVarNoData
!------------------------------------------------------------------------------------
endwhere
!------------------------------------------------------------------------------------
else
!------------------------------------------------------------------------------------
! Check value on mask
dVarNoData = -9999.0
where(a2iVarMask.ge.0.0)
!------------------------------------------------------------------------------------
! Lower limit
where(a2dVarValue.lt.dVarValueMin)
a2dVarValue = dVarValueMin
endwhere
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Upper limit
where(a2dVarValue.gt.dVarValueMax)
a2dVarValue = dVarValueMax
endwhere
!------------------------------------------------------------------------------------
elsewhere
!------------------------------------------------------------------------------------
! Mask undefined cell(s)
a2dVarValue = dVarNoData
!------------------------------------------------------------------------------------
endwhere
!------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Update variable
a2dVarValueUpd = a2dVarValue
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Info
call mprintf(.true., iINFO_Extra, ' Check variable limit (max and min) ... OK')
!------------------------------------------------------------------------------------
end function check2Dvar
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine to open file unit
subroutine S3M_Tools_Generic_SetUnit(iFileUnitMin, iFileUnitMax, iFileUnit)
!------------------------------------------------------------------------------------------
! Variable(s) declaration
logical :: bIsUsed
integer(kind = 4), intent(in) :: iFileUnitMin, iFileUnitMax
integer(kind = 4), intent(out) :: iFileUnit
integer(kind = 4) :: iFileUnitInit
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Search debug unit
do iFileUnitInit = iFileUnitMin, iFileUnitMax
inquire(unit = iFileUnitInit, opened = bIsUsed)
if (.not. bIsUsed) exit
end do
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Pass to global variables
iFileUnit = iFileUnitInit
!------------------------------------------------------------------------------------------
end subroutine S3M_Tools_Generic_SetUnit
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine to remove file
subroutine S3M_Tools_Generic_RemoveFile(sCommandRemoveRaw, sFileName, bFatalError)
!------------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: iErr
character(len = 700) :: sFileName
character(len = 700), intent(in) :: sCommandRemoveRaw
character(len = 700) :: sCommandRemoveDef
logical :: bFatalError
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Initialize variable(s)
iErr = 0
sCommandRemoveDef = sCommandRemoveRaw
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Define command line
call S3M_Tools_Generic_ReplaceText(sCommandRemoveDef, 'filename', trim(sFileName))
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Call command line using system to zip file
call execute_command_line(sCommandRemoveDef, EXITSTAT = iErr)
if(iErr /= 0) then
if (bFatalError) then
call mprintf(.true., iERROR, 'Error in removing file: '//trim(sFileName) )
else
iErr = 0
return
endif
endif
!------------------------------------------------------------------------------------------
end subroutine S3M_Tools_Generic_RemoveFile
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine to zip file
subroutine S3M_Tools_Generic_ZipFile(sCommandZipRaw, sFileNameZip, sFileNameUnzip, bFatalError)
!------------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: iErr
character(len = 700) :: sFileNameZip, sFileNameUnzip
character(len = 700), intent(in) :: sCommandZipRaw
character(len = 700) :: sCommandZipDef
logical :: bFatalError
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Initialize variable(s)
iErr = 0
sCommandZipDef = sCommandZipRaw
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Define command line
call S3M_Tools_Generic_ReplaceText(sCommandZipDef, 'filenamezip', trim(sFileNameZip))
call S3M_Tools_Generic_ReplaceText(sCommandZipDef, 'filenameunzip', trim(sFileNameUnzip))
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Call command liene using system to zip file
call execute_command_line(sCommandZipDef, EXITSTAT = iErr)
if(iErr /= 0) then
if (bFatalError) then
call mprintf(.true., iERROR, 'Error in zipping file: '//trim(sFileNameUnzip) )
else
iErr = 0
return
endif
endif
!------------------------------------------------------------------------------------------
end subroutine S3M_Tools_Generic_ZipFile
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine to unzip file
subroutine S3M_Tools_Generic_UnzipFile(sCommandUnzipRaw, sFileNameZip, sFileNameUnzip, bFatalError)
!------------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: iErr
character(len = 700) :: sFileNameZip, sFileNameUnzip
character(len = 700), intent(in) :: sCommandUnzipRaw
character(len = 700) :: sCommandUnzipDef
logical :: bFatalError
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Initialize variable(s)
iErr = 0
sCommandUnzipDef = sCommandUnzipRaw
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Define command line
call S3M_Tools_Generic_ReplaceText(sCommandUnzipDef, 'filenamezip', trim(sFileNameZip))
call S3M_Tools_Generic_ReplaceText(sCommandUnzipDef, 'filenameunzip', trim(sFileNameUnzip))
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Call command line using system to unzip file
call execute_command_line(sCommandUnzipDef, EXITSTAT = iErr)
if(iErr /= 0) then
if (bFatalError) then
call mprintf(.true., iERROR, 'Error in unzipping file: '//trim(sFileNameZip) )
else
iErr = 0
return
endif
endif
!------------------------------------------------------------------------------------------
end subroutine S3M_Tools_Generic_UnzipFile
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine to create new folder
subroutine S3M_Tools_Generic_CreateFolder(sCommandCreateFolder, sPathFolder, bFatalError)
!------------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: iErr
character(len = 700) :: sPathFolder
character(len = 700), intent(in) :: sCommandCreateFolder
logical :: bFatalError
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Initialize variable(s)
iErr = 0
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Define command line
call S3M_Tools_Generic_ReplaceText(sCommandCreateFolder, 'path', sPathFolder)
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Call command line using system to create the folder
call execute_command_line(trim(sCommandCreateFolder), EXITSTAT = iErr)
if(iErr /= 0) then
if (bFatalError) then
call mprintf(.true., iERROR, 'Error in creating folder: '//trim(sPathFolder) )
else
iErr = 0
return
endif
endif
!------------------------------------------------------------------------------------------
end subroutine S3M_Tools_Generic_CreateFolder
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine to smooth data time series
subroutine S3M_Tools_Generic_SmoothTimeSeries(a1dDataT, iRank, iNsmooth)
!------------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: it, iit
integer(kind = 4) :: iRank, iNsmooth
real(kind = 4) :: dNum, dNumT, dVdif, dSumA, dSumB, dMean
real(kind = 4), dimension(iRank) :: a1dDataT, a1dDataTmp
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Initialize variable(s)
dNumT = 0; dVdif = 0
dSumB = sum(a1dDataT, dim = 1)
!iRank = size(a1dDataT,dim = 1)
a1dDataTmp = 0
a1dDataTmp = a1dDataT
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Smooth data
do it = 1 + iNsmooth, iRank - iNsmooth - 1
dNum = 0; dMean = 0;
if(a1dDataT(it).gt.0.0) dNumT = dNumT + 1
do iit = it - iNsmooth, it + iNsmooth
if(a1dDataT(iit).ge.0.0)then
dMean = dMean + a1dDataT(iit)
dNum = dNum + 1
endif
enddo
if(dNum.gt.0)then
dMean = dMean/dNum
a1dDataTmp(it) = dMean
endif
enddo
a1dDataT = a1dDataTmp
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Control data
dSumA = sum(a1dDataT, dim = 1)
if(dNumT.gt.0)then
dVdif = (dSumB - dSumA)/dNumT
endif
! Control V difference between the raw and generated data series
if(dVdif.lt.0.0) then
where(a1dDataT.gt.abs(dVdif))
a1dDataT = a1dDataT + dVdif
endwhere
else
where(a1dDataT.gt.0)
a1dDataT = a1dDataT + dVdif
endwhere
endif
!------------------------------------------------------------------------------------------
end subroutine S3M_Tools_Generic_SmoothTimeSeries
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine to realize grid switcher between land-forcing data
subroutine S3M_Tools_Generic_SwitchGrid(iFlagGrid, &
iRowsL, iColsL, a2dVarL, &
iRowsF, iColsF, a2dVarF, &
a2dVarMask, a2iVarIndexX, a2iVarIndexY, &
dNoData_Opt)
!------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: iFlagGrid
integer(kind = 4) :: iI, iJ, iRowsL, iColsL, iRowsF, iColsF
real(kind = 4), intent(in), dimension(iRowsF, iColsF) :: a2dVarF
real(kind = 4), intent(out), dimension(iRowsL, iColsL) :: a2dVarL
integer(kind = 4), intent(in), dimension(iRowsL, iColsL) :: a2iVarIndexX, a2iVarIndexY
real(kind = 4), intent(in), dimension(iRowsL, iColsL) :: a2dVarMask
real(kind = 4), optional :: dNoData_Opt
real(kind = 4) :: dNoData_Set
real(kind = 4), parameter :: dNoData_Def = -9999.0
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Initialize variable(s)
iI = 0; iJ = 0;
a2dVarL = -9999.0;
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Set no data value
dNoData_Set = dNoData_Def
if (present(dNoData_Opt)) dNoData_Set = dNoData_Opt
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Check flag condition
call mprintf(.true., iINFO_Extra, ' Switching grid procedure ...')
if (iFlagGrid .eq. 1) then
!------------------------------------------------------------------------------------
! Info
call mprintf(.true., iINFO_Extra, ' Grids are different!')
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Check values
if (any(a2dVarF.ne.dNoData_Set)) then
!------------------------------------------------------------------------------------
! Regrid F data on L data using X and Y indexes
forall(iI = 1:iRowsL, iJ = 1:iColsL, a2dVarMask(iI,iJ) .ge. 0.0)
a2dVarL(iI, iJ) = a2dVarF(a2iVarIndexY(iI, iJ), a2iVarIndexX(iI,iJ))
endforall
! Info
call mprintf(.true., iINFO_Extra, ' Switching grid procedure ... OK')
!------------------------------------------------------------------------------------
else
!------------------------------------------------------------------------------------
! Data not defined --> array initialized with nodata value
a2dVarL = dNoData_Set
!------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------
elseif (iFlagGrid .eq. 0) then
!------------------------------------------------------------------------------------
! Info
call mprintf(.true., iINFO_Extra, ' Grids are equal!')
!------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------
! Compute output results
a2dVarL = a2dVarF
! Info
call mprintf(.true., iINFO_Extra, ' Switching grid procedure ... SKIPPED')
!------------------------------------------------------------------------------------
else
!------------------------------------------------------------------------------------
! Exit
call mprintf(.true., iERROR, ' Incorrect switch flag grid ' )
!------------------------------------------------------------------------------------
endif
!------------------------------------------------------------------------------------
end subroutine S3M_Tools_Generic_SwitchGrid
!------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------
! Subroutine to compute indexes between land-forcing grids
subroutine S3M_Tools_Generic_CreateIndexGrid(idim_land, jdim_land, idim_forcing, jdim_forcing, &
lat_land, lon_land, lat_forcing, lon_forcing, &
lat_res_land, lon_res_land, lat_res_forcing, lon_res_forcing, &
flag, &
a2iVarXIndex, a2iVarYIndex)
!------------------------------------------------------------------------------------------
! Variable(s) declaration
integer(kind = 4) :: i, j, flag
integer(kind = 4) :: idim_land, jdim_land, idim_forcing, jdim_forcing