forked from NOAA-EMC/fv3atm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_cap_cpl.F90
578 lines (466 loc) · 27.2 KB
/
module_cap_cpl.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
module module_cap_cpl
!
!*** this module contains the debug subroutines for fv3 coupled run
!
! revision history
! 12 Mar 2018: J. Wang Pull coupled subroutines from fv3_cap.F90 to this module
!
use ESMF
use NUOPC
use module_cplfields, only : FieldInfo
!
implicit none
private
public clock_cplIntval
! public realizeConnectedInternCplField
public realizeConnectedCplFields
public diagnose_cplFields
!
contains
!-----------------------------------------------------------------------------
!-----------------------------------------------------------------------------
subroutine clock_cplIntval(gcomp, CF)
type(ESMF_GridComp) :: gcomp
type(ESMF_Config) :: CF
!
real(ESMF_KIND_R8) :: medAtmCouplingIntervalSec
type(ESMF_Clock) :: fv3Clock
type(ESMF_TimeInterval) :: fv3Step
integer :: rc
!
call ESMF_ConfigGetAttribute(config=CF, value=medAtmCouplingIntervalSec, &
label="atm_coupling_interval_sec:", default=-1.0_ESMF_KIND_R8, rc=RC)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) &
call ESMF_Finalize(endflag=ESMF_END_ABORT)
if (medAtmCouplingIntervalSec > 0._ESMF_KIND_R8) then ! The coupling time step is provided
call ESMF_TimeIntervalSet(fv3Step, s_r8=medAtmCouplingIntervalSec, rc=RC)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) &
call ESMF_Finalize(endflag=ESMF_END_ABORT)
call ESMF_GridCompGet(gcomp, clock=fv3Clock, rc=RC)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) &
call ESMF_Finalize(endflag=ESMF_END_ABORT)
call ESMF_ClockSet(fv3Clock, timestep=fv3Step, rc=RC)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) &
call ESMF_Finalize(endflag=ESMF_END_ABORT)
endif
end subroutine clock_cplIntval
!-----------------------------------------------------------------------------
subroutine addFieldMetadata(field, key, values, rc)
! This subroutine implements a preliminary method to provide metadata to
! a coupled model that is accessing the field via reference sharing
! (NUOPC SharedStatusField=.true.). The method sets a (key, values) pair
! in the field's array ESMF_Info object to retrieve an array of strings
! encoding metadata.
!
! Such a capability should be implemented in the standard NUOPC connector
! for more general applications, possibly providing access to the field's
! ESMF_Info object.
type(ESMF_Field) :: field
character(len=*), intent(in) :: key
character(len=*), intent(in) :: values(:)
integer, optional, intent(out) :: rc
! local variable
integer :: localrc
type(ESMF_Array) :: array
type(ESMF_Info) :: info
! begin
if (present(rc)) rc = ESMF_SUCCESS
call ESMF_FieldGet(field, array=array, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__, rcToReturn=rc)) return
call ESMF_InfoGetFromHost(array, info, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__, rcToReturn=rc)) return
call ESMF_InfoSet(info, key, values, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__, rcToReturn=rc)) return
end subroutine addFieldMetadata
!-----------------------------------------------------------------------------
#if 0
subroutine realizeConnectedInternCplField(state, field, standardName, grid, rc)
type(ESMF_State) :: state
type(ESMF_Field), optional :: field
character(len=*), optional :: standardName
type(ESMF_Grid), optional :: grid
integer, intent(out), optional :: rc
! local variables
character(len=80) :: fieldName
type(ESMF_ArraySpec) :: arrayspec
integer :: i, localrc
logical :: isConnected
real(ESMF_KIND_R8), pointer :: fptr(:,:)
if (present(rc)) rc = ESMF_SUCCESS
fieldName = standardName ! use standard name as field name
!! Create fields using wam2dmesh if they are WAM fields
isConnected = NUOPC_IsConnected(state, fieldName=fieldName, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__, rcToReturn=rc)) return
if (isConnected) then
field = ESMF_FieldCreate(grid, ESMF_TYPEKIND_R8, name=fieldName, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__, rcToReturn=rc)) return
call NUOPC_Realize(state, field=field, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__, rcToReturn=rc)) return
call ESMF_FieldGet(field, farrayPtr=fptr, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__, rcToReturn=rc)) return
fptr=0._ESMF_KIND_R8 ! zero out the entire field
call NUOPC_SetAttribute(field, name="Updated", value="true", rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__, rcToReturn=rc)) return
else
! remove a not connected Field from State
call ESMF_StateRemove(state, (/fieldName/), rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__, rcToReturn=rc)) return
endif
end subroutine realizeConnectedInternCplField
#endif
!-----------------------------------------------------------------------------
subroutine realizeConnectedCplFields(state, grid, &
numLevels, numSoilLayers, numTracers, &
fields_info, state_tag, fieldList, fill_value, rc)
use field_manager_mod, only: MODEL_ATMOS
use tracer_manager_mod, only: get_number_tracers, get_tracer_names
type(ESMF_State), intent(inout) :: state
type(ESMF_Grid), intent(in) :: grid
integer, intent(in) :: numLevels
integer, intent(in) :: numSoilLayers
integer, intent(in) :: numTracers
type(FieldInfo), dimension(:), intent(in) :: fields_info
character(len=*), intent(in) :: state_tag !< Import or export.
type(ESMF_Field), dimension(:), intent(out) :: fieldList
real(ESMF_KIND_R8), optional , intent(in) :: fill_value
integer, intent(out) :: rc
! local variables
integer :: item, pos, tracerCount
logical :: isConnected
type(ESMF_Field) :: field
real(ESMF_KIND_R8) :: l_fill_value
real(ESMF_KIND_R8), parameter :: d_fill_value = 0._ESMF_KIND_R8
type(ESMF_StateIntent_Flag) :: stateintent
character(len=32), allocatable, dimension(:) :: tracerNames, tracerUnits
! begin
rc = ESMF_SUCCESS
if (present(fill_value)) then
l_fill_value = fill_value
else
l_fill_value = d_fill_value
end if
! attach list of tracer names to exported tracer field as metadata
call ESMF_StateGet(state, stateintent=stateintent, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
if (stateintent == ESMF_STATEINTENT_EXPORT) then
call get_number_tracers(MODEL_ATMOS, num_tracers=tracerCount)
allocate(tracerNames(tracerCount), tracerUnits(tracerCount))
do item = 1, tracerCount
call get_tracer_names(MODEL_ATMOS, item, tracerNames(item), units=tracerUnits(item))
end do
end if
do item = 1, size(fields_info)
isConnected = NUOPC_IsConnected(state, fieldName=trim(fields_info(item)%name), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
if (isConnected) then
call ESMF_StateGet(state, field=field, itemName=trim(fields_info(item)%name), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMF_FieldEmptySet(field, grid=grid, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
select case (fields_info(item)%type)
case ('l','layer')
call ESMF_FieldEmptyComplete(field, typekind=ESMF_TYPEKIND_R8, &
ungriddedLBound=(/1/), ungriddedUBound=(/numLevels/), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
case ('i','interface')
call ESMF_FieldEmptyComplete(field, typekind=ESMF_TYPEKIND_R8, &
ungriddedLBound=(/1/), ungriddedUBound=(/numLevels+1/), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
case ('t','tracer')
call ESMF_FieldEmptyComplete(field, typekind=ESMF_TYPEKIND_R8, &
ungriddedLBound=(/1, 1/), ungriddedUBound=(/numLevels, numTracers/), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
if (allocated(tracerNames)) then
call addFieldMetadata(field, 'tracerNames', tracerNames, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
end if
if (allocated(tracerUnits)) then
call addFieldMetadata(field, 'tracerUnits', tracerUnits, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
end if
case ('s','surface')
call ESMF_FieldEmptyComplete(field, typekind=ESMF_TYPEKIND_R8, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
case ('g','soil')
call ESMF_FieldEmptyComplete(field, typekind=ESMF_TYPEKIND_R8, &
ungriddedLBound=(/1/), ungriddedUBound=(/numSoilLayers/), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
case default
call ESMF_LogSetError(ESMF_RC_NOT_VALID, &
msg="exportFieldType = '"//trim(fields_info(item)%type)//"' not recognized", &
line=__LINE__, file=__FILE__, rcToReturn=rc)
return
end select
call NUOPC_Realize(state, field=field, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
! -- initialize field value
call ESMF_FieldFill(field, dataFillScheme="const", const1=l_fill_value, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
! -- save field
fieldList(item) = field
call ESMF_LogWrite('realizeConnectedCplFields '//trim(state_tag)//' Field '//trim(fields_info(item)%name) &
// ' is connected ', ESMF_LOGMSG_INFO, line=__LINE__, file=__FILE__, rc=rc)
else
! remove a not connected Field from State
call ESMF_StateRemove(state, (/trim(fields_info(item)%name)/), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMF_LogWrite('realizeConnectedCplFields '//trim(state_tag)//' Field '//trim(fields_info(item)%name) &
// ' is not connected ', ESMF_LOGMSG_INFO, line=__LINE__, file=__FILE__, rc=rc)
end if
end do
if (allocated(tracerNames)) deallocate(tracerNames)
if (allocated(tracerUnits)) deallocate(tracerUnits)
end subroutine realizeConnectedCplFields
!-----------------------------------------------------------------------------
subroutine diagnose_cplFields(gcomp, clock_fv3, fcstpe, &
statewrite_flag, stdiagnose_flag, state_tag)
type(ESMF_GridComp), intent(in) :: gcomp
type(ESMF_Clock),intent(in) :: clock_fv3
logical, intent(in) :: fcstpe
logical, intent(in) :: statewrite_flag
integer, intent(in) :: stdiagnose_flag
character(len=*), intent(in) :: state_tag !< Import or export.
character(len=*),parameter :: subname='(module_cap_cpl:diagnose_cplFields)'
type(ESMF_Time) :: currTime
type(ESMF_State) :: state
character(len=240) :: timestr
integer :: timeslice = 1
character(len=160) :: nuopcMsg
character(len=160) :: filename
integer :: rc
!
call ESMF_ClockGet(clock_fv3, currTime=currTime, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMF_TimeGet(currTime, timestring=timestr, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMF_ClockPrint(clock_fv3, options="currTime", preString="current time: ", unit=nuopcMsg)
call ESMF_LogWrite(trim(subname)//' '//trim(state_tag)//' '//trim(nuopcMsg), ESMF_LOGMSG_INFO)
if(trim(state_tag) .eq. 'import')then
call ESMF_GridCompGet(gcomp, importState=state, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
if(stdiagnose_flag > 0 .and. fcstpe)then
call state_diagnose(state, ':IS', rc=rc)
end if
! Dump Fields out
if (statewrite_flag) then
write(filename,'(A)') 'fv3_cap_import_'//trim(timestr)//'_'
call State_RWFields_tiles(state,trim(filename), timeslice, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
end if
end if
if(trim(state_tag) .eq. 'export')then
call ESMF_GridCompGet(gcomp, exportState=state, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
if(stdiagnose_flag > 0 .and. fcstpe)then
call state_diagnose(state, ':ES', rc=rc)
end if
! Dump Fields out
if (statewrite_flag) then
write(filename,'(A)') 'fv3_cap_export_'//trim(timestr)//'_'
call State_RWFields_tiles(state,trim(filename), timeslice, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
end if
end if
end subroutine diagnose_cplFields
!-----------------------------------------------------------------------------
subroutine ESMFPP_RegridWriteState(state, fileName, timeslice, rc)
type(ESMF_State), intent(in) :: state
character(len=*), intent(in) :: fileName
integer, intent(in) :: timeslice
integer, intent(out) :: rc
! local
type(ESMF_Field) :: field
type(ESMF_Grid) :: outGrid
integer :: i, icount
character(64), allocatable :: itemNameList(:)
type(ESMF_StateItem_Flag), allocatable :: typeList(:)
rc = ESMF_SUCCESS
! 1degx1deg
outGrid = ESMF_GridCreate1PeriDimUfrm(maxIndex=(/360,180/), &
minCornerCoord=(/0.0_ESMF_KIND_R8,-90.0_ESMF_KIND_R8/), &
maxCornerCoord=(/360.0_ESMF_KIND_R8,90.0_ESMF_KIND_R8/), &
staggerLocList=(/ESMF_STAGGERLOC_CORNER, ESMF_STAGGERLOC_CENTER/), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMF_StateGet(state, itemCount=icount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
allocate(typeList(icount), itemNameList(icount))
call ESMF_StateGet(state, itemTypeList=typeList, itemNameList=itemNameList, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
do i = 1, icount
if(typeList(i) == ESMF_STATEITEM_FIELD) then
call ESMF_LogWrite("RegridWrite Field Name Initiated: "//trim(itemNameList(i)), ESMF_LOGMSG_INFO)
call ESMF_StateGet(state, itemName=itemNameList(i), field=field, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMFPP_RegridWrite(field, outGrid, ESMF_REGRIDMETHOD_BILINEAR, &
fileName//trim(itemNameList(i))//'.nc', trim(itemNameList(i)), timeslice, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMF_LogWrite("RegridWrite Field Name done: "//trim(itemNameList(i)), ESMF_LOGMSG_INFO)
endif
enddo
deallocate(typeList, itemNameList)
call ESMF_GridDestroy(outGrid,noGarbage=.true., rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
end subroutine ESMFPP_RegridWriteState
subroutine ESMFPP_RegridWrite(inField, outGrid, regridMethod, fileName, fieldName, timeslice, rc)
! input arguments
type(ESMF_Field), intent(in) :: inField
type(ESMF_Grid), intent(in) :: outGrid
type(ESMF_RegridMethod_Flag), intent(in) :: regridMethod
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: fieldName
integer, intent(in) :: timeslice
integer, intent(inout) :: rc
! local variables
integer :: srcTermProcessing
type(ESMF_Routehandle) :: rh
type(ESMF_Field) :: outField
outField = ESMF_FieldCreate(outGrid, typekind=ESMF_TYPEKIND_R8, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
! Perform entire regridding arithmetic on the destination PET
srcTermProcessing = 0
! For other options for the regrid operation, please refer to:
! http://www.earthsystemmodeling.org/esmf_releases/last_built/ESMF_refdoc/node5.html#SECTION050366000000000000000
call ESMF_FieldRegridStore(inField, outField, regridMethod=regridMethod, &
unmappedaction=ESMF_UNMAPPEDACTION_IGNORE, &
srcTermProcessing=srcTermProcessing, Routehandle=rh, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
! Use fixed ascending order for the sum terms based on their source
! sequence index to ensure bit-for-bit reproducibility
call ESMF_FieldRegrid(inField, outField, Routehandle=rh, &
termorderflag=ESMF_TERMORDER_SRCSEQ, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMF_FieldWrite(outField, fileName, variableName=fieldName, timeslice=timeslice, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMF_FieldRegridRelease(routehandle=rh, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMF_FieldDestroy(outField,noGarbage=.true., rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
rc = ESMF_SUCCESS
end subroutine ESMFPP_RegridWrite
!-----------------------------------------------------------------------------
! This subroutine requires ESMFv8 - for coupled FV3
subroutine State_RWFields_tiles(state,filename,timeslice,rc)
type(ESMF_State), intent(in) :: state
character(len=*), intent(in) :: fileName
integer, intent(in) :: timeslice
integer, intent(out) :: rc
! local
type(ESMF_Field) :: firstESMFFLD
type(ESMF_Field),allocatable :: flds(:)
type(ESMF_GridComp) :: IOComp
type(ESMF_Grid) :: gridFv3
character(len=256) :: msgString
integer :: i, icount, ifld
integer :: fieldcount, firstfld
character(64), allocatable :: itemNameList(:), fldNameList(:)
type(ESMF_StateItem_Flag), allocatable :: typeList(:)
character(len=*),parameter :: subname='(module_cap_cpl:State_RWFields_tiles)'
! local variables
rc = ESMF_SUCCESS
!call ESMF_LogWrite(trim(subname)//trim(filename)//": called", ESMF_LOGMSG_INFO, rc=rc)
call ESMF_StateGet(state, itemCount=icount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
allocate(typeList(icount), itemNameList(icount))
call ESMF_StateGet(state, itemTypeList=typeList, itemNameList=itemNameList, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
! find first stateitem that is a field and the count of fields
firstfld = 0; fieldcount = 0
do i = icount,1,-1
if(typeList(i) == ESMF_STATEITEM_FIELD) firstfld = i
if(typeList(i) == ESMF_STATEITEM_FIELD) fieldcount = fieldcount + 1
enddo
!write(msgString,*) trim(subname)//' icount = ',icount," fieldcount =
!",fieldcount," firstfld = ",firstfld
!call ESMF_LogWrite(trim(msgString), ESMF_LOGMSG_INFO, rc=rc)
allocate(flds(fieldCount),fldNameList(fieldCount))
ifld = 1
do i = 1, icount
if(typeList(i) == ESMF_STATEITEM_FIELD) then
fldNameList(ifld) = itemNameList(i)
ifld = ifld + 1
endif
enddo
call ESMF_LogWrite(trim(subname)//": write "//trim(filename)//"tile1-tile6", ESMF_LOGMSG_INFO, rc=rc)
! get first field
call ESMF_StateGet(state, itemName=itemNameList(firstfld), field=firstESMFFLD, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
call ESMF_FieldGet(firstESMFFLD, grid=gridFv3, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
IOComp = ESMFIO_Create(gridFv3, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
do ifld=1, fieldCount
call ESMF_StateGet(state, itemName=fldNameList(ifld), field=flds(ifld), rc=rc)
enddo
call ESMFIO_Write(IOComp, filename, flds, filePath='./', rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) return ! bail out
! -- Finalize ESMFIO
deallocate(flds)
deallocate(fldNameList)
call ESMFIO_Destroy(IOComp, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=__FILE__)) call ESMF_Finalize()
!call ESMF_LogWrite(trim(subname)//trim(filename)//": finished", ESMF_LOGMSG_INFO, rc=rc)
end subroutine State_RWFields_tiles
!-----------------------------------------------------------------------------
subroutine state_diagnose(State,string, rc)
! ----------------------------------------------
! Diagnose status of state
! ----------------------------------------------
type(ESMF_State), intent(inout) :: State
character(len=*), intent(in), optional :: string
integer, intent(out), optional :: rc
! local variables
integer :: i,j,n
integer :: itemCount
character(len=64) ,pointer :: itemNameList(:)
character(len=64) :: lstring
character(len=256) :: tmpstr
type(ESMF_Field) :: lfield
type(ESMF_StateItem_Flag) :: itemType
real(ESMF_KIND_R8), pointer :: dataPtr2d(:,:)
real(ESMF_KIND_R8), pointer :: dataPtr3d(:,:,:)
integer :: lrc, dimCount
character(len=*),parameter :: subname='(FV3: state_diagnose)'
lstring = ''
if (present(string)) then
lstring = trim(string)
endif
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
call ESMF_StateGet(State, itemCount=itemCount, rc=lrc)
if (ESMF_LogFoundError(rcToCheck=lrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
allocate(itemNameList(itemCount))
call ESMF_StateGet(State, itemNameList=itemNameList, rc=lrc)
if (ESMF_LogFoundError(rcToCheck=lrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
do n = 1, itemCount
call ESMF_StateGet(State, itemName=trim(itemNameList(n)), itemType=itemType, rc=lrc)
if (ESMF_LogFoundError(rcToCheck=lrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
if(itemType == ESMF_STATEITEM_FIELD)then
call ESMF_StateGet(State, itemName=trim(itemNameList(n)), field=lfield, rc=lrc)
if (ESMF_LogFoundError(rcToCheck=lrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
call ESMF_FieldGet(lfield, dimCount=dimcount, rc=lrc)
if (ESMF_LogFoundError(rcToCheck=lrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
if(dimcount == 2)then
call ESMF_FieldGet(lfield, farrayPtr=dataPtr2d, rc=lrc)
if (ESMF_LogFoundError(rcToCheck=lrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
write(tmpstr,'(A,3g14.7)') trim(subname)//' '//trim(lstring)//':'//trim(itemNameList(n))//' ', &
minval(dataPtr2d),maxval(dataPtr2d),sum(dataPtr2d)
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO, rc=lrc)
else
call ESMF_FieldGet(lfield, farrayPtr=dataPtr3d, rc=lrc)
if (ESMF_LogFoundError(rcToCheck=lrc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
write(tmpstr,'(A,3g14.7)') trim(subname)//' '//trim(lstring)//':'//trim(itemNameList(n))//' ', &
minval(dataPtr3d),maxval(dataPtr3d),sum(dataPtr3d)
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO, rc=lrc)
end if
end if
enddo
deallocate(itemNameList)
if (present(rc)) rc = lrc
call ESMF_LogWrite(subname//' exit', ESMF_LOGMSG_INFO)
end subroutine state_diagnose
!-----------------------------------------------------------------------------
end module module_cap_cpl