MOM_restart.F90
1! This file is part of MOM6, the Modular Ocean Model version 6.
2! See the LICENSE file for licensing information.
3! SPDX-License-Identifier: Apache-2.0
4
5!> The MOM6 facility for reading and writing restart files, and querying what has been read.
6module mom_restart
7
8use, intrinsic :: iso_fortran_env, only : int64
9use mom_array_transform, only : rotate_array, rotate_vector, rotate_array_pair
10use mom_checksums, only : chksum => field_checksum
11use mom_domains, only : pe_here, num_pes, agrid, bgrid_ne, cgrid_ne
12use mom_error_handler, only : mom_error, mom_mesg, fatal, warning, note, is_root_pe, mom_get_verbosity
13use mom_file_parser, only : get_param, log_param, log_version, param_file_type
14use mom_grid, only : ocean_grid_type
15use mom_io, only : create_mom_file, file_exists
16use mom_io, only : mom_infra_file, mom_field
17use mom_io, only : mom_read_data, read_data, mom_write_field, field_exists
18use mom_io, only : vardesc, var_desc, query_vardesc, modify_vardesc
19use mom_io, only : multiple, readonly_file, single_file
20use mom_io, only : center, corner, north_face, east_face
21use mom_io, only : axis_info, get_axis_info
22use mom_io, only : insert_ensemble_appendix
23use mom_string_functions, only : lowercase
24use mom_time_manager, only : time_type, time_type_to_real, real_to_time
25use mom_time_manager, only : days_in_month, get_date, set_date
26use mom_verticalgrid, only : verticalgrid_type
27
28implicit none ; private
29
30public restart_init, restart_end, restore_state, register_restart_field
31public copy_restart_var, copy_restart_vector
32public save_restart, query_initialized, set_initialized, only_read_from_restarts
33public restart_registry_lock, restart_init_end, vardesc
34public restart_files_exist, determine_is_new_run, is_new_run
35public register_restart_field_as_obsolete, register_restart_pair
36public lock_check
37
38! A note on unit descriptions in comments: MOM6 uses units that can be rescaled for dimensional
39! consistency testing. These are noted in comments with units like Z, H, L, and T, along with
40! their mks counterparts with notation like "a velocity [Z T-1 ~> m s-1]". If the units
41! vary with the Boussinesq approximation, the Boussinesq variant is given first.
42! The functions in this module work with variables with arbitrary units, in which case the
43! arbitrary rescaled units are indicated with [A ~> a], while the unscaled units are just [a].
44
45!> A type for making arrays of pointers to 4-d arrays
46type p4d
47 real, dimension(:,:,:,:), pointer :: p => null() !< A pointer to a 4d array in arbitrary rescaled units [A ~> a]
48end type p4d
49
50!> A type for making arrays of pointers to 3-d arrays
51type p3d
52 real, dimension(:,:,:), pointer :: p => null() !< A pointer to a 3d array in arbitrary rescaled units [A ~> a]
53end type p3d
54
55!> A type for making arrays of pointers to 2-d arrays
56type p2d
57 real, dimension(:,:), pointer :: p => null() !< A pointer to a 2d array in arbitrary rescaled units [A ~> a]
58end type p2d
59
60!> A type for making arrays of pointers to 1-d arrays
61type p1d
62 real, dimension(:), pointer :: p => null() !< A pointer to a 1d array in arbitrary rescaled units [A ~> a]
63end type p1d
64
65!> A type for making arrays of pointers to scalars
66type p0d
67 real, pointer :: p => null() !< A pointer to a scalar in arbitrary rescaled units [A ~> a]
68end type p0d
69
70!> A structure with information about a single restart field
71type field_restart
72 type(vardesc) :: vars !< Description of a field that is to be read from or written
73 !! to the restart file.
74 logical :: mand_var !< If .true. the run will abort if this field is not successfully
75 !! read from the restart file.
76 logical :: initialized !< .true. if this field has been read from the restart file.
77 character(len=32) :: var_name !< A name by which a variable may be queried.
78 real :: conv = 1.0 !< A factor by which a restart field should be multiplied before it
79 !! is written to a restart file, usually to convert it to MKS or
80 !! other standard units [a A-1 ~> 1]. When read, the restart field
81 !! is multiplied by the reciprocal of this factor.
82end type field_restart
83
84!> A structure to store information about restart fields that are no longer used
85type obsolete_restart
86 character(len=32) :: field_name !< Name of restart field that is no longer in use
87 character(len=32) :: replacement_name !< Name of replacement restart field, if applicable
88end type obsolete_restart
89
90!> A restart registry and the control structure for restarts
91type, public :: mom_restart_cs ; private
92 logical :: initialized = .false. !< True if this control structure has been initialized.
93 logical :: restart !< restart is set to .true. if the run has been started from a full restart
94 !! file. Otherwise some fields must be initialized approximately.
95 integer :: novars = 0 !< The number of restart fields that have been registered.
96 integer :: num_obsolete_vars = 0 !< The number of obsolete restart fields that have been registered.
97 logical :: parallel_restartfiles !< If true, the IO layout is used to group processors that write
98 !! to the same restart file or each processor writes its own
99 !! (numbered) restart file. If false, a single restart file is
100 !! generated after internally combining output from all PEs.
101 logical :: new_run !< If true, the input filenames and restart file existence will
102 !! result in a new run that is not initialized from restart files.
103 logical :: new_run_set = .false. !< If true, new_run has been determined for this restart_CS.
104 logical :: checksum_required !< If true, require the restart checksums to match and error out otherwise.
105 !! Users may want to avoid this comparison if for example the restarts are
106 !! made from a run with a different mask_table than the current run,
107 !! in which case the checksums will not match and cause crash.
108 logical :: symmetric_checksums !< If true, do the restart checksums on all the edge points for
109 !! a non-reentrant grid. Setting this to true requires that
110 !! SYMMETRIC_MEMORY_ is defined at compile time.
111 logical :: unsigned_zeros !< If true, convert any negative zeros that would be written to
112 !! the restart file into ordinary unsigned zeros. This does not
113 !! change answers, but it can be helpful in comparing restart
114 !! files after grid rotation, for example.
115 logical :: reentrant_x !< If true, the domain is reentrant in the x-direction. This is only
116 !! used here to determine the extent of the restart checksums.
117 logical :: reentrant_y !< If true, the domain is reentrant in the y-direction. This is only
118 !! used here to determine the extent of the restart checksums.
119 character(len=240) :: restartfile !< The name or name root for MOM restart files.
120 integer :: turns !< Number of quarter turns from input to model domain
121 logical :: locked = .false. !< If true this registry has been locked and no further restart
122 !! fields can be added without explicitly unlocking the registry.
123
124 !> An array of descriptions of the registered fields
125 type(field_restart), pointer :: restart_field(:) => null()
126
127 !> An array of obsolete restart fields
128 type(obsolete_restart), pointer :: restart_obsolete(:) => null()
129
130 !>@{ Pointers to the fields that have been registered for restarts
131 type(p0d), pointer :: var_ptr0d(:) => null()
132 type(p1d), pointer :: var_ptr1d(:) => null()
133 type(p2d), pointer :: var_ptr2d(:) => null()
134 type(p3d), pointer :: var_ptr3d(:) => null()
135 type(p4d), pointer :: var_ptr4d(:) => null()
136 !>@}
137 integer :: max_fields !< The maximum number of restart fields
138 character(len=32) :: ensemble_appendix_prefix !< The prefix after which the ensemble id appendix is added
139 !! in output file names.
140end type mom_restart_cs
141
142!> Register fields for restarts
143interface register_restart_field
144 module procedure register_restart_field_ptr4d, register_restart_field_4d
145 module procedure register_restart_field_ptr3d, register_restart_field_3d
146 module procedure register_restart_field_ptr2d, register_restart_field_2d
147 module procedure register_restart_field_ptr1d, register_restart_field_1d
148 module procedure register_restart_field_ptr0d, register_restart_field_0d
149end interface
150
151!> Register a pair of restart fields whose rotations map onto each other
152interface register_restart_pair
153 module procedure register_restart_pair_ptr2d
154 module procedure register_restart_pair_ptr3d
155 module procedure register_restart_pair_ptr4d
156end interface register_restart_pair
157
158!> Indicate whether a field has been read from a restart file
159interface query_initialized
160 module procedure query_initialized_name
161 module procedure query_initialized_0d, query_initialized_0d_name
162 module procedure query_initialized_1d, query_initialized_1d_name
163 module procedure query_initialized_2d, query_initialized_2d_name
164 module procedure query_initialized_3d, query_initialized_3d_name
165 module procedure query_initialized_4d, query_initialized_4d_name
166end interface
167
168!> Specify that a field has been initialized, even if it was not read from a restart file
169interface set_initialized
170 module procedure set_initialized_name, set_initialized_0d_name
171 module procedure set_initialized_1d_name, set_initialized_2d_name
172 module procedure set_initialized_3d_name, set_initialized_4d_name
173end interface
174
175!> Copy the restart variable with the specified name into an array, perhaps after rotation
176interface copy_restart_var
177 module procedure copy_restart_var_3d
178end interface copy_restart_var
179
180!> Copy the restart vector component variables with the specified names into a pair of arrays,
181!! perhaps after rotation
182interface copy_restart_vector
183 module procedure copy_restart_vector_3d
184end interface copy_restart_vector
185
186!> Read optional variables from restart files.
187interface only_read_from_restarts
188 module procedure only_read_restart_field_4d
189 module procedure only_read_restart_field_3d
190 module procedure only_read_restart_field_2d
191! module procedure only_read_restart_field_1d
192! module procedure only_read_restart_field_0d
193 module procedure only_read_restart_pair_3d
194end interface
195
196contains
197
198!> Register a restart field as obsolete
199subroutine register_restart_field_as_obsolete(field_name, replacement_name, CS)
200 character(*), intent(in) :: field_name !< Name of restart field that is no longer in use
201 character(*), intent(in) :: replacement_name !< Name of replacement restart field, if applicable
202 type(mom_restart_cs), intent(inout) :: cs !< MOM restart control struct
203
204 cs%num_obsolete_vars = cs%num_obsolete_vars+1
205 cs%restart_obsolete(cs%num_obsolete_vars)%field_name = field_name
206 cs%restart_obsolete(cs%num_obsolete_vars)%replacement_name = replacement_name
207end subroutine register_restart_field_as_obsolete
208
209!> Register a 3-d field for restarts, providing the metadata in a structure
210subroutine register_restart_field_ptr3d(f_ptr, var_desc, mandatory, CS, conversion)
211 real, dimension(:,:,:), &
212 target, intent(in) :: f_ptr !< A pointer to the field to be read or written
213 !! in arbitrary rescaled units [A ~> a]
214 type(vardesc), intent(in) :: var_desc !< A structure with metadata about this variable
215 logical, intent(in) :: mandatory !< If true, the run will abort if this field is not
216 !! successfully read from the restart file.
217 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
218 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
219 !! before it is written [a A-1 ~> 1], 1 by default.
220
221 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
222 "register_restart_field: Module must be initialized before it is used.")
223
224 call lock_check(cs, var_desc)
225
226 cs%novars = cs%novars+1
227 if (cs%novars > cs%max_fields) return ! This is an error that will be reported
228 ! once the total number of fields is known.
229
230 cs%restart_field(cs%novars)%vars = var_desc
231 cs%restart_field(cs%novars)%mand_var = mandatory
232 cs%restart_field(cs%novars)%initialized = .false.
233 cs%restart_field(cs%novars)%conv = 1.0
234 if (present(conversion)) cs%restart_field(cs%novars)%conv = conversion
235 call query_vardesc(cs%restart_field(cs%novars)%vars, &
236 name=cs%restart_field(cs%novars)%var_name, &
237 caller="register_restart_field_ptr3d")
238
239 cs%var_ptr3d(cs%novars)%p => f_ptr
240 cs%var_ptr4d(cs%novars)%p => null()
241 cs%var_ptr2d(cs%novars)%p => null()
242 cs%var_ptr1d(cs%novars)%p => null()
243 cs%var_ptr0d(cs%novars)%p => null()
244
245end subroutine register_restart_field_ptr3d
246
247!> Register a 4-d field for restarts, providing the metadata in a structure
248subroutine register_restart_field_ptr4d(f_ptr, var_desc, mandatory, CS, conversion)
249 real, dimension(:,:,:,:), &
250 target, intent(in) :: f_ptr !< A pointer to the field to be read or written
251 !! in arbitrary rescaled units [A ~> a]
252 type(vardesc), intent(in) :: var_desc !< A structure with metadata about this variable
253 logical, intent(in) :: mandatory !< If true, the run will abort if this field is not
254 !! successfully read from the restart file.
255 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
256 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
257 !! before it is written [a A-1 ~> 1], 1 by default.
258
259 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
260 "register_restart_field: Module must be initialized before it is used.")
261
262 call lock_check(cs, var_desc)
263
264 cs%novars = cs%novars+1
265 if (cs%novars > cs%max_fields) return ! This is an error that will be reported
266 ! once the total number of fields is known.
267
268 cs%restart_field(cs%novars)%vars = var_desc
269 cs%restart_field(cs%novars)%mand_var = mandatory
270 cs%restart_field(cs%novars)%initialized = .false.
271 cs%restart_field(cs%novars)%conv = 1.0
272 if (present(conversion)) cs%restart_field(cs%novars)%conv = conversion
273 call query_vardesc(cs%restart_field(cs%novars)%vars, &
274 name=cs%restart_field(cs%novars)%var_name, &
275 caller="register_restart_field_ptr4d")
276
277 cs%var_ptr4d(cs%novars)%p => f_ptr
278 cs%var_ptr3d(cs%novars)%p => null()
279 cs%var_ptr2d(cs%novars)%p => null()
280 cs%var_ptr1d(cs%novars)%p => null()
281 cs%var_ptr0d(cs%novars)%p => null()
282
283end subroutine register_restart_field_ptr4d
284
285!> Register a 2-d field for restarts, providing the metadata in a structure
286subroutine register_restart_field_ptr2d(f_ptr, var_desc, mandatory, CS, conversion)
287 real, dimension(:,:), &
288 target, intent(in) :: f_ptr !< A pointer to the field to be read or written
289 !! in arbitrary rescaled units [A ~> a]
290 type(vardesc), intent(in) :: var_desc !< A structure with metadata about this variable
291 logical, intent(in) :: mandatory !< If true, the run will abort if this field is not
292 !! successfully read from the restart file.
293 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
294 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
295 !! before it is written [a A-1 ~> 1], 1 by default.
296
297 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
298 "register_restart_field: Module must be initialized before it is used.")
299
300 call lock_check(cs, var_desc)
301
302 cs%novars = cs%novars+1
303 if (cs%novars > cs%max_fields) return ! This is an error that will be reported
304 ! once the total number of fields is known.
305
306 cs%restart_field(cs%novars)%vars = var_desc
307 cs%restart_field(cs%novars)%mand_var = mandatory
308 cs%restart_field(cs%novars)%initialized = .false.
309 cs%restart_field(cs%novars)%conv = 1.0
310 if (present(conversion)) cs%restart_field(cs%novars)%conv = conversion
311 call query_vardesc(cs%restart_field(cs%novars)%vars, &
312 name=cs%restart_field(cs%novars)%var_name, &
313 caller="register_restart_field_ptr2d")
314
315 cs%var_ptr2d(cs%novars)%p => f_ptr
316 cs%var_ptr4d(cs%novars)%p => null()
317 cs%var_ptr3d(cs%novars)%p => null()
318 cs%var_ptr1d(cs%novars)%p => null()
319 cs%var_ptr0d(cs%novars)%p => null()
320
321end subroutine register_restart_field_ptr2d
322
323!> Register a 1-d field for restarts, providing the metadata in a structure
324subroutine register_restart_field_ptr1d(f_ptr, var_desc, mandatory, CS, conversion)
325 real, dimension(:), target, intent(in) :: f_ptr !< A pointer to the field to be read or written
326 !! in arbitrary rescaled units [A ~> a]
327 type(vardesc), intent(in) :: var_desc !< A structure with metadata about this variable
328 logical, intent(in) :: mandatory !< If true, the run will abort if this field is not
329 !! successfully read from the restart file.
330 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
331 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
332 !! before it is written [a A-1 ~> 1], 1 by default.
333
334 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
335 "register_restart_field: Module must be initialized before it is used.")
336
337 call lock_check(cs, var_desc)
338
339 cs%novars = cs%novars+1
340 if (cs%novars > cs%max_fields) return ! This is an error that will be reported
341 ! once the total number of fields is known.
342
343 cs%restart_field(cs%novars)%vars = var_desc
344 cs%restart_field(cs%novars)%mand_var = mandatory
345 cs%restart_field(cs%novars)%initialized = .false.
346 cs%restart_field(cs%novars)%conv = 1.0
347 if (present(conversion)) cs%restart_field(cs%novars)%conv = conversion
348 call query_vardesc(cs%restart_field(cs%novars)%vars, &
349 name=cs%restart_field(cs%novars)%var_name, &
350 caller="register_restart_field_ptr1d")
351
352 cs%var_ptr1d(cs%novars)%p => f_ptr
353 cs%var_ptr4d(cs%novars)%p => null()
354 cs%var_ptr3d(cs%novars)%p => null()
355 cs%var_ptr2d(cs%novars)%p => null()
356 cs%var_ptr0d(cs%novars)%p => null()
357
358end subroutine register_restart_field_ptr1d
359
360!> Register a 0-d field for restarts, providing the metadata in a structure
361subroutine register_restart_field_ptr0d(f_ptr, var_desc, mandatory, CS, conversion)
362 real, target, intent(in) :: f_ptr !< A pointer to the field to be read or written
363 !! in arbitrary rescaled units [A ~> a]
364 type(vardesc), intent(in) :: var_desc !< A structure with metadata about this variable
365 logical, intent(in) :: mandatory !< If true, the run will abort if this field is not
366 !! successfully read from the restart file.
367 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
368 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
369 !! before it is written [a A-1 ~> 1], 1 by default.
370
371 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
372 "register_restart_field: Module must be initialized before it is used.")
373
374 call lock_check(cs, var_desc)
375
376 cs%novars = cs%novars+1
377 if (cs%novars > cs%max_fields) return ! This is an error that will be reported
378 ! once the total number of fields is known.
379
380 cs%restart_field(cs%novars)%vars = var_desc
381 cs%restart_field(cs%novars)%mand_var = mandatory
382 cs%restart_field(cs%novars)%initialized = .false.
383 cs%restart_field(cs%novars)%conv = 1.0
384 if (present(conversion)) cs%restart_field(cs%novars)%conv = conversion
385 call query_vardesc(cs%restart_field(cs%novars)%vars, &
386 name=cs%restart_field(cs%novars)%var_name, &
387 caller="register_restart_field_ptr0d")
388
389 cs%var_ptr0d(cs%novars)%p => f_ptr
390 cs%var_ptr4d(cs%novars)%p => null()
391 cs%var_ptr3d(cs%novars)%p => null()
392 cs%var_ptr2d(cs%novars)%p => null()
393 cs%var_ptr1d(cs%novars)%p => null()
394
395end subroutine register_restart_field_ptr0d
396
397
398!> Register a pair of rotationally equivalent 2d restart fields
399subroutine register_restart_pair_ptr2d(a_ptr, b_ptr, a_desc, b_desc, &
400 mandatory, CS, conversion, scalar_pair)
401 real, dimension(:,:), target, intent(in) :: a_ptr !< First field pointer
402 !! in arbitrary rescaled units [A ~> a]
403 real, dimension(:,:), target, intent(in) :: b_ptr !< Second field pointer
404 !! in arbitrary rescaled units [A ~> a]
405 type(vardesc), intent(in) :: a_desc !< First field descriptor
406 type(vardesc), intent(in) :: b_desc !< Second field descriptor
407 logical, intent(in) :: mandatory !< If true, abort if field is missing
408 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control structure
409 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
410 !! before it is written [a A-1 ~> 1], 1 by default.
411 logical, optional, intent(in) :: scalar_pair !< If true, the arrays describe a pair of
412 !! scalars, instead of vector components
413 !! whose signs change when rotated
414
415 ! Local variables
416 real :: a_conv, b_conv ! Factors to multipy the a- and b-components by before they are written,
417 ! including sign changes to account for grid rotation [a A-1 ~> 1]
418
419 call lock_check(cs, a_desc)
420 call set_conversion_pair(a_conv, b_conv, cs%turns, conversion, scalar_pair)
421
422 if (modulo(cs%turns, 2) == 0) then ! This is the usual case.
423 call register_restart_field(a_ptr, a_desc, mandatory, cs, conversion=a_conv)
424 call register_restart_field(b_ptr, b_desc, mandatory, cs, conversion=b_conv)
425 else
426 call register_restart_field(b_ptr, a_desc, mandatory, cs, conversion=a_conv)
427 call register_restart_field(a_ptr, b_desc, mandatory, cs, conversion=b_conv)
428 endif
429end subroutine register_restart_pair_ptr2d
430
431
432!> Register a pair of rotationally equivalent 3d restart fields
433subroutine register_restart_pair_ptr3d(a_ptr, b_ptr, a_desc, b_desc, &
434 mandatory, CS, conversion, scalar_pair)
435 real, dimension(:,:,:), target, intent(in) :: a_ptr !< First field pointer
436 !! in arbitrary rescaled units [A ~> a]
437 real, dimension(:,:,:), target, intent(in) :: b_ptr !< Second field pointer
438 !! in arbitrary rescaled units [A ~> a]
439 type(vardesc), intent(in) :: a_desc !< First field descriptor
440 type(vardesc), intent(in) :: b_desc !< Second field descriptor
441 logical, intent(in) :: mandatory !< If true, abort if field is missing
442 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control structure
443 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
444 !! before it is written [a A-1 ~> 1], 1 by default.
445 logical, optional, intent(in) :: scalar_pair !< If true, the arrays describe a pair of
446 !! scalars, instead of vector components
447 !! whose signs change when rotated
448
449 ! Local variables
450 real :: a_conv, b_conv ! Factors to multipy the a- and b-components by before they are written,
451 ! including sign changes to account for grid rotation [a A-1 ~> 1]
452
453 call lock_check(cs, a_desc)
454 call set_conversion_pair(a_conv, b_conv, cs%turns, conversion, scalar_pair)
455
456 if (modulo(cs%turns, 2) == 0) then ! This is the usual case.
457 call register_restart_field(a_ptr, a_desc, mandatory, cs, conversion=a_conv)
458 call register_restart_field(b_ptr, b_desc, mandatory, cs, conversion=b_conv)
459 else
460 call register_restart_field(b_ptr, a_desc, mandatory, cs, conversion=a_conv)
461 call register_restart_field(a_ptr, b_desc, mandatory, cs, conversion=b_conv)
462 endif
463end subroutine register_restart_pair_ptr3d
464
465
466!> Register a pair of rotationally equivalent 2d restart fields
467subroutine register_restart_pair_ptr4d(a_ptr, b_ptr, a_desc, b_desc, &
468 mandatory, CS, conversion, scalar_pair)
469 real, dimension(:,:,:,:), target, intent(in) :: a_ptr !< First field pointer
470 !! in arbitrary rescaled units [A ~> a]
471 real, dimension(:,:,:,:), target, intent(in) :: b_ptr !< Second field pointer
472 !! in arbitrary rescaled units [A ~> a]
473 type(vardesc), intent(in) :: a_desc !< First field descriptor
474 type(vardesc), intent(in) :: b_desc !< Second field descriptor
475 logical, intent(in) :: mandatory !< If true, abort if field is missing
476 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control structure
477 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
478 !! before it is written [a A-1 ~> 1], 1 by default.
479 logical, optional, intent(in) :: scalar_pair !< If true, the arrays describe a pair of
480 !! scalars, instead of vector components
481 !! whose signs change when rotated
482
483 ! Local variables
484 real :: a_conv, b_conv ! Factors to multipy the a- and b-components by before they are written,
485 ! including sign changes to account for grid rotation [a A-1 ~> 1]
486
487 call lock_check(cs, a_desc)
488 call set_conversion_pair(a_conv, b_conv, cs%turns, conversion, scalar_pair)
489
490 if (modulo(cs%turns, 2) == 0) then ! This is the usual case.
491 call register_restart_field(a_ptr, a_desc, mandatory, cs, conversion=a_conv)
492 call register_restart_field(b_ptr, b_desc, mandatory, cs, conversion=b_conv)
493 else
494 call register_restart_field(b_ptr, a_desc, mandatory, cs, conversion=a_conv)
495 call register_restart_field(a_ptr, b_desc, mandatory, cs, conversion=b_conv)
496 endif
497end subroutine register_restart_pair_ptr4d
498
499!> Set a pair of factors to multiply by the components of a vector when writing
500!! that include any sign changes needed to account for grid rotation.
501subroutine set_conversion_pair(u_conv, v_conv, turns, conversion, scalar_pair)
502 real, intent(out) :: u_conv !< A factor to multiply the u-component of a vector by before it is
503 !! written, including sign changes due to grid rotation [a A-1 ~> 1]
504 real, intent(out) :: v_conv !< A factor to multiply the u-component of a vector by before it is
505 !! written, including sign changes due to grid rotation [a A-1 ~> 1]
506 integer, intent(in) :: turns !< Number of quarter turns from input to model domain
507 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
508 !! before it is written [a A-1 ~> 1], 1 by default.
509 logical, optional, intent(in) :: scalar_pair !< If true, the arrays describe a pair of scalars,
510 !! instead of vector components whose signs change when rotated
511
512 ! Local variables
513 integer :: q_turns
514 logical :: scalars
515
516 u_conv = 1.0 ; v_conv = 1.0
517 if (present(conversion)) then
518 u_conv = conversion ; v_conv = conversion
519 endif
520
521 scalars = .false. ; if (present(scalar_pair)) scalars = scalar_pair
522 if (scalars) return
523
524 q_turns = modulo(turns, 4)
525 if (q_turns == 1) then
526 v_conv = -1.0*v_conv
527 elseif (q_turns == 2) then
528 u_conv = -1.0*u_conv ; v_conv = -1.0*v_conv
529 elseif (q_turns == 3) then
530 u_conv = -1.0*u_conv
531 endif
532
533end subroutine set_conversion_pair
534
535
536! The following provide alternate interfaces to register restarts.
537
538!> Register a 4-d field for restarts, providing the metadata as individual arguments
539subroutine register_restart_field_4d(f_ptr, name, mandatory, CS, longname, units, conversion, &
540 hor_grid, z_grid, t_grid, extra_axes)
541 real, dimension(:,:,:,:), &
542 target, intent(in) :: f_ptr !< A pointer to the field to be read or written
543 !! in arbitrary rescaled units [A ~> a]
544 character(len=*), intent(in) :: name !< variable name to be used in the restart file
545 logical, intent(in) :: mandatory !< If true, the run will abort if this field is not
546 !! successfully read from the restart file.
547 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
548 character(len=*), optional, intent(in) :: longname !< variable long name
549 character(len=*), optional, intent(in) :: units !< variable units
550 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
551 !! before it is written [a A-1 ~> 1], 1 by default.
552 character(len=*), optional, intent(in) :: hor_grid !< variable horizontal staggering, 'h' if absent
553 character(len=*), optional, intent(in) :: z_grid !< variable vertical staggering, 'L' if absent
554 character(len=*), optional, intent(in) :: t_grid !< time description: s, p, or 1, 's' if absent
555 type(axis_info), dimension(:), &
556 optional, intent(in) :: extra_axes !< dimensions other than space-time
557
558 type(vardesc) :: vd
559 character(len=32), dimension(:), allocatable :: dim_names
560 integer :: n, n_extradims
561
562 ! first 2 dimensions in dim_names are reserved for i,j
563 ! so extra_dimensions are shifted to index 3.
564 ! this is designed not to break the behavior in SIS2
565 ! (see register_restart_field_4d in SIS_restart.F90)
566 if (present(extra_axes)) then
567 n_extradims = size(extra_axes)
568 allocate(dim_names(n_extradims+2))
569 dim_names(1) = ""
570 dim_names(2) = ""
571 do n=3,n_extradims+2
572 dim_names(n) = extra_axes(n-2)%name
573 enddo
574 endif
575
576 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart: " // &
577 "register_restart_field_4d: Module must be initialized before "//&
578 "it is used to register "//trim(name))
579
580 call lock_check(cs, name=name)
581
582 if (present(extra_axes)) then
583 vd = var_desc(name, units=units, longname=longname, hor_grid=hor_grid, &
584 z_grid=z_grid, t_grid=t_grid, dim_names=dim_names, extra_axes=extra_axes)
585 else
586 vd = var_desc(name, units=units, longname=longname, hor_grid=hor_grid, &
587 z_grid=z_grid, t_grid=t_grid)
588 endif
589
590 call register_restart_field_ptr4d(f_ptr, vd, mandatory, cs, conversion)
591
592end subroutine register_restart_field_4d
593
594!> Register a 3-d field for restarts, providing the metadata as individual arguments
595subroutine register_restart_field_3d(f_ptr, name, mandatory, CS, longname, units, conversion, &
596 hor_grid, z_grid, t_grid, extra_axes)
597 real, dimension(:,:,:), &
598 target, intent(in) :: f_ptr !< A pointer to the field to be read or written
599 !! in arbitrary rescaled units [A ~> a]
600 character(len=*), intent(in) :: name !< variable name to be used in the restart file
601 logical, intent(in) :: mandatory !< If true, the run will abort if this field is not
602 !! successfully read from the restart file.
603 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
604 character(len=*), optional, intent(in) :: longname !< variable long name
605 character(len=*), optional, intent(in) :: units !< variable units
606 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
607 !! before it is written [a A-1 ~> 1], 1 by default.
608 character(len=*), optional, intent(in) :: hor_grid !< variable horizontal staggering, 'h' if absent
609 character(len=*), optional, intent(in) :: z_grid !< variable vertical staggering, 'L' if absent
610 character(len=*), optional, intent(in) :: t_grid !< time description: s, p, or 1, 's' if absent
611 type(axis_info), dimension(:), &
612 optional, intent(in) :: extra_axes !< dimensions other than space-time
613
614 type(vardesc) :: vd
615 character(len=32), dimension(:), allocatable :: dim_names
616 integer :: n, n_extradims
617
618 ! first 2 dimensions in dim_names are reserved for i,j
619 ! so extra_dimensions are shifted to index 3.
620 ! this is designed not to break the behavior in SIS2
621 ! (see register_restart_field_4d in SIS_restart.F90)
622 if (present(extra_axes)) then
623 n_extradims = size(extra_axes)
624 allocate(dim_names(n_extradims+2))
625 dim_names(1) = ""
626 dim_names(2) = ""
627 do n=3,n_extradims+2
628 dim_names(n) = extra_axes(n-2)%name
629 enddo
630 endif
631
632 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart: " // &
633 "register_restart_field_3d: Module must be initialized before "//&
634 "it is used to register "//trim(name))
635
636 call lock_check(cs, name=name)
637
638 if (present(extra_axes)) then
639 vd = var_desc(name, units=units, longname=longname, hor_grid=hor_grid, &
640 z_grid=z_grid, t_grid=t_grid, dim_names=dim_names, extra_axes=extra_axes)
641 else
642 vd = var_desc(name, units=units, longname=longname, hor_grid=hor_grid, &
643 z_grid=z_grid, t_grid=t_grid)
644 endif
645
646 call register_restart_field_ptr3d(f_ptr, vd, mandatory, cs, conversion)
647
648end subroutine register_restart_field_3d
649
650!> Register a 2-d field for restarts, providing the metadata as individual arguments
651subroutine register_restart_field_2d(f_ptr, name, mandatory, CS, longname, units, conversion, &
652 hor_grid, z_grid, t_grid)
653 real, dimension(:,:), &
654 target, intent(in) :: f_ptr !< A pointer to the field to be read or written
655 !! in arbitrary rescaled units [A ~> a]
656 character(len=*), intent(in) :: name !< variable name to be used in the restart file
657 logical, intent(in) :: mandatory !< If true, the run will abort if this field is not
658 !! successfully read from the restart file.
659 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
660 character(len=*), optional, intent(in) :: longname !< variable long name
661 character(len=*), optional, intent(in) :: units !< variable units
662 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
663 !! before it is written [a A-1 ~> 1], 1 by default.
664 character(len=*), optional, intent(in) :: hor_grid !< variable horizontal staggering, 'h' if absent
665 character(len=*), optional, intent(in) :: z_grid !< variable vertical staggering, '1' if absent
666 character(len=*), optional, intent(in) :: t_grid !< time description: s, p, or 1, 's' if absent
667
668 type(vardesc) :: vd
669 character(len=8) :: Zgrid
670
671 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart: " // &
672 "register_restart_field_2d: Module must be initialized before "//&
673 "it is used to register "//trim(name))
674
675 zgrid = '1' ; if (present(z_grid)) zgrid = z_grid
676
677 call lock_check(cs, name=name)
678
679 vd = var_desc(name, units=units, longname=longname, hor_grid=hor_grid, &
680 z_grid=zgrid, t_grid=t_grid)
681
682 call register_restart_field_ptr2d(f_ptr, vd, mandatory, cs, conversion)
683
684end subroutine register_restart_field_2d
685
686!> Register a 1-d field for restarts, providing the metadata as individual arguments
687subroutine register_restart_field_1d(f_ptr, name, mandatory, CS, longname, units, conversion, &
688 hor_grid, z_grid, t_grid)
689 real, dimension(:), target, intent(in) :: f_ptr !< A pointer to the field to be read or written
690 !! in arbitrary rescaled units [A ~> a]
691 character(len=*), intent(in) :: name !< variable name to be used in the restart file
692 logical, intent(in) :: mandatory !< If true, the run will abort if this field is not
693 !! successfully read from the restart file.
694 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
695 character(len=*), optional, intent(in) :: longname !< variable long name
696 character(len=*), optional, intent(in) :: units !< variable units
697 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
698 !! before it is written [a A-1 ~> 1], 1 by default.
699 character(len=*), optional, intent(in) :: hor_grid !< variable horizontal staggering, '1' if absent
700 character(len=*), optional, intent(in) :: z_grid !< variable vertical staggering, 'L' if absent
701 character(len=*), optional, intent(in) :: t_grid !< time description: s, p, or 1, 's' if absent
702
703 type(vardesc) :: vd
704 character(len=8) :: hgrid
705
706 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart: " // &
707 "register_restart_field_3d: Module must be initialized before "//&
708 "it is used to register "//trim(name))
709
710 hgrid = '1' ; if (present(hor_grid)) hgrid = hor_grid
711
712 call lock_check(cs, name=name)
713
714 vd = var_desc(name, units=units, longname=longname, hor_grid=hgrid, &
715 z_grid=z_grid, t_grid=t_grid)
716
717 call register_restart_field_ptr1d(f_ptr, vd, mandatory, cs, conversion)
718
719end subroutine register_restart_field_1d
720
721!> Register a 0-d field for restarts, providing the metadata as individual arguments
722subroutine register_restart_field_0d(f_ptr, name, mandatory, CS, longname, units, conversion, &
723 t_grid)
724 real, target, intent(in) :: f_ptr !< A pointer to the field to be read or written
725 !! in arbitrary rescaled units [A ~> a]
726 character(len=*), intent(in) :: name !< variable name to be used in the restart file
727 logical, intent(in) :: mandatory !< If true, the run will abort if this field is not
728 !! successfully read from the restart file.
729 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
730 character(len=*), optional, intent(in) :: longname !< variable long name
731 character(len=*), optional, intent(in) :: units !< variable units
732 real, optional, intent(in) :: conversion !< A factor to multiply a restart field by
733 !! before it is written [a A-1 ~> 1], 1 by default.
734 character(len=*), optional, intent(in) :: t_grid !< time description: s, p, or 1, 's' if absent
735
736 type(vardesc) :: vd
737
738 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart: " // &
739 "register_restart_field_0d: Module must be initialized before "//&
740 "it is used to register "//trim(name))
741
742 call lock_check(cs, name=name)
743
744 vd = var_desc(name, units=units, longname=longname, hor_grid='1', &
745 z_grid='1', t_grid=t_grid)
746
747 call register_restart_field_ptr0d(f_ptr, vd, mandatory, cs, conversion)
748
749end subroutine register_restart_field_0d
750
751
752!> query_initialized_name determines whether a named field has been successfully
753!! read from a restart file or has otherwise been recorded as being initialized.
754function query_initialized_name(name, CS) result(query_initialized)
755 character(len=*), intent(in) :: name !< The name of the field that is being queried
756 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
757 logical :: query_initialized
758
759 integer :: m, n
760
761 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
762 "query_initialized: Module must be initialized before it is used.")
763
764 if (cs%novars > cs%max_fields) call restart_error(cs)
765
766 query_initialized = .false.
767 n = cs%novars+1
768 do m=1,cs%novars
769 if (trim(name) == cs%restart_field(m)%var_name) then
770 if (cs%restart_field(m)%initialized) query_initialized = .true.
771 n = m ; exit
772 endif
773 enddo
774 if ((n==cs%novars+1) .and. (is_root_pe())) &
775 call mom_error(note,"MOM_restart: Unknown restart variable "//name// &
776 " queried for initialization.")
777
778 if ((is_root_pe()) .and. query_initialized) &
779 call mom_error(note,"MOM_restart: "//name// &
780 " initialization confirmed by name.")
781
782end function query_initialized_name
783
784!> Indicate whether the field pointed to by f_ptr has been initialized from a restart file.
785function query_initialized_0d(f_ptr, CS) result(query_initialized)
786 real, target, intent(in) :: f_ptr !< A pointer to the field that is being queried [arbitrary]
787 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
788 logical :: query_initialized
789
790 integer :: m, n
791
792 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
793 "query_initialized: Module must be initialized before it is used.")
794
795 if (cs%novars > cs%max_fields) call restart_error(cs)
796
797 query_initialized = .false.
798 n = cs%novars+1
799 do m=1,cs%novars
800 if (associated(cs%var_ptr0d(m)%p,f_ptr)) then
801 if (cs%restart_field(m)%initialized) query_initialized = .true.
802 n = m ; exit
803 endif
804 enddo
805
806end function query_initialized_0d
807
808!> Indicate whether the field pointed to by f_ptr has been initialized from a restart file.
809function query_initialized_1d(f_ptr, CS) result(query_initialized)
810 real, dimension(:), target, intent(in) :: f_ptr !< A pointer to the field that is being queried [arbitrary]
811 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
812 logical :: query_initialized
813
814 integer :: m, n
815
816 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
817 "query_initialized: Module must be initialized before it is used.")
818
819 if (cs%novars > cs%max_fields) call restart_error(cs)
820
821 query_initialized = .false.
822 n = cs%novars+1
823 do m=1,cs%novars
824 if (associated(cs%var_ptr1d(m)%p,f_ptr)) then
825 if (cs%restart_field(m)%initialized) query_initialized = .true.
826 n = m ; exit
827 endif
828 enddo
829
830end function query_initialized_1d
831
832!> Indicate whether the field pointed to by f_ptr has been initialized from a restart file.
833function query_initialized_2d(f_ptr, CS) result(query_initialized)
834 real, dimension(:,:), &
835 target, intent(in) :: f_ptr !< A pointer to the field that is being queried [arbitrary]
836 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
837 logical :: query_initialized
838
839 integer :: m, n
840
841 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
842 "query_initialized: Module must be initialized before it is used.")
843
844 if (cs%novars > cs%max_fields) call restart_error(cs)
845
846 query_initialized = .false.
847 n = cs%novars+1
848 do m=1,cs%novars
849 if (associated(cs%var_ptr2d(m)%p,f_ptr)) then
850 if (cs%restart_field(m)%initialized) query_initialized = .true.
851 n = m ; exit
852 endif
853 enddo
854
855end function query_initialized_2d
856
857!> Indicate whether the field pointed to by f_ptr has been initialized from a restart file.
858function query_initialized_3d(f_ptr, CS) result(query_initialized)
859 real, dimension(:,:,:), &
860 target, intent(in) :: f_ptr !< A pointer to the field that is being queried [arbitrary]
861 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
862 logical :: query_initialized
863
864 integer :: m, n
865
866 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
867 "query_initialized: Module must be initialized before it is used.")
868
869 if (cs%novars > cs%max_fields) call restart_error(cs)
870
871 query_initialized = .false.
872 n = cs%novars+1
873 do m=1,cs%novars
874 if (associated(cs%var_ptr3d(m)%p,f_ptr)) then
875 if (cs%restart_field(m)%initialized) query_initialized = .true.
876 n = m ; exit
877 endif
878 enddo
879
880end function query_initialized_3d
881
882!> Indicate whether the field pointed to by f_ptr has been initialized from a restart file.
883function query_initialized_4d(f_ptr, CS) result(query_initialized)
884 real, dimension(:,:,:,:), &
885 target, intent(in) :: f_ptr !< A pointer to the field that is being queried [arbitrary]
886 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
887 logical :: query_initialized
888
889 integer :: m, n
890
891 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
892 "query_initialized: Module must be initialized before it is used.")
893
894 if (cs%novars > cs%max_fields) call restart_error(cs)
895
896 query_initialized = .false.
897 n = cs%novars+1
898 do m=1,cs%novars
899 if (associated(cs%var_ptr4d(m)%p,f_ptr)) then
900 if (cs%restart_field(m)%initialized) query_initialized = .true.
901 n = m ; exit
902 endif
903 enddo
904
905end function query_initialized_4d
906
907!> Indicate whether the field stored in f_ptr or with the specified variable
908!! name has been initialized from a restart file.
909function query_initialized_0d_name(f_ptr, name, CS) result(query_initialized)
910 real, target, intent(in) :: f_ptr !< The field that is being queried [arbitrary]
911 character(len=*), intent(in) :: name !< The name of the field that is being queried
912 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
913 logical :: query_initialized
914
915 integer :: m, n
916
917 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
918 "query_initialized: Module must be initialized before it is used.")
919
920 if (cs%novars > cs%max_fields) call restart_error(cs)
921
922 query_initialized = .false.
923 n = cs%novars+1
924 do m=1,cs%novars
925 if (associated(cs%var_ptr0d(m)%p,f_ptr)) then
926 if (cs%restart_field(m)%initialized) query_initialized = .true.
927 n = m ; exit
928 endif
929 enddo
930 if (n==cs%novars+1) then
931 if (is_root_pe()) &
932 call mom_error(note,"MOM_restart: Unable to find "//name//" queried by pointer, "//&
933 "probably because of the suspect comparison of pointers by ASSOCIATED.")
934 query_initialized = query_initialized_name(name, cs)
935 endif
936
937end function query_initialized_0d_name
938
939!> Indicate whether the field stored in f_ptr or with the specified variable
940!! name has been initialized from a restart file.
941function query_initialized_1d_name(f_ptr, name, CS) result(query_initialized)
942 real, dimension(:), &
943 target, intent(in) :: f_ptr !< The field that is being queried [arbitrary]
944 character(len=*), intent(in) :: name !< The name of the field that is being queried
945 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
946 logical :: query_initialized
947
948 integer :: m, n
949
950 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
951 "query_initialized: Module must be initialized before it is used.")
952
953 if (cs%novars > cs%max_fields) call restart_error(cs)
954
955 query_initialized = .false.
956 n = cs%novars+1
957 do m=1,cs%novars
958 if (associated(cs%var_ptr1d(m)%p,f_ptr)) then
959 if (cs%restart_field(m)%initialized) query_initialized = .true.
960 n = m ; exit
961 endif
962 enddo
963 if (n==cs%novars+1) then
964 if (is_root_pe()) &
965 call mom_error(note,"MOM_restart: Unable to find "//name//" queried by pointer, "//&
966 "probably because of the suspect comparison of pointers by ASSOCIATED.")
967 query_initialized = query_initialized_name(name, cs)
968 endif
969
970end function query_initialized_1d_name
971
972!> Indicate whether the field stored in f_ptr or with the specified variable
973!! name has been initialized from a restart file.
974function query_initialized_2d_name(f_ptr, name, CS) result(query_initialized)
975 real, dimension(:,:), &
976 target, intent(in) :: f_ptr !< The field that is being queried [arbitrary]
977 character(len=*), intent(in) :: name !< The name of the field that is being queried
978 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
979 logical :: query_initialized
980
981 integer :: m, n
982
983 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
984 "query_initialized: Module must be initialized before it is used.")
985
986 if (cs%novars > cs%max_fields) call restart_error(cs)
987
988 query_initialized = .false.
989 n = cs%novars+1
990 do m=1,cs%novars
991 if (associated(cs%var_ptr2d(m)%p,f_ptr)) then
992 if (cs%restart_field(m)%initialized) query_initialized = .true.
993 n = m ; exit
994 endif
995 enddo
996 if (n==cs%novars+1) then
997 if (is_root_pe()) &
998 call mom_error(note,"MOM_restart: Unable to find "//name//" queried by pointer, "//&
999 "probably because of the suspect comparison of pointers by ASSOCIATED.")
1000 query_initialized = query_initialized_name(name, cs)
1001 endif
1002
1003end function query_initialized_2d_name
1004
1005!> Indicate whether the field stored in f_ptr or with the specified variable
1006!! name has been initialized from a restart file.
1007function query_initialized_3d_name(f_ptr, name, CS) result(query_initialized)
1008 real, dimension(:,:,:), &
1009 target, intent(in) :: f_ptr !< The field that is being queried [arbitrary]
1010 character(len=*), intent(in) :: name !< The name of the field that is being queried
1011 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
1012 logical :: query_initialized
1013
1014 integer :: m, n
1015
1016 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1017 "query_initialized: Module must be initialized before it is used.")
1018
1019 if (cs%novars > cs%max_fields) call restart_error(cs)
1020
1021 query_initialized = .false.
1022 n = cs%novars+1
1023 do m=1,cs%novars
1024 if (associated(cs%var_ptr3d(m)%p,f_ptr)) then
1025 if (cs%restart_field(m)%initialized) query_initialized = .true.
1026 n = m ; exit
1027 endif
1028 enddo
1029 if (n==cs%novars+1) then
1030 if (is_root_pe()) &
1031 call mom_error(note, "MOM_restart: Unable to find "//name//" queried by pointer, "//&
1032 "possibly because of the suspect comparison of pointers by ASSOCIATED.")
1033 query_initialized = query_initialized_name(name, cs)
1034 endif
1035
1036end function query_initialized_3d_name
1037
1038!> Indicate whether the field stored in f_ptr or with the specified variable
1039!! name has been initialized from a restart file.
1040function query_initialized_4d_name(f_ptr, name, CS) result(query_initialized)
1041 real, dimension(:,:,:,:), &
1042 target, intent(in) :: f_ptr !< The field that is being queried [arbitrary]
1043 character(len=*), intent(in) :: name !< The name of the field that is being queried
1044 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
1045 logical :: query_initialized
1046
1047 integer :: m, n
1048
1049 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1050 "query_initialized: Module must be initialized before it is used.")
1051
1052 if (cs%novars > cs%max_fields) call restart_error(cs)
1053
1054 query_initialized = .false.
1055 n = cs%novars+1
1056 do m=1,cs%novars
1057 if (associated(cs%var_ptr4d(m)%p,f_ptr)) then
1058 if (cs%restart_field(m)%initialized) query_initialized = .true.
1059 n = m ; exit
1060 endif
1061 enddo
1062 if (n==cs%novars+1) then
1063 if (is_root_pe()) &
1064 call mom_error(note, "MOM_restart: Unable to find "//name//" queried by pointer, "//&
1065 "possibly because of the suspect comparison of pointers by ASSOCIATED.")
1066 query_initialized = query_initialized_name(name, cs)
1067 endif
1068
1069end function query_initialized_4d_name
1070
1071!> set_initialized_name records that a named field has been initialized.
1072subroutine set_initialized_name(name, CS)
1073 character(len=*), intent(in) :: name !< The name of the field that is being set
1074 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
1075
1076 integer :: m
1077
1078 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1079 "set_initialized: Module must be initialized before it is used.")
1080
1081 do m=1,cs%novars ; if (trim(name) == trim(cs%restart_field(m)%var_name)) then
1082 cs%restart_field(m)%initialized = .true. ; exit
1083 endif ; enddo
1084
1085 if ((m==cs%novars+1) .and. (is_root_pe())) &
1086 call mom_error(note,"MOM_restart: Unknown restart variable "//name// &
1087 " used in set_initialized call.")
1088
1089end subroutine set_initialized_name
1090
1091!> Record that the array in f_ptr with the given name has been initialized.
1092subroutine set_initialized_0d_name(f_ptr, name, CS)
1093 real, target, intent(in) :: f_ptr !< The variable that has been initialized [arbitrary]
1094 character(len=*), intent(in) :: name !< The name of the field that has been initialized
1095 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
1096
1097 integer :: m
1098
1099 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1100 "set_initialized: Module must be initialized before it is used.")
1101
1102 do m=1,cs%novars ; if (associated(cs%var_ptr0d(m)%p,f_ptr)) then
1103 cs%restart_field(m)%initialized = .true. ; exit
1104 endif ; enddo
1105
1106 if (m==cs%novars+1) then
1107 if (is_root_pe()) &
1108 call mom_error(note,"MOM_restart: Unable to find "//name//" queried by pointer, "//&
1109 "probably because of the suspect comparison of pointers by ASSOCIATED.")
1110 call set_initialized_name(name, cs)
1111 endif
1112
1113end subroutine set_initialized_0d_name
1114
1115!> Record that the array in f_ptr with the given name has been initialized.
1116subroutine set_initialized_1d_name(f_ptr, name, CS)
1117 real, dimension(:), &
1118 target, intent(in) :: f_ptr !< The array that has been initialized [arbitrary]
1119 character(len=*), intent(in) :: name !< The name of the field that has been initialized
1120 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
1121
1122 integer :: m
1123
1124 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1125 "set_initialized: Module must be initialized before it is used.")
1126
1127 do m=1,cs%novars ; if (associated(cs%var_ptr1d(m)%p,f_ptr)) then
1128 cs%restart_field(m)%initialized = .true. ; exit
1129 endif ; enddo
1130
1131 if (m==cs%novars+1) then
1132 if (is_root_pe()) &
1133 call mom_error(note,"MOM_restart: Unable to find "//name//" queried by pointer, "//&
1134 "probably because of the suspect comparison of pointers by ASSOCIATED.")
1135 call set_initialized_name(name, cs)
1136 endif
1137
1138end subroutine set_initialized_1d_name
1139
1140!> Record that the array in f_ptr with the given name has been initialized.
1141subroutine set_initialized_2d_name(f_ptr, name, CS)
1142 real, dimension(:,:), &
1143 target, intent(in) :: f_ptr !< The array that has been initialized [arbitrary]
1144 character(len=*), intent(in) :: name !< The name of the field that has been initialized
1145 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
1146
1147 integer :: m
1148
1149 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1150 "set_initialized: Module must be initialized before it is used.")
1151
1152 do m=1,cs%novars ; if (associated(cs%var_ptr2d(m)%p,f_ptr)) then
1153 cs%restart_field(m)%initialized = .true. ; exit
1154 endif ; enddo
1155
1156 if (m==cs%novars+1) then
1157 if (is_root_pe()) &
1158 call mom_error(note,"MOM_restart: Unable to find "//name//" queried by pointer, "//&
1159 "probably because of the suspect comparison of pointers by ASSOCIATED.")
1160 call set_initialized_name(name, cs)
1161 endif
1162
1163end subroutine set_initialized_2d_name
1164
1165!> Record that the array in f_ptr with the given name has been initialized.
1166subroutine set_initialized_3d_name(f_ptr, name, CS)
1167 real, dimension(:,:,:), &
1168 target, intent(in) :: f_ptr !< The array that has been initialized [arbitrary]
1169 character(len=*), intent(in) :: name !< The name of the field that has been initialized
1170 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
1171
1172 integer :: m
1173
1174 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1175 "set_initialized: Module must be initialized before it is used.")
1176
1177 do m=1,cs%novars ; if (associated(cs%var_ptr3d(m)%p,f_ptr)) then
1178 cs%restart_field(m)%initialized = .true. ; exit
1179 endif ; enddo
1180
1181 if (m==cs%novars+1) then
1182 if (is_root_pe()) &
1183 call mom_error(note,"MOM_restart: Unable to find "//name//" queried by pointer, "//&
1184 "probably because of the suspect comparison of pointers by ASSOCIATED.")
1185 call set_initialized_name(name, cs)
1186 endif
1187
1188end subroutine set_initialized_3d_name
1189
1190!> Record that the array in f_ptr with the given name has been initialized.
1191subroutine set_initialized_4d_name(f_ptr, name, CS)
1192 real, dimension(:,:,:,:), &
1193 target, intent(in) :: f_ptr !< The array that has been initialized [arbitrary]
1194 character(len=*), intent(in) :: name !< The name of the field that has been initialized
1195 type(mom_restart_cs), intent(inout) :: CS !< MOM restart control struct
1196
1197 integer :: m
1198
1199 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1200 "set_initialized: Module must be initialized before it is used.")
1201
1202 do m=1,cs%novars ; if (associated(cs%var_ptr4d(m)%p,f_ptr)) then
1203 cs%restart_field(m)%initialized = .true. ; exit
1204 endif ; enddo
1205
1206 if (m==cs%novars+1) then
1207 if (is_root_pe()) &
1208 call mom_error(note,"MOM_restart: Unable to find "//name//" queried by pointer, "//&
1209 "probably because of the suspect comparison of pointers by ASSOCIATED.")
1210 call set_initialized_name(name, cs)
1211 endif
1212
1213end subroutine set_initialized_4d_name
1214
1215
1216!====================== only_read_from_restarts variants =======================
1217
1218!> Try to read a named 4-d field from the restart files
1219subroutine only_read_restart_field_4d(varname, f_ptr, G, CS, position, filename, directory, success, scale)
1220 character(len=*), intent(in) :: varname !< The variable name to be used in the restart file
1221 real, dimension(:,:,:,:), intent(inout) :: f_ptr !< The array for the field to be read
1222 !! in arbitrary rescaled units [A ~> a]
1223 type(ocean_grid_type), intent(in) :: G !< The ocean's grid structure
1224 type(mom_restart_cs), intent(in) :: CS !< MOM restart control struct
1225 integer, optional, intent(in) :: position !< A coded integer indicating the horizontal
1226 !! position of this variable
1227 character(len=*), optional, intent(in) :: filename !< The list of restart file names or a single
1228 !! character 'r' to read automatically named files
1229 character(len=*), optional, intent(in) :: directory !< The directory in which to seek restart files.
1230 logical, optional, intent(out) :: success !< True if the field was read successfully
1231 real, optional, intent(in) :: scale !< A factor by which the field will be scaled
1232 !! [A a-1 ~> 1] to convert from the units in
1233 !! the file to the internal units of this field
1234
1235 ! Local variables
1236 character(len=:), allocatable :: file_path ! The full path to the file with the variable
1237 logical :: found ! True if the variable was found.
1238 logical :: is_global ! True if the variable is in a global file.
1239
1240 found = find_var_in_restart_files(varname, g, cs, file_path, filename, directory, is_global)
1241
1242 if (found) then
1243 call mom_read_data(file_path, varname, f_ptr, g%domain, timelevel=1, position=position, &
1244 scale=scale, global_file=is_global)
1245 endif
1246 if (present(success)) success = found
1247
1248end subroutine only_read_restart_field_4d
1249
1250!> Try to read a named 3-d field from the restart files
1251subroutine only_read_restart_field_3d(varname, f_ptr, G, CS, position, filename, directory, success, scale)
1252 character(len=*), intent(in) :: varname !< The variable name to be used in the restart file
1253 real, dimension(:,:,:), intent(inout) :: f_ptr !< The array for the field to be read
1254 !! in arbitrary rescaled units [A ~> a]
1255 type(ocean_grid_type), intent(in) :: G !< The ocean's grid structure
1256 type(mom_restart_cs), intent(in) :: CS !< MOM restart control struct
1257 integer, optional, intent(in) :: position !< A coded integer indicating the horizontal
1258 !! position of this variable
1259 character(len=*), optional, intent(in) :: filename !< The list of restart file names or a single
1260 !! character 'r' to read automatically named files
1261 character(len=*), optional, intent(in) :: directory !< The directory in which to seek restart files.
1262 logical, optional, intent(out) :: success !< True if the field was read successfully
1263 real, optional, intent(in) :: scale !< A factor by which the field will be scaled
1264 !! [A a-1 ~> 1] to convert from the units in
1265 !! the file to the internal units of this field
1266
1267 ! Local variables
1268 character(len=:), allocatable :: file_path ! The full path to the file with the variable
1269 logical :: found ! True if the variable was found.
1270 logical :: is_global ! True if the variable is in a global file.
1271
1272 found = find_var_in_restart_files(varname, g, cs, file_path, filename, directory, is_global)
1273
1274 if (found) then
1275 call mom_read_data(file_path, varname, f_ptr, g%domain, timelevel=1, position=position, &
1276 scale=scale, global_file=is_global)
1277 endif
1278 if (present(success)) success = found
1279
1280end subroutine only_read_restart_field_3d
1281
1282!> Try to read a named 2-d field from the restart files
1283subroutine only_read_restart_field_2d(varname, f_ptr, G, CS, position, filename, directory, success, scale)
1284 character(len=*), intent(in) :: varname !< The variable name to be used in the restart file
1285 real, dimension(:,:), intent(inout) :: f_ptr !< The array for the field to be read
1286 !! in arbitrary rescaled units [A ~> a]
1287 type(ocean_grid_type), intent(in) :: G !< The ocean's grid structure
1288 type(mom_restart_cs), intent(in) :: CS !< MOM restart control struct
1289 integer, optional, intent(in) :: position !< A coded integer indicating the horizontal
1290 !! position of this variable
1291 character(len=*), optional, intent(in) :: filename !< The list of restart file names or a single
1292 !! character 'r' to read automatically named files
1293 character(len=*), optional, intent(in) :: directory !< The directory in which to seek restart files.
1294 logical, optional, intent(out) :: success !< True if the field was read successfully
1295 real, optional, intent(in) :: scale !< A factor by which the field will be scaled
1296 !! [A a-1 ~> 1] to convert from the units in
1297 !! the file to the internal units of this field
1298
1299 ! Local variables
1300 character(len=:), allocatable :: file_path ! The full path to the file with the variable
1301 logical :: found ! True if the variable was found.
1302 logical :: is_global ! True if the variable is in a global file.
1303
1304 found = find_var_in_restart_files(varname, g, cs, file_path, filename, directory, is_global)
1305
1306 if (found) then
1307 call mom_read_data(file_path, varname, f_ptr, g%domain, timelevel=1, position=position, &
1308 scale=scale, global_file=is_global)
1309 endif
1310 if (present(success)) success = found
1311
1312end subroutine only_read_restart_field_2d
1313
1314
1315!> Try to read a named 3-d field from the restart files
1316subroutine only_read_restart_pair_3d(a_ptr, b_ptr, a_name, b_name, G, CS, &
1317 stagger, filename, directory, success, scale)
1318 real, dimension(:,:,:), intent(inout) :: a_ptr !< The array for the first field to be read
1319 !! in arbitrary rescaled units [A ~> a]
1320 real, dimension(:,:,:), intent(inout) :: b_ptr !< The array for the second field to be read
1321 !! in arbitrary rescaled units [A ~> a]
1322 character(len=*), intent(in) :: a_name !< The first variable name to be used in the restart file
1323 character(len=*), intent(in) :: b_name !< The second variable name to be used in the restart file
1324 type(ocean_grid_type), intent(in) :: G !< The ocean's grid structure
1325 type(mom_restart_cs), intent(in) :: CS !< MOM restart control struct
1326 integer, optional, intent(in) :: stagger !< A coded integer indicating the horizontal
1327 !! position of this pair of variables
1328 character(len=*), optional, intent(in) :: filename !< The list of restart file names or a single
1329 !! character 'r' to read automatically named files
1330 character(len=*), optional, intent(in) :: directory !< The directory in which to seek restart files.
1331 logical, optional, intent(out) :: success !< True if the field was read successfully
1332 real, optional, intent(in) :: scale !< A factor by which the fields will be scaled
1333 !! [A a-1 ~> 1] to convert from the units in
1334 !! the file to the internal units of this field
1335
1336 ! Local variables
1337 character(len=:), allocatable :: file_path_a ! The full path to the file with the first variable
1338 character(len=:), allocatable :: file_path_b ! The full path to the file with the second variable
1339 integer :: a_pos, b_pos ! A coded position for the two variables.
1340 logical :: a_found, b_found ! True if the variables were found.
1341 logical :: global_a, global_b ! True if the variables are in global files.
1342
1343 a_found = find_var_in_restart_files(a_name, g, cs, file_path_a, filename, directory, global_a)
1344 b_found = find_var_in_restart_files(b_name, g, cs, file_path_b, filename, directory, global_b)
1345
1346 a_pos = east_face ; b_pos = north_face
1347 if (present(stagger)) then ; select case (stagger)
1348 case (agrid) ; a_pos = center ; b_pos = center
1349 case (bgrid_ne) ; a_pos = corner ; b_pos = corner
1350 case (cgrid_ne) ; a_pos = east_face ; b_pos = north_face
1351 case default ; a_pos = east_face ; b_pos = north_face
1352 end select ; endif
1353
1354 if (a_found .and. b_found) then
1355 call mom_read_data(file_path_a, a_name, a_ptr, g%domain, timelevel=1, position=a_pos, &
1356 scale=scale, global_file=global_b, file_may_be_4d=.true.)
1357 call mom_read_data(file_path_b, b_name, b_ptr, g%domain, timelevel=1, position=b_pos, &
1358 scale=scale, global_file=global_b, file_may_be_4d=.true.)
1359 endif
1360 if (present(success)) success = (a_found .and. b_found)
1361
1362end subroutine only_read_restart_pair_3d
1363
1364!> Return an indication of whether the named variable is in the restart files, and provide the full path
1365!! to the restart file in which a variable is found.
1366function find_var_in_restart_files(varname, G, CS, file_path, filename, directory, is_global) result (found)
1367 character(len=*), intent(in) :: varname !< The variable name to be used in the restart file
1368 type(ocean_grid_type), intent(in) :: g !< The ocean's grid structure
1369 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
1370 character(len=:), allocatable, intent(out) :: file_path !< The full path to the file in which the
1371 !! variable is found
1372 character(len=*), optional, intent(in) :: filename !< The list of restart file names or a single
1373 !! character 'r' to read automatically named files
1374 character(len=*), optional, intent(in) :: directory !< The directory in which to seek restart files.
1375 logical, optional, intent(out) :: is_global !< True if the file is global.
1376 logical :: found !< True if the named variable was found in the restart files.
1377
1378 ! Local variables
1379 character(len=240), allocatable, dimension(:) :: file_paths ! The possible file names.
1380 character(len=:), allocatable :: dir ! The directory to read from.
1381 character(len=:), allocatable :: fname ! The list of file names.
1382 logical, allocatable, dimension(:) :: global_file ! True if the file is global
1383 integer :: n, num_files
1384
1385 dir = "./INPUT/" ; if (present(directory)) dir = trim(directory)
1386
1387 ! Set the default return values.
1388 found = .false.
1389 file_path = ""
1390 if (present(is_global)) is_global = .false.
1391
1392 fname = 'r'
1393 if (present(filename)) then
1394 if (.not.((len_trim(filename) == 1) .and. (filename(1:1) == 'F'))) fname = filename
1395 endif
1396
1397 num_files = get_num_restart_files(fname, dir, g, cs)
1398 if (num_files == 0) return
1399 allocate(file_paths(num_files), global_file(num_files))
1400 num_files = open_restart_units(fname, dir, g, cs, file_paths=file_paths, global_files=global_file)
1401
1402 do n=1,num_files ; if (field_exists(file_paths(n), varname, mom_domain=g%domain)) then
1403 found = .true.
1404 file_path = file_paths(n)
1405 if (present(is_global)) is_global = global_file(n)
1406 exit
1407 endif ; enddo
1408
1409 deallocate(file_paths, global_file)
1410
1411end function find_var_in_restart_files
1412
1413!====================== end of the only_read_from_restarts variants =======================
1414
1415
1416!> Copy the restart variable with the specified name into a 3-d array, perhaps after rotation
1417subroutine copy_restart_var_3d(var, name, CS, unrotate)
1418 real, dimension(:,:,:), intent(inout) :: var !< The field that is being copied [arbitrary]
1419 character(len=*), intent(in) :: name !< The name of the field that is being copied
1420 type(mom_restart_cs), intent(in) :: CS !< MOM restart control struct
1421 logical, optional, intent(in) :: unrotate !< If present and true, the output is on an unrotated grid.
1422
1423 logical :: keep_rotation
1424 character(len=256) :: size_msg !< The array sizes
1425 integer :: m, n
1426
1427 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1428 "query_initialized: Module must be initialized before it is used.")
1429
1430 if (cs%novars > cs%max_fields) call restart_error(cs)
1431
1432 keep_rotation = .true. ; if (present(unrotate)) keep_rotation = .not.unrotate
1433
1434 n = cs%novars+1
1435 do m=1,cs%novars
1436 if (trim(name) == cs%restart_field(m)%var_name) then
1437 if (.not.associated(cs%var_ptr3d(m)%p)) then
1438 call mom_error(fatal, "MOM_restart: copy_restart_var(_3d) "//&
1439 "attempted to copy restart variable "//name//" with the wrong rank.")
1440 elseif (cs%restart_field(m)%initialized) then
1441 if (cs%turns == 0 .or. keep_rotation) then
1442 if ( size_mismatch_3d(var, cs%var_ptr3d(m)%p, cs%turns, size_msg) ) &
1443 call mom_error(fatal, "MOM_restart: copy_restart_var(_3d) "//&
1444 "attempted to copy restart variable "//name//" with the wrong sizes, "//trim(size_msg))
1445
1446 var(:,:,:) = cs%var_ptr3d(m)%p(:,:,:)
1447 else
1448 call rotate_array(cs%var_ptr3d(m)%p, -cs%turns, var)
1449 endif
1450 else
1451 call mom_error(note, "MOM_restart: copy_restart_var(_3d) "//&
1452 "attempted to copy uninitialized restart variable "//name//".")
1453 endif
1454 n = m ; exit
1455 endif
1456 enddo
1457 if ((n==cs%novars+1) .and. (is_root_pe())) &
1458 call mom_error(note, "MOM_restart: copy_restart_var(_3d) "//&
1459 "attempted to copy unknown restart variable "//name//".")
1460
1461end subroutine copy_restart_var_3d
1462
1463
1464!> Copy the restart vector component variables with the specified names into a pair
1465!! of 3-d arrays, perhaps after rotation
1466subroutine copy_restart_vector_3d(u_var, v_var, u_name, v_name, CS, unrotate, scalar_pair)
1467 real, dimension(:,:,:), intent(inout) :: u_var !< The u-component of the field that is being copied [arbitrary]
1468 real, dimension(:,:,:), intent(inout) :: v_var !< The u-component of the field that is being copied [arbitrary]
1469 character(len=*), intent(in) :: u_name !< The name of the u-component of the field that is being copied
1470 character(len=*), intent(in) :: v_name !< The name of the v-component of the field that is being copied
1471 type(mom_restart_cs), intent(in) :: CS !< MOM restart control struct
1472 logical, optional, intent(in) :: unrotate !< If present and true, the output is on an unrotated grid.
1473 logical, optional, intent(in) :: scalar_pair !< If true, the arrays describe a pair of
1474 !! scalars, instead of vector components
1475 !! whose signs change when rotated
1476
1477 logical :: keep_rotation, scalars
1478 character(len=256) :: size_msg !< The array sizes
1479 integer :: m, n_u, n_v
1480
1481 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1482 "query_initialized: Module must be initialized before it is used.")
1483
1484 if (cs%novars > cs%max_fields) call restart_error(cs)
1485
1486 keep_rotation = .true. ; if (present(unrotate)) keep_rotation = .not.unrotate
1487
1488 n_u = cs%novars+1 ; n_v = cs%novars+1
1489 do m=1,cs%novars
1490 if (trim(u_name) == cs%restart_field(m)%var_name) then
1491 if (.not.associated(cs%var_ptr3d(m)%p)) then
1492 call mom_error(fatal, "MOM_restart: copy_restart_vector(_3d) "//&
1493 "attempted to copy restart variable "//trim(u_name)//" with the wrong rank.")
1494 elseif (cs%restart_field(m)%initialized) then
1495 n_u = m
1496 else
1497 call mom_error(note, "MOM_restart: copy_restart_vector(_3d) "//&
1498 "attempted to copy uninitialized restart variable "//trim(u_name)//".")
1499 n_u = -1
1500 endif
1501 endif
1502 if (trim(v_name) == cs%restart_field(m)%var_name) then
1503 if (.not.associated(cs%var_ptr3d(m)%p)) then
1504 call mom_error(fatal, "MOM_restart: copy_restart_vector(_3d) "//&
1505 "attempted to copy restart variable "//trim(v_name)//" with the wrong rank.")
1506 elseif (cs%restart_field(m)%initialized) then
1507 n_v = m
1508 else
1509 call mom_error(note, "MOM_restart: copy_restart_vector(_3d) "//&
1510 "attempted to copy uninitialized restart variable "//trim(v_name)//".")
1511 n_v = -1
1512 endif
1513 endif
1514 enddo
1515 if ((n_u==cs%novars+1) .and. (is_root_pe())) &
1516 call mom_error(note, "MOM_restart: copy_restart_vector(_3d) "//&
1517 "attempted to copy unknown restart variable "//trim(u_name)//".")
1518 if ((n_v==cs%novars+1) .and. (is_root_pe())) &
1519 call mom_error(note, "MOM_restart: copy_restart_vector(_3d) "//&
1520 "attempted to copy unknown restart variable "//trim(v_name)//".")
1521
1522 if ((n_u>0) .and. (n_u<=cs%novars) .and. (n_v>0) .and. (n_v<=cs%novars)) then
1523 ! Now actually update the vector.
1524 if ( size_mismatch_3d(u_var, cs%var_ptr3d(n_u)%p, cs%turns, size_msg) ) &
1525 call mom_error(fatal, "MOM_restart: copy_restart_vector(_3d) "//&
1526 "attempted to copy restart variable "//trim(u_name)//" with the wrong sizes, "//trim(size_msg))
1527 if ( size_mismatch_3d(v_var, cs%var_ptr3d(n_v)%p, cs%turns, size_msg) ) &
1528 call mom_error(fatal, "MOM_restart: copy_restart_vector(_3d) "//&
1529 "attempted to copy restart variable "//trim(v_name)//" with the wrong sizes, "//trim(size_msg))
1530
1531 if (cs%turns == 0 .or. keep_rotation) then
1532 u_var(:,:,:) = cs%var_ptr3d(n_u)%p(:,:,:)
1533 v_var(:,:,:) = cs%var_ptr3d(n_v)%p(:,:,:)
1534 else
1535 scalars = .false. ; if (present(scalar_pair)) scalars = scalar_pair
1536 if ((modulo(cs%turns, 2) == 0) .and. scalars) then
1537 call rotate_array_pair(cs%var_ptr3d(n_u)%p, cs%var_ptr3d(n_v)%p, -cs%turns, u_var, v_var)
1538 elseif (modulo(cs%turns, 2) == 0) then
1539 call rotate_vector(cs%var_ptr3d(n_u)%p, cs%var_ptr3d(n_v)%p, -cs%turns, u_var, v_var)
1540 elseif (scalars) then ! This is less common
1541 call rotate_array_pair(cs%var_ptr3d(n_v)%p, cs%var_ptr3d(n_u)%p, -cs%turns, u_var, v_var)
1542 else
1543 call rotate_vector(cs%var_ptr3d(n_v)%p, cs%var_ptr3d(n_u)%p, -cs%turns, u_var, v_var)
1544 endif
1545 endif
1546 endif
1547
1548end subroutine copy_restart_vector_3d
1549
1550!> Indicate if two 3-d arrays are not of the same size after rotation is considered.
1551logical function size_mismatch_3d(var_a, var_b, turns, size_msg)
1552 real, intent(in) :: var_a(:,:,:) !< The first field being compared
1553 real, intent(in) :: var_b(:,:,:) !< The second field being compared
1554 integer, intent(in) :: turns !< Number of quarter turns from input to model domain
1555 character(len=256), intent(out) :: size_msg !< The array sizes
1556
1557 if (modulo(turns, 2) == 0) then
1558 size_mismatch_3d = ( (size(var_a,1) /= size(var_b,1)) .or. &
1559 (size(var_a,2) /= size(var_b,2)) .or. &
1560 (size(var_a,3) /= size(var_b,3)) )
1561 else
1562 size_mismatch_3d = ( (size(var_a,1) /= size(var_b,2)) .or. &
1563 (size(var_a,2) /= size(var_b,1)) .or. &
1564 (size(var_a,3) /= size(var_b,3)) )
1565 endif
1566 write(size_msg, '(3(1x,I0), " vs ", 3(1x,I0))') size(var_a,1), size(var_a,2), size(var_a,3), &
1567 size(var_b,1), size(var_b,2), size(var_b,3)
1568end function size_mismatch_3d
1569
1570
1571!> save_restart saves all registered variables to restart files.
1572subroutine save_restart(directory, time, G, CS, time_stamped, filename, GV, num_rest_files, write_IC)
1573 character(len=*), intent(in) :: directory !< The directory where the restart files
1574 !! are to be written
1575 type(time_type), intent(in) :: time !< The current model time
1576 type(ocean_grid_type), intent(inout) :: g !< The ocean's grid structure as seen from the driver.
1577 type(mom_restart_cs), intent(inout) :: cs !< MOM restart control struct
1578 logical, optional, intent(in) :: time_stamped !< If present and true, add time-stamp
1579 !! to the restart file names
1580 character(len=*), optional, intent(in) :: filename !< A filename that overrides the name in CS%restartfile
1581 type(verticalgrid_type), &
1582 optional, intent(in) :: gv !< The ocean's vertical grid structure
1583 integer, optional, intent(out) :: num_rest_files !< number of restart files written
1584 logical, optional, intent(in) :: write_ic !< If present and true, initial conditions
1585 !! are being written
1586
1587 ! Local variables
1588 type(vardesc) :: vars(cs%max_fields) ! Descriptions of the fields that
1589 ! are to be read from the restart file.
1590 type(mom_field) :: fields(cs%max_fields) ! Opaque types containing metadata describing
1591 ! each variable that will be written.
1592 character(len=512) :: restartpath ! The restart file path (dir/file).
1593 character(len=256) :: restartname ! The restart file name (no dir).
1594 character(len=8) :: suffix ! A suffix (like _2) that is appended
1595 ! to the name of files after the first.
1596 integer(kind=int64) :: var_sz, size_in_file ! The size in bytes of each variable
1597 ! and the variables already in a file.
1598 integer(kind=int64), parameter :: max_file_size = 4294967292_int64 ! The maximum size in bytes for the
1599 ! starting position of each variable in a file's record,
1600 ! based on the use of NetCDF 3.6 or later. For earlier
1601 ! versions of NetCDF, the value was 2147483647_int64.
1602 integer :: start_var, next_var ! The starting variables of the
1603 ! current and next files.
1604 type(mom_infra_file) :: io_handle ! The I/O handle of the open fileset
1605 integer :: m, nz, na
1606 integer :: num_files ! The number of restart files that will be used.
1607 integer :: seconds, days, year, month, hour, minute
1608 character(len=8) :: z_grid, t_grid ! Variable grid info.
1609 integer :: pos ! A coded integer indicating the horizontal staggering of a variable
1610 real :: conv ! Shorthand for the conversion factor [a A-1 ~> 1]
1611 real :: restart_time ! The model time at whic the restart file is being written [days]
1612 character(len=32) :: filename_appendix = '' ! Appendix to filename for ensemble runs
1613 integer :: length ! The length of a text string.
1614 character(len=256) :: mesg, var_name
1615 integer(kind=int64) :: check_val(cs%max_fields,1)
1616 logical :: verbose
1617 integer :: isl, iel, jsl, jel
1618 integer :: turns ! Number of quarter turns from input to model domain
1619 integer, parameter :: nmax_extradims = 5
1620 type(axis_info), dimension(:), allocatable :: extra_axes
1621
1622 turns = cs%turns
1623
1624 allocate (extra_axes(nmax_extradims))
1625
1626 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1627 "save_restart: Module must be initialized before it is used.")
1628
1629 if (cs%novars > cs%max_fields) call restart_error(cs)
1630 verbose = (is_root_pe() .and. (mom_get_verbosity() >= 7))
1631
1632 ! With parallel read & write, it is possible to disable the following...
1633 num_files = 0
1634 next_var = 0
1635 nz = 1 ; if (present(gv)) nz = gv%ke
1636
1637 restart_time = time_type_to_real(time) / 86400.0
1638
1639 restartname = trim(cs%restartfile)
1640 if (present(filename)) restartname = trim(filename)
1641 if (PRESENT(time_stamped)) then ; if (time_stamped) then
1642 call get_date(time, year, month, days, hour, minute, seconds)
1643 ! Compute the year-day, because I don't like months. - RWH
1644 do m=1,month-1
1645 days = days + days_in_month(set_date(year, m, 2, 0, 0, 0))
1646 enddo
1647 seconds = seconds + 60*minute + 3600*hour
1648 if (year <= 9999) then
1649 write(restartname,'("_Y",I4.4,"_D",I3.3,"_S",I5.5)') year, days, seconds
1650 elseif (year <= 99999) then
1651 write(restartname,'("_Y",I5.5,"_D",I3.3,"_S",I5.5)') year, days, seconds
1652 else
1653 write(restartname,'("_Y",I10.10,"_D",I3.3,"_S",I5.5)') year, days, seconds
1654 endif
1655 restartname = trim(cs%restartfile)//trim(restartname)
1656 endif ; endif
1657
1658 ! Determine if there is a filename_appendix (used for ensemble runs).
1659 call insert_ensemble_appendix(restartname, cs%ensemble_appendix_prefix)
1660
1661 next_var = 1
1662 do while (next_var <= cs%novars )
1663 start_var = next_var
1664 size_in_file = 8*(2*g%Domain%niglobal+2*g%Domain%njglobal+2*nz+1000)
1665
1666 do m=start_var,cs%novars
1667 call query_vardesc(cs%restart_field(m)%vars, position=pos, &
1668 z_grid=z_grid, t_grid=t_grid, caller="save_restart", &
1669 extra_axes=extra_axes)
1670
1671 var_sz = get_variable_byte_size(pos, z_grid, t_grid, g, nz)
1672 ! factor in size of extra axes, or multiply by 1
1673 do na=1,nmax_extradims
1674 var_sz = var_sz*extra_axes(na)%ax_size
1675 enddo
1676
1677 if ((m==start_var) .OR. (size_in_file < max_file_size-var_sz)) then
1678 size_in_file = size_in_file + var_sz
1679 else ; exit
1680 endif
1681
1682 enddo
1683 next_var = m
1684
1685 restartpath = trim(directory) // trim(restartname)
1686
1687 write(suffix,'("_",I0)') num_files
1688
1689 length = len_trim(restartpath)
1690 if (length < 3) then ! This case is very uncommon but this test avoids segmentation-faults.
1691 if (num_files > 0) restartpath = trim(restartpath) // suffix
1692 restartpath = trim(restartpath)//".nc"
1693 elseif (restartpath(length-2:length) == ".nc") then
1694 if (num_files > 0) restartpath = restartpath(1:length-3)//trim(suffix)//".nc"
1695 else
1696 if (num_files > 0) restartpath = trim(restartpath) // suffix
1697 restartpath = trim(restartpath)//".nc"
1698 endif
1699
1700 do m=start_var,next_var-1
1701 vars(m-start_var+1) = cs%restart_field(m)%vars
1702 enddo
1703 call query_vardesc(vars(1), t_grid=t_grid, position=pos, caller="save_restart")
1704 t_grid = adjustl(t_grid)
1705 if (t_grid(1:1) /= 'p') &
1706 call modify_vardesc(vars(1), t_grid='s', caller="save_restart")
1707
1708 !Prepare the checksum of the restart fields to be written to restart files
1709 do m=start_var,next_var-1
1710
1711 call query_vardesc(vars(m), position=pos, name=var_name, caller="save_restart")
1712 if (modulo(turns, 2) == 0) then
1713 call get_checksum_loop_ranges(g, cs, pos, isl, iel, jsl, jel)
1714 else ! Note that G is always the unrotated grid as it is seen by the driver level.
1715 call get_checksum_loop_ranges(g, cs, pos, jsl, jel, isl, iel)
1716 endif
1717 if (verbose) then
1718 if (pos == center) then
1719 write(mesg, '(" is in CENTER position, checksum range",4(1x,I0))') isl, iel, jsl, jel
1720 elseif (pos == corner) then
1721 write(mesg, '(" is in CORNER position, checksum range",4(1x,I0))') isl, iel, jsl, jel
1722 elseif (pos == north_face) then
1723 write(mesg, '(" is in NORTH_FACE position, checksum range",4(1x,I0))') isl, iel, jsl, jel
1724 elseif (pos == east_face) then
1725 write(mesg, '(" is in EAST_FACE position, checksum range",4(1x,I0))') isl, iel, jsl, jel
1726 else
1727 write(mesg, '(" is in another position, ",I0,", checksum range",4(1x,I0))') pos, isl, iel, jsl, jel
1728 endif
1729 call mom_mesg(trim(var_name)//mesg)
1730 endif
1731
1732 conv = cs%restart_field(m)%conv
1733 if (associated(cs%var_ptr3d(m)%p)) then
1734 check_val(m-start_var+1,1) = chksum(cs%var_ptr3d(m)%p(isl:iel,jsl:jel,:), turns=-turns, unscale=conv)
1735 elseif (associated(cs%var_ptr2d(m)%p)) then
1736 check_val(m-start_var+1,1) = chksum(cs%var_ptr2d(m)%p(isl:iel,jsl:jel), turns=-turns, unscale=conv)
1737 elseif (associated(cs%var_ptr4d(m)%p)) then
1738 check_val(m-start_var+1,1) = chksum(cs%var_ptr4d(m)%p(isl:iel,jsl:jel,:,:), turns=-turns, unscale=conv)
1739 elseif (associated(cs%var_ptr1d(m)%p)) then
1740 check_val(m-start_var+1,1) = chksum(cs%var_ptr1d(m)%p(:), unscale=conv)
1741 elseif (associated(cs%var_ptr0d(m)%p)) then
1742 check_val(m-start_var+1,1) = chksum(cs%var_ptr0d(m)%p, pelist=(/pe_here()/), unscale=conv)
1743 endif
1744 enddo
1745
1746 if (cs%parallel_restartfiles) then
1747 call create_mom_file(io_handle, trim(restartpath), vars, next_var-start_var, &
1748 fields, multiple, g=g, gv=gv, checksums=check_val, extra_axes=extra_axes)
1749 else
1750 call create_mom_file(io_handle, trim(restartpath), vars, next_var-start_var, &
1751 fields, single_file, g=g, gv=gv, checksums=check_val, extra_axes=extra_axes)
1752 endif
1753
1754 do m=start_var,next_var-1
1755 if (associated(cs%var_ptr3d(m)%p)) then
1756 call mom_write_field(io_handle, fields(m-start_var+1), g%Domain, cs%var_ptr3d(m)%p, &
1757 restart_time, unscale=cs%restart_field(m)%conv, turns=-turns, &
1758 zero_zeros=cs%unsigned_zeros)
1759 elseif (associated(cs%var_ptr2d(m)%p)) then
1760 call mom_write_field(io_handle, fields(m-start_var+1), g%Domain, cs%var_ptr2d(m)%p, &
1761 restart_time, unscale=cs%restart_field(m)%conv, turns=-turns, &
1762 zero_zeros=cs%unsigned_zeros)
1763 elseif (associated(cs%var_ptr4d(m)%p)) then
1764 call mom_write_field(io_handle, fields(m-start_var+1), g%Domain, cs%var_ptr4d(m)%p, &
1765 restart_time, unscale=cs%restart_field(m)%conv, turns=-turns, &
1766 zero_zeros=cs%unsigned_zeros)
1767 elseif (associated(cs%var_ptr1d(m)%p)) then
1768 call mom_write_field(io_handle, fields(m-start_var+1), cs%var_ptr1d(m)%p, &
1769 restart_time, unscale=cs%restart_field(m)%conv, &
1770 zero_zeros=cs%unsigned_zeros)
1771 elseif (associated(cs%var_ptr0d(m)%p)) then
1772 call mom_write_field(io_handle, fields(m-start_var+1), cs%var_ptr0d(m)%p, &
1773 restart_time, unscale=cs%restart_field(m)%conv, &
1774 zero_zeros=cs%unsigned_zeros)
1775 endif
1776 enddo
1777
1778 call io_handle%close()
1779
1780 num_files = num_files+1
1781
1782 enddo
1783
1784 if (present(num_rest_files)) num_rest_files = num_files
1785
1786end subroutine save_restart
1787
1788!> restore_state reads the model state from previously generated files. All
1789!! restart variables are read from the first file in the input filename list
1790!! in which they are found.
1791subroutine restore_state(filename, directory, day, G, CS)
1792 character(len=*), intent(in) :: filename !< The list of restart file names or a single
1793 !! character 'r' to read automatically named files
1794 character(len=*), intent(in) :: directory !< The directory in which to find restart files
1795 type(time_type), intent(out) :: day !< The time of the restarted run
1796 type(ocean_grid_type), intent(in) :: g !< The ocean's grid structure
1797 type(mom_restart_cs), intent(inout) :: cs !< MOM restart control struct
1798
1799 ! Local variables
1800 real :: scale ! A scaling factor for reading a field [A a-1 ~> 1] to convert
1801 ! from the units in the file to the internal units of this field
1802 real :: conv ! The output conversion factor for writing a field [a A-1 ~> 1]
1803 character(len=512) :: mesg ! A message for warnings.
1804 character(len=80) :: varname ! A variable's name.
1805 integer :: num_file ! The number of files (restart files and others
1806 ! explicitly in filename) that are open.
1807 integer :: i, n, m, missing_fields
1808 integer :: isl, iel, jsl, jel
1809 integer :: nvar, ntime, pos
1810
1811 type(mom_infra_file) :: io_handles(cs%max_fields) ! The I/O units of all open files.
1812 character(len=200) :: unit_path(cs%max_fields) ! The file names.
1813 logical :: unit_is_global(cs%max_fields) ! True if the file is global.
1814
1815 real :: t1, t2 ! Two times from the start of different files [days].
1816 real, allocatable :: time_vals(:) ! Times from a file extracted with getl_file_times [days]
1817 type(mom_field), allocatable :: fields(:)
1818 logical :: is_there_a_checksum ! Is there a valid checksum that should be checked.
1819 integer(kind=int64) :: checksum_file ! The checksum value recorded in the input file.
1820 integer(kind=int64) :: checksum_data ! The checksum value for the data that was read in.
1821
1822 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
1823 "restore_state: Module must be initialized before it is used.")
1824
1825 if (cs%novars > cs%max_fields) call restart_error(cs)
1826
1827 ! Get NetCDF ids for all of the restart files.
1828 if ((len_trim(filename) == 1) .and. (filename(1:1) == 'F')) then
1829 num_file = open_restart_units('r', directory, g, cs, io_handles=io_handles, &
1830 file_paths=unit_path, global_files=unit_is_global)
1831 else
1832 num_file = open_restart_units(filename, directory, g, cs, io_handles=io_handles, &
1833 file_paths=unit_path, global_files=unit_is_global)
1834 endif
1835
1836 if (num_file == 0) then
1837 write(mesg,'("Unable to find any restart files specified by ",A," in directory ",A,".")') &
1838 trim(filename), trim(directory)
1839 call mom_error(fatal,"MOM_restart: "//mesg)
1840 endif
1841
1842 ! Get the time from the first file in the list that has one.
1843 do n=1,num_file
1844 call io_handles(n)%get_file_times(time_vals, ntime)
1845 if (ntime < 1) cycle
1846
1847 t1 = time_vals(1)
1848 deallocate(time_vals)
1849
1850 day = real_to_time(t1*86400.0)
1851 exit
1852 enddo
1853
1854 if (n>num_file) call mom_error(warning, "MOM_restart: No times found in restart files.")
1855
1856 ! Check the remaining files for different times and issue a warning
1857 ! if they differ from the first time.
1858 do m = n+1,num_file
1859 call io_handles(n)%get_file_times(time_vals, ntime)
1860 if (ntime < 1) cycle
1861
1862 t2 = time_vals(1)
1863 deallocate(time_vals)
1864
1865 if (t1 /= t2 .and. is_root_pe()) then
1866 write(mesg,'("WARNING: Restart file ",I0," has time ",F10.4,"whereas &
1867 &simulation is restarted at ",F10.4," (differing by ",F10.4,").")') &
1868 m, t1, t2, t1-t2
1869 call mom_error(warning, "MOM_restart: "//mesg)
1870 endif
1871 enddo
1872
1873 ! Read each variable from the first file in which it is found.
1874 do n=1,num_file
1875 call io_handles(n)%get_file_info(nvar=nvar)
1876
1877 allocate(fields(nvar))
1878 call io_handles(n)%get_file_fields(fields(1:nvar))
1879
1880 do m=1, nvar
1881 call io_handles(n)%get_field_atts(fields(m), name=varname)
1882 do i=1,cs%num_obsolete_vars
1883 if (adjustl(lowercase(trim(varname))) == adjustl(lowercase(trim(cs%restart_obsolete(i)%field_name)))) then
1884 call mom_error(fatal, "MOM_restart restore_state: Attempting to use obsolete restart field "//&
1885 trim(varname)//" - the new corresponding restart field is "//&
1886 trim(cs%restart_obsolete(i)%replacement_name))
1887 endif
1888 enddo
1889 enddo
1890
1891 missing_fields = 0
1892
1893 do m=1,cs%novars
1894 if (cs%restart_field(m)%initialized) cycle
1895 call query_vardesc(cs%restart_field(m)%vars, position=pos, caller="restore_state")
1896 conv = cs%restart_field(m)%conv
1897 if (conv == 0.0) then ; scale = 1.0 ; else ; scale = 1.0 / conv ; endif
1898
1899 if (modulo(cs%turns, 2) == 0) then
1900 call get_checksum_loop_ranges(g, cs, pos, isl, iel, jsl, jel)
1901 else ! Note that G is always the unrotated grid as it is used during initialization.
1902 call get_checksum_loop_ranges(g, cs, pos, jsl, jel, isl, iel)
1903 endif
1904 do i=1, nvar
1905 call io_handles(n)%get_field_atts(fields(i), name=varname)
1907 checksum_data = -1
1908 if (cs%checksum_required) then
1909 call io_handles(n)%read_field_chksum(fields(i), checksum_file, is_there_a_checksum)
1910 else
1911 checksum_file = -1
1912 is_there_a_checksum = .false. ! Do not need to do data checksumming.
1913 endif
1914
1915 if (associated(cs%var_ptr1d(m)%p)) then
1916 ! Read a 1d array, which should be invariant to domain decomposition.
1917 call mom_read_data(unit_path(n), varname, cs%var_ptr1d(m)%p, &
1918 timelevel=1, scale=scale, mom_domain=g%Domain)
1919 if (is_there_a_checksum) checksum_data = chksum(cs%var_ptr1d(m)%p(:), unscale=conv)
1920 elseif (associated(cs%var_ptr0d(m)%p)) then ! Read a scalar...
1921 call mom_read_data(unit_path(n), varname, cs%var_ptr0d(m)%p, &
1922 timelevel=1, scale=scale, mom_domain=g%Domain)
1923 if (is_there_a_checksum) checksum_data = chksum(cs%var_ptr0d(m)%p, pelist=(/pe_here()/), unscale=conv)
1924 elseif (associated(cs%var_ptr2d(m)%p)) then ! Read a 2d array.
1925 if (pos /= 0) then
1926 call mom_read_data(unit_path(n), varname, cs%var_ptr2d(m)%p, &
1927 g%Domain, timelevel=1, position=pos, scale=scale, turns=cs%turns)
1928 else ! This array is not domain-decomposed. This variant may be under-tested.
1929 call mom_error(fatal, &
1930 "MOM_restart does not support 2-d arrays without domain decomposition.")
1931 ! call read_data(unit_path(n), varname, CS%var_ptr2d(m)%p,no_domain=.true., timelevel=1)
1932 endif
1933 if (is_there_a_checksum) checksum_data = chksum(cs%var_ptr2d(m)%p(isl:iel,jsl:jel), unscale=conv)
1934 elseif (associated(cs%var_ptr3d(m)%p)) then ! Read a 3d array.
1935 if (pos /= 0) then
1936 call mom_read_data(unit_path(n), varname, cs%var_ptr3d(m)%p, &
1937 g%Domain, timelevel=1, position=pos, scale=scale, turns=cs%turns)
1938 else ! This array is not domain-decomposed. This variant may be under-tested.
1939 call mom_error(fatal, &
1940 "MOM_restart does not support 3-d arrays without domain decomposition.")
1941 ! call read_data(unit_path(n), varname, CS%var_ptr3d(m)%p, no_domain=.true., timelevel=1)
1942 endif
1943 if (is_there_a_checksum) checksum_data = chksum(cs%var_ptr3d(m)%p(isl:iel,jsl:jel,:), unscale=conv)
1944 elseif (associated(cs%var_ptr4d(m)%p)) then ! Read a 4d array.
1945 if (pos /= 0) then
1946 call mom_read_data(unit_path(n), varname, cs%var_ptr4d(m)%p, &
1947 g%Domain, timelevel=1, position=pos, scale=scale, &
1948 global_file=unit_is_global(n), turns=cs%turns)
1949 else ! This array is not domain-decomposed. This variant may be under-tested.
1950 call mom_error(fatal, &
1951 "MOM_restart does not support 4-d arrays without domain decomposition.")
1952 ! call read_data(unit_path(n), varname, CS%var_ptr4d(m)%p, no_domain=.true., timelevel=1)
1953 endif
1954 if (is_there_a_checksum) checksum_data = chksum(cs%var_ptr4d(m)%p(isl:iel,jsl:jel,:,:), unscale=conv)
1955 else
1956 call mom_error(fatal, "MOM_restart restore_state: No pointers set for "//trim(varname))
1957 endif
1958
1959 if (is_root_pe() .and. is_there_a_checksum .and. (checksum_file /= checksum_data)) then
1960 write (mesg,'(a,Z16,a,Z16,a)') "Checksum of input field "// trim(varname)//" ",checksum_data,&
1961 " does not match value ", checksum_file, &
1962 " stored in "//trim(unit_path(n)//"." )
1963 call mom_error(fatal, "MOM_restart(restore_state): "//trim(mesg) )
1964 endif
1965
1966 cs%restart_field(m)%initialized = .true.
1967 exit ! Start search for next restart variable.
1968 endif
1969 enddo
1970 if (i>nvar) missing_fields = missing_fields+1
1971 enddo
1972
1973 deallocate(fields)
1974 if (missing_fields == 0) exit
1975 enddo
1976
1977 do n=1,num_file
1978 call io_handles(n)%close()
1979 enddo
1980
1981 ! Check whether any mandatory fields have not been found.
1982 cs%restart = .true.
1983 do m=1,cs%novars
1984 if (.not.(cs%restart_field(m)%initialized)) then
1985 cs%restart = .false.
1986 if (cs%restart_field(m)%mand_var) then
1987 call mom_error(fatal,"MOM_restart: Unable to find mandatory variable " &
1988 //trim(cs%restart_field(m)%var_name)//" in restart files.")
1989 endif
1990 endif
1991 enddo
1992
1993 ! Lock the restart registry so that no further variables can be registered.
1994 cs%locked = .true.
1995
1996end subroutine restore_state
1997
1998
1999
2000!> restart_files_exist determines whether any restart files exist.
2001function restart_files_exist(filename, directory, G, CS)
2002 character(len=*), intent(in) :: filename !< The list of restart file names or a single
2003 !! character 'r' to read automatically named files
2004 character(len=*), intent(in) :: directory !< The directory in which to find restart files
2005 type(ocean_grid_type), intent(in) :: g !< The ocean's grid structure
2006 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
2007 logical :: restart_files_exist !< The function result, which indicates whether
2008 !! any of the explicitly or automatically named
2009 !! restart files exist in directory
2010 integer :: num_files
2011
2012 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
2013 "restart_files_exist: Module must be initialized before it is used.")
2014
2015 if ((len_trim(filename) == 1) .and. (filename(1:1) == 'F')) then
2016 num_files = get_num_restart_files('r', directory, g, cs)
2017 else
2018 num_files = get_num_restart_files(filename, directory, g, cs)
2019 endif
2020 restart_files_exist = (num_files > 0)
2021end function restart_files_exist
2022
2023!> determine_is_new_run determines from the value of filename and the existence
2024!! automatically named restart files in directory whether this would be a new,
2025!! and as a side effect stores this information in CS.
2026function determine_is_new_run(filename, directory, G, CS) result(is_new_run)
2027 character(len=*), intent(in) :: filename !< The list of restart file names or a single
2028 !! character 'r' to read automatically named files
2029 character(len=*), intent(in) :: directory !< The directory in which to find restart files
2030 type(ocean_grid_type), intent(in) :: g !< The ocean's grid structure
2031 type(mom_restart_cs), intent(inout) :: cs !< MOM restart control struct
2032 logical :: is_new_run !< The function result, which indicates whether
2033 !! this is a new run, based on the value of
2034 !! filename and whether restart files exist
2035
2036 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
2037 "determine_is_new_run: Module must be initialized before it is used.")
2038
2039 if (len_trim(filename) > 1) then
2040 cs%new_run = .false.
2041 elseif (len_trim(filename) == 0) then
2042 cs%new_run = .true.
2043 elseif (filename(1:1) == 'n') then
2044 cs%new_run = .true.
2045 elseif (filename(1:1) == 'F') then
2046 cs%new_run = (get_num_restart_files('r', directory, g, cs) == 0)
2047 else
2048 cs%new_run = .false.
2049 endif
2050
2051 cs%new_run_set = .true.
2052 is_new_run = cs%new_run
2053end function determine_is_new_run
2054
2055!> is_new_run returns whether this is going to be a new run based on the
2056!! information stored in CS by a previous call to determine_is_new_run.
2057function is_new_run(CS)
2058 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
2059
2060 logical :: is_new_run !< The function result, which had been stored in CS during
2061 !! a previous call to determine_is_new_run
2062
2063 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
2064 "is_new_run: Module must be initialized before it is used.")
2065
2066 if (.not.cs%new_run_set) call mom_error(fatal, "MOM_restart " // &
2067 "determine_is_new_run must be called for a restart file before is_new_run.")
2068
2069 is_new_run = cs%new_run
2070end function is_new_run
2071
2072!> open_restart_units determines the number of existing restart files and optionally opens
2073!! them and returns unit ids, paths and whether the files are global or spatially decomposed.
2074function open_restart_units(filename, directory, G, CS, IO_handles, file_paths, &
2075 global_files) result(num_files)
2076 character(len=*), intent(in) :: filename !< The list of restart file names or a single
2077 !! character 'r' to read automatically named files
2078 character(len=*), intent(in) :: directory !< The directory in which to find restart files
2079 type(ocean_grid_type), intent(in) :: g !< The ocean's grid structure
2080 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
2081
2082 type(mom_infra_file), dimension(:), &
2083 optional, intent(out) :: io_handles !< The I/O handles of all opened files
2084 character(len=*), dimension(:), &
2085 optional, intent(out) :: file_paths !< The full paths to open files
2086 logical, dimension(:), &
2087 optional, intent(out) :: global_files !< True if a file is global
2088
2089 integer :: num_files !< The number of files (both automatically named restart
2090 !! files and others explicitly in filename) that have been opened.
2091
2092 ! Local variables
2093 character(len=256) :: filepath ! The path (dir/file) to the file being opened.
2094 character(len=256) :: fname ! The name of the current file.
2095 character(len=8) :: suffix ! A suffix (like "_2") that is added to any
2096 ! additional restart files.
2097 integer :: num_restart ! The number of restart files that have already
2098 ! been opened using their numbered suffix.
2099 integer :: start_char ! The location of the starting character in the
2100 ! current file name.
2101 integer :: nf ! The number of files that have been found so far
2102 integer :: m, length
2103 logical :: still_looking ! If true, the code is still looking for automatically named files
2104 logical :: fexists ! True if a file has been found
2105 character(len=32) :: filename_appendix = '' ! Filename appendix for ensemble runs
2106 character(len=80) :: restartname
2107
2108 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
2109 "open_restart_units: Module must be initialized before it is used.")
2110
2111 ! Get NetCDF ids for all of the restart files.
2112 num_restart = 0 ; nf = 0 ; start_char = 1
2113 do while (start_char <= len_trim(filename) )
2114 do m=start_char,len_trim(filename)
2115 if (filename(m:m) == ' ') exit
2116 enddo
2117 fname = filename(start_char:m-1)
2118 start_char = m
2119 do while (start_char <= len_trim(filename))
2120 if (filename(start_char:start_char) == ' ') then
2121 start_char = start_char + 1
2122 else
2123 exit
2124 endif
2125 enddo
2126
2127 if ((fname(1:1)=='r') .and. ( len_trim(fname) == 1)) then
2128 still_looking = (num_restart <= 0) ! Avoid going through the file list twice.
2129 do while (still_looking)
2130 restartname = trim(cs%restartfile)
2131 call insert_ensemble_appendix(restartname, cs%ensemble_appendix_prefix)
2132 filepath = trim(directory) // trim(restartname)
2133
2134 write(suffix,'("_",I0)') num_restart
2135 if (num_restart > 0) filepath = trim(filepath) // suffix
2136
2137 filepath = trim(filepath)//".nc"
2138
2139 num_restart = num_restart + 1
2140 ! Look for a global netCDF file.
2141 inquire(file=filepath, exist=fexists)
2142 if (fexists) then
2143 nf = nf + 1
2144 if (present(io_handles)) &
2145 call io_handles(nf)%open(trim(filepath), readonly_file, &
2146 mom_domain=g%Domain, threading=multiple, fileset=single_file)
2147 if (present(global_files)) global_files(nf) = .true.
2148 if (present(file_paths)) file_paths(nf) = filepath
2149 elseif (cs%parallel_restartfiles) then
2150 ! Look for decomposed files using the I/O Layout.
2151 fexists = file_exists(filepath, g%Domain)
2152 if (fexists) then
2153 nf = nf + 1
2154 if (present(io_handles)) &
2155 call io_handles(nf)%open(trim(filepath), readonly_file, mom_domain=g%Domain)
2156 if (present(global_files)) global_files(nf) = .false.
2157 if (present(file_paths)) file_paths(nf) = filepath
2158 endif
2159 endif
2160
2161 if (fexists) then
2162 if (is_root_pe() .and. (present(io_handles))) &
2163 call mom_error(note, "MOM_restart: MOM run restarted using : "//trim(filepath))
2164 else
2165 still_looking = .false. ; exit
2166 endif
2167 enddo ! while (still_looking) loop
2168 else
2169 filepath = trim(directory)//trim(fname)
2170 inquire(file=filepath, exist=fexists)
2171 if (.not. fexists) filepath = trim(filepath)//".nc"
2172
2173 inquire(file=filepath, exist=fexists)
2174 if (fexists) then
2175 nf = nf + 1
2176 if (present(io_handles)) &
2177 call io_handles(nf)%open(trim(filepath), readonly_file, &
2178 mom_domain=g%Domain, threading=multiple, fileset=single_file)
2179 if (present(global_files)) global_files(nf) = .true.
2180 if (present(file_paths)) file_paths(nf) = filepath
2181 if (is_root_pe() .and. (present(io_handles))) &
2182 call mom_error(note, "MOM_restart: MOM run restarted using : "//trim(filepath))
2183 else
2184 if (present(io_handles)) &
2185 call mom_error(warning, "MOM_restart: Unable to find restart file : "//trim(filepath))
2186 endif
2187
2188 endif
2189 enddo ! while (start_char < len_trim(filename)) loop
2190 num_files = nf
2191
2192end function open_restart_units
2193
2194!> get_num_restart_files returns the number of existing restart files that match the provided
2195!! directory structure and other information stored in the control structure and optionally
2196!! also provides the full paths to these files.
2197function get_num_restart_files(filenames, directory, G, CS, file_paths) result(num_files)
2198 character(len=*), intent(in) :: filenames !< The list of restart file names or a single
2199 !! character 'r' to read automatically named files
2200 character(len=*), intent(in) :: directory !< The directory in which to find restart files
2201 type(ocean_grid_type), intent(in) :: g !< The ocean's grid structure
2202 type(mom_restart_cs), intent(in) :: cs !< MOM restart control struct
2203 character(len=*), dimension(:), &
2204 optional, intent(out) :: file_paths !< The full paths to the restart files.
2205
2206 integer :: num_files !< The function result, the number of files (both automatically named
2207 !! restart files and others explicitly in filename) that have been opened
2208
2209 if (.not.cs%initialized) call mom_error(fatal, "MOM_restart " // &
2210 "get_num_restart_files: Module must be initialized before it is used.")
2211
2212 ! This call uses open_restart_units without the optional arguments needed to actually
2213 ! open the files to determine the number of restart files.
2214 num_files = open_restart_units(filenames, directory, g, cs, file_paths=file_paths)
2215
2216end function get_num_restart_files
2217
2218
2219!> Initialize this module and set up a restart control structure.
2220subroutine restart_init(param_file, CS, restart_root)
2221 type(param_file_type), intent(in) :: param_file !< A structure to parse for run-time parameters
2222 type(mom_restart_cs), pointer :: cs !< A pointer to a MOM_restart_CS object that is allocated here
2223 character(len=*), optional, &
2224 intent(in) :: restart_root !< A filename root that overrides the value
2225 !! set by RESTARTFILE to enable the use of this module by
2226 !! other components than MOM.
2227
2228 logical :: rotate_index
2229
2230 ! This include declares and sets the variable "version".
2231# include "version_variable.h"
2232 character(len=40) :: mdl = "MOM_restart" ! This module's name.
2233 logical :: all_default ! If true, all parameters are using their default values.
2234
2235 if (associated(cs)) then
2236 call mom_error(warning, "restart_init called with an associated control structure.")
2237 return
2238 endif
2239 allocate(cs)
2240
2241 cs%initialized = .true.
2242
2243 ! Determine whether all paramters are set to their default values.
2244 call get_param(param_file, mdl, "PARALLEL_RESTARTFILES", cs%parallel_restartfiles, &
2245 default=.false., do_not_log=.true.)
2246 call get_param(param_file, mdl, "MAX_FIELDS", cs%max_fields, default=100, do_not_log=.true.)
2247 call get_param(param_file, mdl, "RESTART_CHECKSUMS_REQUIRED", cs%checksum_required, &
2248 default=.true., do_not_log=.true.)
2249 call get_param(param_file, mdl, "RESTART_SYMMETRIC_CHECKSUMS", cs%symmetric_checksums, &
2250 default=.false., do_not_log=.true.)
2251 call get_param(param_file, mdl, "RESTART_UNSIGNED_ZEROS", cs%unsigned_zeros, &
2252 default=.false., do_not_log=.true.)
2253 all_default = ((.not.cs%parallel_restartfiles) .and. (cs%max_fields == 100) .and. &
2254 (cs%checksum_required) .and. (.not.cs%symmetric_checksums) .and. (.not.cs%unsigned_zeros))
2255 if (.not.present(restart_root)) then
2256 call get_param(param_file, mdl, "RESTARTFILE", cs%restartfile, &
2257 default="MOM.res", do_not_log=.true.)
2258 all_default = (all_default .and. (trim(cs%restartfile) == trim("MOM.res")))
2259 endif
2260
2261 ! Read all relevant parameters and write them to the model log.
2262 call log_version(param_file, mdl, version, "", all_default=all_default)
2263 call get_param(param_file, mdl, "PARALLEL_RESTARTFILES", cs%parallel_restartfiles, &
2264 "If true, the IO layout is used to group processors that write to the same "//&
2265 "restart file or each processor writes its own (numbered) restart file. "//&
2266 "If false, a single restart file is generated combining output from all PEs.", &
2267 default=.false.)
2268
2269 if (present(restart_root)) then
2270 cs%restartfile = restart_root
2271 call log_param(param_file, mdl, "RESTARTFILE from argument", cs%restartfile)
2272 else
2273 call get_param(param_file, mdl, "RESTARTFILE", cs%restartfile, &
2274 "The name-root of the restart file.", default="MOM.res")
2275 endif
2276 call get_param(param_file, mdl, "MAX_FIELDS", cs%max_fields, &
2277 "The maximum number of restart fields that can be used.", &
2278 default=100)
2279 call get_param(param_file, mdl, "RESTART_CHECKSUMS_REQUIRED", cs%checksum_required, &
2280 "If true, require the restart checksums to match and error out otherwise. "//&
2281 "Users may want to avoid this comparison if for example the restarts are "//&
2282 "made from a run with a different mask_table than the current run, "//&
2283 "in which case the checksums will not match and cause crash.",&
2284 default=.true.)
2285 call get_param(param_file, mdl, "ENSEMBLE_APPENDIX_PREFIX", cs%ensemble_appendix_prefix, &
2286 "If set to a non-empty string, this value specifies the substring after which "//&
2287 "the ensemble appendix is inserted in restart, initial conditions, and ocean "//&
2288 "geometry file names. If the specified substring is not found in any of those "//&
2289 "output file names, the model terminates with an error.", &
2290 default="")
2291 call get_param(param_file, mdl, "RESTART_SYMMETRIC_CHECKSUMS", cs%symmetric_checksums, &
2292 "If true, do the restart checksums on all the edge points for a non-reentrant "//&
2293 "grid. This requires that SYMMETRIC_MEMORY_ is defined at compile time.", &
2294 default=.false.)
2295 call get_param(param_file, mdl, "RESTART_UNSIGNED_ZEROS", cs%unsigned_zeros, &
2296 "If true, convert any negative zeros that would be written to the restart file "//&
2297 "into ordinary unsigned zeros. This does not change answers, but it can be "//&
2298 "helpful in comparing restart files after grid rotation, for example.", &
2299 default=.false.)
2300 call get_param(param_file, mdl, "REENTRANT_X", cs%reentrant_x, &
2301 "If true, the domain is zonally reentrant.", default=.true., do_not_log=.true.)
2302 call get_param(param_file, mdl, "REENTRANT_Y", cs%reentrant_y, &
2303 "If true, the domain is meridionally reentrant.", default=.false., do_not_log=.true.)
2304
2305 ! Maybe not the best place to do this?
2306 call get_param(param_file, mdl, "ROTATE_INDEX", rotate_index, &
2307 default=.false., do_not_log=.true.)
2308
2309 cs%turns = 0
2310 if (rotate_index) then
2311 call get_param(param_file, mdl, "INDEX_TURNS", cs%turns, &
2312 default=1, do_not_log=.true.)
2313 endif
2314
2315 allocate(cs%restart_field(cs%max_fields))
2316 allocate(cs%restart_obsolete(cs%max_fields))
2317 allocate(cs%var_ptr0d(cs%max_fields))
2318 allocate(cs%var_ptr1d(cs%max_fields))
2319 allocate(cs%var_ptr2d(cs%max_fields))
2320 allocate(cs%var_ptr3d(cs%max_fields))
2321 allocate(cs%var_ptr4d(cs%max_fields))
2322
2323 cs%locked = .false.
2324
2325end subroutine restart_init
2326
2327!> Issue an error message if the restart_registry is locked.
2328subroutine lock_check(CS, var_desc, name)
2329 type(mom_restart_cs), intent(in) :: cs !< A MOM_restart_CS object (intent in)
2330 type(vardesc), optional, intent(in) :: var_desc !< A structure with metadata about this variable
2331 character(len=*), optional, intent(in) :: name !< variable name to be used in the restart file
2332
2333 character(len=256) :: var_name ! A variable name.
2334
2335 if (cs%locked) then
2336 if (present(var_desc)) then
2337 call query_vardesc(var_desc, name=var_name)
2338 call mom_error(fatal, "Attempted to register "//trim(var_name)//" but the restart registry is locked.")
2339 elseif (present(name)) then
2340 call mom_error(fatal, "Attempted to register "//trim(name)//" but the restart registry is locked.")
2341 else
2342 call mom_error(fatal, "Attempted to register a variable but the restart registry is locked.")
2343 endif
2344 endif
2345
2346end subroutine lock_check
2347
2348!> Lock the restart registry so that an error is issued if any further restart variables are registered.
2349subroutine restart_registry_lock(CS, unlocked)
2350 type(mom_restart_cs), intent(inout) :: cs !< A MOM_restart_CS object (intent inout)
2351 logical, optional, intent(in) :: unlocked !< If present and true, unlock the registry
2352
2353 cs%locked = .true.
2354 if (present(unlocked)) cs%locked = .not.unlocked
2355end subroutine restart_registry_lock
2356
2357!> Indicate that all variables have now been registered and lock the registry.
2358subroutine restart_init_end(CS)
2359 type(mom_restart_cs), pointer :: cs !< A pointer to a MOM_restart_CS object
2360
2361 if (associated(cs)) then
2362 cs%locked = .true.
2363
2364 if (cs%novars == 0) call restart_end(cs)
2365 endif
2366
2367end subroutine restart_init_end
2368
2369!> Deallocate memory associated with a MOM_restart_CS variable.
2370subroutine restart_end(CS)
2371 type(mom_restart_cs), pointer :: cs !< A pointer to a MOM_restart_CS object
2372
2373 if (associated(cs%restart_field)) deallocate(cs%restart_field)
2374 if (associated(cs%restart_obsolete)) deallocate(cs%restart_obsolete)
2375 if (associated(cs%var_ptr0d)) deallocate(cs%var_ptr0d)
2376 if (associated(cs%var_ptr1d)) deallocate(cs%var_ptr1d)
2377 if (associated(cs%var_ptr2d)) deallocate(cs%var_ptr2d)
2378 if (associated(cs%var_ptr3d)) deallocate(cs%var_ptr3d)
2379 if (associated(cs%var_ptr4d)) deallocate(cs%var_ptr4d)
2380 deallocate(cs)
2381
2382end subroutine restart_end
2383
2384subroutine restart_error(CS)
2385 type(mom_restart_cs), intent(in) :: CS !< MOM restart control struct
2386
2387 character(len=16) :: num ! String for error messages
2388
2389 if (cs%novars > cs%max_fields) then
2390 write(num,'(I0)') cs%novars
2391 call mom_error(fatal,"MOM_restart: Too many fields registered for " // &
2392 "restart. Set MAX_FIELDS to be at least "//trim(num)//" in the MOM input file.")
2393 else
2394 call mom_error(fatal,"MOM_restart: Unspecified fatal error.")
2395 endif
2396end subroutine restart_error
2397
2398!> Return bounds for computing checksums to store in restart files
2399subroutine get_checksum_loop_ranges(G, CS, pos, isL, ieL, jsL, jeL)
2400 type(ocean_grid_type), intent(in) :: G !< The ocean's grid structure
2401 type(mom_restart_cs), intent(in) :: CS !< MOM restart control structure
2402 integer, intent(in) :: pos !< A coded integer indicating the horizontal staggering
2403 !! of a variable
2404 integer, intent(out) :: isL !< i-start for checksum
2405 integer, intent(out) :: ieL !< i-end for checksum
2406 integer, intent(out) :: jsL !< j-start for checksum
2407 integer, intent(out) :: jeL !< j-end for checksum
2408
2409 ! Regular non-symmetric compute domain
2410 isl = g%isc-g%isd+1
2411 iel = g%iec-g%isd+1
2412 jsl = g%jsc-g%jsd+1
2413 jel = g%jec-g%jsd+1
2414
2415 ! Expand range east or south for symmetric arrays
2416 if (cs%symmetric_checksums) then
2417 if (.not.g%symmetric) call mom_error(fatal, &
2418 "Setting SYMMETRIC_RESTART_CHECKSUMS to true only works with symmetric memory allocation, "//&
2419 "which is specified at compile time by defining the cpp macro SYMMETRIC_MEMORY_.")
2420
2421 if (((pos == east_face) .or. (pos == corner)) .and. (.not.cs%reentrant_x)) then ! For u-, q-points only
2422 if (g%isc+g%idg_offset == 1) isl = isl - 1 ! Include western edge in checksums only for western PEs
2423 endif
2424 if (((pos == north_face) .or. (pos == corner)) .and. (.not.cs%reentrant_y)) then ! For v-, q-points only
2425 if (g%jsc+g%jdg_offset == 1) jsl = jsl - 1 ! Include southern edge in checksums only for southern PEs
2426 endif
2427 endif
2428
2429end subroutine get_checksum_loop_ranges
2430
2431!> get the size of a variable in bytes
2432function get_variable_byte_size(pos, z_grid, t_grid, G, num_z) result(var_sz)
2433 integer, intent(in) :: pos !< An integer indicating the horizontal staggering position
2434 character(len=8), intent(in) :: z_grid !< The vertical grid string to interpret
2435 character(len=8), intent(in) :: t_grid !< A time string to interpret
2436 type(ocean_grid_type), intent(in) :: g !< The ocean's grid structure
2437 integer, intent(in) :: num_z !< The number of vertical layers in the grid
2438 integer(kind=int64) :: var_sz !< The function result, the size in bytes of a variable
2439
2440 ! Local variables
2441 integer :: var_periods ! The number of entries in a time-periodic axis
2442 character(len=8) :: t_grid_read, t_grid_tmp ! Modified versions of t_grid
2443
2444 if (pos == 0) then
2445 var_sz = 8
2446 else ! This may be an overestimate, as it is based on symmetric-memory corner points.
2447 var_sz = 8*(g%Domain%niglobal+1)*(g%Domain%njglobal+1)
2448 endif
2449
2450 select case (trim(z_grid))
2451 case ('L') ; var_sz = var_sz * num_z
2452 case ('i') ; var_sz = var_sz * (num_z+1)
2453 end select
2454
2455 t_grid_tmp = adjustl(t_grid)
2456 if (t_grid_tmp(1:1) == 'p') then
2457 if (len_trim(t_grid_tmp(2:8)) > 0) then
2458 var_periods = -1
2459 t_grid_read = adjustl(t_grid_tmp(2:8))
2460 read(t_grid_read,*) var_periods
2461 if (var_periods > 1) var_sz = var_sz * var_periods
2462 endif
2463 endif
2464
2465end function get_variable_byte_size
2466
2467end module mom_restart