forked from NOAA-GFDL/FMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfms_io.F90
8651 lines (7515 loc) · 377 KB
/
fms_io.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
!***********************************************************************
!* GNU Lesser General Public License
!*
!* This file is part of the GFDL Flexible Modeling System (FMS).
!*
!* FMS is free software: you can redistribute it and/or modify it under
!* the terms of the GNU Lesser General Public License as published by
!* the Free Software Foundation, either version 3 of the License, or (at
!* your option) any later version.
!*
!* FMS is distributed in the hope that it will be useful, but WITHOUT
!* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
!* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
!* for more details.
!*
!* You should have received a copy of the GNU Lesser General Public
!* License along with FMS. If not, see <http://www.gnu.org/licenses/>.
!***********************************************************************
module fms_io_mod
#include <fms_platform.h>
!
!
! <CONTACT EMAIL="Zhi.Liang@noaa.gov">
! Zhi Liang
! </CONTACT>
! <CONTACT EMAIL="Matthew.Harrison@noaa.gov">
! M.J. Harrison
! </CONTACT>
!
! <REVIEWER EMAIL="Matthew.Harrison@noaa.gov">
! M.J. Harrison
! </REVIEWER>
! <REVIEWER EMAIL="Bruce.Wyman@noaa.gov">
! B. Wyman
! </REVIEWER>
!<DESCRIPTION>
! This module is for writing and reading restart data in NetCDF format.
! fms_io_init must be called before the first write_data/read_data call
! For writing, fms_io_exit must be called after ALL write calls have
! been made. Typically, fms_io_init and fms_io_exit are placed in the
! main (driver) program while read_data and write_data can be called where needed.
! Presently, two combinations of threading and fileset are supported, users can choose
! one line of the following by setting namelist:
!
! With the introduction of netCDF restart files, there is a need for a global
! switch to turn on/off netCDF restart options in all of the modules that deal with
! restart files. Here two more namelist variables (logical type) are introduced to fms_io
!
! fms_netcdf_override
! fms_netcdf_restart
!
! because default values of both flags are .true., the default behavior of the entire model is
! to use netCDF IO mode. To turn off netCDF restart, simply set fms_netcdf_restart to .false.
!
!</DESCRIPTION>
! <NAMELIST NAME="fms_io_nml">
! <DATA NAME="threading_read" TYPE="character">
! threading_read can be 'single' or 'multi'
! </DATA>
! <DATA NAME="fms_netcdf_override" TYPE="logical">
! .true. : fms_netcdf_restart overrides individual do_netcdf_restart value (default behavior)
! .false.: individual module settings has a precedence over the global setting, therefore fms_netcdf_restart is ignored
! </DATA>
! <DATA NAME="fms_netcdf_restart" TYPE="logical">
! .true. : all modules deal with restart files will operate under netCDF mode (default behavior)
! .false.: all modules deal with restart files will operate under binary mode
! This flag is effective only when fms_netcdf_override is .true. When fms_netcdf_override is .false., individual
! module setting takes over.
! </DATA>
! <DATA NAME="time_stamped_restart" TYPE="logical">
! .true. : time_stamp will be added to the restart file name as a prefix when
! optional argument time_stamp is passed into routine save_restart.
! .false.: time_stmp will not be added to the restart file name even though
! time_stamp is passed into save_restart.
! default is true.
! </DATA>
! <DATA NAME="print_chksum" TYPE="logical">
! set print_chksum (default is false) to true to print out chksum of fields that are
! read and written through save_restart/restore_state. The chksum is accross all the
! processors, so there will be only one chksum even there are multiple-tiles in the
! grid. For the multiple case, the filename appeared in the message will contain
! tile1 because the message is print out from root pe and on root pe the tile id is tile1.
! </DATA>
! <DATA NAME="debug_mask_list" TYPE="logical">
! set debug_mask_list (default is false) to true to print out mask_list reading from mask_table.
! </DATA>
! <DATA NAME="checksum_required" TYPE="logical">
! Set checksum_required (default is true) to true to compare checksums stored in the attribute of a
! field against the checksum after reading in the data. This check mitigates the possibility of data
! that gets corrupted on write or read from being used in a n ongoing fashion. The checksum is across
! all the processors, so there will be only one checksum even if there are multiple-tiles in the
! grid. For the decomposed file case, the filename appearing in the message will contain tile1
! because the message is printed out from the root pe and on root pe the tile id is tile1.
!
! Set checksum_required to false if you do not want to compare checksums.
! </DATA>
!</NAMELIST>
use mpp_io_mod, only: mpp_open, mpp_close, mpp_io_init, mpp_io_exit, mpp_read, mpp_write
use mpp_io_mod, only: mpp_write_meta, mpp_get_info, mpp_get_atts, mpp_get_fields
use mpp_io_mod, only: mpp_read_compressed, mpp_write_compressed, mpp_def_dim
use mpp_io_mod, only: mpp_write_unlimited_axis, mpp_read_distributed_ascii
use mpp_io_mod, only: mpp_get_axes, mpp_get_axis_data, mpp_get_att_char, mpp_get_att_name
use mpp_io_mod, only: mpp_get_att_real_scalar, mpp_attribute_exist, mpp_is_dist_ioroot
use mpp_io_mod, only: fieldtype, axistype, atttype, default_field, default_axis, default_att
use mpp_io_mod, only: MPP_NETCDF, MPP_ASCII, MPP_MULTI, MPP_SINGLE, MPP_OVERWR, MPP_RDONLY
use mpp_io_mod, only: MPP_IEEE32, MPP_NATIVE, MPP_DELETE, MPP_APPEND, MPP_SEQUENTIAL, MPP_DIRECT
use mpp_io_mod, only: MAX_FILE_SIZE, mpp_get_att_value
use mpp_io_mod, only: mpp_get_dimension_length
use mpp_domains_mod, only: domain2d, domain1d, NULL_DOMAIN1D, NULL_DOMAIN2D, operator( .EQ. )
use mpp_domains_mod, only: CENTER, EAST, WEST, NORTH, SOUTH, CORNER
use mpp_domains_mod, only: mpp_get_domain_components, mpp_get_compute_domain, mpp_get_data_domain
use mpp_domains_mod, only: mpp_get_domain_shift, mpp_get_global_domain, mpp_global_field, mpp_domain_is_tile_root_pe
use mpp_domains_mod, only: mpp_get_ntile_count, mpp_get_current_ntile, mpp_get_tile_id
use mpp_domains_mod, only: mpp_get_pelist, mpp_get_io_domain, mpp_get_domain_npes
use mpp_domains_mod, only: domainUG, mpp_pass_SG_to_UG, mpp_get_UG_domain_ntiles, mpp_get_UG_domain_tile_id
use mpp_mod, only: mpp_error, FATAL, NOTE, WARNING, mpp_pe, mpp_root_pe, mpp_npes, stdlog, stdout
use mpp_mod, only: mpp_broadcast, ALL_PES, mpp_chksum, mpp_get_current_pelist, mpp_npes, lowercase
use mpp_mod, only: input_nml_file, mpp_get_current_pelist_name, uppercase
use mpp_mod, only: mpp_gather, mpp_scatter, mpp_send, mpp_recv, mpp_sync_self, COMM_TAG_1, EVENT_RECV
use mpp_mod, only: MPP_FILL_DOUBLE,MPP_FILL_INT
use platform_mod, only: r8_kind
!----------
!ug support
use mpp_parameter_mod, only: COMM_TAG_2
use mpp_domains_mod, only: mpp_get_UG_io_domain
use mpp_domains_mod, only: mpp_domain_UG_is_tile_root_pe
use mpp_domains_mod, only: mpp_get_UG_domain_npes
use mpp_domains_mod, only: mpp_get_UG_domain_pelist
use mpp_io_mod, only: mpp_io_unstructured_write
use mpp_io_mod, only: mpp_io_unstructured_read
use mpp_io_mod, only: mpp_file_is_opened
!----------
implicit none
private
integer, parameter, private :: max_split_file = 50
integer, parameter, private :: max_fields=400
integer, parameter, private :: max_axes=40
integer, parameter, private :: max_atts=20
integer, parameter, private :: max_domains = 10
integer, parameter, private :: MAX_TIME_LEVEL_REGISTER = 2
integer, parameter, private :: MAX_TIME_LEVEL_WRITE = 20
integer, parameter :: max_axis_size=10000
! Index postions for axes in restart_file_type
! This is done so the user may define the axes
! in any order but a check can be performed
! to ensure no registration of duplicate axis
!----------
!ug support
integer(INT_KIND),parameter,public :: XIDX = 1
integer(INT_KIND),parameter,public :: YIDX = 2
integer(INT_KIND),parameter,public :: CIDX = 3
integer(INT_KIND),parameter,public :: ZIDX = 4
integer(INT_KIND),parameter,public :: HIDX = 5
integer(INT_KIND),parameter,public :: TIDX = 6
integer(INT_KIND),parameter,public :: UIDX = 7
integer(INT_KIND),parameter,public :: CCIDX = 8
!---------
integer, parameter, private :: NIDX=8
type meta_type
type(meta_type), pointer :: prev=>null(), next=>null()
!!$ Gfortran on gaea does not yet support deferred length character strings
!!$ character(len=:),allocatable :: name
character(len=256) :: name
real, allocatable :: rval(:)
integer, allocatable :: ival(:)
!!$ Gfortran on gaea does not yet support deferred length character strings
!!$ character(len=:), allocatable :: cval
character(len=256) :: cval
end type meta_type
type ax_type
private
character(len=128) :: name = ''
character(len=128) :: units = ''
character(len=128) :: longname = ''
character(len=8) :: cartesian = ''
character(len=256) :: compressed = ''
character(len=128) :: dimlen_name = ''
character(len=128) :: dimlen_lname = ''
character(len=128) :: calendar = ''
integer :: sense !Orientation of z axis definition
integer :: dimlen !max dim of elements across global domain
real :: min !valid min for real axis data
integer :: imin !valid min for integer axis data
integer,allocatable :: idx(:) !compressed io-domain index vector
integer,allocatable :: nelems(:) !num elements for each rank in io domain
real, pointer :: data(:) =>NULL() !real axis values (not used if time axis)
type(domain2d),pointer :: domain =>NULL() ! domain associated with compressed axis
!----------
!ug support
type(domainUG),pointer :: domain_ug => null() !<A pointer to an unstructured mpp domain.
integer(INT_KIND) :: nelems_for_current_rank !<The number of grid points registered to the current rank (used for error checking).
!----------
end type ax_type
type var_type
private
character(len=128) :: name = ''
character(len=128) :: longname = ''
character(len=128) :: units = ''
real, dimension(:,:,:,:), _ALLOCATABLE :: buffer _NULL
logical :: domain_present = .FALSE.
integer :: domain_idx = -1
logical :: is_dimvar = .FALSE.
logical :: read_only = .FALSE.
logical :: owns_data = .FALSE. ! if true, restart owns the data and will deallocate them when freed
type(fieldtype) :: field
type(axistype) :: axis
integer :: position
integer :: ndim
integer :: siz(5) ! X/Y/Z/T/A extent of fields (data domain
! size for distributed writes;global size for reads)
integer :: gsiz(4) ! global X/Y/Z/A extent of fields
integer :: id_axes(4) ! store index for x/y/z/a axistype.
logical :: initialized ! indicate if the field is read or not in routine save_state.
logical :: mandatory ! indicate if the field is mandatory to be when restart.
integer :: is, ie, js, je ! index of the data in compute domain
real :: default_data
character(len=8) :: compressed_axis !< If on a compressed axis, which axis
integer, dimension(:), allocatable :: pelist
integer :: ishift, jshift ! can be used to shift indices when no_domain=T
integer :: x_halo, y_halo ! can be used to indicate halo size when no_domain=T
!----------
!ug support
type(domainUG),pointer :: domain_ug => null() !<A pointer to an unstructured mpp domain.
integer(INT_KIND),dimension(5) :: field_dimension_order !<Array telling the ordering of the dimensions for the field.
integer(INT_KIND),dimension(NIDX) :: field_dimension_sizes !<Array of sizes of the dimensions for the field.
!----------
end type var_type
type Ptr0Dr
real, pointer :: p => NULL()
end type Ptr0Dr
type Ptr1Dr
real, dimension(:), pointer :: p => NULL()
end type Ptr1Dr
type Ptr2Dr
real, dimension(:,:), pointer :: p => NULL()
end type Ptr2Dr
type Ptr3Dr
real, dimension(:,:,:), pointer :: p => NULL()
end type Ptr3Dr
type Ptr2Dr8
real(DOUBLE_KIND), dimension(:,:), pointer :: p => NULL()
end type Ptr2Dr8
type Ptr3Dr8
real(DOUBLE_KIND), dimension(:,:,:), pointer :: p => NULL()
end type Ptr3Dr8
type Ptr4Dr
real, dimension(:,:,:,:), pointer :: p => NULL()
end type Ptr4Dr
type Ptr0Di
integer, pointer :: p => NULL()
end type Ptr0Di
type Ptr1Di
integer, dimension(:), pointer :: p => NULL()
end type Ptr1Di
type Ptr2Di
integer, dimension(:,:), pointer :: p => NULL()
end type Ptr2Di
type Ptr3Di
integer, dimension(:,:,:), pointer :: p => NULL()
end type Ptr3Di
type restart_file_type
private
integer :: unit = -1 ! mpp_io unit for netcdf file
character(len=128) :: name = ''
integer :: register_id = 0
integer :: nvar = 0
integer :: natt = 0
integer :: max_ntime = 0
logical :: is_root_pe = .FALSE.
logical :: is_compressed = .FALSE.
logical :: unlimited_axis = .FALSE.
integer :: tile_count = 1
type(ax_type), allocatable :: axes(:) ! Currently define X,Y,Compressed, unlimited and maybe Z
type(meta_type), pointer :: first =>NULL() ! pointer to first additional global metadata element
type(var_type), dimension(:), pointer :: var => NULL()
type(Ptr0Dr), dimension(:,:), pointer :: p0dr => NULL()
type(Ptr1Dr), dimension(:,:), pointer :: p1dr => NULL()
type(Ptr2Dr), dimension(:,:), pointer :: p2dr => NULL()
type(Ptr3Dr), dimension(:,:), pointer :: p3dr => NULL()
type(Ptr2Dr8), dimension(:,:), pointer :: p2dr8 => NULL()
type(Ptr3Dr8), dimension(:,:), pointer :: p3dr8 => NULL()
type(Ptr4Dr), dimension(:,:), pointer :: p4dr => NULL()
type(Ptr0Di), dimension(:,:), pointer :: p0di => NULL()
type(Ptr1Di), dimension(:,:), pointer :: p1di => NULL()
type(Ptr2Di), dimension(:,:), pointer :: p2di => NULL()
type(Ptr3Di), dimension(:,:), pointer :: p3di => NULL()
end type restart_file_type
interface read_data
module procedure read_data_4d_new
module procedure read_data_3d_new
module procedure read_data_2d_new
module procedure read_data_2d_UG
module procedure read_data_1d_new
module procedure read_data_scalar_new
module procedure read_data_i3d_new
module procedure read_data_i2d_new
module procedure read_data_i1d_new
module procedure read_data_iscalar_new
module procedure read_data_2d, read_ldata_2d, read_idata_2d
module procedure read_data_3d, read_data_4d
#ifdef OVERLOAD_C8
module procedure read_cdata_2d,read_cdata_3d,read_cdata_4d
#endif
module procedure read_data_text
module procedure read_data_2d_region
module procedure read_data_3d_region
#ifdef OVERLOAD_R8
module procedure read_data_2d_region_r8
module procedure read_data_3d_region_r8
#endif
end interface
interface read_distributed
module procedure read_distributed_r1D
module procedure read_distributed_r3D
module procedure read_distributed_r5D
module procedure read_distributed_i1D
module procedure read_distributed_iscalar
module procedure read_distributed_a1D
end interface
! Only need read compressed att; write is handled in with
! mpp_io calls in save_compressed_restart
interface read_compressed
module procedure read_compressed_i1d
module procedure read_compressed_i2d
module procedure read_compressed_1d
module procedure read_compressed_2d
module procedure read_compressed_3d
end interface read_compressed
interface write_data
module procedure write_data_4d_new
module procedure write_data_3d_new
module procedure write_data_2d_new
module procedure write_data_1d_new
module procedure write_data_scalar_new
module procedure write_data_i3d_new
module procedure write_data_i2d_new
module procedure write_data_i1d_new
module procedure write_data_iscalar_new
module procedure write_data_2d, write_ldata_2d, write_idata_2d
module procedure write_data_3d, write_data_4d
#ifdef OVERLOAD_C8
module procedure write_cdata_2d,write_cdata_3d,write_cdata_4d
#endif
end interface
interface register_restart_field
module procedure register_restart_field_r0d
module procedure register_restart_field_r1d
module procedure register_restart_field_r2d
module procedure register_restart_field_r3d
#ifdef OVERLOAD_R8
module procedure register_restart_field_r2d8
module procedure register_restart_field_r3d8
module procedure register_restart_field_r2d8_2level
module procedure register_restart_field_r3d8_2level
#endif
module procedure register_restart_field_r4d
module procedure register_restart_field_i0d
module procedure register_restart_field_i1d
module procedure register_restart_field_i2d
module procedure register_restart_field_i3d
module procedure register_restart_field_r0d_2level
module procedure register_restart_field_r1d_2level
module procedure register_restart_field_r2d_2level
module procedure register_restart_field_r3d_2level
module procedure register_restart_field_i0d_2level
module procedure register_restart_field_i1d_2level
module procedure register_restart_field_i2d_2level
module procedure register_restart_field_i3d_2level
module procedure register_restart_region_r2d
module procedure register_restart_region_r3d
end interface
interface register_restart_axis
module procedure register_restart_axis_r1d
module procedure register_restart_axis_i1d
module procedure register_restart_axis_unlimited
end interface
interface reset_field_pointer
module procedure reset_field_pointer_r0d
module procedure reset_field_pointer_r1d
module procedure reset_field_pointer_r2d
module procedure reset_field_pointer_r3d
module procedure reset_field_pointer_r4d
module procedure reset_field_pointer_i0d
module procedure reset_field_pointer_i1d
module procedure reset_field_pointer_i2d
module procedure reset_field_pointer_i3d
module procedure reset_field_pointer_r0d_2level
module procedure reset_field_pointer_r1d_2level
module procedure reset_field_pointer_r2d_2level
module procedure reset_field_pointer_r3d_2level
module procedure reset_field_pointer_i0d_2level
module procedure reset_field_pointer_i1d_2level
module procedure reset_field_pointer_i2d_2level
module procedure reset_field_pointer_i3d_2level
end interface
interface restore_state
module procedure restore_state_all
module procedure restore_state_one_field
end interface
interface query_initialized
module procedure query_initialized_id
module procedure query_initialized_name
module procedure query_initialized_r2d
module procedure query_initialized_r3d
module procedure query_initialized_r4d
end interface
interface set_initialized
module procedure set_initialized_id
module procedure set_initialized_name
module procedure set_initialized_r2d
module procedure set_initialized_r3d
module procedure set_initialized_r4d
end interface
interface get_global_att_value
module procedure get_global_att_value_text
module procedure get_global_att_value_real
end interface
interface get_var_att_value
module procedure get_var_att_value_text
end interface
interface parse_mask_table
module procedure parse_mask_table_2d
module procedure parse_mask_table_3d
end interface
interface get_mosaic_tile_file
module procedure get_mosaic_tile_file_sg
module procedure get_mosaic_tile_file_ug
end interface
integer :: num_files_r = 0 ! number of currently opened files for reading
integer :: num_files_w = 0 ! number of currently opened files for writing
integer :: num_domains = 0 ! number of domains in array_domain
integer :: num_registered_files = 0 ! mumber of files registered by calling register_restart_file
integer :: thread_r, form
logical :: module_is_initialized = .FALSE.
character(len=128):: error_msg
logical :: great_circle_algorithm=.FALSE.
!------ private data, pointer to current 2d domain ------
! entrained from fms_mod. This will be deprecated in the future.
type(domain2D), pointer, private :: Current_domain =>NULL()
integer, private :: is,ie,js,je ! compute domain
integer, private :: isd,ied,jsd,jed ! data domain
integer, private :: isg,ieg,jsg,jeg ! global domain
character(len=128), dimension(:), allocatable :: registered_file ! file names registered through register_restart_file
type(restart_file_type), dimension(:), allocatable :: files_read ! store files that are read through read_data
type(restart_file_type), dimension(:), allocatable, target :: files_write ! store files that are written through write_data
type(domain2d), dimension(max_domains), target, save :: array_domain
type(domain1d), dimension(max_domains), save :: domain_x, domain_y
public :: read_data, read_compressed, write_data, read_distributed
public :: fms_io_init, fms_io_exit, field_size, get_field_size
public :: open_namelist_file, open_restart_file, open_ieee32_file, close_file
public :: set_domain, nullify_domain, get_domain_decomp, return_domain
public :: open_file, open_direct_file
public :: get_restart_io_mode, get_tile_string, string
public :: get_mosaic_tile_grid, get_mosaic_tile_file, get_file_name, get_mosaic_tile_file_ug
public :: get_global_att_value, get_var_att_value
public :: file_exist, field_exist
public :: register_restart_field, register_restart_axis, save_restart, restore_state
public :: set_meta_global
public :: save_restart_border, restore_state_border
public :: restart_file_type, query_initialized, set_initialized, free_restart_type
public :: reset_field_name, reset_field_pointer
private :: lookup_field_r, lookup_axis, unique_axes
public :: dimension_size
public :: set_filename_appendix, get_instance_filename
public :: get_filename_appendix, nullify_filename_appendix
public :: parse_mask_table
public :: get_great_circle_algorithm
public :: write_version_number
character(len=32), save :: filename_appendix = ''
!--- public interface ---
interface string
module procedure string_from_integer
module procedure string_from_real
end interface
!--- namelist interface
logical :: fms_netcdf_override = .true.
logical :: fms_netcdf_restart = .true.
character(len=32) :: threading_read = 'multi'
character(len=32) :: format = 'netcdf'
logical :: read_all_pe = .TRUE.
character(len=64) :: iospec_ieee32 = '-N ieee_32'
integer :: max_files_w = 40
integer :: max_files_r = 40
integer :: dr_set_size = 10
logical :: read_data_bug = .false.
logical :: time_stamp_restart = .true.
logical :: print_chksum = .false.
logical :: show_open_namelist_file_warning = .false.
logical :: debug_mask_list = .false.
logical :: checksum_required = .true.
namelist /fms_io_nml/ fms_netcdf_override, fms_netcdf_restart, &
threading_read, format, read_all_pe, iospec_ieee32,max_files_w,max_files_r, &
read_data_bug, time_stamp_restart, print_chksum, show_open_namelist_file_warning, &
debug_mask_list, checksum_required, dr_set_size
integer :: pack_size ! = 1 for double = 2 for float
! Include variable "version" to be written to log file.
#include<file_version.h>
!----------
!ug support
public :: fms_io_unstructured_register_restart_axis
public :: fms_io_unstructured_register_restart_field
public :: fms_io_unstructured_save_restart
public :: fms_io_unstructured_read
public :: fms_io_unstructured_get_field_size
public :: fms_io_unstructured_file_unit
public :: fms_io_unstructured_field_exist
interface fms_io_unstructured_register_restart_axis
module procedure fms_io_unstructured_register_restart_axis_r1D
module procedure fms_io_unstructured_register_restart_axis_i1D
module procedure fms_io_unstructured_register_restart_axis_u
end interface fms_io_unstructured_register_restart_axis
interface fms_io_unstructured_register_restart_field
module procedure fms_io_unstructured_register_restart_field_r_0d
module procedure fms_io_unstructured_register_restart_field_r_1d
module procedure fms_io_unstructured_register_restart_field_r_2d
module procedure fms_io_unstructured_register_restart_field_r_3d
#ifdef OVERLOAD_R8
module procedure fms_io_unstructured_register_restart_field_r8_2d
module procedure fms_io_unstructured_register_restart_field_r8_3d
#endif
module procedure fms_io_unstructured_register_restart_field_i_0d
module procedure fms_io_unstructured_register_restart_field_i_1d
module procedure fms_io_unstructured_register_restart_field_i_2d
end interface fms_io_unstructured_register_restart_field
interface fms_io_unstructured_read
module procedure fms_io_unstructured_read_r_scalar
module procedure fms_io_unstructured_read_r_1D
module procedure fms_io_unstructured_read_r_2D
module procedure fms_io_unstructured_read_r_3D
module procedure fms_io_unstructured_read_i_scalar
module procedure fms_io_unstructured_read_i_1D
module procedure fms_io_unstructured_read_i_2D
end interface fms_io_unstructured_read
!----------
contains
! <SUBROUTINE NAME="get_restart_io_mode">
! <DESCRIPTION>
! With the introduction of netCDF restart files, there is a need for a global
! switch to turn on/off netCDF restart options in all of the modules that deal with
! restart files. Here two more namelist variables (logical type) are introduced to fms_io
!
! fms_netcdf_override
! fms_netcdf_restart
!
! because default values of both flags are .true., the default behavior of the entire model is
! to use netCDF IO mode. To turn off netCDF restart, simply set fms_netcdf_restart to .false.
!
! </DESCRIPTION>
! <TEMPLATE>
! call get_fms_io_mode(do_netcdf_restart)
! </TEMPLATE>
! <INOUT NAME="do_netcdf_restart" TYPE="logical">
! This the input argument that contains the individual module setting of restart IO mode.
! Upon return from this subroutine, this output argument contains the actual setting of restart IO mode
! the calling module will be using
! </INOUT>
! </SUBROUTINE>
subroutine get_restart_io_mode(do_netcdf_restart)
logical, intent(inout) :: do_netcdf_restart
if(fms_netcdf_override) do_netcdf_restart = fms_netcdf_restart
end subroutine get_restart_io_mode
!.....................................................................
! <SUBROUTINE NAME="fms_io_init">
! <DESCRIPTION>
! Initialize fms_io module
! </DESCRIPTION>
! <TEMPLATE>
! call fms_io_init()
! </TEMPLATE>
subroutine fms_io_init()
integer :: i, unit, io_status, logunit
integer, allocatable, dimension(:) :: pelist
real(DOUBLE_KIND) :: doubledata = 0
real :: realarray(4)
character(len=256) :: grd_file, filename
logical :: is_mosaic_grid
character(len=4096) :: attvalue
if (module_is_initialized) return
call mpp_io_init()
#ifdef INTERNAL_FILE_NML
read (input_nml_file, fms_io_nml, iostat=io_status)
if (io_status > 0) then
call mpp_error(FATAL,'=>fms_io_init: Error reading input.nml')
endif
#else
call mpp_open(unit, 'input.nml',form=MPP_ASCII,action=MPP_RDONLY)
read(unit,fms_io_nml,iostat=io_status)
if (io_status > 0) then
call mpp_error(FATAL,'=>fms_io_init: Error reading input.nml')
endif
call mpp_close (unit)
#endif
! take namelist options if present
! determine packsize
pack_size = size(transfer(doubledata, realarray))
if( pack_size .NE. 1 .AND. pack_size .NE. 2) call mpp_error(FATAL,'=>fms_io_init: pack_size should be 1 or 2')
select case (threading_read)
case ('multi')
thread_r = MPP_MULTI
case ('single')
thread_r = MPP_SINGLE
case default
call mpp_error(FATAL,'fms_io_init: threading_read should be multi/single but you chose'//trim(threading_read))
end select
! take namelist options if present
select case(format)
case ('netcdf')
form=MPP_NETCDF
case default
call mpp_error(FATAL,'fms_io_init: only NetCDF format currently supported in fms_io')
end select
! Initially allocate files_write and files_read
allocate(files_write(max_files_w),files_read(max_files_r))
allocate(registered_file(max_files_w))
do i = 1, max_domains
array_domain(i) = NULL_DOMAIN2D
enddo
!---- initialize module domain2d pointer ----
nullify (Current_domain)
!This is set here instead of at the end of the routine to prevent the read_data call below from stopping the model
module_is_initialized = .TRUE.
! Record the version number in the log file
call write_version_number("FMS_IO_MOD", version)
!--- read INPUT/grid_spec.nc to decide the value of great_circle_algorithm
!--- great_circle_algorithm could be true only for mosaic grid.
great_circle_algorithm = .false.
grd_file = "INPUT/grid_spec.nc"
is_mosaic_grid = .FALSE.
if (file_exist(grd_file)) then
if(field_exist(grd_file, 'atm_mosaic_file')) then ! coupled grid
is_mosaic_grid = .TRUE.
else if(field_exist(grd_file, "gridfiles")) then
call read_data(grd_file, "gridfiles", filename, level=1)
grd_file = 'INPUT/'//trim(filename)
is_mosaic_grid = .TRUE.
endif
endif
if(is_mosaic_grid) then
if( get_global_att_value(grd_file, "great_circle_algorithm", attvalue) ) then
if(trim(attvalue) == "TRUE") then
great_circle_algorithm = .true.
else if(trim(attvalue) == "FALSE") then
great_circle_algorithm = .false.
else
call mpp_error(FATAL, "fms_io(fms_io_init: value of global attribute great_circle_algorithm in file"// &
trim(grd_file)//" should be TRUE of FALSE")
endif
endif
endif
if(great_circle_algorithm .AND. (mpp_pe() == mpp_root_pe()) ) then
call mpp_error(NOTE,"fms_io_mod: great_circle algorithm will be used in the model run")
endif
end subroutine fms_io_init
! </SUBROUTINE>
! <SUBROUTINE NAME="fms_io_exit">
! <DESCRIPTION>
! This routine is called after ALL fields have been written to temporary files
! The result NETCDF files are created here.
! </DESCRIPTION>
! <TEMPLATE>
! call fms_io_exit
! </TEMPLATE>
subroutine fms_io_exit()
integer :: num_x_axes, num_y_axes, num_z_axes
integer :: unit
real, dimension(max_axis_size) :: axisdata
real :: tlev
integer, dimension(max_axes) :: id_x_axes, siz_x_axes
integer, dimension(max_axes) :: id_y_axes, siz_y_axes
integer, dimension(max_axes) :: id_z_axes, siz_z_axes
type(axistype), dimension(max_axes) :: x_axes, y_axes, z_axes
type(axistype) :: t_axes
type(var_type), pointer, save :: cur_var=>NULL()
integer :: i, j, k, kk
character(len=256) :: filename
character(len=10) :: axisname
logical :: domain_present
logical :: write_on_this_pe
type(domain2d), pointer :: io_domain =>NULL()
if( .NOT.module_is_initialized )return !make sure it's only called once per PE
do i=1,max_axis_size
axisdata(i) = i
enddo
! each field has an associated domain type (may be undefined).
! each file only needs to write unique axes (i.e. if 2 fields share an identical axis, then only write the axis once)
! unique axes are defined by the global size and domain decomposition (i.e. can support identical axis sizes with
! different domain decomposition)
do i = 1, num_files_w
filename = files_write(i)%name
!--- check if any field in this file present domain.
domain_present = .false.
do j = 1, files_write(i)%nvar
if (files_write(i)%var(j)%domain_present) then
domain_present = .true.
exit
end if
end do
!--- get the unique axes for all the fields.
num_x_axes = unique_axes(files_write(i), 1, id_x_axes, siz_x_axes, domain_x)
num_y_axes = unique_axes(files_write(i), 2, id_y_axes, siz_y_axes, domain_y)
num_z_axes = unique_axes(files_write(i), 3, id_z_axes, siz_z_axes )
if( domain_present ) then
call mpp_open(unit,trim(filename),action=MPP_OVERWR,form=form, &
is_root_pe=files_write(i)%is_root_pe, domain=array_domain(files_write(i)%var(j)%domain_idx))
else ! global data
call mpp_open(unit,trim(filename),action=MPP_OVERWR,form=form,threading=MPP_SINGLE,&
fileset=MPP_SINGLE, is_root_pe=files_write(i)%is_root_pe)
end if
write_on_this_pe = .false.
if(domain_present) then
io_domain => mpp_get_io_domain(array_domain(files_write(i)%var(j)%domain_idx))
if(associated(io_domain)) then
if(mpp_domain_is_tile_root_pe(io_domain)) write_on_this_pe = .true.
endif
endif
!--- always write out from root pe
if( files_write(i)%is_root_pe ) write_on_this_pe = .true.
do j = 1, num_x_axes
if (j < 10) then
write(axisname,'(a,i1)') 'xaxis_',j
else
write(axisname,'(a,i2)') 'xaxis_',j
endif
if(id_x_axes(j) > 0) then
call mpp_write_meta(unit,x_axes(j),axisname,'none',axisname, &
data=axisdata(1:siz_x_axes(j)),domain=domain_x(id_x_axes(j)),cartesian='X')
else
call mpp_write_meta(unit,x_axes(j),axisname,'none',axisname, &
data=axisdata(1:siz_x_axes(j)),cartesian='X')
endif
end do
do j = 1, num_y_axes
if (j < 10) then
write(axisname,'(a,i1)') 'yaxis_',j
else
write(axisname,'(a,i2)') 'yaxis_',j
endif
if(id_y_axes(j) > 0) then
call mpp_write_meta(unit,y_axes(j),axisname,'none',axisname, &
data=axisdata(1:siz_y_axes(j)),domain=domain_y(id_y_axes(j)),cartesian='Y')
else
call mpp_write_meta(unit,y_axes(j),axisname,'none',axisname, &
data=axisdata(1:siz_y_axes(j)),cartesian='Y')
endif
end do
do j = 1, num_z_axes
if (j < 10) then
write(axisname,'(a,i1)') 'zaxis_',j
else
write(axisname,'(a,i2)') 'zaxis_',j
endif
call mpp_write_meta(unit,z_axes(j),axisname,'none',axisname, &
data=axisdata(1:siz_z_axes(j)),cartesian='Z')
end do
! write time axis (comment out if no time axis)
call mpp_write_meta(unit,t_axes,&
'Time','time level','Time',cartesian='T')
! write metadata for fields
do j = 1, files_write(i)%nvar
cur_var => files_write(i)%var(j)
call mpp_write_meta(unit,cur_var%field, (/x_axes(cur_var%id_axes(1)), &
y_axes(cur_var%id_axes(2)), z_axes(cur_var%id_axes(3)), t_axes/), cur_var%name, &
'none',cur_var%name,pack=pack_size)
enddo
! write values for ndim of spatial axes
do j = 1, num_x_axes
call mpp_write(unit,x_axes(j))
enddo
do j = 1, num_y_axes
call mpp_write(unit,y_axes(j))
enddo
do j = 1, num_z_axes
call mpp_write(unit,z_axes(j))
enddo
! write data of each field
do k = 1, files_write(i)%max_ntime
do j = 1, files_write(i)%nvar
cur_var => files_write(i)%var(j)
tlev=k
! If some fields only have one time level, we do not need to write the second level, just keep
! the data missing.
! If some fields only have one time level, we just write out 0 to the other level
if(k > cur_var%siz(4)) then
cur_var%buffer(:,:,:,1) = 0.0
kk = 1
else
kk = k
end if
if(cur_var%domain_present) then
call mpp_write(unit, cur_var%field,array_domain(cur_var%domain_idx), cur_var%buffer(:,:,:,kk), tlev, &
default_data=cur_var%default_data)
else if (write_on_this_pe) then
call mpp_write(unit, cur_var%field, cur_var%buffer(:,:,:,kk), tlev)
end if
enddo ! end j loop
enddo ! end k loop
call mpp_close(unit)
enddo ! end i loop
!--- release the memory
do i = 1, num_files_w
do j = 1, files_write(i)%nvar
deallocate(files_write(i)%var(j)%buffer)
end do
end do
cur_var=>NULL()
module_is_initialized = .false.
num_files_w = 0
num_files_r = 0
end subroutine fms_io_exit
!.....................................................................
! </SUBROUTINE>
! <SUBROUTINE NAME="write_data">
!<DESCRIPTION>
! This subroutine performs writing "fieldname" to file "filename". All values of "fieldname"
! will be written to a temporary file. The final NETCDF file will be created only at a later step
! when the user calls fms_io_exit. Therefore, make sure that fms_io_exit is called after all
! fields have been written by this subroutine.
!</DESCRIPTION>
! <TEMPLATE>
! call write_data(filename, fieldname, data, domain)
! </TEMPLATE>
! <IN NAME="filename" TYPE="character" DIM="(*)">
! File name
! </IN>
! <IN NAME="fieldname" TYPE="character" DIM="(*)">
! Field name
! </IN>
! <IN NAME="data" TYPE="real">
! array containing data of fieldname
! </IN>
! <IN NAME="domain" TYPE="domain, optional">
! domain of fieldname
! </IN>
!=================================================================================
subroutine write_data_i3d_new(filename, fieldname, data, domain, &
no_domain, position, tile_count, data_default)
character(len=*), intent(in) :: filename, fieldname
integer, dimension(:,:,:), intent(in) :: data
type(domain2d), intent(in), optional :: domain
logical, intent(in), optional :: no_domain
integer, intent(in), optional :: position, tile_count, data_default
real :: default_data
default_data = TRANSFER(MPP_FILL_INT,default_data)
if(present(data_default)) default_data = real(data_default)
call write_data_3d_new(filename, fieldname, real(data), domain, &
no_domain, .false., position, tile_count, data_default=default_data)
end subroutine write_data_i3d_new
!.....................................................................
subroutine write_data_i2d_new(filename, fieldname, data, domain, &
no_domain, position, tile_count, data_default)
character(len=*), intent(in) :: filename, fieldname
integer, dimension(:,:), intent(in) :: data
type(domain2d), intent(in), optional :: domain
logical, intent(in), optional :: no_domain
integer, intent(in), optional :: position, tile_count, data_default
real :: default_data
default_data = TRANSFER(MPP_FILL_INT,default_data)
if(present(data_default)) default_data = real(data_default)
call write_data_2d_new(filename, fieldname, real(data), domain, &
no_domain, position, tile_count, data_default=default_data)
end subroutine write_data_i2d_new
!.....................................................................
subroutine write_data_i1d_new(filename, fieldname, data, domain, &
no_domain, tile_count, data_default)
type(domain2d), intent(in), optional :: domain
character(len=*), intent(in) :: filename, fieldname
integer, dimension(:), intent(in) :: data
logical, intent(in), optional :: no_domain
integer, intent(in), optional :: tile_count, data_default
real :: default_data
default_data = TRANSFER(MPP_FILL_INT,default_data)
if(present(data_default)) default_data = real(data_default)
call write_data_1d_new(filename, fieldname, real(data), domain, &
no_domain, tile_count, data_default=default_data)
end subroutine write_data_i1d_new
!.....................................................................
subroutine write_data_iscalar_new(filename, fieldname, data, domain, &
no_domain, tile_count, data_default)
type(domain2d), intent(in), optional :: domain
character(len=*), intent(in) :: filename, fieldname
integer, intent(in) :: data
logical, intent(in), optional :: no_domain
integer, intent(in), optional :: tile_count, data_default
real :: default_data
default_data = TRANSFER(MPP_FILL_INT,default_data)