-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathnetcdf_io.F90
2395 lines (2067 loc) · 98.6 KB
/
netcdf_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/>.
!***********************************************************************
!> @defgroup netcdf_io_mod netcdf_io_mod
!> @ingroup fms2_io
!> @brief Creates a basic netcdf type and routines to extend for other uses
!!
!> Handles type definitions and interfaces for netcdf I/O.
!> @addtogroup netcdf_io_mod
!> @{
module netcdf_io_mod
#ifndef MAX_NUM_RESTART_VARS_
#define MAX_NUM_RESTART_VARS_ 250
#endif
use netcdf
use mpp_mod
use fms_io_utils_mod
use platform_mod
implicit none
private
!Module constants.
integer, parameter, public :: default_deflate_level = 0 !< The default (no compression) deflate level to use
integer, parameter :: variable_missing = -1
integer, parameter :: dimension_missing = -1
integer, parameter, public :: no_unlimited_dimension = -1 !> No unlimited dimension in file.
character(len=1), parameter :: missing_path = ""
integer, parameter :: missing_ncid = -1
integer, parameter :: missing_rank = -1
integer, parameter, public :: define_mode = 0
integer, parameter, public :: data_mode = 1
integer, parameter, public :: max_num_restart_vars = MAX_NUM_RESTART_VARS_
integer, parameter, public :: unlimited = nf90_unlimited !> Wrapper to specify unlimited dimension.
integer, parameter :: dimension_not_found = 0
integer, parameter, public :: max_num_compressed_dims = 10 !> Maximum number of compressed
!! dimensions allowed.
integer, private :: fms2_ncchksz = -1 !< Chunksize (bytes) used in nc_open and nc_create
integer, private :: fms2_nc_format_param = -1 !< Netcdf format type param used in nc_create
character (len = 10), private :: fms2_nc_format !< Netcdf format type used in netcdf_file_open
integer, private :: fms2_header_buffer_val = -1 !< value used in NF__ENDDEF
integer, private :: fms2_deflate_level = default_deflate_level !< Netcdf deflate level to use in
!! nf90_def_var (integer between 1 to 9)
logical, private :: fms2_shuffle = .false. !< Flag indicating whether to use the netcdf shuffle filter
logical, private :: fms2_is_netcdf4 = .false. !< Flag indicating whether the default netcdf file format is netcdf4
!> @}
!> @brief information needed fr regional restart variables
!> @ingroup netcdf_io_mod
type, private :: bc_information
integer, dimension(:), allocatable :: indices !< Indices for the halo region for the variable
!! (starting x, ending x, starting y, ending y)
integer, dimension(:), allocatable :: global_size !< Size of the variable for each dimension
integer, dimension(:), allocatable :: pelist !< List of pelist that have the data for the variable
logical :: is_root_pe !< Flag indicating if this is the root_pe from the pelist
integer :: x_halo !< Number of halos in x
integer :: y_halo !< Number of halos in y
integer :: jshift !< Shift in the x axis (from center)
integer :: ishift !< Shift in the y axis (from center)
real(kind=r4_kind), dimension(:,:), allocatable :: globaldata2d_r4 !< 2d data pointer.
real(kind=r4_kind), dimension(:,:,:), allocatable :: globaldata3d_r4 !< 3d data pointer.
real(kind=r8_kind), dimension(:,:), allocatable :: globaldata2d_r8 !< 2d data pointer.
real(kind=r8_kind), dimension(:,:,:), allocatable :: globaldata3d_r8 !< 3d data pointer.
character(len=32) :: chksum !< The variable's checksum
logical :: data_on_file_root !< Flag indicating if the file root is part of the pelist that
!!contains data
endtype bc_information
!> @brief Restart variable.
!> @ingroup netcdf_io_mod
type, private :: RestartVariable_t
character(len=256) :: varname !< Variable name.
class(*), pointer :: data0d => null() !< Scalar data pointer.
class(*), dimension(:), pointer :: data1d => null() !< 1d data pointer.
class(*), dimension(:,:), pointer :: data2d => null() !< 2d data pointer.
class(*), dimension(:,:,:), pointer :: data3d => null() !< 3d data pointer.
class(*), dimension(:,:,:,:), pointer :: data4d => null() !< 4d data pointer.
class(*), dimension(:,:,:,:,:), pointer :: data5d => null() !< 5d data pointer.
logical :: was_read !< Flag to support legacy "query_initialized" feature, which
!! keeps track if a file was read.
logical :: is_bc_variable !< Flag indicating if variable is a bc_variable
type(bc_information) :: bc_info !< information about the boundary condition variable
endtype RestartVariable_t
!> @brief Compressed dimension.
!> @ingroup netcdf_io_mod
type, private :: CompressedDimension_t
character(len=256) :: dimname !< Dimension name.
integer, dimension(:), allocatable :: npes_corner !< Array of starting
!! indices for each rank.
integer, dimension(:), allocatable :: npes_nelems !< Number of elements
!! associated with each
!! rank.
integer :: nelems !< Total size of the dimension.
endtype CompressedDimension_t
!> @brief information about the current dimensions for regional restart variables
!> @ingroup netcdf_io_mod
type, private :: dimension_information
integer, dimension(5) :: xlen !> The size of each unique x dimension
integer, dimension(5) :: ylen !> The size of each unique y dimension
integer, dimension(5) :: zlen !> The size of each unique z dimension
integer, dimension(3) :: cur_dim_len !> Number of unique:
!! cur_dim_len(1) : x dimensions
!! cur_dim_len(2) : y dimensions
!! cur_dim_len(3) : z dimensions
endtype dimension_information
!> @brief Netcdf file type.
!> @ingroup netcdf_io_mod
type, public :: FmsNetcdfFile_t
character(len=FMS_PATH_LEN) :: path !< File path.
logical :: is_readonly !< Flag telling if the file is readonly.
integer :: ncid !< Netcdf file id.
character(len=256) :: nc_format !< Netcdf file format.
logical :: is_netcdf4 !< Flag indicating if the netcdf file type is netcdf4
integer, dimension(:), allocatable :: pelist !< List of ranks who will
!! communicate.
integer :: io_root !< I/O root rank of the pelist.
logical :: is_root !< Flag telling if the current rank is the
!! I/O root.
logical :: is_restart !< Flag telling if the this file is a restart
!! file (that has internal pointers to data).
logical :: mode_is_append !! true if file is open in "append" mode
logical, allocatable :: is_open !< Allocated and set to true if opened.
type(RestartVariable_t), dimension(:), allocatable :: restart_vars !< Array of registered
!! restart variables.
integer :: num_restart_vars !< Number of registered restart variables.
type(CompressedDimension_t), dimension(:), allocatable :: compressed_dims !< "Compressed" dimension.
integer :: num_compressed_dims !< Number of compressed dimensions.
logical :: is_diskless !< Flag telling whether this is a diskless file.
character (len=20) :: time_name
type(dimension_information) :: bc_dimensions !<information about the current dimensions for regional
!! restart variables
logical :: use_collective = .false. !< Flag indicating if we should open the file for collective input
!! this should be set to .true. in the user application if they want
!! collective reads (put before open_file())
integer :: tile_comm=MPP_COMM_NULL !< MPI communicator used for collective reads.
!! To be replaced with a real communicator at user request
endtype FmsNetcdfFile_t
!> @brief Range type for a netcdf variable.
!> @ingroup netcdf_io_mod
type, public :: Valid_t
logical :: has_range !< Flag that's true if both min/max exist for a variable.
logical :: has_min !< Flag that's true if min exists for a variable.
logical :: has_max !< Flag that's true if max exists for a variable.
logical :: has_fill !< Flag that's true a user defined fill value.
logical :: has_missing !< Flag that's true a user defined missing value.
real(kind=r8_kind) :: fill_val !< Unpacked fill value for a variable.
real(kind=r8_kind) :: min_val !< Unpacked minimum value allowed for a variable.
real(kind=r8_kind) :: max_val !< Unpacked maximum value allowed for a variable.
real(kind=r8_kind) :: missing_val !< Unpacked missing value for a variable.
endtype Valid_t
public :: netcdf_io_init
public :: netcdf_file_open
public :: netcdf_file_close
public :: netcdf_add_dimension
public :: netcdf_add_variable
public :: netcdf_add_restart_variable
public :: global_att_exists
public :: variable_att_exists
public :: register_global_attribute
public :: register_variable_attribute
public :: get_global_attribute
public :: get_variable_attribute
public :: get_num_dimensions
public :: get_dimension_names
public :: dimension_exists
public :: is_dimension_unlimited
public :: get_dimension_size
public :: get_num_variables
public :: get_variable_names
public :: variable_exists
public :: get_variable_num_dimensions
public :: get_variable_dimension_names
public :: get_variable_size
public :: get_variable_unlimited_dimension_index
public :: netcdf_read_data
public :: netcdf_write_data
public :: compressed_write
public :: netcdf_save_restart
public :: netcdf_restore_state
public :: get_valid
public :: is_valid
public :: get_unlimited_dimension_name
public :: netcdf_file_open_wrap
public :: netcdf_file_close_wrap
public :: netcdf_add_variable_wrap
public :: netcdf_save_restart_wrap
public :: compressed_write_0d_wrap
public :: compressed_write_1d_wrap
public :: compressed_write_2d_wrap
public :: compressed_write_3d_wrap
public :: compressed_write_4d_wrap
public :: compressed_write_5d_wrap
public :: compressed_read_0d
public :: compressed_read_1d
public :: compressed_read_2d
public :: compressed_read_3d
public :: compressed_read_4d
public :: compressed_read_5d
public :: register_compressed_dimension
public :: register_unlimited_compressed_axis
public :: netcdf_add_restart_variable_0d_wrap
public :: netcdf_add_restart_variable_1d_wrap
public :: netcdf_add_restart_variable_2d_wrap
public :: netcdf_add_restart_variable_3d_wrap
public :: netcdf_add_restart_variable_4d_wrap
public :: netcdf_add_restart_variable_5d_wrap
public :: register_restart_region_2d
public :: register_restart_region_3d
public :: compressed_start_and_count
public :: get_fill_value
public :: get_variable_sense
public :: get_variable_missing
public :: get_variable_units
public :: get_time_calendar
public :: is_registered_to_restart
public :: set_netcdf_mode
public :: check_netcdf_code
public :: check_if_open
public :: set_fileobj_time_name
public :: write_restart_bc
public :: read_restart_bc
public :: flush_file
!> @ingroup netcdf_io_mod
interface netcdf_add_restart_variable
module procedure netcdf_add_restart_variable_0d
module procedure netcdf_add_restart_variable_1d
module procedure netcdf_add_restart_variable_2d
module procedure netcdf_add_restart_variable_3d
module procedure netcdf_add_restart_variable_4d
module procedure netcdf_add_restart_variable_5d
end interface netcdf_add_restart_variable
!> @ingroup netcdf_io_mod
interface netcdf_read_data
module procedure netcdf_read_data_0d
module procedure netcdf_read_data_1d
module procedure netcdf_read_data_2d
module procedure netcdf_read_data_3d
module procedure netcdf_read_data_4d
module procedure netcdf_read_data_5d
end interface netcdf_read_data
!> @ingroup netcdf_io_mod
interface netcdf_write_data
module procedure netcdf_write_data_0d
module procedure netcdf_write_data_1d
module procedure netcdf_write_data_2d
module procedure netcdf_write_data_3d
module procedure netcdf_write_data_4d
module procedure netcdf_write_data_5d
end interface netcdf_write_data
!> @ingroup netcdf_io_mod
interface compressed_write
module procedure compressed_write_0d
module procedure compressed_write_1d
module procedure compressed_write_2d
module procedure compressed_write_3d
module procedure compressed_write_4d
module procedure compressed_write_5d
end interface compressed_write
!> @ingroup netcdf_io_mod
interface register_global_attribute
module procedure register_global_attribute_0d
module procedure register_global_attribute_1d
end interface register_global_attribute
!> @ingroup netcdf_io_mod
interface register_variable_attribute
module procedure register_variable_attribute_0d
module procedure register_variable_attribute_1d
end interface register_variable_attribute
!> @ingroup netcdf_io_mod
interface get_global_attribute
module procedure get_global_attribute_0d
module procedure get_global_attribute_1d
end interface get_global_attribute
!> @ingroup netcdf_io_mod
interface get_variable_attribute
module procedure get_variable_attribute_0d
module procedure get_variable_attribute_1d
end interface get_variable_attribute
!> @ingroup netcdf_io_mod
interface scatter_data_bc
module procedure scatter_data_bc_2d
module procedure scatter_data_bc_3d
end interface scatter_data_bc
!> @ingroup netcdf_io_mod
interface gather_data_bc
module procedure gather_data_bc_2d
module procedure gather_data_bc_3d
end interface gather_data_bc
!> The interface is needed to accomodate pgi because it can't handle class * and there was no other way around it
!> @ingroup netcdf_io_mod
interface is_valid
module procedure is_valid_r8
module procedure is_valid_r4
end interface is_valid
!> @addtogroup netcdf_io_mod
!> @{
contains
!> @brief Accepts the namelist fms2_io_nml variables relevant to netcdf_io_mod
subroutine netcdf_io_init (chksz, header_buffer_val, netcdf_default_format, deflate_level, shuffle)
integer, intent(in) :: chksz !< Chunksize (bytes) used in nc_open and nc_create
character (len = 10), intent(in) :: netcdf_default_format !< Netcdf format type param used in nc_create
integer, intent(in) :: header_buffer_val !< Value used in NF__ENDDEF
integer, intent(in) :: deflate_level !< Netcdf deflate level to use in nf90_def_var
!! (integer between 1 to 9)
logical, intent(in) :: shuffle !< Flag indicating whether to use the netcdf shuffle filter
fms2_ncchksz = chksz
fms2_deflate_level = deflate_level
fms2_shuffle = shuffle
fms2_is_netcdf4 = .false.
fms2_header_buffer_val = header_buffer_val
if (string_compare(netcdf_default_format, "64bit", .true.)) then
fms2_nc_format_param = nf90_64bit_offset
call string_copy(fms2_nc_format, "64bit")
elseif (string_compare(netcdf_default_format, "classic", .true.)) then
fms2_nc_format_param = nf90_classic_model
call string_copy(fms2_nc_format, "classic")
elseif (string_compare(netcdf_default_format, "netcdf4", .true.)) then
fms2_nc_format_param = nf90_netcdf4
fms2_is_netcdf4 = .true.
call string_copy(fms2_nc_format, "netcdf4")
else
call error("unrecognized netcdf file format "//trim(netcdf_default_format)// &
'. The acceptable values are "64bit", "classic", "netcdf4". Check fms2_io_nml: netcdf_default_format')
endif
end subroutine netcdf_io_init
!> @brief Check for errors returned by netcdf.
!! @internal
subroutine check_netcdf_code(err, msg)
integer, intent(in) :: err !< Code returned by netcdf.
character(len=*), intent(in) :: msg !< Error message to be appended to the FATAL
character(len=80) :: buf
if (err .ne. nf90_noerr) then
buf = nf90_strerror(err)
call error(trim(buf)//": "//trim(msg))
endif
end subroutine check_netcdf_code
!> @brief Switch to the correct netcdf mode.
!! @internal
subroutine set_netcdf_mode(ncid, mode)
integer, intent(in) :: ncid !< Netcdf file id.
integer, intent(in) :: mode !< Netcdf file mode.
integer :: err
if (mode .eq. define_mode) then
err = nf90_redef(ncid)
if (err .eq. nf90_eindefine .or. err .eq. nf90_eperm) then
return
endif
elseif (mode .eq. data_mode) then
if (fms2_header_buffer_val == -1) call error("set_netcdf_mode: fms2_header_buffer_val not set, call fms2_io_init")
err = nf90_enddef(ncid, h_minfree=fms2_header_buffer_val)
if (err .eq. nf90_enotindefine .or. err .eq. nf90_eperm) then
return
endif
else
call error("mode must be either define_mode or data_mode.")
endif
call check_netcdf_code(err, "set_netcdf_mode")
end subroutine set_netcdf_mode
!> @brief Get the id of a dimension from its name.
!! @return Dimension id, or dimension_missing if it doesn't exist.
!! @internal
function get_dimension_id(ncid, dimension_name, msg, allow_failure) &
result(dimid)
integer, intent(in) :: ncid !< Netcdf file id.
character(len=*), intent(in) :: dimension_name !< Dimension name.
character(len=*), intent(in) :: msg !< Error message
logical, intent(in), optional :: allow_failure !< Flag that prevents
!! crash if dimension
!! does not exist.
integer :: dimid
integer :: err
err = nf90_inq_dimid(ncid, trim(dimension_name), dimid)
if (present(allow_failure)) then
if (allow_failure .and. err .eq. nf90_ebaddim) then
dimid = dimension_missing
return
endif
endif
call check_netcdf_code(err, msg)
end function get_dimension_id
!> @brief Get the id of a variable from its name.
!! @return Variable id, or variable_missing if it doesn't exist.
!! @internal
function get_variable_id(ncid, variable_name, msg, allow_failure) &
result(varid)
integer, intent(in) :: ncid !< Netcdf file object.
character(len=*), intent(in) :: variable_name !< Variable name.
character(len=*), intent(in) :: msg !< Error message
logical, intent(in), optional :: allow_failure !< Flag that prevents
!! crash if variable does
!! not exist.
integer :: varid
integer :: err
err = nf90_inq_varid(ncid, trim(variable_name), varid)
if (present(allow_failure)) then
if (allow_failure .and. err .eq. nf90_enotvar) then
varid = variable_missing
return
endif
endif
call check_netcdf_code(err, msg)
end function get_variable_id
!> @brief Determine if an attribute exists.
!! @return Flag telling if the attribute exists.
!! @internal
function attribute_exists(ncid, varid, attribute_name, msg) &
result(att_exists)
integer, intent(in) :: ncid !< Netcdf file id.
integer, intent(in) :: varid !< Variable id.
character(len=*), intent(in) :: attribute_name !< Attribute name.
character(len=*), intent(in), optional :: msg !< Error message
logical :: att_exists
integer :: err
err = nf90_inquire_attribute(ncid, varid, trim(attribute_name))
if (err .eq. nf90_enotatt) then
att_exists = .false.
else
call check_netcdf_code(err, msg)
att_exists = .true.
endif
end function attribute_exists
!> @brief Get the type of a netcdf attribute.
!! @return The netcdf type of the attribute.
!! @internal
function get_attribute_type(ncid, varid, attname, msg) &
result(xtype)
integer, intent(in) :: ncid !< Netcdf file id.
integer, intent(in) :: varid !< Variable id.
character(len=*), intent(in) :: attname !< Attribute name.
character(len=*), intent(in), optional :: msg !< Error message
integer :: xtype
integer :: err
err = nf90_inquire_attribute(ncid, varid, attname, xtype=xtype)
call check_netcdf_code(err, msg)
end function get_attribute_type
!> @brief Get the type of a netcdf variable.
!! @return The netcdf type of the variable.
!! @internal
function get_variable_type(ncid, varid, msg) &
result(xtype)
integer, intent(in) :: ncid !< Netcdf file id.
integer, intent(in) :: varid !< Variable id.
character(len=*), intent(in), optional :: msg !< Error message to append to netcdf error code
integer :: xtype
integer :: err
err = nf90_inquire_variable(ncid, varid, xtype=xtype)
call check_netcdf_code(err, msg)
end function get_variable_type
!> @brief Open a netcdf file.
!! @return .true. if open succeeds, or else .false.
function netcdf_file_open(fileobj, path, mode, nc_format, pelist, is_restart, dont_add_res_to_filename) &
result(success)
class(FmsNetcdfFile_t), intent(inout) :: fileobj !< File object.
character(len=*), intent(in) :: path !< File path.
character(len=*), intent(in) :: mode !< File mode. Allowed values are:
!! "read", "append", "write", or
!! "overwrite".
character(len=*), intent(in), optional :: nc_format !< Netcdf format that
!! new files are written
!! as. Allowed values
!! are: "64bit", "classic",
!! or "netcdf4". Defaults to
!! "64bit". This overwrites
!! the value set in the fms2io
!! namelist
integer, dimension(:), intent(in), optional :: pelist !< List of ranks associated
!! with this file. If not
!! provided, only the current
!! rank will be able to
!! act on the file.
logical, intent(in), optional :: is_restart !< Flag telling if this file
!! is a restart file. Defaults
!! to false.
logical, intent(in), optional :: dont_add_res_to_filename !< Flag indicating not to add
!! ".res" to the filename
logical :: success
integer :: nc_format_param
integer :: err
integer :: netcdf4 !< Query the file for the _IsNetcdf4 global attribute in the event
!! that the open for collective reads fails
character(len=FMS_PATH_LEN) :: buf !< File path with .res in the filename if it is a restart
character(len=FMS_PATH_LEN) :: buf2 !< File path with the filename appendix if there is one
logical :: is_res
logical :: dont_add_res !< flag indicated to not add ".res" to the filename
if (allocated(fileobj%is_open)) then
if (fileobj%is_open) then
success = .true.
return
endif
endif
!< Only add ".res" to the file path if is_restart is set to true
!! and dont_add_res_to_filename is set to false.
is_res = .false.
if (present(is_restart)) then
is_res = is_restart
endif
dont_add_res = .false.
if (present(dont_add_res_to_filename)) then
dont_add_res = dont_add_res_to_filename
endif
if (is_res .and. .not. dont_add_res) then
call restart_filepath_mangle(buf, trim(path))
else
call string_copy(buf, trim(path))
endif
!< If it is a restart add the filename_appendix to the filename
if (is_res) then
call get_instance_filename(trim(buf), buf2)
else
call string_copy(buf2, trim(buf))
endif
!Check if the file exists.
success = .true.
if (string_compare(mode, "read", .true.) .or. string_compare(mode, "append", .true.)) then
success = file_exists(buf2)
if (.not. success) then
return
endif
endif
!Store properties in the derived type.
call string_copy(fileobj%path, trim(buf2))
if (present(pelist)) then
allocate(fileobj%pelist(size(pelist)))
fileobj%pelist(:) = pelist(:)
else
allocate(fileobj%pelist(1))
fileobj%pelist(1) = mpp_pe()
endif
fileobj%io_root = fileobj%pelist(1)
fileobj%is_root = mpp_pe() .eq. fileobj%io_root
fileobj%is_netcdf4 = .false.
if (fms2_ncchksz == -1) call error("netcdf_file_open:: fms2_ncchksz not set, call fms2_io_init")
if (fms2_nc_format_param == -1) call error("netcdf_file_open:: fms2_nc_format_param not set, call fms2_io_init")
if (present(nc_format)) then
if (string_compare(nc_format, "64bit", .true.)) then
nc_format_param = nf90_64bit_offset
elseif (string_compare(nc_format, "classic", .true.)) then
nc_format_param = nf90_classic_model
elseif (string_compare(nc_format, "netcdf4", .true.)) then
fileobj%is_netcdf4 = .true.
nc_format_param = nf90_netcdf4
else
call error("unrecognized netcdf file format: '"//trim(nc_format)//"' for file:"//trim(fileobj%path)//&
&"Check your open_file call, the acceptable values are 64bit, classic, netcdf4")
endif
call string_copy(fileobj%nc_format, nc_format)
else
call string_copy(fileobj%nc_format, trim(fms2_nc_format))
nc_format_param = fms2_nc_format_param
fileobj%is_netcdf4 = fms2_is_netcdf4
endif
!Open the file with netcdf if this rank is the I/O root.
if (fileobj%is_root .and. .not.(fileobj%use_collective)) then
if (string_compare(mode, "read", .true.)) then
err = nf90_open(trim(fileobj%path), nf90_nowrite, fileobj%ncid, chunksize=fms2_ncchksz)
elseif (string_compare(mode, "append", .true.)) then
err = nf90_open(trim(fileobj%path), nf90_write, fileobj%ncid, chunksize=fms2_ncchksz)
elseif (string_compare(mode, "write", .true.)) then
err = nf90_create(trim(fileobj%path), ior(nf90_noclobber, nc_format_param), fileobj%ncid, chunksize=fms2_ncchksz)
elseif (string_compare(mode,"overwrite",.true.)) then
err = nf90_create(trim(fileobj%path), ior(nf90_clobber, nc_format_param), fileobj%ncid, chunksize=fms2_ncchksz)
else
call error("unrecognized file mode: '"//trim(mode)//"' for file:"//trim(fileobj%path)//&
&"Check your open_file call, the acceptable values are read, append, write, overwrite")
endif
call check_netcdf_code(err, "netcdf_file_open:"//trim(fileobj%path))
elseif(fileobj%use_collective .and. (fileobj%tile_comm /= MPP_COMM_NULL)) then
if(string_compare(mode, "read", .true.)) then
! Open the file for collective reads if the user requested that treatment in their application.
! NetCDF does not have the ability to specify collective I/O at the file basis
! so we must activate each variable in netcdf_read_data_2d() and netcdf_read_data_3d()
err = nf90_open(trim(fileobj%path), ior(NF90_NOWRITE, NF90_MPIIO), fileobj%ncid, &
comm=fileobj%tile_comm, info=MPP_INFO_NULL)
if(err /= nf90_noerr) then
err = nf90_open(trim(fileobj%path), nf90_nowrite, fileobj%ncid)
err = nf90_get_att(fileobj%ncid, nf90_global, "_IsNetcdf4", netcdf4)
err = nf90_close(fileobj%ncid)
if(netcdf4 /= 1) then
call mpp_error(NOTE,"netcdf_file_open: Open for collective read failed because the file is not &
netCDF-4 format."// &
" Falling back to parallel independent for file "// trim(fileobj%path))
endif
err = nf90_open(trim(fileobj%path), nf90_nowrite, fileobj%ncid, chunksize=fms2_ncchksz)
endif
elseif (string_compare(mode, "write", .true.)) then
call mpp_error(FATAL,"netcdf_file_open: Attempt to create a file for collective write"// &
" This feature is not implemented"// trim(fileobj%path))
!err = nf90_create(trim(fileobj%path), ior(nf90_noclobber, nc_format_param), fileobj%ncid, &
! comm=fileobj%tile_comm, info=MPP_INFO_NULL)
elseif (string_compare(mode,"overwrite",.true.)) then
call mpp_error(FATAL,"netcdf_file_open: Attempt to create a file for collective overwrite"// &
" This feature is not implemented"// trim(fileobj%path))
!err = nf90_create(trim(fileobj%path), ior(nf90_clobber, nc_format_param), fileobj%ncid, &
! comm=fileobj%tile_comm, info=MPP_INFO_NULL)
else
call error("unrecognized file mode: '"//trim(mode)//"' for file:"//trim(fileobj%path)//&
&"Check your open_file call, the acceptable values are read, append, write, overwrite")
endif
call check_netcdf_code(err, "netcdf_file_open:"//trim(fileobj%path))
else
fileobj%ncid = missing_ncid
endif
fileobj%is_diskless = .false.
!Allocate memory.
fileobj%is_restart = is_res
if (fileobj%is_restart) then
allocate(fileobj%restart_vars(max_num_restart_vars))
fileobj%num_restart_vars = 0
endif
fileobj%is_readonly = string_compare(mode, "read", .true.)
fileobj%mode_is_append = string_compare(mode, "append", .true.)
allocate(fileobj%compressed_dims(max_num_compressed_dims))
fileobj%num_compressed_dims = 0
! Set the is_open flag to true for this file object.
if (.not.allocated(fileobj%is_open)) allocate(fileobj%is_open)
fileobj%is_open = .true.
fileobj%bc_dimensions%xlen = 0
fileobj%bc_dimensions%ylen = 0
fileobj%bc_dimensions%zlen = 0
fileobj%bc_dimensions%cur_dim_len = 0
end function netcdf_file_open
!> @brief Close a netcdf file.
subroutine netcdf_file_close(fileobj)
class(FmsNetcdfFile_t),intent(inout) :: fileobj !< File object.
integer :: err
integer :: i
if (fileobj%is_root) then
err = nf90_close(fileobj%ncid)
call check_netcdf_code(err, "netcdf_file_close:"//trim(fileobj%path))
endif
if (allocated(fileobj%is_open)) fileobj%is_open = .false.
fileobj%path = missing_path
fileobj%ncid = missing_ncid
if (allocated(fileobj%pelist)) then
deallocate(fileobj%pelist)
endif
fileobj%io_root = missing_rank
fileobj%is_root = .false.
if (allocated(fileobj%restart_vars)) then
deallocate(fileobj%restart_vars)
endif
fileobj%is_restart = .false.
fileobj%num_restart_vars = 0
do i = 1, fileobj%num_compressed_dims
if (allocated(fileobj%compressed_dims(i)%npes_corner)) then
deallocate(fileobj%compressed_dims(i)%npes_corner)
endif
if (allocated(fileobj%compressed_dims(i)%npes_nelems)) then
deallocate(fileobj%compressed_dims(i)%npes_nelems)
endif
enddo
if (allocated(fileobj%compressed_dims)) then
deallocate(fileobj%compressed_dims)
endif
end subroutine netcdf_file_close
!> @brief Get the index of a compressed dimension in a file object.
!! @return Index of the compressed dimension.
!! @internal
function get_compressed_dimension_index(fileobj, dim_name) &
result(dindex)
class(FmsNetcdfFile_t), intent(in) :: fileobj !< File object.
character(len=*), intent(in) :: dim_name !< Dimension name.
integer :: dindex
integer :: i
dindex = dimension_not_found
do i = 1, fileobj%num_compressed_dims
if (string_compare(fileobj%compressed_dims(i)%dimname, dim_name)) then
dindex = i
return
endif
enddo
end function get_compressed_dimension_index
!> @brief Add a compressed dimension to a file object.
!! @internal
subroutine append_compressed_dimension(fileobj, dim_name, npes_corner, &
npes_nelems)
class(FmsNetcdfFile_t), intent(inout) :: fileobj !< File object.
character(len=*), intent(in) :: dim_name !< Dimension name.
integer, dimension(:), intent(in) :: npes_corner !< Array of starting
!! indices for each rank.
integer, dimension(:), intent(in) :: npes_nelems !< Number of elements
!! associated with each
!! rank.
integer :: n
if (get_compressed_dimension_index(fileobj, dim_name) .ne. dimension_not_found) then
call error("dimension "//trim(dim_name)//" already registered" &
//" to file "//trim(fileobj%path)//".")
endif
fileobj%num_compressed_dims = fileobj%num_compressed_dims + 1
n = fileobj%num_compressed_dims
if (n .gt. max_num_compressed_dims) then
call error("number of compressed dimensions exceeds limit.")
endif
call string_copy(fileobj%compressed_dims(n)%dimname, dim_name)
if (size(npes_corner) .ne. size(fileobj%pelist) .or. &
size(npes_nelems) .ne. size(fileobj%pelist)) then
call error("incorrect size for input npes_corner or npes_nelems arrays.")
endif
allocate(fileobj%compressed_dims(n)%npes_corner(size(fileobj%pelist)))
fileobj%compressed_dims(n)%npes_corner(:) = npes_corner(:)
allocate(fileobj%compressed_dims(n)%npes_nelems(size(fileobj%pelist)))
fileobj%compressed_dims(n)%npes_nelems(:) = npes_nelems(:)
fileobj%compressed_dims(n)%nelems = sum(fileobj%compressed_dims(n)%npes_nelems)
end subroutine append_compressed_dimension
!> @brief Add a "compressed" unlimited dimension to a netcdf file.
!! @note Here compressed means that every rank has a different dimension_length
!! compressed. This was written specifically for the icebergs restarts.
subroutine register_unlimited_compressed_axis(fileobj, dimension_name, dimension_length)
class(FmsNetcdfFile_t), intent(inout) :: fileobj !< File object.
character(len=*), intent(in) :: dimension_name !< Dimension name.
integer, intent(in) :: dimension_length !< Dimension length for the current rank
integer, dimension(:), allocatable :: npes_start !< The starting index of the dimension for each of the PEs
integer, dimension(:), allocatable :: npes_count !< The size of the dimension for each of the PEs
integer :: i !< For do loops
integer :: err !< Netcdf error
integer :: dimid !< Netcdf id for the dimension
!Gather all local dimension lengths on the I/O root pe.
allocate(npes_start(size(fileobj%pelist)))
allocate(npes_count(size(fileobj%pelist)))
call mpp_gather((/dimension_length/),npes_count,pelist=fileobj%pelist)
npes_start(1) = 1
do i = 1, size(fileobj%pelist)-1
npes_start(i+1) = npes_start(i) + npes_count(i)
enddo
call append_compressed_dimension(fileobj, dimension_name, npes_start, &
npes_count)
if (fileobj%is_root .and. .not. fileobj%is_readonly) then
call set_netcdf_mode(fileobj%ncid, define_mode)
err = nf90_def_dim(fileobj%ncid, trim(dimension_name), unlimited, dimid)
call check_netcdf_code(err, "Netcdf_add_dimension: file:"//trim(fileobj%path)//" dimension name:"// &
& trim(dimension_name))
endif
end subroutine register_unlimited_compressed_axis
!> @brief Add a dimension to a file.
subroutine netcdf_add_dimension(fileobj, dimension_name, dimension_length, &
is_compressed)
class(FmsNetcdfFile_t), intent(inout) :: fileobj !< File object.
character(len=*), intent(in) :: dimension_name !< Dimension name.
integer, intent(in) :: dimension_length !< Dimension length.
logical, intent(in), optional :: is_compressed !< Changes the meaning of dim_len from
!! referring to the total size of the
!! dimension (when false) to the local
!! size for the current rank (when true).
integer :: dim_len
integer, dimension(:), allocatable :: npes_start
integer, dimension(:), allocatable :: npes_count
integer :: i
integer :: err
integer :: dimid
dim_len = dimension_length
if (present(is_compressed)) then
if (is_compressed) then
!Gather all local dimension lengths on the I/O root pe.
allocate(npes_start(size(fileobj%pelist)))
allocate(npes_count(size(fileobj%pelist)))
do i = 1, size(fileobj%pelist)
if (fileobj%pelist(i) .eq. mpp_pe()) then
npes_count(i) = dim_len
else
call mpp_recv(npes_count(i), fileobj%pelist(i), block=.false.)
call mpp_send(dim_len, fileobj%pelist(i))
endif
enddo
call mpp_sync_self(check=event_recv)
call mpp_sync_self(check=event_send)
npes_start(1) = 1
do i = 1, size(fileobj%pelist)-1
npes_start(i+1) = npes_start(i) + npes_count(i)
enddo
call append_compressed_dimension(fileobj, dimension_name, npes_start, &
npes_count)
dim_len = sum(npes_count)
endif
endif
if (fileobj%is_root .and. .not. fileobj%is_readonly) then
call set_netcdf_mode(fileobj%ncid, define_mode)
err = nf90_def_dim(fileobj%ncid, trim(dimension_name), dim_len, dimid)
call check_netcdf_code(err, "Netcdf_add_dimension: file:"//trim(fileobj%path)//" dimension name:"// &
& trim(dimension_name))
endif
end subroutine netcdf_add_dimension
!> @brief Add a compressed dimension.
subroutine register_compressed_dimension(fileobj, dimension_name, &
npes_corner, npes_nelems)
class(FmsNetcdfFile_t), intent(inout) :: fileobj !< File object.
character(len=*), intent(in) :: dimension_name !< Dimension name.
integer, dimension(:), intent(in) :: npes_corner !< Array of starting
!! indices for each rank.
integer, dimension(:), intent(in) :: npes_nelems !< Number of elements
!! associated with each
!! rank.
integer :: dsize
integer :: fdim_size
call append_compressed_dimension(fileobj, dimension_name, npes_corner, npes_nelems)
dsize = sum(npes_nelems)
if (fileobj%is_readonly) then
call get_dimension_size(fileobj, dimension_name, fdim_size, broadcast=.true.)
if (fdim_size .ne. dsize) then
call error("dimension "//trim(dimension_name)//" does not match" &
//" the size of the associated compressed axis.")
endif
else
call netcdf_add_dimension(fileobj, dimension_name, dsize)
endif
end subroutine register_compressed_dimension
!> @brief Add a variable to a file.
subroutine netcdf_add_variable(fileobj, variable_name, variable_type, dimensions, chunksizes)
class(FmsNetcdfFile_t), intent(in) :: fileobj !< File object.
character(len=*), intent(in) :: variable_name !< Variable name.
character(len=*), intent(in) :: variable_type !< Variable type. Allowed
!! values are: "char", "int", "int64",
!! "float", or "double".
character(len=*), dimension(:), intent(in), optional :: dimensions !< Dimension names.
integer, optional, intent(in) :: chunksizes(:) !< netcdf chunksize to use for this variable
!! This feature is only
!! available for netcdf4 files
integer :: err
integer, dimension(:), allocatable :: dimids
integer :: vtype
integer :: varid
integer :: i
character(len=200) :: append_error_msg !< Msg to be appended to FATAL error message
append_error_msg = "netcdf_add_variable: file:"//trim(fileobj%path)//" variable:"//trim(variable_name)
if (fileobj%is_root) then
call set_netcdf_mode(fileobj%ncid, define_mode)
if (string_compare(variable_type, "int", .true.)) then
vtype = nf90_int
elseif (string_compare(variable_type, "int64", .true.)) then
if ( .not. fileobj%is_netcdf4) call error(trim(fileobj%path)//&
&": 64 bit integers are only supported with 'netcdf4' file format"//&
&". Set netcdf_default_format='netcdf4' in the fms2_io namelist OR "//&
&"add nc_format='netcdf4' to your open_file call")
vtype = nf90_int64
elseif (string_compare(variable_type, "float", .true.)) then
vtype = nf90_float
elseif (string_compare(variable_type, "double", .true.)) then
vtype = nf90_double
elseif (string_compare(variable_type, "char", .true.)) then
vtype = nf90_char
if (.not. present(dimensions)) then
call error("String variables require a string length dimension:"//trim(append_error_msg))
endif
else
call error("Unsupported variable type:"//trim(append_error_msg))
endif
if (present(dimensions)) then
allocate(dimids(size(dimensions)))
do i = 1, size(dimids)
dimids(i) = get_dimension_id(fileobj%ncid, trim(dimensions(i)),msg=append_error_msg)
enddo
if (fileobj%is_netcdf4) then
err = nf90_def_var(fileobj%ncid, trim(variable_name), vtype, dimids, varid, &
&deflate_level=fms2_deflate_level, shuffle=fms2_shuffle, chunksizes=chunksizes)
else
if (fms2_deflate_level .ne. default_deflate_level .or. fms2_shuffle .or. present(chunksizes)) &
&call mpp_error(NOTE,"Not able to use deflate_level or chunksizes if not using netcdf4"// &
& " ignoring them")
err = nf90_def_var(fileobj%ncid, trim(variable_name), vtype, dimids, varid)
endif
deallocate(dimids)
else
err = nf90_def_var(fileobj%ncid, trim(variable_name), vtype, varid)