forked from NOAA-EMC/CMEPS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmed_io_mod.F90
2153 lines (1835 loc) · 85.5 KB
/
med_io_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_io_mod
!------------------------------------------
! Create mediator history files
!------------------------------------------
use med_kind_mod , only : CX=>SHR_KIND_CX, CS=>SHR_KIND_CS, CL=>SHR_KIND_CL, I8=>SHR_KIND_I8, R8=>SHR_KIND_R8
use med_kind_mod , only : R4=>SHR_KIND_R4
use med_constants_mod , only : fillvalue => SHR_CONST_SPVAL
use ESMF , only : ESMF_VM, ESMF_LogWrite, ESMF_LOGMSG_INFO, ESMF_LogFoundError, ESMF_LOGMSG_ERROR
use ESMF , only : ESMF_SUCCESS, ESMF_FAILURE, ESMF_END_ABORT, ESMF_LOGERR_PASSTHRU
use ESMF , only : ESMF_VMGetCurrent, ESMF_VMGet, ESMF_VMBroadCast, ESMF_Finalize
use NUOPC , only : NUOPC_FieldDictionaryGetEntry
use NUOPC , only : NUOPC_FieldDictionaryHasEntry
use pio , only : file_desc_t, iosystem_desc_t
use med_internalstate_mod , only : logunit, med_id, maintask
use med_constants_mod , only : dbug_flag => med_constants_dbug_flag
use med_methods_mod , only : FB_getFieldN => med_methods_FB_getFieldN
use med_methods_mod , only : FB_getFldPtr => med_methods_FB_getFldPtr
use med_methods_mod , only : FB_getNameN => med_methods_FB_getNameN
use med_utils_mod , only : chkerr => med_utils_ChkErr
implicit none
private
! public member functions:
public :: med_io_wopen
public :: med_io_close
public :: med_io_redef
public :: med_io_enddef
public :: med_io_sec2hms
public :: med_io_read
public :: med_io_define_time
public :: med_io_write_time
public :: med_io_write
public :: med_io_init
public :: med_io_date2yyyymmdd
public :: med_io_datetod2string
public :: med_io_ymd2date
! private member functions
private :: med_io_file_exists
! public data members:
interface med_io_read
module procedure med_io_read_FB
module procedure med_io_read_int
module procedure med_io_read_int1d
module procedure med_io_read_r8
module procedure med_io_read_r81d
module procedure med_io_read_char
end interface med_io_read
interface med_io_write
module procedure med_io_write_FB
module procedure med_io_write_int
module procedure med_io_write_int1d
module procedure med_io_write_r8
module procedure med_io_write_r81d
module procedure med_io_write_char
end interface med_io_write
interface med_io_date2ymd
module procedure med_io_date2ymd_int
module procedure med_io_date2ymd_long
end interface med_io_date2ymd
interface med_io_datetod2string
module procedure med_io_datetod2string_int
module procedure med_io_datetod2string_long
end interface med_io_datetod2string
interface med_io_ymd2date
module procedure med_io_ymd2date_int
module procedure med_io_ymd2date_long
end interface med_io_ymd2date
! module data
character(*),parameter :: prefix = "med_io_"
character(*),parameter :: modName = "(med_io_mod) "
character(*),parameter :: version = "cmeps0"
integer :: pio_iotype
integer :: pio_ioformat
type(iosystem_desc_t), pointer :: io_subsystem
character(*),parameter :: u_FILE_u = &
__FILE__
!=================================================================================
contains
!=================================================================================
logical function med_io_file_exists(vm, filename)
!---------------
! inquire if i/o file exists
!---------------
! input/output variables
type(ESMF_VM) :: vm
character(len=*), intent(in) :: filename
! local variables
integer :: tmp(1)
integer :: iam
integer :: rc
!-------------------------------------------------------------------------------
tmp(1) = 0
call ESMF_VMGet(vm, localPet=iam, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
med_io_file_exists = .false.
if (iam==0) then
inquire(file=trim(filename),exist=med_io_file_exists)
if (med_io_file_exists) tmp(1) = 1
end if
call ESMF_VMBroadCast(vm, tmp, 1, 0, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if(tmp(1) == 1) med_io_file_exists = .true.
end function med_io_file_exists
!===============================================================================
subroutine med_io_init(gcomp, rc)
!---------------
! initialize pio
!---------------
use ESMF , only : ESMF_GridComp, ESMF_UtilStringUpperCase
#ifdef CESMCOUPLED
use shr_pio_mod , only : shr_pio_getiosys, shr_pio_getiotype, shr_pio_getioformat
#else
use pio , only : pio_init, pio_setdebuglevel, pio_set_rearr_opts
use pio , only : PIO_64BIT_OFFSET, PIO_64BIT_DATA
use pio , only : PIO_IOTYPE_NETCDF, PIO_IOTYPE_PNETCDF, PIO_IOTYPE_NETCDF4C, PIO_IOTYPE_NETCDF4P
use pio , only : PIO_REARR_BOX, PIO_REARR_SUBSET
use pio , only : PIO_REARR_COMM_P2P, PIO_REARR_COMM_COLL
use pio , only : PIO_REARR_COMM_FC_2D_ENABLE, PIO_REARR_COMM_FC_2D_DISABLE
use pio , only : PIO_REARR_COMM_FC_1D_COMP2IO, PIO_REARR_COMM_FC_1D_IO2COMP
use NUOPC, only : NUOPC_CompAttributeGet
#endif
! input/output arguments
type(ESMF_GridComp), intent(in) :: gcomp
integer , intent(out) :: rc
#ifndef CESMCOUPLED
! local variables
type(ESMF_VM) :: vm
integer :: ret
integer :: comm
integer :: localPet, petCount
integer :: pio_numiotasks
integer :: pio_stride
integer :: pio_rearranger
integer :: pio_root
integer :: pio_debug_level
integer :: pio_rearr_comm_type
integer :: pio_rearr_comm_fcd
logical :: pio_rearr_comm_enable_hs_comp2io
logical :: pio_rearr_comm_enable_isend_comp2io
integer :: pio_rearr_comm_max_pend_req_comp2io
logical :: pio_rearr_comm_enable_hs_io2comp
logical :: pio_rearr_comm_enable_isend_io2comp
integer :: pio_rearr_comm_max_pend_req_io2comp
logical :: isPresent, isSet
character(len=CS) :: cvalue
character(*), parameter :: subname = '(med_io_init)'
!-------------------------------------------------------------------------------
#endif
#ifdef CESMCOUPLED
io_subsystem => shr_pio_getiosys(med_id)
pio_iotype = shr_pio_getiotype(med_id)
pio_ioformat = shr_pio_getioformat(med_id)
#else
! query VM
call ESMF_VMGetCurrent(vm=vm, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMGet(vm, mpiCommunicator=comm, localPet=localPet, petCount=petCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! query component specific PIO attributes
! pio_netcdf_format
call NUOPC_CompAttributeGet(gcomp, name='pio_netcdf_format', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
cvalue = ESMF_UtilStringUpperCase(cvalue)
if (trim(cvalue) .eq. 'CLASSIC') then
pio_ioformat = 0
else if (trim(cvalue) .eq. '64BIT_OFFSET') then
pio_ioformat = PIO_64BIT_OFFSET
else if (trim(cvalue) .eq. '64BIT_DATA') then
pio_ioformat = PIO_64BIT_DATA
else
call ESMF_LogWrite(trim(subname)//': need to provide valid option for pio_ioformat (CLASSIC|64BIT_OFFSET|64BIT_DATA)', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
else
cvalue = '64BIT_OFFSET'
pio_ioformat = PIO_64BIT_OFFSET
end if
if (localPet == 0) write(logunit,*) trim(subname), ' : pio_netcdf_format = ', trim(cvalue), pio_ioformat
! pio_typename
call NUOPC_CompAttributeGet(gcomp, name='pio_typename', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
cvalue = ESMF_UtilStringUpperCase(cvalue)
if (trim(cvalue) .eq. 'NETCDF') then
pio_iotype = PIO_IOTYPE_NETCDF
else if (trim(cvalue) .eq. 'PNETCDF') then
pio_iotype = PIO_IOTYPE_PNETCDF
else if (trim(cvalue) .eq. 'NETCDF4C') then
pio_iotype = PIO_IOTYPE_NETCDF4C
else if (trim(cvalue) .eq. 'NETCDF4P') then
pio_iotype = PIO_IOTYPE_NETCDF4P
else
call ESMF_LogWrite(trim(subname)//': need to provide valid option for pio_typename (NETCDF|PNETCDF|NETCDF4C|NETCDF4P)', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
else
cvalue = 'NETCDF'
pio_iotype = PIO_IOTYPE_NETCDF
end if
if (localPet == 0) write(logunit,*) trim(subname), ' : pio_typename = ', trim(cvalue), pio_iotype
! pio_root
call NUOPC_CompAttributeGet(gcomp, name='pio_root', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) pio_root
if (pio_root < 0) then
pio_root = 1
endif
pio_root = min(pio_root, petCount-1)
else
pio_root = 1
end if
if (localPet == 0) write(logunit,*) trim(subname), ' : pio_root = ', pio_root
! pio_stride
call NUOPC_CompAttributeGet(gcomp, name='pio_stride', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) pio_stride
else
pio_stride = -99
end if
if (localPet == 0) write(logunit,*) trim(subname), ' : pio_stride = ', pio_stride
! pio_numiotasks
call NUOPC_CompAttributeGet(gcomp, name='pio_numiotasks', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) pio_numiotasks
else
pio_numiotasks = -99
end if
if (localPet == 0) write(logunit,*) trim(subname), ' : pio_numiotasks = ', pio_numiotasks
! check for parallel IO, it requires at least two io pes
if (petCount > 1 .and. pio_numiotasks == 1 .and. &
(pio_iotype .eq. PIO_IOTYPE_PNETCDF .or. pio_iotype .eq. PIO_IOTYPE_NETCDF4P)) then
pio_numiotasks = 2
pio_stride = min(pio_stride, petCount/2)
if (localPet == 0) then
write(logunit,*) ' parallel io requires at least two io pes - following parameters are updated:'
write(logunit,*) trim(subname), ' : pio_stride = ', pio_stride
write(logunit,*) trim(subname), ' : pio_numiotasks = ', pio_numiotasks
end if
endif
! check/set/correct io pio parameters
if (pio_stride > 0 .and. pio_numiotasks < 0) then
pio_numiotasks = max(1, petCount/pio_stride)
if (localPet == 0) write(logunit,*) trim(subname), ' : update pio_numiotasks = ', pio_numiotasks
else if(pio_numiotasks > 0 .and. pio_stride < 0) then
pio_stride = max(1, petCount/pio_numiotasks)
if (localPet == 0) write(logunit,*) trim(subname), ' : update pio_stride = ', pio_stride
else if(pio_numiotasks < 0 .and. pio_stride < 0) then
pio_stride = max(1,petCount/4)
pio_numiotasks = max(1,petCount/pio_stride)
if (localPet == 0) write(logunit,*) trim(subname), ' : update pio_numiotasks = ', pio_numiotasks
if (localPet == 0) write(logunit,*) trim(subname), ' : update pio_stride = ', pio_stride
end if
if (pio_stride == 1) then
pio_root = 0
endif
if (pio_root + (pio_stride)*(pio_numiotasks-1) >= petCount .or. &
pio_stride <= 0 .or. pio_numiotasks <= 0 .or. pio_root < 0 .or. pio_root > petCount-1) then
if (petCount < 100) then
pio_stride = max(1, petCount/4)
else if(petCount < 1000) then
pio_stride = max(1, petCount/8)
else
pio_stride = max(1, petCount/16)
end if
if(pio_stride > 1) then
pio_numiotasks = petCount/pio_stride
pio_root = min(1, petCount-1)
else
pio_numiotasks = petCount
pio_root = 0
end if
if (localPet == 0) then
write(logunit,*) 'pio_stride, iotasks or root out of bounds - resetting to defaults:'
write(logunit,*) trim(subname), ' : pio_root = ', pio_root
write(logunit,*) trim(subname), ' : pio_stride = ', pio_stride
write(logunit,*) trim(subname), ' : pio_numiotasks = ', pio_numiotasks
end if
end if
! pio_rearranger
call NUOPC_CompAttributeGet(gcomp, name='pio_rearranger', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
cvalue = ESMF_UtilStringUpperCase(cvalue)
if (trim(cvalue) .eq. 'BOX') then
pio_rearranger = PIO_REARR_BOX
else if (trim(cvalue) .eq. 'SUBSET') then
pio_rearranger = PIO_REARR_SUBSET
else
call ESMF_LogWrite(trim(subname)//': need to provide valid option for pio_rearranger (BOX|SUBSET)', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
else
cvalue = 'SUBSET'
pio_rearranger = PIO_REARR_SUBSET
end if
if (localPet == 0) write(logunit,*) trim(subname), ' : pio_rearranger = ', trim(cvalue), pio_rearranger
! init PIO
allocate(io_subsystem)
if (localPet == 0) write(logunit,*) trim(subname),' calling pio init'
call pio_init(localPet, comm, pio_numiotasks, 0, pio_stride, pio_rearranger, io_subsystem, base=pio_root)
! PIO debug related options
! pio_debug_level
call NUOPC_CompAttributeGet(gcomp, name='pio_debug_level', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) pio_debug_level
if (pio_debug_level < 0 .or. pio_debug_level > 6) then
call ESMF_LogWrite(trim(subname)//': need to provide valid option for pio_debug_level (0-6)', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
else
pio_debug_level = 0
end if
if (localPet == 0) write(logunit,*) trim(subname), ' : pio_debug_level = ', pio_debug_level
! set PIO debug level
call pio_setdebuglevel(pio_debug_level)
! query shared PIO rearranger attributes
! pio_rearr_comm_type
call NUOPC_CompAttributeGet(gcomp, name='pio_rearr_comm_type', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
cvalue = ESMF_UtilStringUpperCase(cvalue)
if (trim(cvalue) .eq. 'P2P') then
pio_rearr_comm_type = PIO_REARR_COMM_P2P
else if (trim(cvalue) .eq. 'COLL') then
pio_rearr_comm_type = PIO_REARR_COMM_COLL
else
call ESMF_LogWrite(trim(subname)//': need to provide valid option for pio_rearr_comm_type (P2P|COLL)', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
else
cvalue = 'P2P'
pio_rearr_comm_type = PIO_REARR_COMM_P2P
end if
if (localPet == 0) write(logunit,*) trim(subname), ' : pio_rearr_comm_type = ', trim(cvalue), pio_rearr_comm_type
! pio_rearr_comm_fcd
call NUOPC_CompAttributeGet(gcomp, name='pio_rearr_comm_fcd', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
cvalue = ESMF_UtilStringUpperCase(cvalue)
if (trim(cvalue) .eq. '2DENABLE') then
pio_rearr_comm_fcd = PIO_REARR_COMM_FC_2D_ENABLE
else if (trim(cvalue) .eq. 'IO2COMP') then
pio_rearr_comm_fcd = PIO_REARR_COMM_FC_1D_IO2COMP
else if (trim(cvalue) .eq. 'COMP2IO') then
pio_rearr_comm_fcd = PIO_REARR_COMM_FC_1D_COMP2IO
else if (trim(cvalue) .eq. '2DDISABLE') then
pio_rearr_comm_fcd = PIO_REARR_COMM_FC_2D_DISABLE
else
call ESMF_LogWrite(trim(subname)//': need to provide valid option for pio_rearr_comm_fcd (2DENABLE|IO2COMP|COMP2IO|2DDISABLE)', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if
else
cvalue = '2DENABLE'
pio_rearr_comm_fcd = PIO_REARR_COMM_FC_2D_ENABLE
end if
if (localPet == 0) write(logunit,*) trim(subname), ' : pio_rearr_comm_fcd = ', trim(cvalue), pio_rearr_comm_fcd
! pio_rearr_comm_enable_hs_comp2io
call NUOPC_CompAttributeGet(gcomp, name='pio_rearr_comm_enable_hs_comp2io', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) pio_rearr_comm_enable_hs_comp2io
else
pio_rearr_comm_enable_hs_comp2io = .true.
end if
! pio_rearr_comm_enable_isend_comp2io
call NUOPC_CompAttributeGet(gcomp, name='pio_rearr_comm_enable_isend_comp2io', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) pio_rearr_comm_enable_isend_comp2io
else
pio_rearr_comm_enable_isend_comp2io = .false.
end if
! pio_rearr_comm_max_pend_req_comp2io
call NUOPC_CompAttributeGet(gcomp, name='pio_rearr_comm_max_pend_req_comp2io', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) pio_rearr_comm_max_pend_req_comp2io
else
pio_rearr_comm_max_pend_req_comp2io = 0
end if
! pio_rearr_comm_enable_hs_io2comp
call NUOPC_CompAttributeGet(gcomp, name='pio_rearr_comm_enable_hs_io2comp', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) pio_rearr_comm_enable_hs_io2comp
else
pio_rearr_comm_enable_hs_io2comp = .false.
end if
! pio_rearr_comm_enable_isend_io2comp
call NUOPC_CompAttributeGet(gcomp, name='pio_rearr_comm_enable_isend_io2comp', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) pio_rearr_comm_enable_isend_io2comp
else
pio_rearr_comm_enable_isend_io2comp = .true.
end if
! pio_rearr_comm_max_pend_req_io2comp
call NUOPC_CompAttributeGet(gcomp, name='pio_rearr_comm_max_pend_req_io2comp', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) pio_rearr_comm_max_pend_req_io2comp
else
pio_rearr_comm_max_pend_req_io2comp = 64
end if
! print out PIO rearranger parameters
if (localPet == 0) then
write(logunit,*) trim(subname), ' : pio_rearr_comm_enable_hs_comp2io = ', pio_rearr_comm_enable_hs_comp2io
write(logunit,*) trim(subname), ' : pio_rearr_comm_enable_isend_comp2io = ', pio_rearr_comm_enable_isend_comp2io
write(logunit,*) trim(subname), ' : pio_rearr_comm_max_pend_req_comp2io = ', pio_rearr_comm_max_pend_req_comp2io
write(logunit,*) trim(subname), ' : pio_rearr_comm_enable_hs_io2comp = ', pio_rearr_comm_enable_hs_io2comp
write(logunit,*) trim(subname), ' : pio_rearr_comm_enable_isend_io2comp = ', pio_rearr_comm_enable_isend_io2comp
write(logunit,*) trim(subname), ' : pio_rearr_comm_max_pend_req_io2comp = ', pio_rearr_comm_max_pend_req_io2comp
end if
! set PIO rearranger options
if (localPet == 0) write(logunit,*) trim(subname),' calling pio_set_rearr_opts'
ret = pio_set_rearr_opts(io_subsystem, pio_rearr_comm_type, pio_rearr_comm_fcd, &
pio_rearr_comm_enable_hs_comp2io, pio_rearr_comm_enable_isend_comp2io, &
pio_rearr_comm_max_pend_req_comp2io, &
pio_rearr_comm_enable_hs_io2comp, pio_rearr_comm_enable_isend_io2comp, &
pio_rearr_comm_max_pend_req_io2comp)
#endif
end subroutine med_io_init
!===============================================================================
subroutine med_io_wopen(filename, io_file, vm, rc, clobber, file_ind, model_doi_url)
!---------------
! open netcdf file
!---------------
use pio , only : PIO_IOTYPE_PNETCDF, PIO_IOTYPE_NETCDF, PIO_BCAST_ERROR, PIO_INTERNAL_ERROR
use pio , only : pio_openfile, pio_createfile, PIO_GLOBAL, pio_enddef
use pio , only : pio_put_att, pio_redef, pio_get_att
use pio , only : pio_seterrorhandling, pio_file_is_open, pio_clobber, pio_write, pio_noclobber
! input/output arguments
character(*), intent(in) :: filename
type(file_desc_t), intent(inout) :: io_file
type(ESMF_VM) :: vm
integer, intent(out) :: rc
logical, optional, intent(in) :: clobber
integer, optional, intent(in) :: file_ind
character(CL), optional, intent(in) :: model_doi_url
! local variables
logical :: lclobber
integer :: rcode
integer :: nmode
integer :: lfile_ind
integer :: iam
character(CL) :: lversion
character(CL) :: lmodel_doi_url
character(*),parameter :: subName = '(med_io_wopen) '
!-------------------------------------------------------------------------------
lversion=trim(version)
lclobber = .false.
if (present(clobber)) lclobber=clobber
lmodel_doi_url = 'unset'
if (present(model_doi_url)) lmodel_doi_url = model_doi_url
lfile_ind = 0
if (present(file_ind)) lfile_ind=file_ind
call ESMF_VMGet(vm, localPet=iam, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (.not. pio_file_is_open(io_file)) then
if (med_io_file_exists(vm, filename)) then
if (lclobber) then
nmode = pio_clobber
! only applies to classic NETCDF files.
if(pio_iotype == PIO_IOTYPE_NETCDF .or. pio_iotype == PIO_IOTYPE_PNETCDF) then
nmode = ior(nmode,pio_ioformat)
endif
rcode = pio_createfile(io_subsystem, io_file, pio_iotype, trim(filename), nmode)
if(iam==0) write(logunit,'(a)') trim(subname)//' creating file '//trim(filename)
rcode = pio_put_att(io_file,pio_global,"file_version",version)
rcode = pio_put_att(io_file,pio_global,"model_doi_url",lmodel_doi_url)
else
rcode = pio_openfile(io_subsystem, io_file, pio_iotype, trim(filename), pio_write)
if (iam==0) write(logunit,'(a)') trim(subname)//' opening file '//trim(filename)
call pio_seterrorhandling(io_file,PIO_BCAST_ERROR)
rcode = pio_get_att(io_file,pio_global,"file_version",lversion)
call pio_seterrorhandling(io_file,PIO_INTERNAL_ERROR)
if (trim(lversion) /= trim(version)) then
rcode = pio_redef(io_file)
rcode = pio_put_att(io_file,pio_global,"file_version",version)
rcode = pio_enddef(io_file)
endif
endif
else
nmode = pio_noclobber
! only applies to classic NETCDF files.
if(pio_iotype == PIO_IOTYPE_NETCDF .or. pio_iotype == PIO_IOTYPE_PNETCDF) then
nmode = ior(nmode,pio_ioformat)
endif
rcode = pio_createfile(io_subsystem, io_file, pio_iotype, trim(filename), nmode)
if (iam==0) write(logunit,'(a)') trim(subname) //' creating file '// trim(filename)
rcode = pio_put_att(io_file,pio_global,"file_version",version)
rcode = pio_put_att(io_file,pio_global,"model_doi_url",lmodel_doi_url)
endif
else
! filename is already open, just return
endif
end subroutine med_io_wopen
!===============================================================================
subroutine med_io_close(io_file, rc)
!---------------
! close netcdf file
!---------------
use pio, only: pio_file_is_open, pio_closefile
! input/output variables
type(file_desc_t) :: io_file
integer , intent(out) :: rc
! local variables
character(*),parameter :: subName = '(med_io_close) '
!-------------------------------------------------------------------------------
rc = ESMF_SUCCESS
if (pio_file_is_open(io_file)) then
call pio_closefile(io_file)
endif
end subroutine med_io_close
!===============================================================================
subroutine med_io_redef(io_file)
use pio, only : pio_redef
! input/output variables
type(file_desc_t) :: io_file
! local variables
integer :: rcode
!-------------------------------------------------------------------------------
rcode = pio_redef(io_file)
end subroutine med_io_redef
!===============================================================================
subroutine med_io_enddef(io_file)
use pio, only : pio_enddef
! input/output variables
type(file_desc_t) :: io_file
! local variables
integer :: rcode
!-------------------------------------------------------------------------------
rcode = pio_enddef(io_file)
end subroutine med_io_enddef
!===============================================================================
character(len=24) function med_io_date2yyyymmdd (date)
integer, intent(in) :: date ! date expressed as an integer: yyyymmdd
call med_io_datetod2string(date_str = med_io_date2yyyymmdd, ymd = date)
end function med_io_date2yyyymmdd
!===============================================================================
character(len=8) function med_io_sec2hms (seconds, rc)
! input arguments
integer, intent(in) :: seconds
integer, intent(out) :: rc
! local variables
integer :: hours ! hours of hh:mm:ss
integer :: minutes ! minutes of hh:mm:ss
integer :: secs ! seconds of hh:mm:ss
!----------------------------------------------------------------------
rc = ESMF_SUCCESS
if (seconds < 0 .or. seconds > 86400) then
write(logunit,*)'med_io_sec2hms: bad input seconds:', seconds
call ESMF_LogWrite('med_io_sec2hms: bad input seconds', ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
end if
hours = seconds / 3600
minutes = (seconds - hours*3600) / 60
secs = (seconds - hours*3600 - minutes*60)
if (minutes < 0 .or. minutes > 60) then
write(logunit,*)'med_io_sec2hms: bad minutes = ',minutes
call ESMF_LogWrite('med_io_sec2hms: bad minutes', ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
end if
if (secs < 0 .or. secs > 60) then
write(logunit,*)'med_io_sec2hms: bad secs = ',secs
call ESMF_LogWrite('med_io_sec2hms: bad secs', ESMF_LOGMSG_INFO)
rc = ESMF_FAILURE
return
end if
write(med_io_sec2hms,80) hours, minutes, secs
80 format(i2.2,':',i2.2,':',i2.2)
end function med_io_sec2hms
!===============================================================================
subroutine med_io_write_FB(io_file, FB, whead, wdata, nx, ny, nt, &
fillval, pre, flds, tavg, use_float, ntile, rc)
!---------------
! Write FB to netcdf file
!---------------
use ESMF, only : operator(==)
use ESMF , only : ESMF_LogWrite, ESMF_LOGMSG_INFO, ESMF_SUCCESS, ESMF_FAILURE, ESMF_END_ABORT
use ESMF , only : ESMF_FieldBundleIsCreated, ESMF_FieldBundle, ESMF_Mesh, ESMF_DistGrid
use ESMF , only : ESMF_FieldBundleGet, ESMF_FieldGet, ESMF_MeshGet, ESMF_DistGridGet
use ESMF , only : ESMF_Field, ESMF_FieldGet, ESMF_AttributeGet
use ESMF , only : ESMF_CoordSys_Flag, ESMF_COORDSYS_SPH_DEG, ESMF_COORDSYS_SPH_RAD, ESMF_COORDSYS_CART
use pio , only : var_desc_t, io_desc_t, pio_offset_kind
use pio , only : pio_def_dim, pio_inq_dimid, pio_real, pio_def_var, pio_put_att, pio_double
use pio , only : pio_inq_varid, pio_setframe, pio_write_darray, pio_initdecomp, pio_freedecomp
use pio , only : pio_syncfile
! input/output variables
type(file_desc_t) :: io_file
type(ESMF_FieldBundle) , intent(in) :: FB ! data to be written
logical , intent(in) :: whead ! write header
logical , intent(in) :: wdata ! write data
integer , intent(in) :: nx ! 2d grid size if available
integer , intent(in) :: ny ! 2d grid size if available
integer , optional , intent(in) :: nt ! time sample
real(r8), optional , intent(in) :: fillval ! fill value
character(len=*), optional , intent(in) :: pre ! prefix to variable name
character(len=*), optional , intent(in) :: flds(:) ! specific fields to write out
logical, optional , intent(in) :: tavg ! is this a tavg
logical, optional , intent(in) :: use_float ! write output as float rather than double
integer, optional , intent(in) :: ntile ! number of nx * ny tiles
integer , intent(out):: rc
! local variables
type(ESMF_Field) :: field
type(ESMF_Mesh) :: mesh
type(ESMF_Distgrid) :: distgrid
type(ESMF_CoordSys_Flag) :: coordsys
integer :: rcode
integer :: nf,ns,ng
integer :: k,n
integer :: ndims, nelements
integer ,target :: dimid2(2)
integer ,target :: dimid3(3)
integer ,target :: dimid4(4)
integer ,pointer :: dimid(:)
type(var_desc_t) :: varid
type(io_desc_t) :: iodesc
integer(kind=Pio_Offset_Kind) :: frame
character(CL) :: itemc ! string converted to char
character(CL) :: name1 ! var name
character(CL) :: cunit ! var units
character(CL) :: lpre ! local prefix
character(CS) :: coordvarnames(2) ! coordinate variable names
character(CS) :: coordnames(2) ! coordinate long names
character(CS) :: coordunits(2) ! coordinate units
integer :: lnx,lny,lntile
logical :: luse_float
real(r8) :: lfillvalue
integer, pointer :: minIndexPTile(:,:)
integer, pointer :: maxIndexPTile(:,:)
integer :: dimCount, tileCount
integer, pointer :: Dof(:)
real(r8), pointer :: fldptr1(:)
real(r8), pointer :: fldptr2(:,:)
real(r8), allocatable :: ownedElemCoords(:), ownedElemCoords_x(:), ownedElemCoords_y(:)
character(CS) :: cnumber
character(CL) :: tmpstr
type(ESMF_Field) :: lfield
integer :: rank
integer :: ungriddedUBound(1) ! currently the size must equal 1 for rank 2 fields
integer :: gridToFieldMap(1) ! currently the size must equal 1 for rank 2 fields
logical :: tiles
character(CL), allocatable :: fieldNameList(:)
character(*),parameter :: subName = '(med_io_write_FB) '
!-------------------------------------------------------------------------------
rc = ESMF_Success
lfillvalue = fillvalue
if (present(fillval)) lfillvalue = fillval
lpre = ' '
if (present(pre)) lpre = trim(pre)
luse_float = .false.
if (present(use_float)) luse_float = use_float
tiles = .false.
if (present(ntile)) then
if (ntile > 0) tiles = .true.
end if
! Error check
if (.not. ESMF_FieldBundleIsCreated(FB, rc=rc)) then
call ESMF_LogWrite(trim(subname)//" FB "//trim(lpre)//" not created", ESMF_LOGMSG_INFO)
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
return
endif
! Get number of fields
if (present(flds)) then
nf = size(flds)
else
call ESMF_FieldBundleGet(FB, fieldCount=nf, rc=rc)
write(tmpstr,*) subname//' field count = '//trim(lpre), nf
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
if (nf < 1) then
call ESMF_LogWrite(trim(subname)//" FB "//trim(lpre)//" empty", ESMF_LOGMSG_INFO)
if (dbug_flag > 5) then
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
endif
rc = ESMF_Success
return
endif
allocate(fieldNameList(nf))
call ESMF_FieldBundleGet(FB, fieldNameList=fieldNameList, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end if
! Get field bundle mesh from first field
call FB_getFieldN(FB, 1, field, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(field, mesh=mesh, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Get mesh distgrid and number of elements
call ESMF_MeshGet(mesh, elementDistgrid=distgrid, coordSys=coordsys, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_MeshGet(mesh, spatialDim=ndims, numOwnedElements=nelements, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
write(tmpstr,*) subname, 'ndims, nelements = ', ndims, nelements
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
! Define coordinate attributes according to CoordSys
if (coordsys == ESMF_COORDSYS_CART) then
coordvarnames(1) = trim(lpre)//'_x'
coordvarnames(2) = trim(lpre)//'_y'
coordnames = (/'x-coordinate', 'y-coordinate'/)
coordunits = (/'unitless','unitless'/)
else
coordvarnames(1) = trim(lpre)//'_lon'
coordvarnames(2) = trim(lpre)//'_lat'
coordnames = (/'longitude', 'latitude '/)
if (coordsys == ESMF_COORDSYS_SPH_DEG) coordunits = (/'degrees_E', 'degrees_N'/)
if (coordsys == ESMF_COORDSYS_SPH_RAD) coordunits = (/'radians ', 'radians '/)
end if
! Set element coordinates
if (.not. allocated(ownedElemCoords) .and. ndims > 0 .and. nelements > 0) then
allocate(ownedElemCoords(ndims*nelements))
allocate(ownedElemCoords_x(ndims*nelements/2))
allocate(ownedElemCoords_y(ndims*nelements/2))
call ESMF_MeshGet(mesh, ownedElemCoords=ownedElemCoords, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
ownedElemCoords_x = ownedElemCoords(1::2)
ownedElemCoords_y = ownedElemCoords(2::2)
end if
! Get tile info
call ESMF_DistGridGet(distgrid, dimCount=dimCount, tileCount=tileCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(minIndexPTile(dimCount, tileCount), maxIndexPTile(dimCount, tileCount))
call ESMF_DistGridGet(distgrid, minIndexPTile=minIndexPTile, maxIndexPTile=maxIndexPTile, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! write(tmpstr,*) subname,' counts = ',dimcount,tilecount,minindexptile,maxindexptile
! call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
! TODO: this is not getting the global size correct for a FB coming in that does not have
! all the global grid values in the distgrid - e.g. CTSM
ng = maxval(maxIndexPTile)
if (tiles) then
lnx = ng
lny = 1
lntile = 1
if (nx > 0) lnx = nx
if (ny > 0) lny = ny
if (ntile > 0) lntile = ntile
write(tmpstr,*) subname, 'ng,lnx,lny,lntile = ',ng,lnx,lny,lntile
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
if (lnx*lny*lntile /= ng) then
write(tmpstr,*) subname,' ERROR: grid size not consistent ',ng,lnx,lny,lntile
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
call ESMF_Finalize(endflag=ESMF_END_ABORT)
end if
else
lnx = ng
lny = 1
if (nx > 0) lnx = nx
if (ny > 0) lny = ny
if (lnx*lny /= ng) then
write(tmpstr,*) subname,' WARNING: grid2d size not consistent ',ng,lnx,lny
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
endif
end if
deallocate(minIndexPTile, maxIndexPTile)
if (present(nt)) then
frame = nt
else
frame = -1
end if
! Write header
if (whead) then
if (tiles) then
rcode = pio_def_dim(io_file, trim(lpre)//'_nx', lnx, dimid3(1))
rcode = pio_def_dim(io_file, trim(lpre)//'_ny', lny, dimid3(2))
rcode = pio_def_dim(io_file, trim(lpre)//'_ntile', lntile, dimid3(3))
if (present(nt)) then
dimid4(1:3) = dimid3
rcode = pio_inq_dimid(io_file, 'time', dimid4(4))
dimid => dimid4
else
dimid => dimid3
endif
else
rcode = pio_def_dim(io_file, trim(lpre)//'_nx', lnx, dimid2(1))
rcode = pio_def_dim(io_file, trim(lpre)//'_ny', lny, dimid2(2))
if (present(nt)) then
dimid3(1:2) = dimid2
rcode = pio_inq_dimid(io_file, 'time', dimid3(3))
dimid => dimid3
else
dimid => dimid2
endif
endif
write(tmpstr,*) subname,' dimid = ',dimid
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
do k = 1,nf
! Determine field name
if (present(flds)) then
itemc = trim(flds(k))
else
itemc = trim(fieldNameList(k))
end if
! Determine rank of field with name itemc
call ESMF_FieldBundleGet(FB, itemc, field=lfield, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldGet(lfield, rank=rank, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! TODO (mvertens, 2019-03-13): this is a temporary mod to NOT write hgt
if (trim(itemc) /= "hgt") then
if (rank == 2) then
call ESMF_FieldGet(lfield, ungriddedUBound=ungriddedUBound, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
write(cnumber,'(i0)') ungriddedUbound(1)
call ESMF_LogWrite(trim(subname)//':'//'field '//trim(itemc)// &
' has an griddedUBound of '//trim(cnumber), ESMF_LOGMSG_INFO)
! Create a new output variable for each element of the undistributed dimension
do n = 1,ungriddedUBound(1)
if (trim(itemc) /= "hgt") then
write(cnumber,'(i0)') n
name1 = trim(lpre)//'_'//trim(itemc)//trim(cnumber)
call ESMF_LogWrite(trim(subname)//': defining '//trim(name1), ESMF_LOGMSG_INFO)
if (luse_float) then
rcode = pio_def_var(io_file, trim(name1), PIO_REAL, dimid, varid)
rcode = pio_put_att(io_file, varid,"_FillValue",real(lfillvalue,r4))
else
rcode = pio_def_var(io_file, trim(name1), PIO_DOUBLE, dimid, varid)
rcode = pio_put_att(io_file,varid,"_FillValue",lfillvalue)
end if
if (NUOPC_FieldDictionaryHasEntry(trim(itemc))) then
call NUOPC_FieldDictionaryGetEntry(itemc, canonicalUnits=cunit, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
rcode = pio_put_att(io_file, varid, "units" , trim(cunit))
end if
rcode = pio_put_att(io_file, varid, "standard_name", trim(name1))
if (present(tavg)) then
if (tavg) then
rcode = pio_put_att(io_file, varid, "cell_methods", "time: mean")
endif
endif
end if
end do
else
name1 = trim(lpre)//'_'//trim(itemc)
call ESMF_LogWrite(trim(subname)//':'//trim(itemc)//':'//trim(name1),ESMF_LOGMSG_INFO)
if (luse_float) then
rcode = pio_def_var(io_file, trim(name1), PIO_REAL, dimid, varid)
rcode = pio_put_att(io_file, varid, "_FillValue", real(lfillvalue, r4))
else
rcode = pio_def_var(io_file, trim(name1), PIO_DOUBLE, dimid, varid)
rcode = pio_put_att(io_file, varid, "_FillValue", lfillvalue)
end if
if (NUOPC_FieldDictionaryHasEntry(trim(itemc))) then
call NUOPC_FieldDictionaryGetEntry(itemc, canonicalUnits=cunit, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
rcode = pio_put_att(io_file, varid, "units", trim(cunit))
end if
rcode = pio_put_att(io_file, varid, "standard_name", trim(name1))
if (present(tavg)) then
if (tavg) then
rcode = pio_put_att(io_file, varid, "cell_methods", "time: mean")
endif
end if