forked from ESCOMP/CMEPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmed_methods_mod.F90
2675 lines (2164 loc) · 102 KB
/
med_methods_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_methods_mod
!-----------------------------------------------------------------------------
! Generic operation methods used by the Mediator Component.
!-----------------------------------------------------------------------------
use med_kind_mod , only : CX=>SHR_KIND_CX, CS=>SHR_KIND_CS, CL=>SHR_KIND_CL, R8=>SHR_KIND_R8
use ESMF , only : operator(<), operator(/=), operator(+), operator(-), operator(*) , operator(>=)
use ESMF , only : operator(<=), operator(>), operator(==)
use ESMF , only : ESMF_FieldStatus_Flag
use ESMF , only : ESMF_LogWrite, ESMF_LOGMSG_INFO, ESMF_SUCCESS, ESMF_FAILURE
use ESMF , only : ESMF_LOGERR_PASSTHRU, ESMF_LogFoundError, ESMF_LOGMSG_ERROR
use ESMF , only : ESMF_MAXSTR, ESMF_LOGMSG_WARNING
use med_constants_mod , only : dbug_flag => med_constants_dbug_flag
use med_constants_mod , only : czero => med_constants_czero
use med_constants_mod , only : spval_init => med_constants_spval_init
use med_utils_mod , only : ChkErr => med_utils_ChkErr
implicit none
private
interface med_methods_FieldPtr_compare ; module procedure &
med_methods_FieldPtr_compare1, &
med_methods_FieldPtr_compare2
end interface
interface med_methods_check_for_nans
module procedure med_methods_check_for_nans_1d
module procedure med_methods_check_for_nans_2d
end interface med_methods_check_for_nans
! used/reused in module
logical, public :: mediator_checkfornans ! set in med.F90 AdvertiseFields
logical :: isPresent
character(len=1024) :: msgString
type(ESMF_FieldStatus_Flag) :: status
character(*) , parameter :: u_FILE_u = &
__FILE__
public med_methods_FB_copy
public med_methods_FB_accum
public med_methods_FB_average
public med_methods_FB_init
public med_methods_FB_init_pointer
public med_methods_FB_reset
public med_methods_FB_diagnose
public med_methods_FB_write
public med_methods_FB_FldChk
public med_methods_FB_GetFldPtr
public med_methods_FB_getNameN
public med_methods_FB_getFieldN
public med_methods_FB_getNumflds
public med_methods_FB_Field_diagnose
public med_methods_FB_GeomPrint
public med_methods_FB_getdata2d
public med_methods_FB_getdata1d
public med_methods_FB_getmesh
public med_methods_FB_check_for_nans
public med_methods_State_reset
public med_methods_State_diagnose
public med_methods_State_GeomPrint
public med_methods_State_SetScalar
public med_methods_State_GetScalar
public med_methods_State_GetNumFields
public med_methods_Field_getdata1d
public med_methods_Field_getdata2d
public med_methods_Field_diagnose
public med_methods_Field_GeomPrint
public med_methods_FieldPtr_compare
public med_methods_Clock_TimePrint
private med_methods_Mesh_Print
private med_methods_Grid_Print
private med_methods_Field_GetFldPtr
#ifdef DIAGNOSE
private med_methods_Array_diagnose
#endif
private med_methods_check_for_nans
!-----------------------------------------------------------------------------
contains
!-----------------------------------------------------------------------------
subroutine med_methods_FB_init_pointer(StateIn, FBout, flds_scalar_name, name, rc)
! ----------------------------------------------
! Create FBout from StateIn mesh and pointer
! ----------------------------------------------
use ESMF , only : ESMF_Field, ESMF_FieldGet, ESMF_FieldCreate
use ESMF , only : ESMF_FieldBundle, ESMF_FieldBundleAdd, ESMF_FieldBundleCreate
use ESMF , only : ESMF_State, ESMF_StateGet, ESMF_Mesh, ESMF_MeshLoc
use ESMF , only : ESMF_AttributeGet, ESMF_INDEX_DELOCAL
! input/output variables
type(ESMF_State) , intent(in) :: StateIn ! input state
type(ESMF_FieldBundle), intent(inout) :: FBout ! output field bundle
character(len=*) , intent(in) :: flds_scalar_name ! name of scalar fields
character(len=*) , intent(in) :: name
integer , intent(out) :: rc
! local variables
logical :: isPresent
integer :: n,n1
type(ESMF_Field) :: lfield
type(ESMF_Field) :: newfield
type(ESMF_MeshLoc) :: meshloc
type(ESMF_Mesh) :: lmesh
integer :: lrank
integer :: fieldCount
integer :: ungriddedCount
integer :: ungriddedLBound(1)
integer :: ungriddedUBound(1)
real(R8), pointer :: dataptr1d(:)
real(R8), pointer :: dataptr2d(:,:)
character(ESMF_MAXSTR), allocatable :: lfieldNameList(:)
character(len=*), parameter :: subname='(med_methods_FB_init_pointer)'
! ----------------------------------------------
! Create empty FBout
FBout = ESMF_FieldBundleCreate(name=trim(name), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Get fields from StateIn
call ESMF_StateGet(StateIn, itemCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(lfieldNameList(fieldCount))
call ESMF_StateGet(StateIn, itemNameList=lfieldNameList, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Remove scalar field and blank fields from field bundle
do n = 1, fieldCount
if (trim(lfieldnamelist(n)) == trim(flds_scalar_name) .or. trim(lfieldnamelist(n)) == '') then
do n1 = n, fieldCount-1
lfieldnamelist(n1) = lfieldnamelist(n1+1)
enddo
fieldCount = fieldCount - 1
endif
enddo ! n
! Only create the fieldbundle if the number of non-scalar fields is > 0
if (fieldCount > 0) then
! Get mesh from first non-scalar field in StateIn (assumes all the fields have the same mesh)
call ESMF_StateGet(StateIn, itemName=lfieldNameList(1), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, mesh=lmesh, meshloc=meshloc, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Loop over fields in StateIn skipping the field with just scalar data
do n = 1, fieldCount
! get field from StateIn
call ESMF_StateGet(StateIn, itemName=lfieldNameList(n), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! determine rank of field
call ESMF_FieldGet(lfield, rank=lrank, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (lrank == 2) then
! determine ungridded lower and upper bounds for lfield
call ESMF_AttributeGet(lfield, name="UngriddedLBound", convention="NUOPC", &
purpose="Instance", itemCount=ungriddedCount, isPresent=isPresent, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (ungriddedCount /= 1) then
call ESMF_LogWrite(trim(subname)//": ERROR ungriddedCount for "// &
trim(lfieldnamelist(n))//" must be 1 if rank is 2 ", ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
end if
! set ungridded dimensions for field
call ESMF_AttributeGet(lfield, name="UngriddedLBound", convention="NUOPC", &
purpose="Instance", valueList=ungriddedLBound, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_AttributeGet(lfield, name="UngriddedUBound", convention="NUOPC", &
purpose="Instance", valueList=ungriddedUBound, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! get 2d pointer for field
call ESMF_FieldGet(lfield, farrayptr=dataptr2d, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! create new field with an ungridded dimension
newfield = ESMF_FieldCreate(lmesh, dataptr2d, ESMF_INDEX_DELOCAL, &
meshloc=meshloc, name=lfieldNameList(n), &
ungriddedLbound=ungriddedLbound, ungriddedUbound=ungriddedUbound, gridToFieldMap=(/2/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
else if (lrank == 1) then
! get 1d pointer for field
call ESMF_FieldGet(lfield, farrayptr=dataptr1d, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! create new field without an ungridded dimension
newfield = ESMF_FieldCreate(lmesh, dataptr1d, ESMF_INDEX_DELOCAL, &
meshloc=meshloc, name=lfieldNameList(n), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
else
call ESMF_LogWrite(trim(subname)//": ERROR only rank1 and rank2 are supported for rank of fields ", &
ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
end if
! Add new field to FBout
call ESMF_FieldBundleAdd(FBout, (/newfield/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end do ! end of loop over input state fields
end if ! end of fieldcount > 0
deallocate(lfieldNameList)
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//": FBout from input State and field pointers", ESMF_LOGMSG_INFO)
end if
end subroutine med_methods_FB_init_pointer
!-----------------------------------------------------------------------------
subroutine med_methods_FB_init(FBout, flds_scalar_name, fieldNameList, FBgeom, STgeom, FBflds, STflds, name, rc)
! ----------------------------------------------
! Create FBout from fieldNameList, FBflds, STflds, FBgeom or STgeom in that order or priority
! Pass in FBgeom OR STgeom, get mesh from that object
! ----------------------------------------------
use ESMF , only : ESMF_Field, ESMF_FieldBundle, ESMF_FieldBundleCreate, ESMF_FieldBundleGet
use ESMF , only : ESMF_State, ESMF_Mesh, ESMF_StaggerLoc, ESMF_MeshLoc
use ESMF , only : ESMF_StateGet, ESMF_FieldGet, ESMF_FieldBundleAdd, ESMF_FieldCreate
use ESMF , only : ESMF_TYPEKIND_R8, ESMF_FIELDSTATUS_EMPTY, ESMF_AttributeGet
! input/output variables
type(ESMF_FieldBundle), intent(inout) :: FBout ! output field bundle
character(len=*) , intent(in) :: flds_scalar_name ! name of scalar fields
character(len=*) , intent(in), optional :: fieldNameList(:) ! names of fields to use in output field bundle
type(ESMF_FieldBundle), intent(in), optional :: FBgeom ! input field bundle geometry to use
type(ESMF_State) , intent(in), optional :: STgeom ! input state geometry to use
type(ESMF_FieldBundle), intent(in), optional :: FBflds ! input field bundle fields
type(ESMF_State) , intent(in), optional :: STflds ! input state fields
character(len=*) , intent(in), optional :: name ! name to use for output field bundle
integer , intent(out) :: rc
! local variables
integer :: n,n1
integer :: fieldCount,fieldCountgeom
character(ESMF_MAXSTR) :: lname
type(ESMF_Field) :: field,lfield
type(ESMF_Mesh) :: lmesh
type(ESMF_MeshLoc) :: meshloc
integer :: ungriddedCount
integer :: ungriddedCount_in
integer, allocatable :: ungriddedLBound(:)
integer, allocatable :: ungriddedUBound(:)
logical :: isPresent
character(ESMF_MAXSTR), allocatable :: lfieldNameList(:)
character(len=*), parameter :: subname='(med_methods_FB_init)'
! ----------------------------------------------
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
endif
rc = ESMF_SUCCESS
lname = 'undefined'
if (present(name)) then
lname = trim(name)
endif
lname = 'FB '//trim(lname)
!---------------------------------
! check argument consistency and
! verify that geom argument has a field
!---------------------------------
if (present(fieldNameList) .and. present(FBflds) .and. present(STflds)) then
call ESMF_LogWrite(trim(subname)//": ERROR only fieldNameList, FBflds, or STflds can be an argument", &
ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
endif
if (present(FBgeom) .and. present(STgeom)) then
call ESMF_LogWrite(trim(subname)//": ERROR FBgeom and STgeom cannot both be arguments", &
ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
endif
if (.not.present(FBgeom) .and. .not.present(STgeom)) then
call ESMF_LogWrite(trim(subname)//": ERROR FBgeom or STgeom must be an argument", &
ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
endif
if (present(FBgeom)) then
call ESMF_FieldBundleGet(FBgeom, fieldCount=fieldCountGeom, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
elseif (present(STgeom)) then
call ESMF_StateGet(STgeom, itemCount=fieldCountGeom, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
else
call ESMF_LogWrite(trim(subname)//": ERROR FBgeom or STgeom must be passed", ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
endif
!---------------------------------
! determine the names of fields that will be in FBout
!---------------------------------
if (present(fieldNameList)) then
fieldcount = size(fieldNameList)
allocate(lfieldNameList(fieldcount))
lfieldNameList = fieldNameList
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//":"//trim(lname)//" fieldNameList from argument", ESMF_LOGMSG_INFO)
end if
elseif (present(FBflds)) then
call ESMF_FieldBundleGet(FBflds, fieldCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(lfieldNameList(fieldCount))
call ESMF_FieldBundleGet(FBflds, fieldNameList=lfieldNameList, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//":"//trim(lname)//" fieldNameList from FBflds", ESMF_LOGMSG_INFO)
end if
elseif (present(STflds)) then
call ESMF_StateGet(STflds, itemCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(lfieldNameList(fieldCount))
call ESMF_StateGet(STflds, itemNameList=lfieldNameList, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//":"//trim(lname)//" fieldNameList from STflds", ESMF_LOGMSG_INFO)
end if
elseif (present(FBgeom)) then
call ESMF_FieldBundleGet(FBgeom, fieldCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(lfieldNameList(fieldCount))
call ESMF_FieldBundleGet(FBgeom, fieldNameList=lfieldNameList, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//":"//trim(lname)//" fieldNameList from FBgeom", ESMF_LOGMSG_INFO)
end if
elseif (present(STgeom)) then
call ESMF_StateGet(STgeom, itemCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(lfieldNameList(fieldCount))
call ESMF_StateGet(STgeom, itemNameList=lfieldNameList, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//":"//trim(lname)//" fieldNameList from STgeom", ESMF_LOGMSG_INFO)
end if
else
call ESMF_LogWrite(trim(subname)//": ERROR fieldNameList, FBflds, STflds, FBgeom, or STgeom must be passed", &
ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
endif
!---------------------------------
! remove scalar field and blank fields from field bundle
!---------------------------------
do n = 1, fieldCount
if (trim(lfieldnamelist(n)) == trim(flds_scalar_name) .or. &
trim(lfieldnamelist(n)) == '') then
do n1 = n, fieldCount-1
lfieldnamelist(n1) = lfieldnamelist(n1+1)
enddo
fieldCount = fieldCount - 1
endif
enddo ! n
!---------------------------------
! create the mesh(lmesh) that will be used for FBout fields
!---------------------------------
if (fieldcount > 0 .and. fieldcountgeom > 0) then
! Look at only the first field in either the FBgeom and STgeom to get the mesh
if (present(FBgeom)) then
call med_methods_FB_getFieldN(FBgeom, 1, lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//":"//trim(lname)//" mesh from FBgeom", ESMF_LOGMSG_INFO)
end if
elseif (present(STgeom)) then
call med_methods_State_getNameN(STgeom, 1, lname, rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_StateGet(STgeom, itemName=lname, field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//":"//trim(lname)//" mesh from STgeom", ESMF_LOGMSG_INFO)
end if
else
call ESMF_LogWrite(trim(subname)//": ERROR FBgeom or STgeom must be passed", ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
endif
! Make sure the field is not empty - if it is return with an error
call ESMF_FieldGet(lfield, status=status, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (status == ESMF_FIELDSTATUS_EMPTY) then
call ESMF_LogWrite(trim(subname)//":"//trim(lname)//": ERROR field does not have a geom yet ", &
ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u)
rc = ESMF_FAILURE
return
endif
! Assume field is on mesh
call ESMF_FieldGet(lfield, mesh=lmesh, meshloc=meshloc, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//":"//trim(lname)//" use mesh", ESMF_LOGMSG_INFO)
end if
endif ! fieldcount > 0
!---------------------------------
! create FBout
!---------------------------------
FBout = ESMF_FieldBundleCreate(name=trim(lname), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (fieldcountgeom > 0) then
! Now loop over all the fields in the field name list
do n = 1, fieldCount
! Note that input fields come from ONE of FBFlds, STflds, or fieldNamelist input argument
if (present(FBFlds) .or. present(STflds)) then
! ungridded dimensions might be present in the input states or field bundles
if (present(FBflds)) then
call ESMF_FieldBundleGet(FBflds, fieldName=lfieldnamelist(n), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
elseif (present(STflds)) then
call med_methods_State_getNameN(STflds, n, lname, rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_StateGet(STflds, itemName=lname, field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end if
! Determine ungridded lower and upper bounds for lfield
call ESMF_AttributeGet(lfield, name="UngriddedUBound", convention="NUOPC", &
purpose="Instance", itemCount=ungriddedCount_in, isPresent=isPresent, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (isPresent) then
ungriddedCount = ungriddedCount_in
else
ungriddedCount=0 ! initialize in case it was not set
end if
! Create the field on a lmesh
if (ungriddedCount > 0) then
! ungridded dimensions in field
allocate(ungriddedLBound(ungriddedCount), ungriddedUBound(ungriddedCount))
call ESMF_AttributeGet(lfield, name="UngriddedLBound", convention="NUOPC", &
purpose="Instance", valueList=ungriddedLBound, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_AttributeGet(lfield, name="UngriddedUBound", convention="NUOPC", &
purpose="Instance", valueList=ungriddedUBound, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
field = ESMF_FieldCreate(lmesh, ESMF_TYPEKIND_R8, meshloc=meshloc, name=lfieldNameList(n), &
ungriddedLbound=ungriddedLbound, ungriddedUbound=ungriddedUbound, gridToFieldMap=(/2/))
if (chkerr(rc,__LINE__,u_FILE_u)) return
deallocate( ungriddedLbound, ungriddedUbound)
else
! No ungridded dimensions in field
field = ESMF_FieldCreate(lmesh, ESMF_TYPEKIND_R8, meshloc=meshloc, name=lfieldNameList(n), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end if
else if (present(fieldNameList)) then
! Assume no ungridded dimensions if just the field name list is give
field = ESMF_FieldCreate(lmesh, ESMF_TYPEKIND_R8, meshloc=meshloc, name=lfieldNameList(n), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end if
! Add the created field bundle FBout
if (dbug_flag > 1) then
call ESMF_LogWrite(trim(subname)//":"//trim(lname)//" adding field "//trim(lfieldNameList(n)), &
ESMF_LOGMSG_INFO)
end if
call ESMF_FieldBundleAdd(FBout, (/field/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
enddo ! fieldCount
endif ! fieldcountgeom
deallocate(lfieldNameList)
call med_methods_FB_reset(FBout, value=spval_init, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
end subroutine med_methods_FB_init
!-----------------------------------------------------------------------------
subroutine med_methods_FB_getNameN(FB, fieldnum, fieldname, rc)
! ----------------------------------------------
! Get name of field number fieldnum in input field bundle FB
! ----------------------------------------------
use ESMF, only : ESMF_FieldBundle, ESMF_FieldBundleGet
! input/output variables
type(ESMF_FieldBundle), intent(in) :: FB
integer , intent(in) :: fieldnum
character(len=*) , intent(out) :: fieldname
integer , intent(out) :: rc
! local variables
integer :: fieldCount
character(ESMF_MAXSTR) ,pointer :: lfieldnamelist(:)
character(len=*),parameter :: subname='(med_methods_FB_getNameN)'
! ----------------------------------------------
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
endif
rc = ESMF_SUCCESS
fieldname = ' '
call ESMF_FieldBundleGet(FB, fieldCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (fieldnum > fieldCount) then
call ESMF_LogWrite(trim(subname)//": ERROR fieldnum > fieldCount ", ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
endif
allocate(lfieldnamelist(fieldCount))
call ESMF_FieldBundleGet(FB, fieldNameList=lfieldnamelist, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
fieldname = lfieldnamelist(fieldnum)
deallocate(lfieldnamelist)
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
end subroutine med_methods_FB_getNameN
!-----------------------------------------------------------------------------
subroutine med_methods_FB_getFieldN(FB, fieldnum, field, rc)
! ----------------------------------------------
! Get field with number fieldnum in input field bundle FB
! ----------------------------------------------
use ESMF, only : ESMF_Field, ESMF_FieldBundle, ESMF_FieldBundleGet
! input/output variables
type(ESMF_FieldBundle), intent(in) :: FB
integer , intent(in) :: fieldnum
type(ESMF_Field) , intent(inout) :: field
integer , intent(out) :: rc
! local variables
character(len=ESMF_MAXSTR) :: name
character(len=*),parameter :: subname='(med_methods_FB_getFieldN)'
! ----------------------------------------------
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
endif
rc = ESMF_SUCCESS
call med_methods_FB_getNameN(FB, fieldnum, name, rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldBundleGet(FB, fieldName=name, field=field, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
end subroutine med_methods_FB_getFieldN
!-----------------------------------------------------------------------------
subroutine med_methods_State_getNameN(State, fieldnum, fieldname, rc)
! ----------------------------------------------
! Get field number fieldnum name out of State
! ----------------------------------------------
use ESMF, only : ESMF_State, ESMF_StateGet
type(ESMF_State), intent(in) :: State
integer , intent(in) :: fieldnum
character(len=*), intent(out) :: fieldname
integer , intent(out) :: rc
! local variables
integer :: fieldCount
character(ESMF_MAXSTR) ,pointer :: lfieldnamelist(:)
character(len=*),parameter :: subname='(med_methods_State_getNameN)'
! ----------------------------------------------
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
endif
rc = ESMF_SUCCESS
fieldname = ' '
call ESMF_StateGet(State, itemCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (fieldnum > fieldCount) then
call ESMF_LogWrite(trim(subname)//": ERROR fieldnum > fieldCount ", ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
endif
allocate(lfieldnamelist(fieldCount))
call ESMF_StateGet(State, itemNameList=lfieldnamelist, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
fieldname = lfieldnamelist(fieldnum)
deallocate(lfieldnamelist)
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
end subroutine med_methods_State_getNameN
!-----------------------------------------------------------------------------
subroutine med_methods_State_getNumFields(State, fieldnum, rc)
! ----------------------------------------------
! Get field number fieldnum name out of State
! ----------------------------------------------
use NUOPC , only : NUOPC_GetStateMemberLists
use ESMF , only : ESMF_State, ESMF_Field, ESMF_StateGet, ESMF_STATEITEM_FIELD
use ESMF , only : ESMF_StateItem_Flag
type(ESMF_State), intent(in) :: State
integer , intent(inout) :: fieldnum
integer , intent(out) :: rc
! local variables
type(ESMF_Field), pointer :: fieldList(:)
character(len=*),parameter :: subname='(med_methods_State_getNumFields)'
! ----------------------------------------------
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
endif
rc = ESMF_SUCCESS
nullify(fieldList)
call NUOPC_GetStateMemberLists(state, fieldList=fieldList, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
fieldnum = 0
if (associated(fieldList)) then
fieldnum = size(fieldList)
deallocate(fieldList)
endif
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
end subroutine med_methods_State_getNumFields
!-----------------------------------------------------------------------------
subroutine med_methods_FB_reset(FB, value, rc)
! ----------------------------------------------
! Set all fields to value in FB
! If value is not provided, reset to 0.0
! ----------------------------------------------
use ESMF, only : ESMF_FieldBundle, ESMF_FieldBundleGet, ESMF_Field
use ESMF, only : ESMF_FieldBundleIsCreated
! intput/output variables
type(ESMF_FieldBundle) , intent(inout) :: FB
real(R8) , intent(in), optional :: value
integer , intent(out) :: rc
! local variables
integer :: n
integer :: fieldCount
character(ESMF_MAXSTR) ,pointer :: lfieldnamelist(:)
real(R8) :: lvalue
type(ESMF_Field) :: lfield
integer :: lrank
real(R8), pointer :: fldptr1(:)
real(R8), pointer :: fldptr2(:,:)
character(len=*),parameter :: subname='(med_methods_FB_reset)'
! ----------------------------------------------
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
endif
rc = ESMF_SUCCESS
lvalue = czero
if (present(value)) then
lvalue = value
endif
if (ESMF_FieldBundleIsCreated(fieldbundle=FB)) then
call ESMF_FieldBundleGet(FB, fieldCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(lfieldnamelist(fieldCount))
call ESMF_FieldBundleGet(FB, fieldNameList=lfieldnamelist, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
else
fieldCount=0
endif
do n = 1, fieldCount
call ESMF_FieldBundleGet(FB, fieldName=trim(lfieldnamelist(n)), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call med_methods_Field_GetFldPtr(lfield, fldptr1=fldptr1, fldptr2=fldptr2, rank=lrank, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (lrank == 0) then
! no local data
elseif (lrank == 1) then
fldptr1 = lvalue
elseif (lrank == 2) then
fldptr2 = lvalue
else
call ESMF_LogWrite(trim(subname)//": ERROR in rank "//trim(lfieldnamelist(n)), &
ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u)
rc = ESMF_FAILURE
return
endif
enddo
if (ESMF_FieldBundleIsCreated(fieldbundle=FB)) then
deallocate(lfieldnamelist)
endif
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
end subroutine med_methods_FB_reset
!-----------------------------------------------------------------------------
subroutine med_methods_State_reset(State, value, rc)
! ----------------------------------------------
! Set all fields to value in State
! If value is not provided, reset to 0.0
! ----------------------------------------------
use ESMF, only : ESMF_State, ESMF_StateGet, ESMF_Field
! intput/output variables
type(ESMF_State) , intent(inout) :: State
real(R8) , intent(in), optional :: value
integer , intent(out) :: rc
! local variables
integer :: n
integer :: fieldCount
character(ESMF_MAXSTR) ,pointer :: lfieldnamelist(:)
real(R8) :: lvalue
type(ESMF_Field) :: lfield
integer :: lrank
real(R8), pointer :: fldptr1(:)
real(R8), pointer :: fldptr2(:,:)
character(len=*),parameter :: subname='(med_methods_State_reset)'
! ----------------------------------------------
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
endif
rc = ESMF_SUCCESS
lvalue = czero
if (present(value)) then
lvalue = value
endif
call ESMF_StateGet(State, itemCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(lfieldnamelist(fieldCount))
call ESMF_StateGet(State, itemNameList=lfieldnamelist, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
do n = 1, fieldCount
call ESMF_StateGet(State, itemName=trim(lfieldnamelist(n)), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call med_methods_Field_GetFldPtr(lfield, fldptr1=fldptr1, fldptr2=fldptr2, rank=lrank, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (lrank == 0) then
! no local data
elseif (lrank == 1) then
fldptr1 = lvalue
elseif (lrank == 2) then
fldptr2 = lvalue
else
call ESMF_LogWrite(trim(subname)//": ERROR in rank "//trim(lfieldnamelist(n)), &
ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u)
rc = ESMF_FAILURE
return
endif
enddo
deallocate(lfieldnamelist)
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
end subroutine med_methods_State_reset
!-----------------------------------------------------------------------------
subroutine med_methods_FB_average(FB, count, rc)
! ----------------------------------------------
! Set all fields to zero in FB
! ----------------------------------------------
use ESMF, only : ESMF_FieldBundle, ESMF_FieldBundleGet, ESMF_Field
! input/output variables
type(ESMF_FieldBundle), intent(inout) :: FB
integer , intent(in) :: count
integer , intent(out) :: rc
! local variables
integer :: i,j,n
integer :: fieldCount, lrank
character(ESMF_MAXSTR) ,pointer :: lfieldnamelist(:)
real(R8), pointer :: dataPtr1(:)
real(R8), pointer :: dataPtr2(:,:)
type(ESMF_Field) :: lfield
character(len=*),parameter :: subname='(med_methods_FB_average)'
! ----------------------------------------------
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
endif
rc = ESMF_SUCCESS
if (count == 0) then
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": WARNING count is 0", ESMF_LOGMSG_INFO)
end if
!call ESMF_LogWrite(trim(subname)//": WARNING count is 0 set avg to spval", ESMF_LOGMSG_INFO)
!call med_methods_FB_reset(FB, value=spval, rc=rc)
!if (chkerr(rc,__LINE__,u_FILE_u)) return
else
call ESMF_FieldBundleGet(FB, fieldCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(lfieldnamelist(fieldCount))
call ESMF_FieldBundleGet(FB, fieldNameList=lfieldnamelist, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
do n = 1, fieldCount
call ESMF_FieldBundleGet(FB, fieldName=trim(lfieldnamelist(n)), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call med_methods_Field_GetFldPtr(lfield, fldptr1=dataptr1, fldptr2=dataptr2, rank=lrank, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (lrank == 0) then
! no local data
elseif (lrank == 1) then
do i=lbound(dataptr1,1),ubound(dataptr1,1)
dataptr1(i) = dataptr1(i) / real(count, R8)
enddo
elseif (lrank == 2) then
do j=lbound(dataptr2,2),ubound(dataptr2,2)
do i=lbound(dataptr2,1),ubound(dataptr2,1)
dataptr2(i,j) = dataptr2(i,j) / real(count, R8)
enddo
enddo
else
call ESMF_LogWrite(trim(subname)//": ERROR rank not supported ", ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
endif
enddo
deallocate(lfieldnamelist)
endif
if (dbug_flag > 10) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
end subroutine med_methods_FB_average
!-----------------------------------------------------------------------------
subroutine med_methods_FB_diagnose(FB, string, rc)
! ----------------------------------------------
! Diagnose status of FB
! ----------------------------------------------
use ESMF, only : ESMF_FieldBundle, ESMF_FieldBundleGet, ESMF_Field
type(ESMF_FieldBundle) , intent(inout) :: FB
character(len=*) , intent(in), optional :: string
integer , intent(out) :: rc
! local variables
integer :: n
integer :: fieldCount, lrank
character(ESMF_MAXSTR), pointer :: lfieldnamelist(:)
character(len=CL) :: lstring
real(R8), pointer :: dataPtr1d(:)
real(R8), pointer :: dataPtr2d(:,:)
type(ESMF_Field) :: lfield
character(len=*), parameter :: subname='(med_methods_FB_diagnose)'
! ----------------------------------------------
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
rc = ESMF_SUCCESS
lstring = ''
if (present(string)) then
lstring = trim(string) // ' '
endif
! Determine number of fields in field bundle and allocate memory for lfieldnamelist
call ESMF_FieldBundleGet(FB, fieldCount=fieldCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(lfieldnamelist(fieldCount))
! Get the fields in the field bundle
call ESMF_FieldBundleGet(FB, fieldNameList=lfieldnamelist, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! For each field in the bundle, get its memory location and print out the field
do n = 1, fieldCount
call ESMF_FieldBundleGet(FB, fieldName=trim(lfieldnamelist(n)), field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call med_methods_Field_GetFldPtr(lfield, fldptr1=dataptr1d, fldptr2=dataptr2d, rank=lrank, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (lrank == 0) then
! no local data
elseif (lrank == 1) then
if (size(dataPtr1d) > 0) then
write(msgString,'(A,3g14.7,i8)') trim(subname)//' '//trim(lstring)//': '//trim(lfieldnamelist(n))//' ', &
minval(dataPtr1d), maxval(dataPtr1d), sum(dataPtr1d), size(dataPtr1d)
else
write(msgString,'(A,a)') trim(subname)//' '//trim(lstring)//': '//trim(lfieldnamelist(n)), " no data"
endif
elseif (lrank == 2) then
if (size(dataPtr2d) > 0) then
write(msgString,'(A,3g14.7,i8)') trim(subname)//' '//trim(lstring)//': '//trim(lfieldnamelist(n))//' ', &
minval(dataPtr2d), maxval(dataPtr2d), sum(dataPtr2d), size(dataPtr2d)
else
write(msgString,'(A,a)') trim(subname)//' '//trim(lstring)//': '//trim(lfieldnamelist(n)), &
" no data"
endif
else
call ESMF_LogWrite(trim(subname)//": ERROR rank not supported ", ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
endif
call ESMF_LogWrite(trim(msgString), ESMF_LOGMSG_INFO)
enddo
! Deallocate memory
deallocate(lfieldnamelist)
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
end subroutine med_methods_FB_diagnose