-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGEOS_LdasGridComp.F90
1053 lines (850 loc) · 41.3 KB
/
GEOS_LdasGridComp.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
#include "MAPL_Generic.h"
!BOP
! !MODULE: GEOS_LdasGridCompMod - Module to combine children GridComps
module GEOS_LdasGridCompMod
! !USES
use ESMF
use MAPL_Mod
use GEOS_MetforceGridCompMod, only: MetforceSetServices => SetServices
use GEOS_LandGridCompMod, only: LandSetServices => SetServices
use GEOS_LandPertGridCompMod, only: LandPertSetServices => SetServices
use GEOS_EnsGridCompMod, only: EnsSetServices => SetServices
use GEOS_LandAssimGridCompMod, only: LandAssimSetServices => SetServices
use EASE_conv, only: ease_inverse
use LDAS_TileCoordType, only: tile_coord_type , T_TILECOORD_STATE, TILECOORD_WRAP
use LDAS_TileCoordType, only: grid_def_type, io_grid_def_type, operator (==)
use LDAS_TileCoordRoutines, only: get_minExtent_grid, get_ij_ind_from_latlon, io_domain_files
use LDAS_ConvertMod, only: esmf2ldas
use LDAS_PertRoutinesMod, only: get_pert_grid
use LDAS_ensdrv_functions,ONLY: get_io_filename
use LDAS_DateTimeMod,ONLY: date_time_type
use LDAS_ensdrv_mpi, only: MPI_tile_coord_type, MPI_grid_def_type
use LDAS_ensdrv_mpi, only: init_MPI_types,mpicomm,numprocs,myid
use LDAS_ensdrv_mpi, only: root_proc
use LDAS_ensdrv_Globals, only: logunit,logit,root_logit,echo_clsm_ensdrv_glob_param, get_ensid_string
use catch_constants, only: echo_catch_constants
use StieglitzSnow, only: StieglitzSnow_echo_constants
use SurfParams, only: SurfParams_init
implicit none
private
! !PUBLIC MEMBER FUNCTIONS:
public SetServices
! !DESCRIPTION: This gridded component (GC) combines the GridComps:
! METFORCE, LAND, LANDPERT, ENSAVG, and LANDASSIM
! into a new composite LDAS GricComp.
! Include later: LAKE, LANDICE(?), SALTWATER(?)
!EOP
include 'mpif.h'
! All children
integer,allocatable :: LAND(:)
integer,allocatable :: LANDPERT(:)
integer,allocatable :: METFORCE(:)
integer :: ENSAVG, LANDASSIM
! other global variables
integer :: NUM_ENSEMBLE ! number of land ensemble members
logical :: land_assim
logical :: mwRTM
logical :: ensemble_forcing ! switch between deterministic and ensemble forcing
contains
!BOP
! !IROTUINE: SetServices -- Set ESMF services for this component
! !INTERFACE:
subroutine SetServices(gc, rc)
! !ARGUMENTS:
type(ESMF_GridComp), intent(inout) :: gc ! gridded component
integer, optional :: rc ! return code
! ensemble set up:
integer :: i, k
integer :: ens_id
type(MAPL_MetaComp), pointer :: MAPL=>null()
! ErrLog variables
integer :: status
character(len=ESMF_MAXSTR) :: Iam
character(len=ESMF_MAXSTR) :: comp_name
character(len=ESMF_MAXSTR) :: ensid_string,childname
character(len=ESMF_MAXSTR) :: LAND_ASSIM_STR, mwRTM_file, ENS_FORCING_STR
integer :: ens_id_width
! Local variables
type(T_TILECOORD_STATE), pointer :: tcinternal
type(TILECOORD_WRAP) :: tcwrap
type(ESMF_Config) :: CF
integer :: LSM_CHOICE
integer :: FIRST_ENS_ID
! Begin...
! Get my name and setup traceback handle
Iam = 'SetServices'
call ESMF_GridCompGet(gc, name=comp_name,CONFIG=CF, rc=status)
VERIFY_(status)
Iam = trim(comp_name) // "::" // Iam
! Register services for this component
call MAPL_GridCompSetEntryPoint( &
gc, &
ESMF_METHOD_INITIALIZE, &
Initialize, &
rc=status &
)
VERIFY_(status)
call MAPL_GridCompSetEntryPoint( &
gc, &
ESMF_METHOD_RUN, &
Run, &
rc=status &
)
VERIFY_(status)
call MAPL_GridCompSetEntryPoint( &
gc, &
ESMF_METHOD_FINALIZE, &
Finalize, &
rc=status &
)
VERIFY_(status)
! Save the tile_coord variable as an internal state of the GridComp
! memory for tcwrap%tile_coord is allocated in Initialize()
allocate(tcinternal, stat=status)
VERIFY_(status)
tcwrap%ptr => tcinternal
call ESMF_UserCompSetInternalState(gc, 'TILE_COORD', tcwrap, status)
VERIFY_(status)
!create ensemble children
call MAPL_GetObjectFromGC(gc, MAPL, rc=status)
VERIFY_(status)
call MAPL_GetResource ( MAPL, NUM_ENSEMBLE, Label="NUM_LDAS_ENSEMBLE:", DEFAULT=1, RC=STATUS)
VERIFY_(STATUS)
call MAPL_GetResource ( MAPL, ens_id_width, Label="ENS_ID_WIDTH:", DEFAULT=0, RC=STATUS)
VERIFY_(STATUS)
call MAPL_GetResource ( MAPL, FIRST_ENS_ID, Label="FIRST_ENS_ID:", DEFAULT=0, RC=STATUS)
VERIFY_(STATUS)
call MAPL_GetResource ( MAPL, ENS_FORCING_STR, Label="ENSEMBLE_FORCING:", DEFAULT="NO", RC=STATUS)
VERIFY_(STATUS)
ENS_FORCING_STR = ESMF_UtilStringUpperCase(ENS_FORCING_STR, rc=STATUS)
VERIFY_(STATUS)
ensemble_forcing = (trim(ENS_FORCING_STR) == 'YES')
call MAPL_GetResource ( MAPL, LAND_ASSIM_STR, Label="LAND_ASSIM:", DEFAULT="NO", RC=STATUS)
VERIFY_(STATUS)
LAND_ASSIM_STR = ESMF_UtilStringUpperCase(LAND_ASSIM_STR, rc=STATUS)
VERIFY_(STATUS)
land_assim = (trim(LAND_ASSIM_STR) /= 'NO')
call MAPL_GetResource ( MAPL, mwRTM_file, Label="LANDASSIM_INTERNAL_RESTART_FILE:", DEFAULT='', RC=STATUS)
VERIFY_(STATUS)
mwRTM = ( len_trim(mwRTM_file) /= 0 )
call MAPL_GetResource ( MAPL, LSM_CHOICE, Label="LSM_CHOICE:", DEFAULT=1, RC=STATUS)
if (LSM_CHOICE /=1 ) then
_ASSERT( .not. (mwRTM .or. land_assim), "CatchCN is Not Ready for assimilation or mwRTM")
endif
if (ensemble_forcing) then
allocate(METFORCE(NUM_ENSEMBLE))
else
allocate(METFORCE(1))
endif
allocate(LAND(NUM_ENSEMBLE),LANDPERT(NUM_ENSEMBLE))
! ens_id_with = 2 + number of digits = total number of chars in ensid_string ("_eXXXX")
!
! Assert ens_id_width<=2+9 so number of digits remains single-digit and "I1" can be
! hardwired when assembling a format string.
! Assert ens_id_width>=2+3 to avoid user configuration errors when LDAS is coupled into ADAS.
! (Met forcing from the atm ensemble uses hardwired, 3-character ensemble IDs.)
if (NUM_ENSEMBLE > 1) then
_ASSERT( ens_id_width < 12, "Must use ens_id_width <= 11 (2 for '_e' + 9 digits max)")
_ASSERT( ens_id_width >= 5, "Must use ens_id_width >= 5 (2 for '_e' + 3 digits min)")
endif
do i=1,NUM_ENSEMBLE
ens_id = i-1 + FIRST_ENS_ID ! id start form FIRST_ENS_ID
call get_ensid_string(ensid_string, ens_id, ens_id_width, NUM_ENSEMBLE) ! "_eXXXX"
! allow for Catchment ensemble simulation to be forced with single-member met inputs
if (.not. ensemble_forcing ) ensid_string = ''
childname='METFORCE'//trim(ensid_string)
METFORCE(i) = MAPL_AddChild(gc, name=trim(childname), ss=MetforceSetServices, rc=status)
VERIFY_(status)
! exit after i=1 if using deterministic forcing
if (.not. ensemble_forcing ) exit
enddo
do i=1,NUM_ENSEMBLE
ens_id = i-1 + FIRST_ENS_ID ! id start form FIRST_ENS_ID
call get_ensid_string(ensid_string, ens_id, ens_id_width, NUM_ENSEMBLE)
childname='LANDPERT'//trim(ensid_string)
LANDPERT(i) = MAPL_AddChild(gc, name=childname, ss=LandPertSetServices, rc=status)
VERIFY_(status)
childname='LAND'//trim(ensid_string)
LAND(i) = MAPL_AddChild(gc, name=childname, ss=LandSetServices, rc=status)
VERIFY_(status)
enddo
ENSAVG = MAPL_AddChild(gc, name='ENSAVG', ss=EnsSetServices, rc=status)
VERIFY_(status)
if(land_assim .or. mwRTM ) then
LANDASSIM = MAPL_AddChild(gc, name='LANDASSIM', ss=LandAssimSetServices, rc=status)
VERIFY_(status)
endif
! Connections
do i=1,NUM_ENSEMBLE
! -METFORCE-feeds-LANDPERT's-imports-
k = 1
if ( ensemble_forcing ) k = i
call MAPL_AddConnectivity( &
gc, &
SHORT_NAME = ['Tair ', 'Qair ', 'Psurf ', 'Rainf_C', 'Rainf ', &
'Snowf ', 'LWdown ', 'SWdown ', 'PARdrct', 'PARdffs', &
'Wind ', 'RefH '], &
SRC_ID = METFORCE(k), &
DST_ID = LANDPERT(i), &
rc = status &
)
VERIFY_(status)
! -LANDPERT-feeds-LAND's-imports-
call MAPL_AddConnectivity( &
gc, &
SRC_NAME = ['TApert ', 'QApert ', 'UUpert ', &
'UWINDLMTILEpert', 'VWINDLMTILEpert', 'PCUpert ', &
'PLSpert ', 'SNOpert ', 'DRPARpert ', &
'DFPARpert ', 'DRNIRpert ', 'DFNIRpert ', &
'DRUVRpert ', 'DFUVRpert ', 'LWDNSRFpert '], &
SRC_ID = LANDPERT(i), &
DST_NAME = ['TA ', 'QA ', 'UU ', 'UWINDLMTILE',&
'VWINDLMTILE', 'PCU ', 'PLS ', 'SNO ',&
'DRPAR ', 'DFPAR ', 'DRNIR ', 'DFNIR ',&
'DRUVR ', 'DFUVR ', 'LWDNSRF '], &
DST_ID = LAND(i), &
rc = status &
)
VERIFY_(status)
! -METFORCE-feeds-LAND's-imports-
call MAPL_AddConnectivity( &
gc, &
SRC_NAME = ['Psurf', 'RefH ', &
'DUDP ', 'DUSV ', 'DUWT ', 'DUSD ', 'BCDP ', 'BCSV ', &
'BCWT ', 'BCSD ', 'OCDP ', 'OCSV ', 'OCWT ', 'OCSD ', &
'SUDP ', 'SUSV ', 'SUWT ', 'SUSD ', 'SSDP ', 'SSSV ' ], &
SRC_ID = METFORCE(k), &
DST_NAME = ['PS ', 'DZ ', &
'DUDP', 'DUSV', 'DUWT', 'DUSD', 'BCDP', 'BCSV', &
'BCWT', 'BCSD', 'OCDP', 'OCSV', 'OCWT', 'OCSD', &
'SUDP', 'SUSV', 'SUWT', 'SUSD', 'SSDP', 'SSSV' ], &
DST_ID = LAND(i), &
rc = status &
)
VERIFY_(status)
! -LAND-feeds-LANDPERT's-imports-
call MAPL_AddConnectivity( &
gc, &
SRC_NAME = ['TC ','CATDEF ','RZEXC ','SRFEXC ','WESNN1 ','WESNN2 ','WESNN3 ', &
'GHTCNT1','GHTCNT2','GHTCNT3','GHTCNT4','GHTCNT5','GHTCNT6', &
'HTSNNN1','HTSNNN2','HTSNNN3','SNDZN1 ','SNDZN2 ','SNDZN3 '], &
SRC_ID = LAND(i), &
DST_NAME = ['TCPert ','CATDEFPert ','RZEXCPert ','SRFEXCPert ','WESNN1Pert ', &
'WESNN2Pert ','WESNN3Pert ','GHTCNT1Pert','GHTCNT2Pert', &
'GHTCNT3Pert','GHTCNT4Pert','GHTCNT5Pert','GHTCNT6Pert', &
'HTSNNN1Pert','HTSNNN2Pert','HTSNNN3Pert','SNDZN1Pert ', &
'SNDZN2Pert ','SNDZN3Pert '], &
DST_ID = LANDPERT(i), &
rc = status &
)
VERIFY_(status)
enddo
if(land_assim .or. mwRTM) then
! -LAND-feeds-LANDASSIM's-imports-
! Catchment model parameters from first LAND ens member, assumes no parameter perturbations!
call MAPL_AddConnectivity( &
gc, &
SHORT_NAME = ['POROS ', 'COND ','PSIS ','BEE ','WPWET ','GNU ','VGWMAX', &
'BF1 ', 'BF2 ','BF3 ','CDCR1 ','CDCR2 ','ARS1 ', &
'ARS2 ', 'ARS3 ','ARA1 ','ARA2 ','ARA3 ','ARA4 ', &
'ARW1 ', 'ARW2 ','ARW3 ','ARW4 ','TSA1 ','TSA2 ','TSB1 ', &
'TSB2 ', 'ATAU ','BTAU ','ITY ','Z2CH ' ], &
SRC_ID = LAND(1), & ! Note (1) !
DST_ID = LANDASSIM, &
rc = status &
)
VERIFY_(status)
endif
call MAPL_TimerAdd(gc, name="Initialize", rc=status)
VERIFY_(status)
call MAPL_TimerAdd(gc, name="-LocStreamCreate", rc=status)
VERIFY_(status)
call MAPL_TimerAdd(gc, name='Run', rc=status)
VERIFY_(status)
! Terminate all imports
call MAPL_TerminateImport(gc, ALL=.true., rc=status)
VERIFY_(status)
! Call SetServices for children
call MAPL_GenericSetServices(gc, rc=status)
VERIFY_(status)
! End
RETURN_(ESMF_SUCCESS)
end subroutine SetServices
!BOP
! !IROTUINE: Initialize -- initialize method for LDAS GC
! !INTERFACE:
subroutine Initialize(gc, import, export, clock, rc)
!use MAPL_LatLonToCubeRegridderMod
!use MAPL_CubeToLatLonRegridderMod
!use MAPL_CubeToCubeRegridderMod
! !ARGUMENTS:
type(ESMF_GridComp), intent(inout) :: gc ! Gridded component
type(ESMF_State), intent(inout) :: import ! Import state
type(ESMF_State), intent(inout) :: export ! Export state
type(ESMF_Clock), intent(inout) :: clock ! The clock
integer, optional, intent( out) :: rc ! Error code
! !DESCRIPTION:
! The Initialize routine creates the grid, locstream for all surface
! (surf\_locstream). It then splits this 'Surface' locstream based on mask
! (land, lake etc.) and attaches the sub-locstream to the corresponding
! child's GridComp.
!EOP
! ErrLog variables
integer :: status
character(len=ESMF_MAXSTR) :: Iam
character(len=ESMF_MAXSTR) :: comp_name
character(len=ESMF_MAXSTR) :: gridname
! ESMF variables
type(ESMF_Grid) :: agrid ! atospheric grid
type(ESMF_GridComp), pointer :: gcs(:)=>null() ! Children gridcomps
character(len=ESMF_MAXSTR), pointer :: gcnames(:)=>null() ! Children's names
type(ESMF_DELayout) :: layout
type(ESMF_DistGrid) :: distgrid
! MAPL variables
type(MAPL_LocStream) :: surf_locstream
type(MAPL_LocStream) :: land_locstream
type(MAPL_MetaComp), pointer :: MAPL=>null() ! GC's MAPL obj
type(MAPL_MetaComp), pointer :: CHILD_MAPL=>null() ! Child's MAPL obj
! LDAS' tile_coord variable
type(T_TILECOORD_STATE), pointer :: tcinternal
type(TILECOORD_WRAP) :: tcwrap
! Misc variables
character(len=ESMF_MAXSTR) :: tilingfile
character(len=300) :: out_path,decomf
character(len=ESMF_MAXSTR) :: exp_id
character(len=ESMF_MAXSTR) :: LDAS_logit
character(len=ESMF_MAXSTR) :: LAND_PARAMS
character(len=ESMF_MAXSTR) :: grid_type
integer :: total_nt,land_nt_local,i,j
real, pointer :: LandTileLats(:)
real, pointer :: LandTileLons(:)
integer, pointer :: local_id(:)
real(ESMF_KIND_R8), pointer :: centerX(:,:)
real(ESMF_KIND_R8), pointer :: centerY(:,:)
logical :: isEASEv1
logical :: isEASEv2
integer :: I1,IN,J1,JN
real :: lat,lon
type(ESMF_VM) :: vm
integer :: mpierr
logical :: IamRoot
type(tile_coord_type), dimension(:), pointer :: tile_coord_f => null()
integer,dimension(:),pointer :: f2g
integer :: N_catf
integer :: LSM_CHOICE
type(grid_def_type) :: tile_grid_g, pert_grid_g
type(grid_def_type) :: tile_grid_f, pert_grid_f
type(grid_def_type) :: pert_grid_l
type(date_time_type):: start_time
type(ESMF_Time) :: CurrentTime
!type(CubedSphereGridFactory) :: cubed_sphere_factory
!type (CubeToLatLonRegridder) :: cube_to_latlon_prototype
!type (LatLonToCubeRegridder) :: latlon_to_cube_prototype
!type (CubeToCubeRegridder) :: cube_to_cube_prototype
real :: DT, DT_Solar
type(ESMF_Alarm) :: SolarAlarm
type(ESMF_TimeInterval) :: Solar_DT
! Begin...
! Get component's name and setup traceback handle
call ESMF_GridCompget(gc, name=comp_name, rc=status)
VERIFY_(status)
Iam = trim(comp_name) // "::Initialize"
! Get MAPL obj
call MAPL_GetObjectFromGC(gc, MAPL, rc=status)
VERIFY_(status)
! MPI
call ESMF_VmGetCurrent(vm, rc=status)
VERIFY_(status)
IAmRoot = MAPL_Am_I_Root(vm)
call ESMF_VmGet(vm, mpicommunicator=mpicomm, rc=status)
VERIFY_(status)
call MPI_COMM_RANK(mpicomm, myid,mpierr)
call MPI_COMM_SIZE(mpicomm, numprocs, mpierr )
root_proc = IAmRoot
! Turn timers on
call MAPL_TimerOn(MAPL, "TOTAL")
call MAPL_TimerOn(MAPL, "Initialize")
call ESMF_ClockGet(clock, currTime=CurrentTime, rc=status)
VERIFY_(status)
call esmf2ldas(CurrentTime, start_time, rc=status)
VERIFY_(status)
call MAPL_GetResource(MAPL,LDAS_logit,'LDAS_logit:',default = "NO",rc = status)
VERIFY_(status)
logit = (trim(LDAS_logit) /= 'NO')
root_logit = (IamRoot .and. logit)
! Init catchment constants, currently different in GCM and GEOSldas
call MAPL_GetResource(MAPL, LAND_PARAMS,Label="LAND_PARAMS:",DEFAULT="Icarus",RC=STATUS)
VERIFY_(STATUS)
call MAPL_GetResource ( MAPL, LSM_CHOICE, Label="LSM_CHOICE:", DEFAULT=1, RC=STATUS)
VERIFY_(STATUS)
call SurfParams_init(LAND_PARAMS,LSM_CHOICE,RC=STATUS)
VERIFY_(STATUS)
call MAPL_GetResource(MAPL, grid_type,Label="GEOSldas.GRID_TYPE:",RC=STATUS)
VERIFY_(STATUS)
! if (trim(grid_type) == "Cubed-Sphere") then
! call grid_manager%add_prototype("Cubed-Sphere", cubed_sphere_factory)
! associate (method => REGRID_METHOD_BILINEAR, mgr => regridder_manager)
! call mgr%add_prototype('Cubed-Sphere', 'LatLon', method, cube_to_latlon_prototype)
! call mgr%add_prototype('LatLon', 'Cubed-Sphere', method, latlon_to_cube_prototype)
! call mgr%add_prototype('Cubed-Sphere', 'Cubed-Sphere', method, cube_to_cube_prototype)
! end associate
! endif
! Create atmospheric (single level atm grid covers all of surface) grid
call MAPL_GridCreate(gc, rc=status)
VERIFY_(status)
! Get grid info from the gridcomp
call ESMF_GridCompGet(gc, grid=agrid, rc=status)
VERIFY_(status)
! Get distgrid info from grid
call ESMF_GridGet(agrid, distgrid=distgrid, rc=status)
VERIFY_(status)
! Get DElayout info from distgrid
call ESMF_DistGridGet(distgrid, delayout=layout, rc=status)
VERIFY_(status)
! get grid name
call ESMF_GridGet(agrid, name=gridname, rc=status)
VERIFY_(STATUS)
isEASEv1 =.false.
isEASEv2 =.false.
if (index(gridname,'EASEv2') /=0) then
isEASEv2 = .true.
else if (index(gridname,'EASE') /=0) then
isEASEv1 = .true.
endif
if( isEASEv1) then
! To be implemented
endif
if( isEASEv2) then
! Retrieve the coordinates so we can set them
call ESMF_GridGetCoord(agrid, coordDim=1, localDE=0, &
staggerloc=ESMF_STAGGERLOC_CENTER, &
farrayPtr=centerX, rc=status)
VERIFY_(STATUS)
call ESMF_GridGetCoord(agrid, coordDim=2, localDE=0, &
staggerloc=ESMF_STAGGERLOC_CENTER, &
farrayPtr=centerY, rc=status)
VERIFY_(STATUS)
call ESMF_GRID_INTERIOR(agrid,I1,IN,J1,JN)
do I = 1,size(centerX,1)
call ease_inverse(gridname,1.0*(I+I1-2),0.0,lat,lon)
centerX(I,:) = lon * MAPL_DEGREES_TO_RADIANS
enddo
do J = 1,size(centerY,2)
call ease_inverse(gridname,0.0,1.0*(J+J1-2),lat,lon)
centerY(:,J) = lat * MAPL_DEGREES_TO_RADIANS
enddo
endif
! Get tile file location
call MAPL_GetResource( &
MAPL, &
tilingfile, &
'TILING_FILE:', &
default = "tile.data", &
rc = status &
)
VERIFY_(status)
!print*," Create LocStream for all surface (land, lake, landice, saltwater)"
call MAPL_TimerOn(MAPL, "-LocStreamCreate")
call MAPL_LocStreamCreate( &
surf_locstream, &
layout = layout, &
filename = tilingfile, &
name = "Surface", &
grid = agrid, &
rc = status &
)
VERIFY_(status)
call MAPL_TimerOff(MAPL, "-LocStreamCreate")
! Get children and their im/ex states from MAPL obj
call MAPL_Get(MAPL, GCS=gcs, GCNAMES=gcnames, rc=status)
VERIFY_(status)
! Create LAND's locstreams as subset of Surface locstream
! and add it to the children's MAPL objects
call MAPL_TimerOn(MAPL, "-LocStreamCreate")
call MAPL_LocStreamCreate( &
land_locstream, &
surf_locstream, &
name=gcnames(LAND(1)), &
mask=[MAPL_LAND], &
rc=status &
)
VERIFY_(status)
call MAPL_TimerOff(MAPL, "-LocStreamCreate")
! Convert LAND's LocStream to LDAS' tile_coord and save it in the GridComp
! -get-tile-information-from-land's-locstream-
call MAPL_LocStreamGet( &
land_locstream, &
NT_LOCAL=land_nt_local, &
TILELATS=LandTileLats, &
TILELONS=LandTileLons, &
LOCAL_ID=local_id , &
rc=status &
)
VERIFY_(status)
! -get-component's-internal-state-
call ESMF_UserCompGetInternalState(gc, 'TILE_COORD', tcwrap, status)
VERIFY_(status)
tcinternal => tcwrap%ptr
! -allocate-memory-for-tile-coord-
allocate(tcinternal%tile_coord(land_nt_local), stat=status)
VERIFY_(status)
allocate(tcinternal%l2f(land_nt_local))
VERIFY_(status)
call MAPL_GetResource ( MAPL, out_path, Label="OUT_PATH:", DEFAULT="./", RC=STATUS)
call MAPL_GetResource ( MAPL, exp_id, Label="EXP_ID:", DEFAULT="./", RC=STATUS)
call init_MPI_types()
call MPI_Reduce(land_nt_local,total_nt,1,MPI_INT,MPI_SUM,0,mpicomm,mpierr);
decomf = get_io_filename(trim(out_path), trim(exp_id), 'ldas_domdecomp', date_time=start_time, &
dir_name='rc_out', file_ext='.txt')
if (IamRoot) then
call io_domain_files('r',trim(out_path), trim(exp_id),N_catf,f2g,tile_coord_f,tile_grid_g,tile_grid_f,RC)
! WY notes: f2g == tile_coord_f%tile_id
deallocate(f2g)
print*, "Number of tiles: ", N_catf
if(N_catf /= total_nt) then
print*, "total_nt = ", total_nt
stop "tiles number not equal"
endif
open(10,file= trim(decomf), action='write')
write(10,*) N_catf
close(10)
call io_grid_def_type('w', logunit, tile_grid_f, 'tile_grid_f')
! get a grid for perturbations and EnKF:
!
! tile grid ! pert grid
! (defines tile space) ! (used for perturbations and as "hash" grid in EnKF analysis)
! ===========================================================================================================
! lat/lon ! same as tile_grid (i.e., lat/lon)
! -----------------------------------------------------------------------------------------------------------
! EASEv[X] ! same as tile_grid (i.e., EASE)
! -----------------------------------------------------------------------------------------------------------
! cube-sphere ! lat/lon grid of resolution similar to that of (cube-sphere) tile_grid
pert_grid_g = get_pert_grid(tile_grid_g)
if ( .not. (pert_grid_g==tile_grid_g) ) then
! arrive here when tile_grid_g is cube-sphere and pert_grid_g is lat/lon after call to get_pert_grid() above
!1) get pert_i_indg, pert_j_indg for tiles in (full) domain relative to pert_grid_g
do i = 1, N_catf
call get_ij_ind_from_latlon(pert_grid_g,tile_coord_f(i)%com_lat,tile_coord_f(i)%com_lon, &
tile_coord_f(i)%pert_i_indg,tile_coord_f(i)%pert_j_indg)
enddo
!2) determine pert_grid_f
pert_grid_f = get_minExtent_grid(N_catf, tile_coord_f%pert_i_indg, tile_coord_f%pert_j_indg, &
tile_coord_f%min_lon, tile_coord_f%min_lat, tile_coord_f%max_lon, tile_coord_f%max_lat, &
pert_grid_g)
else
pert_grid_f = tile_grid_f
! note that %pert_i_indg and %pert_j_indg were initialized to %i_indg and %j_indg
! in io_tile_coord_type() when tile_coord was read via io_domain_files()
endif
endif
call MPI_BCAST(N_catf,1,MPI_INTEGER,0,mpicomm,mpierr)
if (.not. IamRoot) allocate(tile_coord_f(N_catf))
call MPI_BCAST(tile_coord_f,N_catf, MPI_tile_coord_type,0,mpicomm, mpierr)
call MPI_BCAST(pert_grid_g, 1, MPI_grid_def_type, 0,mpicomm, mpierr)
call MPI_BCAST(pert_grid_f, 1, MPI_grid_def_type, 0,mpicomm, mpierr)
call MPI_BCAST(tile_grid_g, 1, MPI_grid_def_type, 0,mpicomm, mpierr)
block
integer, allocatable :: f2tile_id(:), tile_id2f(:)
integer :: max_id
allocate(f2tile_id(N_catf))
f2tile_id = tile_coord_f%tile_id
max_id = maxval(f2tile_id)
allocate(tile_id2f(max_id),source = 0)
do i = 1, N_catf
tile_id2f(f2tile_id(i)) = i
enddo
tcinternal%l2f = tile_id2f(local_id)
tcinternal%tile_coord = tile_coord_f(tcinternal%l2f)
deallocate(f2tile_id, tile_id2f)
end block
do i = 0, numprocs-1
if( i == myid) then
open(10,file= trim(decomf), action='write',position='append')
do j = 1, land_nt_local
write(10,*) local_id(j), myid
enddo
close(10)
endif
call MPI_Barrier(mpicomm,mpierr)
enddo
allocate(tcinternal%tile_coord_f,source = tile_coord_f)
pert_grid_l = get_minExtent_grid(land_nt_local, &
tcinternal%tile_coord%pert_i_indg, tcinternal%tile_coord%pert_j_indg, &
tcinternal%tile_coord%min_lon, tcinternal%tile_coord%min_lat, &
tcinternal%tile_coord%max_lon, tcinternal%tile_coord%max_lat, &
pert_grid_g)
tcinternal%pgrid_g = pert_grid_g
tcinternal%pgrid_f = pert_grid_f
tcinternal%pgrid_l = pert_grid_l
tcinternal%tgrid_g = tile_grid_g
do i = 1, NUM_ENSEMBLE
call MAPL_GetObjectFromGC(gcs(METFORCE(i)), CHILD_MAPL, rc=status)
VERIFY_(status) ! CHILD = METFORCE
call MAPL_Set(CHILD_MAPL, LocStream=land_locstream, rc=status)
VERIFY_(status)
call ESMF_UserCompSetInternalState(gcs(METFORCE(i)), 'TILE_COORD', tcwrap, status)
VERIFY_(status)
! exit after i=1 if using deterministic forcing
if (.not. ensemble_forcing) exit
enddo
call MAPL_GetObjectFromGC(gcs(ENSAVG), CHILD_MAPL, rc=status)
VERIFY_(status) ! CHILD = ens_avg
call MAPL_Set(CHILD_MAPL, LocStream=land_locstream, rc=status)
VERIFY_(status)
do i = 1,NUM_ENSEMBLE
call MAPL_GetObjectFromGC(gcs(LAND(i)), CHILD_MAPL, rc=status)
VERIFY_(status)
call MAPL_Set(CHILD_MAPL, LocStream=land_locstream, rc=status)
VERIFY_(status)
call MAPL_GetObjectFromGC(gcs(LANDPERT(i)), CHILD_MAPL, rc=status)
VERIFY_(status) ! CHILD = LANDPERT
call MAPL_Set(CHILD_MAPL, LocStream=land_locstream, rc=status)
VERIFY_(status)
! Add LAND's tile_coord to children's GridComps
call ESMF_UserCompSetInternalState(gcs(LAND(i)), 'TILE_COORD', tcwrap, status)
VERIFY_(status)
call ESMF_UserCompSetInternalState(gcs(LANDPERT(i)), 'TILE_COORD', tcwrap, status)
VERIFY_(status)
enddo
if (land_assim .or. mwRTM) then
call MAPL_GetObjectFromGC(gcs(LANDASSIM), CHILD_MAPL, rc=status)
VERIFY_(status)
call MAPL_Set(CHILD_MAPL, LocStream=land_locstream, rc=status)
VERIFY_(status)
call ESMF_UserCompSetInternalState(gcs(LANDASSIM), 'TILE_COORD', tcwrap, status)
VERIFY_(status)
endif
call MAPL_GenericInitialize(gc, import, export, clock, rc=status)
VERIFY_(status)
! solar alarm is created in solar gridcomp. Since GEOSldas doesnot have that gridcomp, it is created here
! -create-nonsticky-alarm-
call MAPL_Get(MAPL, HEARTBEAT = DT, RC=STATUS)
VERIFY_(STATUS)
call MAPL_GetResource ( MAPL, DT_Solar, Label="SOLAR_DT:", DEFAULT=DT, RC=STATUS)
VERIFY_(STATUS)
call ESMF_TimeIntervalSet(SOLAR_DT, s=NINT(DT_Solar), rc=status)
VERIFY_(status)
SolarAlarm = ESMF_AlarmCreate( &
clock, &
name='SOLAR_Alarm', &
ringTime=CurrentTime, &
ringInterval=SOLAR_DT, &
sticky=.false., &
rc=status &
)
VERIFY_(status)
if ( IamRoot) call echo_clsm_ensdrv_glob_param()
if ( IamRoot) call echo_catch_constants(logunit)
if ( IamRoot) call StieglitzSnow_echo_constants(logunit)
! Turn timer off
call MAPL_TimerOff(MAPL, "Initialize")
call MAPL_TimerOff(MAPL, "TOTAL")
RETURN_(ESMF_SUCCESS)
end subroutine Initialize
!BOP
! !IROTUINE: Run -- Run method for the composite Ldas GridComp
! !INTERFACE:
subroutine Run(gc, import, export, clock, rc)
! !ARGUMENTS:
type(ESMF_GridComp), intent(inout) :: gc ! Gridded component
type(ESMF_State), intent(inout) :: import ! Import state
type(ESMF_State), intent(inout) :: export ! Export state
type(ESMF_Clock), intent(inout) :: clock ! The clock
integer, optional, intent( out) :: rc ! Error code
! !DESCRIPTION:
! Calls children's Run methods.
!EOP
! ErrLog variables
integer :: status
character(len=ESMF_MAXSTR) :: Iam
character(len=ESMF_MAXSTR) :: comp_name
! ESMF variables
type(ESMF_VM) :: vm
type(ESMF_GridComp), pointer :: gcs(:)
type(ESMF_State), pointer :: gim(:)
type(ESMF_State), pointer :: gex(:)
type(ESMF_State) :: member_export
character(len=ESMF_MAXSTR), pointer :: gcnames(:)
type(ESMF_Time) :: ModelTimeCur
character(len=ESMF_MAXSTR) :: member_name
character(len=ESMF_MAXSTR) :: ensid_string
! MAPL variables
type(MAPL_MetaComp), pointer :: MAPL
! Misc variables
integer :: igc,i, ens_id, FIRST_ENS_ID, ens_id_width
logical :: IAmRoot
integer :: LSM_CHOICE
type (ESMF_Field) :: field
! Begin...
! Get component's name and setup traceback handle
call ESMF_GridCompget(gc, name=comp_name, rc=status)
VERIFY_(status)
Iam = trim(comp_name) // "::Run"
! Get MAPL obj
call MAPL_GetObjectFromGC(gc, MAPL, rc=status)
VERIFY_(status)
! Turn timers on
call MAPL_TimerOn(MAPL, "TOTAL")
call MAPL_TimerOn(MAPL, "Run")
! Get information about children
call MAPL_Get(MAPL, GCS=gcs, GIM=gim, GEX=gex, GCNAMES=gcnames, rc=status)
VERIFY_(STATUS)
call MAPL_GetResource ( MAPL, ens_id_width, Label="ENS_ID_WIDTH:", DEFAULT=0, RC=STATUS)
VERIFY_(STATUS)
call MAPL_GetResource ( MAPL, FIRST_ENS_ID, Label="FIRST_ENS_ID:", DEFAULT=0, RC=STATUS)
VERIFY_(STATUS)
! MPI
call ESMF_VmGetCurrent(vm, rc=status)
VERIFY_(status)
IAmRoot = MAPL_Am_I_Root(vm)
!call ESMF_VmGet(vm, mpicommunicator=mpicomm, rc=status)
!VERIFY_(status)
call MAPL_GetResource ( MAPL, LSM_CHOICE, Label="LSM_CHOICE:", DEFAULT=1, RC=STATUS)
! Get current time
call ESMF_ClockGet(clock, currTime=ModelTimeCur, rc=status)
VERIFY_(status)
if (IAmRoot) then
call ESMF_TimePrint(ModelTimeCur, options='string', rc=status)
VERIFY_(status)
end if
!phase2 initialization ( executed once)
!adjust mean of perturbed forcing or Progn
do i = 1,NUM_ENSEMBLE
igc = LANDPERT(i)
call MAPL_TimerOn(MAPL, gcnames(igc))
call ESMF_GridCompRun(gcs(igc), importState=gim(igc), exportState=gex(igc), clock=clock, phase=1, userRC=status)
VERIFY_(status)
call MAPL_TimerOff(MAPL, gcnames(igc))
enddo
! Run children GridComps (in order)
! Generate raw perturbed force and progn
do i = 1,NUM_ENSEMBLE
igc = LANDPERT(i)
call MAPL_TimerOn(MAPL, gcnames(igc))
call ESMF_GridCompRun(gcs(igc), importState=gim(igc), exportState=gex(igc), clock=clock, phase=2, userRC=status)
VERIFY_(status)
call MAPL_TimerOff(MAPL, gcnames(igc))
enddo
do i = 1, NUM_ENSEMBLE
igc = METFORCE(i)
call MAPL_TimerOn(MAPL, gcnames(igc))
call ESMF_GridCompRun(gcs(igc), importState=gim(igc), exportState=gex(igc), clock=clock, userRC=status)
VERIFY_(status)
call MAPL_TimerOff(MAPL, gcnames(igc))
! exit after i=1 if using deterministic forcing
if (.not. ensemble_forcing) exit
enddo
do i = 1,NUM_ENSEMBLE
!ApplyForcePert
igc = LANDPERT(i)
call MAPL_TimerOn(MAPL, gcnames(igc))
call ESMF_GridCompRun(gcs(igc), importState=gim(igc), exportState=gex(igc), clock=clock, phase=3, userRC=status)
VERIFY_(status)
call MAPL_TimerOff(MAPL, gcnames(igc))
! Use landpert's output as the input to calculate the ensemble average forcing
! W.J note: So far it is only for the Catchment model.
! To make CatchmentCN work with assim, the export from landgrid and catchmentCN grid need to be modified.
if ( LSM_CHOICE == 1 ) then
call ESMF_GridCompRun(gcs(ENSAVG), importState=gex(igc), exportState=gex(ENSAVG), clock=clock,phase=1, userRC=status)
VERIFY_(status)
endif
! Run the land model
igc = LAND(i)
call MAPL_TimerOn(MAPL, gcnames(igc))
call ESMF_GridCompRun(gcs(igc), importState=gim(igc), exportState=gex(igc), clock=clock, phase=1, userRC=status)
VERIFY_(status)
call ESMF_GridCompRun(gcs(igc), importState=gim(igc), exportState=gex(igc), clock=clock, phase=2, userRC=status)
VERIFY_(status)
call MAPL_TimerOff(MAPL, gcnames(igc))
! ApplyPrognPert - moved: now before calculating ensemble average that is picked up by land analysis and HISTORY; reichle 28 May 2020
igc = LANDPERT(i)
call MAPL_TimerOn(MAPL, gcnames(igc))
call ESMF_GridCompRun(gcs(igc), importState=gim(igc), exportState=gex(igc), clock=clock, phase=4, userRC=status)
VERIFY_(status)
call MAPL_TimerOff(MAPL, gcnames(igc))
! Use LAND's output as the input to calculate the ensemble average
igc = LAND(i)
if (LSM_CHOICE == 1) then
! collect cat_param
ens_id = i-1 + FIRST_ENS_ID ! id start form FIRST_ENS_ID
call get_ensid_string(ensid_string, ens_id, ens_id_width, NUM_ENSEMBLE)
member_name = 'CATCH'//trim(ensid_string)//"_Exports"
call ESMF_StateGet(gex(igc), trim(member_name), member_export, _RC)
call ESMF_StateGet(gex(igc), "Z2CH", field, _RC)
call ESMF_StateAddReplace(member_export, [field],_RC)
call ESMF_StateGet(gex(igc), "LAI", field, _RC)
call ESMF_StateAddReplace(member_export, [field],_RC)
call ESMF_GridCompRun(gcs(ENSAVG), importState=member_export, exportState=gex(ENSAVG), clock=clock,phase=3, userRC=status)
VERIFY_(status)
call ESMF_GridCompRun(gcs(ENSAVG), importState=member_export, exportState=gex(ENSAVG), clock=clock,phase=2, userRC=status)
VERIFY_(status)
if( mwRTM ) then
! Calculate ensemble-average L-band Tb using LAND's output (add up and normalize after last member has been added)
call ESMF_GridCompRun(gcs(LANDASSIM), importState=member_export, exportState=gex(LANDASSIM), clock=clock,phase=3, userRC=status)
VERIFY_(status)
endif
endif
enddo
if ( mwRTM .and. LSM_CHOICE == 1 ) then
! output_smapl4smlmc
call ESMF_GridCompRun(gcs(LANDASSIM), importState=gim(LANDASSIM), exportState=gex(LANDASSIM), clock=clock,phase=4, userRC=status)
VERIFY_(status)
endif
! Run land analysis
if (land_assim) then
igc = LANDASSIM
call MAPL_TimerOn(MAPL, gcnames(igc))
! Get EnKF increments and apply to "cat_progn" (imported from ENSAVG via "use" statement!); otherwise import state is export from ENSAVG
call ESMF_GridCompRun(gcs(igc), importState=gex(ENSAVG), exportState=gex(igc), clock=clock, phase=1, userRC=status)
VERIFY_(status)
do i = 1, NUM_ENSEMBLE
! Extract updated exports from "cat_progn"
call ESMF_GridCompRun(gcs(igc), importState=gim(igc), exportState=gex(LAND(i)), clock=clock, phase=2, userRC=status)
VERIFY_(status)
enddo
call MAPL_TimerOff(MAPL, gcnames(igc))
endif
! Turn timers off
call MAPL_TimerOff(MAPL, "Run")
call MAPL_TimerOff(MAPL, "TOTAL")
! End
RETURN_(ESMF_SUCCESS)
end subroutine Run
!BOP