-
Notifications
You must be signed in to change notification settings - Fork 79
/
med_phases_prep_glc_mod.F90
1062 lines (890 loc) · 49.3 KB
/
med_phases_prep_glc_mod.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 med_phases_prep_glc_mod
!-----------------------------------------------------------------------------
! Mediator phases for preparing glc export from mediator
!-----------------------------------------------------------------------------
use med_kind_mod , only : CX=>SHR_KIND_CX, CS=>SHR_KIND_CS, CL=>SHR_KIND_CL, R8=>SHR_KIND_R8
use NUOPC , only : NUOPC_CompAttributeGet
use ESMF , only : ESMF_LogWrite, ESMF_LOGMSG_INFO, ESMF_LOGMSG_ERROR, ESMF_SUCCESS, ESMF_FAILURE
use ESMF , only : ESMF_VM, ESMF_VMGet, ESMF_VMAllReduce, ESMF_REDUCE_SUM
use ESMF , only : ESMF_Clock,ESMF_ClockGetAlarm
use ESMF , only : ESMF_Alarm, ESMF_AlarmIsRinging, ESMF_AlarmRingerOff
use ESMF , only : ESMF_GridComp, ESMF_GridCompGet
use ESMF , only : ESMF_FieldBundle, ESMF_FieldBundleGet, ESMF_FieldBundleAdd
use ESMF , only : ESMF_FieldBundleCreate, ESMF_FieldBundleIsCreated
use ESMF , only : ESMF_Field, ESMF_FieldGet, ESMF_FieldCreate
use ESMF , only : ESMF_Array, ESMF_ArrayGet, ESMF_ArrayCreate, ESMF_ArrayDestroy
use ESMF , only : ESMF_DistGrid, ESMF_AttributeSet
use ESMF , only : ESMF_Mesh, ESMF_MeshGet, ESMF_MESHLOC_ELEMENT, ESMF_TYPEKIND_R8
use esmFlds , only : compglc, complnd, mapbilnr, mapconsd, mapconsf, compname
use med_internalstate_mod , only : InternalState, mastertask, logunit
use med_constants_mod , only : dbug_flag=>med_constants_dbug_flag
use med_map_mod , only : med_map_routehandles_init, med_map_rh_is_created
use med_map_mod , only : med_map_field_normalized, med_map_field
use med_methods_mod , only : FB_diagnose => med_methods_FB_diagnose
use med_methods_mod , only : FB_reset => med_methods_FB_reset
use med_utils_mod , only : chkerr => med_utils_ChkErr
use glc_elevclass_mod , only : glc_get_num_elevation_classes
use glc_elevclass_mod , only : glc_get_elevation_classes
use glc_elevclass_mod , only : glc_get_fractional_icecov
use perf_mod , only : t_startf, t_stopf
implicit none
private
public :: med_phases_prep_glc_init
public :: med_phases_prep_glc_accum
public :: med_phases_prep_glc_avg
private :: map_lnd2glc
private :: med_phases_prep_glc_renormalize_smb
! glc fields with multiple elevation classes: lnd->glc
! - fields sent from lnd->med to glc ARE IN multiple elevation classes
! - fields sent from med->glc from land ARE NOT IN multiple elevation classes
! Need to keep track of the lnd->med fields destined for glc in the FBlndAccum field bundle.
! Needed for standard lnd->glc mapping
type(ESMF_FieldBundle) :: FBlndAccum_lnd
type(ESMF_FieldBundle) :: FBlndAccum_glc
integer :: FBlndAccumCnt
character(len=14) :: fldnames_fr_lnd(3) = (/'Flgl_qice_elev','Sl_tsrf_elev ','Sl_topo_elev '/)
character(len=14) :: fldnames_to_glc(2) = (/'Flgl_qice ','Sl_tsrf '/)
! Whether to renormalize the SMB for conservation.
! Should be set to true for 2-way coupled runs with evolving ice sheets.
! Does not need to be true for 1-way coupling.
logical :: smb_renormalize
! Needed if renormalize SMB
type(ESMF_Field) :: field_glc_icemask_g
type(ESMF_Field) :: field_glc_icemask_l
type(ESMF_Field) :: field_glc_frac_g
type(ESMF_Field) :: field_glc_frac_l
type(ESMF_Field) :: field_glc_frac_g_ec
type(ESMF_Field) :: field_glc_frac_l_ec
type(ESMF_Field) :: field_lnd_icemask_l
type(ESMF_Field) :: field_lfrac_g
real(r8) , pointer :: aream_l(:) => null() ! cell areas on land grid, for mapping
real(r8) , pointer :: aream_g(:) => null() ! cell areas on glc grid, for mapping
character(len=*), parameter :: qice_fieldname = 'Flgl_qice' ! Name of flux field giving surface mass balance
character(len=*), parameter :: Sg_frac_fieldname = 'Sg_ice_covered'
character(len=*), parameter :: Sg_topo_fieldname = 'Sg_topo'
character(len=*), parameter :: Sg_icemask_fieldname = 'Sg_icemask'
! Size of undistributed dimension from land
integer :: ungriddedCount ! this equals the number of elevation classes + 1 (for bare land)
logical :: init_prep_glc = .false.
character(*), parameter :: u_FILE_u = &
__FILE__
!================================================================================================
contains
!================================================================================================
subroutine med_phases_prep_glc_init(gcomp, rc)
!---------------------------------------
! Create land accumulation field bundles on and and glc grid and initialize accumulation count
!---------------------------------------
! input/output variables
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
type(InternalState) :: is_local
integer :: i,n,ncnt
type(ESMF_Mesh) :: lmesh_glc
type(ESMF_Mesh) :: lmesh_lnd
type(ESMF_Field) :: lfield
real(r8), pointer :: data2d_in(:,:) => null()
real(r8), pointer :: data2d_out(:,:) => null()
real(r8), pointer :: dataptr1d(:) => null()
character(len=CS) :: glc_renormalize_smb
logical :: glc_coupled_fluxes
type(ESMF_Array) :: larray
type(ESMF_DistGrid) :: ldistgrid
integer :: lsize
logical :: isPresent
integer :: fieldCount
type(ESMF_Field), pointer :: fieldlist(:) => null()
integer :: ungriddedUBound_output(1) ! currently the size must equal 1 for rank 2 fieldds
character(len=*),parameter :: subname='(med_phases_prep_glc_init)'
!---------------------------------------
call t_startf('MED:'//subname)
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
endif
rc = ESMF_SUCCESS
!---------------------------------------
! Get the internal state
!---------------------------------------
nullify(is_local%wrap)
call ESMF_GridCompGetInternalState(gcomp, is_local, rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
!---------------------------------------
! Count the number of fields outside of scalar data
!---------------------------------------
if (.not. ESMF_FieldBundleIsCreated(is_local%wrap%FBImp(complnd,complnd))) then
ncnt = 0
call ESMF_LogWrite(trim(subname)//": FBImp(complnd,complnd) is not created", ESMF_LOGMSG_INFO)
else
! The scalar field has been removed from all mediator field bundles - so determine ncnt for below
call ESMF_FieldBundleGet(is_local%wrap%FBImp(complnd,complnd), fieldCount=ncnt, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end if
!---------------------------------------
! Create field bundles for the fldnames_fr_lnd that have an
! undistributed dimension corresponding to elevation classes
!---------------------------------------
if (ncnt > 0) then
! Create accumulation field bundle from land on the land grid (including bare land)
call ESMF_FieldBundleGet(is_local%wrap%FBImp(complnd,complnd), fldnames_fr_lnd(1), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, ungriddedUBound=ungriddedUBound_output, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
ungriddedCount = ungriddedUBound_output(1)
! TODO: check that ungriddedCount = glc_nec+1
call ESMF_FieldBundleGet(is_local%wrap%FBImp(complnd,complnd), fieldCount=fieldCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
allocate(fieldlist(fieldcount))
call ESMF_FieldBundleGet(is_local%wrap%FBImp(complnd,complnd), fieldlist=fieldlist, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(fieldlist(1), mesh=lmesh_lnd, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
deallocate(fieldlist)
FBlndAccum_lnd = ESMF_FieldBundleCreate(name='FBlndAccum_lnd', rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
do n = 1,size(fldnames_fr_lnd)
lfield = ESMF_FieldCreate(lmesh_lnd, ESMF_TYPEKIND_R8, name=fldnames_fr_lnd(n), &
meshloc=ESMF_MESHLOC_ELEMENT, &
ungriddedLbound=(/1/), ungriddedUbound=(/ungriddedCount/), gridToFieldMap=(/2/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldBundleAdd(FBlndAccum_lnd, (/lfield/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_LogWrite(trim(subname)//' adding field '//trim(fldnames_fr_lnd(n))//' to FBLndAccum_lnd', &
ESMF_LOGMSG_INFO)
end do
call FB_reset(FBlndAccum_lnd, value=0.0_r8, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Create accumulation field bundle from land on the glc grid
! Determine glc mesh from the mesh from the first export field to glc
! However FBlndAccum_glc has the fields fldnames_fr_lnd BUT ON the glc grid
call ESMF_FieldBundleGet(is_local%wrap%FBExp(compglc), fieldCount=fieldCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
allocate(fieldlist(fieldcount))
call ESMF_FieldBundleGet(is_local%wrap%FBExp(compglc), fieldlist=fieldlist, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(fieldlist(1), mesh=lmesh_glc, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
deallocate(fieldlist)
FBlndAccum_glc = ESMF_FieldBundleCreate(name='FBlndAccum_glc', rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
do n = 1,size(fldnames_fr_lnd)
lfield = ESMF_FieldCreate(lmesh_glc, ESMF_TYPEKIND_R8, name=fldnames_fr_lnd(n), &
meshloc=ESMF_MESHLOC_ELEMENT, &
ungriddedLbound=(/1/), ungriddedUbound=(/ungriddedCount/), gridToFieldMap=(/2/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldBundleAdd(FBlndAccum_glc, (/lfield/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end do
call FB_reset(FBlndAccum_glc, value=0.0_r8, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Create land fraction field on glc mesh (this is just needed for normalization mapping)
field_lfrac_g = ESMF_FieldCreate(lmesh_glc, ESMF_TYPEKIND_R8, meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Create route handle if it has not been created
if (.not. med_map_RH_is_created(is_local%wrap%RH(complnd,compglc,:),mapbilnr,rc=rc)) then
call ESMF_LogWrite(trim(subname)//" mapbilnr is not created for lnd->glc mapping", &
ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u)
rc = ESMF_FAILURE
return
end if
! Determine if renormalize smb
call NUOPC_CompAttributeGet(gcomp, name='glc_renormalize_smb', value=glc_renormalize_smb, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! TODO: talk to Bill Sacks to determine if this is the correct logic
glc_coupled_fluxes = is_local%wrap%med_coupling_active(compglc,complnd)
! Note glc_coupled_fluxes should be false in the no_evolve cases
! Goes back to the zero-gcm fluxes variable - if zero-gcm fluxes is true than do not renormalize
! The user can set this to true in an evolve cases
select case (glc_renormalize_smb)
case ('on')
smb_renormalize = .true.
case ('off')
smb_renormalize = .false.
case ('on_if_glc_coupled_fluxes')
if (.not. glc_coupled_fluxes) then
! Do not renormalize if med_coupling_active is not true for compglc->complnd
! In this case, conservation is not important
smb_renormalize = .false.
else
smb_renormalize = .true.
end if
case default
write(logunit,*) subname,' ERROR: unknown value for glc_renormalize_smb: ', trim(glc_renormalize_smb)
call ESMF_LogWrite(trim(subname)//' ERROR: unknown value for glc_renormalize_smb: '// trim(glc_renormalize_smb), &
ESMF_LOGMSG_ERROR, line=__LINE__, file=__FILE__)
rc = ESMF_FAILURE
return
end select
! -------------------------------
! If smb will be renormalized then...
! -------------------------------
if (smb_renormalize) then
! determine areas on land mesh
call ESMF_MeshGet(lmesh_lnd, numOwnedElements=lsize, elementDistGrid=ldistgrid, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
allocate(aream_l(lsize), dataptr1d(lsize))
lArray = ESMF_ArrayCreate(ldistgrid, dataptr1d, rc=rc)
call ESMF_MeshGet(lmesh_lnd, elemMaskArray=lArray, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
aream_l(:) = dataptr1d(:)
call ESMF_ArrayDestroy(larray, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
deallocate(dataptr1d)
! determine areas on glc mesh
call ESMF_MeshGet(lmesh_glc, numOwnedElements=lsize, elementDistGrid=ldistgrid, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
allocate(aream_g(lsize), dataptr1d(lsize))
lArray = ESMF_ArrayCreate(ldistgrid, dataptr1d, rc=rc)
call ESMF_MeshGet(lmesh_glc, elemMaskArray=lArray, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
aream_g(:) = dataptr1d(:)
call ESMF_ArrayDestroy(larray, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
deallocate(dataptr1d)
! ice mask without elevation classes on glc and lnd
field_glc_icemask_g = ESMF_FieldCreate(lmesh_glc, ESMF_TYPEKIND_R8, meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
field_glc_icemask_l = ESMF_FieldCreate(lmesh_lnd, ESMF_TYPEKIND_R8, meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! ice fraction without multiple elevation classes on glc and lnd
field_glc_frac_g = ESMF_FieldCreate(lmesh_glc, ESMF_TYPEKIND_R8, meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
field_glc_frac_l = ESMF_FieldCreate(lmesh_lnd, ESMF_TYPEKIND_R8, meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! ice fraction in multiple elevation classes on glc and lnd - NOTE that this includes bare land
field_glc_frac_g_ec = ESMF_FieldCreate(lmesh_glc, ESMF_TYPEKIND_R8, meshloc=ESMF_MESHLOC_ELEMENT, &
ungriddedLbound=(/1/), ungriddedUbound=(/ungriddedCount/), gridToFieldMap=(/2/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
field_glc_frac_l_ec = ESMF_FieldCreate(lmesh_lnd, ESMF_TYPEKIND_R8, meshloc=ESMF_MESHLOC_ELEMENT, &
ungriddedLbound=(/1/), ungriddedUbound=(/ungriddedCount/), gridToFieldMap=(/2/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Create route handle if it has not been created - this will be needed to map the fractions
if (.not. med_map_RH_is_created(is_local%wrap%RH(compglc,complnd,:),mapconsf,rc=rc)) then
call med_map_routehandles_init( compglc, complnd, &
FBSrc=is_local%wrap%FBImp(compglc,compglc), &
FBDst=is_local%wrap%FBImp(compglc,complnd), &
mapindex=mapconsf, &
RouteHandle=is_local%wrap%RH, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end if
end if
end if
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
end if
call t_stopf('MED:'//subname)
end subroutine med_phases_prep_glc_init
!================================================================================================
subroutine med_phases_prep_glc_accum(gcomp, rc)
!---------------------------------------
! Carry out accumulation for the land-ice (glc) component
! Accumulation and averaging is done on the land input to the river component on the land grid
! Mapping from the land to the glc grid is then done with the time averaged fields
!---------------------------------------
! input/output variables
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
type(InternalState) :: is_local
type(ESMF_Field) :: lfield
integer :: i,n,ncnt
real(r8), pointer :: data2d_in(:,:) => null()
real(r8), pointer :: data2d_out(:,:) => null()
character(len=*),parameter :: subname='(med_phases_prep_glc_accum)'
!---------------------------------------
call t_startf('MED:'//subname)
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
endif
rc = ESMF_SUCCESS
!---------------------------------------
! Get the internal state
!---------------------------------------
nullify(is_local%wrap)
call ESMF_GridCompGetInternalState(gcomp, is_local, rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
!---------------------------------------
! Count the number of fields outside of scalar data
!---------------------------------------
if (.not. ESMF_FieldBundleIsCreated(is_local%wrap%FBImp(complnd,complnd))) then
ncnt = 0
call ESMF_LogWrite(trim(subname)//": FBImp(complnd,complnd) is not created", ESMF_LOGMSG_INFO)
else
! The scalar field has been removed from all mediator field bundles - so determine ncnt for below
call ESMF_FieldBundleGet(is_local%wrap%FBImp(complnd,complnd), fieldCount=ncnt, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end if
!---------------------------------------
! accumulator land input to glc on land grid
!---------------------------------------
if (ncnt > 0) then
! Initialize module variables needed to accumulate input to glc
if (.not. init_prep_glc) then
call med_phases_prep_glc_init(gcomp, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
init_prep_glc = .true.
end if
do n = 1, size(fldnames_fr_lnd)
call ESMF_FieldBundleGet(is_local%wrap%FBImp(complnd,complnd), fieldname=trim(fldnames_fr_lnd(n)), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=data2d_in, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldBundleGet(FBlndAccum_lnd, fieldname=fldnames_fr_lnd(n), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=data2d_out, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
do i = 1,size(data2d_out, dim=2)
data2d_out(:,i) = data2d_out(:,i) + data2d_in(:,i)
end do
end do
FBlndAccumCnt = FBlndAccumCnt + 1
if (dbug_flag > 1) then
call FB_diagnose(FBlndAccum_lnd, string=trim(subname)// ' FBlndAccum_lnd ', rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
end if
end if
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
end if
call t_stopf('MED:'//subname)
end subroutine med_phases_prep_glc_accum
!================================================================================================
subroutine med_phases_prep_glc_avg(gcomp, rc)
!---------------------------------------
! Prepare the GLC export Fields from the mediator
!---------------------------------------
! input/output variables
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
type(InternalState) :: is_local
type(ESMF_Clock) :: clock
type(ESMF_Alarm) :: alarm
type(ESMF_Field) :: lfield
integer :: i, n, ncnt ! counters
real(r8), pointer :: data2d(:,:) => null()
real(r8), pointer :: data2d_import(:,:) => null()
character(len=*) , parameter :: subname='(med_phases_prep_glc_avg)'
!---------------------------------------
call t_startf('MED:'//subname)
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
end if
rc = ESMF_SUCCESS
!---------------------------------------
! Get the internal state
!---------------------------------------
nullify(is_local%wrap)
call ESMF_GridCompGetInternalState(gcomp, is_local, rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
!---------------------------------------
! Count the number of fields outside of scalar data, if zero, then return
!---------------------------------------
! Note - the scalar field has been removed from all mediator field bundles - so this is why we check if the
! fieldCount is 0 and not 1 here
call ESMF_FieldBundleGet(is_local%wrap%FBExp(compglc), fieldCount=ncnt, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
if (ncnt == 0) then
call ESMF_LogWrite(trim(subname)//": only scalar data is present in FBExp(compglc), returning", &
ESMF_LOGMSG_INFO)
call t_stopf('MED:'//subname)
RETURN
end if
! Initialize module variables needed to accumulate input to glc
if (.not. init_prep_glc) then
call med_phases_prep_glc_init(gcomp, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
init_prep_glc = .true.
end if
!---------------------------------------
! Determine if avg alarm is ringing - and if not ringing then return
!---------------------------------------
call ESMF_GridCompGet(gcomp, clock=clock, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_ClockGetAlarm(clock, alarmname='alarm_glc_avg', alarm=alarm, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! If the is ringing - turn it off and continue - otherwise reset field bundle to zero and return
if (ESMF_AlarmIsRinging(alarm, rc=rc)) then
call ESMF_AlarmRingerOff( alarm, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
else
call ESMF_LogWrite(trim(subname)//": glc_avg alarm is not ringing - returning", ESMF_LOGMSG_INFO)
! Reset export field bundle to zero
call FB_reset(is_local%wrap%FBExp(compglc), value=0.0_r8, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! turn on stop timer and return
call t_stopf('MED:'//subname)
! return
RETURN
end if
!---------------------------------------
! Average import from accumulated land import FB
!---------------------------------------
call ESMF_LogWrite(trim(subname)//": glc_avg alarm is ringing - averaging input from lnd to glc", ESMF_LOGMSG_INFO)
do n = 1, size(fldnames_fr_lnd)
call ESMF_FieldBundleGet(FBlndAccum_lnd, fieldname=fldnames_fr_lnd(n), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=data2d, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (FBlndAccumCnt > 0) then
! If accumulation count is greater than 0, do the averaging
data2d(:,:) = data2d(:,:) / real(FBlndAccumCnt)
else
! If accumulation count is 0, then simply set the averaged field bundle values from the land
! to the import field bundle values
call ESMF_FieldBundleGet(is_local%wrap%FBImp(complnd,complnd), fieldname=fldnames_fr_lnd(n), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=data2d_import, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
data2d(:,:) = data2d_import(:,:)
end if
end do
if (dbug_flag > 1) then
call FB_diagnose(FBlndAccum_lnd, string=trim(subname)//' FBlndAccum for after avg for field bundle ', rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
end if
!---------------------------------------
! Map accumulated field bundle from land grid (with elevation classes) to glc grid (without elevation classes)
! and set FBExp(compglc) data
!---------------------------------------
call FB_reset(FBlndAccum_glc, value=0.0_r8, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call map_lnd2glc(gcomp, rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 1) then
call FB_diagnose(is_local%wrap%FBExp(compglc), string=trim(subname)//' FBexp(compglc) ', rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
endif
!---------------------------------------
! zero accumulator and accumulated field bundles
!---------------------------------------
FBlndAccumCnt = 0
call FB_reset(FBlndAccum_lnd, value=0.0_r8, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call FB_reset(FBlndAccum_glc, value=0.0_r8, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
!---------------------------------------
! update local scalar data - set valid input flag to .true. TODO:
!---------------------------------------
!call med_infodata_set_valid_glc_input(.true., med_infodata, rc=rc) ! TODO: fix this
!if (chkErr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
call t_stopf('MED:'//subname)
end subroutine med_phases_prep_glc_avg
!================================================================================================
subroutine map_lnd2glc(gcomp, rc)
!---------------------------------------
! map accumulated land fields from the land to the glc mesh
!---------------------------------------
! input/output variables
type(ESMF_GridComp) , intent(inout) :: gcomp
integer , intent(out) :: rc
! local variables
type(InternalState) :: is_local
real(r8), pointer :: topolnd_g_ec(:,:) => null() ! topo in elevation classes
real(r8), pointer :: dataptr_g(:) => null() ! temporary data pointer for one elevation class
real(r8), pointer :: topoglc_g(:) => null() ! ice topographic height on the glc grid extracted from glc import
real(r8), pointer :: data_ice_covered_g(:) => null() ! data for ice-covered regions on the GLC grid
real(r8), pointer :: ice_covered_g(:) => null() ! if points on the glc grid is ice-covered (1) or ice-free (0)
integer , pointer :: elevclass_g(:) => null() ! elevation classes glc grid
real(r8), pointer :: dataexp_g(:) => null() ! pointer into
real(r8), pointer :: dataptr2d(:,:) => null()
real(r8), pointer :: dataptr1d(:) => null()
real(r8) :: elev_l, elev_u ! lower and upper elevations in interpolation range
real(r8) :: d_elev ! elev_u - elev_l
integer :: nfld, ec
integer :: i,j,n,g,lsize_g
integer :: ungriddedUBound_output(1)
integer :: fieldCount
type(ESMF_Field) :: lfield
type(ESMF_Field) :: field_lfrac_l
type(ESMF_Field), pointer :: fieldlist_lnd(:) => null()
type(ESMF_Field), pointer :: fieldlist_glc(:) => null()
character(len=*) , parameter :: subname='(med_phases_prep_glc_mod:med_phases_prep_glc_map_lnd2glc)'
!---------------------------------------
!---------------------------------------
! Get the internal state
!---------------------------------------
nullify(is_local%wrap)
call ESMF_GridCompGetInternalState(gcomp, is_local, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! ------------------------------------------------------------------------
! Map the accumulate land field from the land grid (in multiple elevation classes)
! to the glc grid (in multiple elevation classes) using bilinear interpolation
! ------------------------------------------------------------------------
! TODO(wjs, 2015-01-20) This implies that we pass data to CISM even in places that
! CISM says is ocean (so CISM will ignore the incoming value). This differs from the
! current glint implementation, which sets acab and artm to 0 over ocean (although
! notes that this could lead to a loss of conservation). Figure out how to handle
! this case.
! get fieldlist from FBlndAccum_lnd
call ESMF_FieldBundleGet(FBlndAccum_lnd, fieldCount=fieldCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
allocate(fieldlist_lnd(fieldcount))
call ESMF_FieldBundleGet(FBlndAccum_lnd, fieldlist=fieldlist_lnd, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
allocate(fieldlist_glc(fieldcount))
call ESMF_FieldBundleGet(FBlndAccum_glc, fieldlist=fieldlist_glc, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! get land fraction field on land mesh
call ESMF_FieldBundleGet(is_local%wrap%FBFrac(complnd), fieldname='lfrac', field=field_lfrac_l, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! TODO: is this needed?
call FB_reset(FBlndAccum_glc, value=0.0_r8, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! map accumlated land fields and normalize by the land fraction
do n = 1,fieldcount
call med_map_field_normalized( &
field_src=fieldlist_lnd(n), &
field_dst=fieldlist_glc(n), &
routehandles=is_local%wrap%RH(complnd,compglc,:), &
maptype=mapbilnr, &
field_normsrc=field_lfrac_l, &
field_normdst=field_lfrac_g, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end do
if (dbug_flag > 1) then
call FB_diagnose(FBlndAccum_lnd, string=trim(subname)//' FBlndAccum_lnd ', rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call FB_diagnose(is_local%wrap%FBfrac(complnd), string=trim(subname)//' FBFrac ', rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call FB_diagnose(FBlndAccum_glc, string=trim(subname)//' FBlndAccum_glc ', rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
endif
! ------------------------------------------------------------------------
! Determine elevation class of each glc point on glc grid (output is topoglc_g)
! ------------------------------------------------------------------------
if (dbug_flag > 1) then
call FB_diagnose(is_local%wrap%FBImp(compglc,compglc), &
string=trim(subname)//' FBImp(compglc,compglc) ', rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
end if
call ESMF_FieldBundleGet(is_local%wrap%FBImp(compglc,compglc), fieldname=trim(Sg_frac_fieldname), field=lfield, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=ice_covered_g, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldBundleGet(is_local%wrap%FBImp(compglc,compglc), fieldname=trim(Sg_topo_fieldname), field=lfield, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=topoglc_g, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
! get elevation classes with bare land
! for grid cells that are ice-free, the elevation class is set to 0.
lsize_g = size(ice_covered_g)
allocate(elevclass_g(lsize_g))
call glc_get_elevation_classes(ice_covered_g, topoglc_g, elevclass_g, logunit)
! ------------------------------------------------------------------------
! Determine topo field in multiple elevation classes on the glc grid
! ------------------------------------------------------------------------
call ESMF_FieldBundleGet(FBlndAccum_glc, fieldname='Sl_topo_elev', field=lfield, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=topolnd_g_ec, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
! ------------------------------------------------------------------------
! Loop over fields in export field bundle to GLC
! ------------------------------------------------------------------------
! TODO(wjs, 2015-01-20) This implies that we pass data to CISM even in places that
! CISM says is ocean (so CISM will ignore the incoming value). This differs from the
! current glint implementation, which sets acab and artm to 0 over ocean (although
! notes that this could lead to a loss of conservation). Figure out how to handle this case.
allocate(data_ice_covered_g(lsize_g))
do nfld = 1, size(fldnames_to_glc)
! ------------------------------------------------------------------------
! Perform vertical interpolation of data onto ice sheet topography
! This maps all of the input elevation classes into an export to glc without elevation classes
! ------------------------------------------------------------------------
! Get a pointer to the land data in multiple elevation classes on the glc grid
call ESMF_FieldBundleGet(FBlndAccum_glc, fieldname=trim(fldnames_fr_lnd(nfld)), &
field=lfield, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=dataptr2d, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
! Get a pointer to the data for the field that will be sent to glc (without elevation classes)
call ESMF_FieldBundleGet(is_local%wrap%FBExp(compglc), fieldname=trim(fldnames_to_glc(nfld)), &
field=lfield, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=dataexp_g, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
! First set data_ice_covered_g to bare land everywehre
data_ice_covered_g(:) = 0._r8
! Now overwrite with valid values
do n = 1, lsize_g
! For each ice sheet point, find bounding EC values...
if (topoglc_g(n) < topolnd_g_ec(2,n)) then
! lower than lowest mean EC elevation value
data_ice_covered_g(n) = dataptr2d(2,n)
else if (topoglc_g(n) >= topolnd_g_ec(ungriddedCount, n)) then
! higher than highest mean EC elevation value
data_ice_covered_g(n) = dataptr2d(ungriddedCount,n)
else
! do linear interpolation of data in the vertical
do ec = 3, ungriddedCount
if (topoglc_g(n) < topolnd_g_EC(ec,n)) then
elev_l = topolnd_g_EC(ec-1,n)
elev_u = topolnd_g_EC(ec ,n)
d_elev = elev_u - elev_l
if (d_elev <= 0) then
! This shouldn't happen, but handle it in case it does. In this case,
! let's arbitrarily use the mean of the two elevation classes, rather
! than the weighted mean.
write(logunit,*) subname//' WARNING: topo diff between elevation classes <= 0'
write(logunit,*) 'n, ec, elev_l, elev_u = ', n, ec, elev_l, elev_u
write(logunit,*) 'Simply using mean of the two elevation classes,'
write(logunit,*) 'rather than the weighted mean.'
data_ice_covered_g(n) = dataptr2d(ec-1,n) * 0.5_r8 &
+ dataptr2d(ec ,n) * 0.5_r8
else
data_ice_covered_g(n) = dataptr2d(ec-1,n) * (elev_u - topoglc_g(n)) / d_elev &
+ dataptr2d(ec ,n) * (topoglc_g(n) - elev_l) / d_elev
end if
exit
end if
end do
end if ! topoglc_g(n)
if (elevclass_g(n) /= 0) then
! ice-covered cells have interpolated values
dataexp_g(n) = data_ice_covered_g(n)
else
! non ice-covered cells have bare land value
dataexp_g(n) = real(dataptr2d(1,n))
end if
end do ! lsize_g
! ------------------------------------------------------------------------
! Renormalize surface mass balance (smb, here named dataexp_g) so that the global
! integral on the glc grid is equal to the global integral on the land grid.
! ------------------------------------------------------------------------
! No longer need to make a preemptive adjustment to qice_g to account for area differences
! between CISM and the coupler. In NUOPC, the area correction is done in! the cap not in the
! mediator, so to preserve the bilinear mapping values, do not need to do any area correction
! scaling in the CISM NUOPC cap
if (smb_renormalize) then
call med_phases_prep_glc_renormalize_smb(gcomp, rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
end if
end do ! end of loop over fields
! clean up memory
deallocate(elevclass_g)
deallocate(data_ice_covered_g)
end subroutine map_lnd2glc
!================================================================================================
subroutine med_phases_prep_glc_renormalize_smb(gcomp, rc)
!------------------
! Renormalizes surface mass balance (smb, here named qice_g) so that the global
! integral on the glc grid is equal to the global integral on the land grid.
!
! (1) Map Sg_icemask from the glc grid to the land grid.
! Because of coupler lags, the current Sg_icemask_l might not be up to date with Sg_icemask_g.
! (2) Map Sg_ice_covered from the glc grid (no elevation classes) to the
! land grid (multiple elevation classes)
!
! This is required for conservation - although conservation is only necessary if we
! are running with a fully-interactive, two-way-coupled glc.
!
! Note that, for a case with full two-way coupling, we will only conserve if the
! actual land cover used over the course of the year matches these currently-remapped
! values. This should generally be the case with the current coupling setup.
!
! One could argue that it would be safer (for conservation purposes) if LND sent its
! grid cell average SMB values, or if it sent its own notion of the area in each
! elevation class for the purpose of creating grid cell average SMB values here. But
! these options cause problems if we're not doing full two-way coupling (e.g., in a TG
! case with dlnd, or in the common case where GLC is a diagnostic component that
! doesn't cause updates in the glacier areas in LND). In these cases without full
! two-way coupling, if we use the LND's notion of the area in each elevation class,
! then the conservation corrections would end up correcting for discrepancies in
! elevation class areas between LND and GLC, rather than just correcting for
! discrepancies arising from the remapping of SMB. (And before you get worried: It
! doesn't matter that we are not conserving in these cases without full two-way
! coupling, because GLC isn't connected with the rest of the system in terms of energy
! and mass in these cases. So in these cases, it's okay that the LND integral computed
! here differs from the integral that LND itself would compute.)
!
! For high-level design, see:
! https://docs.google.com/document/d/1H_SuK6SfCv1x6dK91q80dFInPbLYcOkUj_iAa6WRnqQ/edit
!------------------
! input/output variables
type(ESMF_GridComp) :: gcomp
integer , intent(out) :: rc ! return error code
! local variables
! Note: Sg_icemask defines where the ice sheet model can receive a nonzero SMB from the land model.
type(InternalState) :: is_local
type(ESMF_VM) :: vm
type(ESMF_Field) :: lfield
real(r8) , pointer :: qice_g(:) => null() ! SMB (Flgl_qice) on glc grid without elev classes
real(r8) , pointer :: qice_l_ec(:,:) => null() ! SMB (Flgl_qice) on land grid with elev classes
real(r8) , pointer :: glc_topo_g(:) => null() ! ice topographic height on the glc grid cell
real(r8) , pointer :: glc_frac_g(:) => null() ! total ice fraction in each glc cell
real(r8) , pointer :: glc_frac_g_ec(:,:) => null() ! total ice fraction in each glc cell
real(r8) , pointer :: glc_frac_l_ec(:,:) => null() ! EC fractions (Sg_ice_covered) on land grid
real(r8) , pointer :: Sg_icemask_g(:) => null() ! icemask on glc grid
real(r8) , pointer :: Sg_icemask_l(:) => null() ! icemask on land grid
real(r8) , pointer :: lfrac(:) => null() ! land fraction on land grid
real(r8) , pointer :: dataptr1d(:) => null() ! temporary 1d pointer
real(r8) , pointer :: dataptr2d(:,:) => null() ! temporary 2d pointer
integer :: ec ! loop index over elevation classes
integer :: n
! local and global sums of accumulation and ablation; used to compute renormalization factors
real(r8), target :: local_accum_lnd(1), global_accum_lnd(1)
real(r8), target :: local_accum_glc(1), global_accum_glc(1)
real(r8), target :: local_ablat_lnd(1), global_ablat_lnd(1)
real(r8), target :: local_ablat_glc(1), global_ablat_glc(1)
! renormalization factors (should be close to 1, e.g. in range 0.95 to 1.05)
real(r8) :: accum_renorm_factor ! ratio between global accumulation on the two grids
real(r8) :: ablat_renorm_factor ! ratio between global ablation on the two grids
real(r8) :: effective_area ! grid cell area multiplied by min(lfrac,Sg_icemask_l).
! This is the area that can contribute SMB to the ice sheet model.
character(len=*), parameter :: subname='(med_phases_prep_glc_renormalize_smb)'
!---------------------------------------------------------------
call t_startf('MED:'//subname)
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
end if
rc = ESMF_SUCCESS
!---------------------------------------
! Get the internal state
!---------------------------------------
nullify(is_local%wrap)
call ESMF_GridCompGetInternalState(gcomp, is_local, rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
!---------------------------------------
! Map Sg_icemask_g from the glc grid to the land grid.
!---------------------------------------
! determine Sg_icemask_g and set as contents of FBglc_icemask
call ESMF_FieldBundleGet(is_local%wrap%FBImp(compglc,compglc), fieldname=trim(Sg_icemask_fieldname), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=dataptr1d, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(field_glc_icemask_g, farrayptr=Sg_icemask_g, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
Sg_icemask_g(:) = dataptr1d(:)
! map ice mask from glc to lnd with no normalization
! BUG(wjs, 2017-05-11, #1516) I think we actually want norm = .false. here, but this needs more thought
! Below the implementation is without normalization - this should be checked moving forwards
call med_map_field( &
field_src=field_glc_icemask_g, &
field_dst=field_glc_icemask_l, &
routehandles=is_local%wrap%RH(compglc,complnd,:), &
maptype=mapconsf, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! ------------------------------------------------------------------------
! Map frac_field on glc grid without elevation classes to frac_field on land grid with elevation classes
! ------------------------------------------------------------------------
! set FBglc_frac on the glc grid (fractional ice coverage per elevation class)
! glc_topo_g(:) is the topographic height of each glc gridcell
! glc_frac_g(:) is the total ice fraction in each glc gridcell
! glc_frac_g_ec(:,:) are the glc fractions on the glc grid for each elevation class (inner dimension)
! setting glc_frac_g_ec (in the call to glc_get_fractional_icecov) sets the contents of FBglc_frac
call ESMF_FieldBundleGet(is_local%wrap%FBImp(compglc,compglc), fieldname=trim(Sg_topo_fieldname), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=glc_topo_g, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldBundleGet(is_local%wrap%FBImp(compglc,compglc), fieldname=trim(Sg_frac_fieldname), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=glc_frac_g, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(field_glc_frac_g, farrayptr=glc_frac_g, rc=rc) ! module field
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(field_glc_frac_g_ec, farrayptr=glc_frac_g_ec, rc=rc) ! module field
if (chkerr(rc,__LINE__,u_FILE_u)) return
! note that nec = ungriddedCount - 1
call glc_get_fractional_icecov(ungriddedCount-1, glc_topo_g, glc_frac_g, glc_frac_g_ec, logunit)
! map fraction in each elevation class from the glc grid to the land grid and normalize by the icemask on the
! glc grid
call med_map_field_normalized( &
field_src=field_glc_frac_g_ec, &
field_dst=field_glc_frac_l_ec, &
routehandles=is_local%wrap%RH(compglc,complnd,:), &
maptype=mapconsf, &
field_normsrc=field_glc_icemask_g, &
field_normdst=field_glc_icemask_l, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! get fractional ice coverage for each elevation class on the land grid, glc_frac_l_ec(:,:)
call ESMF_FieldGet(field_glc_frac_l_ec, farrayptr=glc_frac_l_ec, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
! determine fraction on land grid, lfrac(:)
call ESMF_FieldBundleGet(is_local%wrap%FBFrac(complnd), fieldname='lfrac', field=lfield, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=lfrac, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
! get Sg_icemask_l(:)
call ESMF_FieldGet(field_glc_icemask_l, farrayptr=Sg_icemask_l, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
! determine qice_l_ec
call ESMF_FieldBundleGet(FBlndAccum_lnd, trim(qice_fieldname)//'_elev', field=lfield, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, farrayptr=qice_l_ec, rc=rc)
if (chkErr(rc,__LINE__,u_FILE_u)) return
!---------------------------------------
! Sum qice_l_ec over all elevation classes for each local land grid cell then do a global sum
!---------------------------------------
local_accum_lnd(1) = 0.0_r8
local_ablat_lnd(1) = 0.0_r8
do n = 1, size(lfrac)
! Calculate effective area for sum - need the mapped Sg_icemask_l
effective_area = min(lfrac(n), Sg_icemask_l(n)) * aream_l(n)
do ec = 1, ungriddedCount
if (qice_l_ec(ec,n) >= 0.0_r8) then
local_accum_lnd(1) = local_accum_lnd(1) + effective_area * glc_frac_l_ec(ec,n) * qice_l_ec(ec,n)
else
local_ablat_lnd(1) = local_ablat_lnd(1) + effective_area * glc_frac_l_ec(ec,n) * qice_l_ec(ec,n)
endif
enddo ! ec
enddo ! n
call ESMF_GridCompGet(gcomp, vm=vm, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMAllreduce(vm, senddata=local_accum_lnd, recvdata=global_accum_lnd, count=1, reduceflag=ESMF_REDUCE_SUM, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMAllreduce(vm, senddata=local_ablat_lnd, recvdata=global_ablat_lnd, count=1, reduceflag=ESMF_REDUCE_SUM, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return