33 type(restart_file_type),
intent(inout),
target :: fileObj
34 character(len=*),
intent(in),
optional :: time_stamp
35 character(len=*),
intent(in),
optional :: directory
36 logical(INT_KIND),
intent(in),
optional :: append
38 real,
intent(in),
optional :: time_level
59 type(domainug),
pointer :: domain
60 integer(INT_KIND) :: mpp_action
62 logical(INT_KIND) :: write_meta_data
64 logical(INT_KIND) :: write_field_data
66 character(len=128) :: dir
67 character(len=80) :: restartname
68 character(len=256) :: restartpath
69 integer(INT_KIND) :: funit
70 type(ax_type),
pointer :: axis
71 type(axistype) :: x_axis
73 logical(INT_KIND) :: x_axis_defined
75 type(axistype) :: y_axis
77 logical(INT_KIND) :: y_axis_defined
79 type(axistype) :: z_axis
81 logical(INT_KIND) :: z_axis_defined
83 type(axistype) :: cc_axis
85 logical(INT_KIND) :: cc_axis_defined
87 type(axistype) :: c_axis
89 logical(INT_KIND) :: c_axis_defined
91 type(axistype) :: h_axis
93 logical(INT_KIND) :: h_axis_defined
95 type(axistype) :: t_axis
97 type(var_type),
pointer :: cur_var
98 integer(INT_KIND) :: num_var_axes
99 type(axistype),
dimension(4) :: var_axes
100 integer(INT_KIND) :: cpack
102 integer(LONG_KIND),
dimension(:),
allocatable :: check_val
108 real,
dimension(:),
allocatable :: r1d
110 real,
dimension(:,:),
allocatable :: r2d
112 integer(INT_KIND) :: i
113 integer(INT_KIND) :: j
114 integer(INT_KIND) :: k
117 if (.not.
associated(fileobj%var))
then
118 call mpp_error(fatal, &
119 "fms_io_unstructured_save_restart:" &
120 //
" the restart object does not conatin any fields.")
126 if (all_field_read_only(fileobj))
then
131 if (.not.
allocated(fileobj%axes))
then
132 call mpp_error(fatal, &
133 "fms_io_unstructured_save_restart: there are no" &
134 //
" registered axes for the file "//trim(fileobj%name))
140 do j = 1,
size(fileobj%axes)
141 if (j .eq. cidx .or. j .eq. hidx .or. j .eq. uidx)
then
142 if (
allocated(fileobj%axes(j)%idx))
then
143 if (.not.
associated(fileobj%axes(j)%domain_ug))
then
144 call mpp_error(fatal, &
145 "fms_io_unstructured_save_restart:" &
146 //
" the axis "//trim(fileobj%axes(j)%name) &
147 //
" in the file "//trim(fileobj%name) &
148 //
" was not registered with an unstructured" &
151 if (
associated(domain))
then
152 if (.not. (domain .EQ. fileobj%axes(j)%domain_ug))
then
153 call mpp_error(fatal, &
154 "fms_io_unstructured_save_restart:" &
155 //
" two axes registered to same" &
156 //
" restart file are associated with" &
157 //
" different unstructured mpp domains.")
160 domain => fileobj%axes(j)%domain_ug
164 if (
associated(fileobj%axes(j)%data))
then
165 if (.not.
associated(fileobj%axes(j)%domain_ug))
then
166 call mpp_error(fatal, &
167 "fms_io_unstructured_save_restart:" &
168 //
" the axis "//trim(fileobj%axes(j)%name) &
169 //
" in the file "//trim(fileobj%name) &
170 //
" was not registered with an unstructured" &
173 if (
associated(domain))
then
174 if (.not. (domain .EQ. fileobj%axes(j)%domain_ug))
then
175 call mpp_error(fatal, &
176 "fms_io_unstructured_save_restart:" &
177 //
" two axes registered to same" &
178 //
" restart file are associated with" &
179 //
" different unstructured mpp domains.")
182 domain => fileobj%axes(j)%domain_ug
190 do j = 1,fileobj%nvar
191 if (.not.
associated(fileobj%var(j)%domain_ug))
then
192 call mpp_error(fatal, &
193 "fms_io_unstructured_save_restart:" &
194 //
" the field "//trim(fileobj%var(j)%name) &
195 //
" in the file "//trim(fileobj%name) &
196 //
" was not registered with an unstructured" &
199 if (.not. (domain .EQ. fileobj%var(j)%domain_ug))
then
200 call mpp_error(fatal, &
201 "fms_io_unstructured_save_restart:" &
202 //
" the unstructured domain associated with" &
203 //
" field "//trim(fileobj%var(j)%name) &
204 //
" in the file "//trim(fileobj%name) &
205 //
" does not match the unstructured domain" &
206 //
" associated with the registered axes.")
211 if (
present(append))
then
212 if (append .and. .not.
present(time_level))
then
213 call mpp_error(fatal, &
214 "fms_io_unstructured_save_compressed_restart:" &
215 //
" a time_level must be present when" &
216 //
" append=.true. for file "//trim(fileobj%name))
225 mpp_action = mpp_overwr
226 write_meta_data = .true.
227 if (
present(append))
then
229 mpp_action = mpp_append
230 write_meta_data = .false.
231 if (time_level .lt. 0.0)
then
232 call mpp_error(fatal, &
233 "fms_io_unstructured_save_restart:" &
234 //
" the inputted time_level cannot be" &
235 //
" negative when append is .true." &
236 //
" for file "//trim(fileobj%name))
244 write_field_data = .true.
245 if (
present(time_level))
then
246 if (time_level .lt. 0)
then
247 write_field_data = .false.
254 if (
present(directory))
then
255 dir = trim(directory)
260 restartname = trim(fileobj%name)
261 if (time_stamp_restart)
then
262 if (
present(time_stamp))
then
263 if (len_trim(restartname) + len_trim(time_stamp) .gt. 79)
then
264 call mpp_error(fatal, &
265 "fms_io_unstructured_save_restart:" &
266 //
" length of restart file name including" &
267 //
" time stamp is greater than allowed" &
268 //
" restart file name length.")
270 restartname = trim(time_stamp)//
"."//trim(restartname)
275 if (len_trim(dir) .gt. 0)
then
276 restartpath = trim(dir)//
"/"//trim(restartname)
278 restartpath = trim(restartname)
282 call mpp_open(funit, &
286 is_root_pe=fileobj%is_root_pe, &
292 if (write_meta_data)
then
296 if (
associated(fileobj%axes(xidx)%data))
then
297 axis => fileobj%axes(xidx)
298 call mpp_write_meta(funit, &
306 x_axis_defined = .true.
308 x_axis_defined = .false.
313 if (
associated(fileobj%axes(yidx)%data))
then
314 axis => fileobj%axes(yidx)
315 call mpp_write_meta(funit, &
323 y_axis_defined = .true.
325 y_axis_defined = .false.
330 if (
associated(fileobj%axes(zidx)%data))
then
331 axis => fileobj%axes(zidx)
332 call mpp_write_meta(funit, &
340 z_axis_defined = .true.
342 z_axis_defined = .false.
347 if (
associated(fileobj%axes(ccidx)%data))
then
348 axis => fileobj%axes(ccidx)
349 call mpp_write_meta(funit, &
357 cc_axis_defined = .true.
359 cc_axis_defined = .false.
364 if (
allocated(fileobj%axes(cidx)%idx))
then
365 axis => fileobj%axes(cidx)
366 call mpp_def_dim(funit, &
367 trim(axis%dimlen_name), &
369 trim(axis%dimlen_lname), &
370 (/(i,i=1,axis%dimlen)/))
371 call mpp_write_meta(funit, &
377 compressed=axis%compressed, &
380 c_axis_defined = .true.
382 c_axis_defined = .false.
387 if (
allocated(fileobj%axes(hidx)%idx))
then
388 axis => fileobj%axes(hidx)
389 call mpp_def_dim(funit, &
390 trim(axis%dimlen_name), &
392 trim(axis%dimlen_lname), &
393 (/(i,i=1,axis%dimlen)/))
394 call mpp_write_meta(funit, &
400 compressed=axis%compressed, &
403 h_axis_defined = .true.
405 h_axis_defined = .false.
409 if (
associated(fileobj%axes(tidx)%data))
then
410 axis => fileobj%axes(tidx)
411 call mpp_write_meta(funit, &
415 longname=axis%longname, &
417 calendar=axis%calendar)
420 call mpp_write_meta(funit, &
429 do j = 1,fileobj%nvar
432 cur_var => fileobj%var(j)
435 if (cur_var%read_only)
then
441 if (cur_var%siz(4) .gt. 1 .and. cur_var%siz(4) .ne. &
442 fileobj%max_ntime)
then
443 call mpp_error(fatal, &
444 "fms_io_unstructured_save_restart: " &
445 //trim(cur_var%name)//
" in file " &
446 //trim(fileobj%name)//
" has more than one" &
447 //
" time level, but the number of time levels" &
448 //
" is not equal to max_ntime.")
454 if (cur_var%ndim .eq. 0)
then
458 num_var_axes = cur_var%ndim
459 do k = 1,cur_var%ndim
460 select case (cur_var%field_dimension_order(k))
468 var_axes(k) = cc_axis
474 call mpp_error(fatal, &
475 "fms_io_unstructured_save_restart:" &
476 //
" unsupported dimension type for" &
477 //
" field "//trim(cur_var%name) &
478 //
" in file "//trim(fileobj%name))
481 if (cur_var%siz(4) .eq. fileobj%max_ntime)
then
482 num_var_axes = num_var_axes + 1
483 var_axes(num_var_axes) = t_axis
494 allocate(check_val(max(1,cur_var%siz(4))))
495 do k = 1,cur_var%siz(4)
496 if (
associated(fileobj%p0dr(k,j)%p))
then
497 check_val(k) = mpp_chksum(fileobj%p0dr(k,j)%p, &
499 mask_val=cur_var%default_data)
500 elseif (
associated(fileobj%p1dr(k,j)%p))
then
501 check_val(k) = mpp_chksum(fileobj%p1dr(k,j)%p, &
502 mask_val=cur_var%default_data)
503 elseif (
associated(fileobj%p2dr(k,j)%p))
then
504 check_val(k) = mpp_chksum(fileobj%p2dr(k,j)%p, &
505 mask_val=cur_var%default_data)
506 elseif (
associated(fileobj%p3dr(k,j)%p))
then
507 check_val(k) = mpp_chksum(fileobj%p3dr(k,j)%p, &
508 mask_val=cur_var%default_data)
509 elseif (
associated(fileobj%p0di(k,j)%p))
then
510 check_val(k) = int(fileobj%p0di(k,j)%p,kind=long_kind)
512 elseif (
associated(fileobj%p1di(k,j)%p))
then
513 check_val(k) = mpp_chksum(fileobj%p1di(k,j)%p, &
514 mask_val=cur_var%default_data)
516 elseif (
associated(fileobj%p2di(k,j)%p))
then
517 check_val(k) = mpp_chksum(fileobj%p2di(k,j)%p, &
518 mask_val=cur_var%default_data)
520 elseif (
associated(fileobj%p3di(k,j)%p))
then
521 call mpp_error(fatal, &
522 "fms_io_unstructured_save_restart:" &
523 //
" 3D integer restart fields are not" &
524 //
" currently supported. (" &
525 //trim(cur_var%name)//
" of file " &
526 //trim(fileobj%name)//
")")
528 call mpp_error(fatal, &
529 "fms_io_unstructured_save_restart:" &
530 //
" there is no pointer associated with " &
531 //
" the data of field " &
532 //trim(cur_var%name)//
" of file " &
533 //trim(fileobj%name))
539 if (write_field_data)
then
540 call mpp_write_meta(funit, &
542 var_axes(1:num_var_axes), &
547 checksum=check_val, &
548 fill=cur_var%default_data)
550 call mpp_write_meta(funit, &
552 var_axes(1:num_var_axes), &
557 fill=cur_var%default_data)
559 deallocate(check_val)
564 if (x_axis_defined)
then
565 call mpp_write(funit, &
568 if (y_axis_defined)
then
569 call mpp_write(funit, &
572 if (c_axis_defined)
then
573 call mpp_write(funit, &
576 if (h_axis_defined)
then
577 call mpp_write(funit, &
580 if (cc_axis_defined)
then
581 call mpp_write(funit, &
584 if (z_axis_defined)
then
585 call mpp_write(funit, &
591 if (write_field_data)
then
594 do k = 1,fileobj%max_ntime
597 if (
present(time_level))
then
604 do j = 1,fileobj%nvar
607 cur_var => fileobj%var(j)
610 if (cur_var%read_only)
then
616 if (k .le. cur_var%siz(4))
then
617 if (
associated(fileobj%p0dr(k,j)%p))
then
618 call mpp_write(funit, &
620 fileobj%p0dr(k,j)%p, &
622 elseif (
associated(fileobj%p1dr(k,j)%p))
then
623 call mpp_io_unstructured_write(funit, &
626 fileobj%p1dr(k,j)%p, &
627 fileobj%axes(cur_var%field_dimension_order(1))%nelems, &
629 default_data=cur_var%default_data)
630 elseif (
associated(fileobj%p2dr(k,j)%p))
then
631 call mpp_io_unstructured_write(funit, &
634 fileobj%p2dr(k,j)%p, &
635 fileobj%axes(cur_var%field_dimension_order(1))%nelems, &
637 default_data=cur_var%default_data)
638 elseif (
associated(fileobj%p3dr(k,j)%p))
then
639 call mpp_io_unstructured_write(funit, &
642 fileobj%p3dr(k,j)%p, &
643 fileobj%axes(cur_var%field_dimension_order(1))%nelems, &
645 default_data=cur_var%default_data)
646 elseif (
associated(fileobj%p0di(k,j)%p))
then
647 r0d = real(fileobj%p0di(k,j)%p)
648 call mpp_write(funit, &
652 elseif (
associated(fileobj%p1di(k,j)%p))
then
653 allocate(r1d(
size(fileobj%p1di(k,j)%p,1)))
654 r1d = real(fileobj%p1di(k,j)%p)
655 call mpp_io_unstructured_write(funit, &
659 fileobj%axes(cur_var%field_dimension_order(1))%nelems, &
661 default_data=cur_var%default_data)
663 elseif (
associated(fileobj%p2di(k,j)%p))
then
664 allocate(r2d(
size(fileobj%p2di(k,j)%p,1),
size(fileobj%p2di(k,j)%p,2)))
665 r2d = real(fileobj%p2di(k,j)%p)
666 call mpp_io_unstructured_write(funit, &
670 fileobj%axes(cur_var%field_dimension_order(1))%nelems, &
672 default_data=cur_var%default_data)
675 call mpp_error(fatal, &
676 "fms_io_unstructured_save_restart:" &
677 //
" there is no pointer associated" &
678 //
" with the data of field " &
679 //trim(cur_var%name)//
" of file " &
680 //trim(fileobj%name))
689 call mpp_close(funit)
subroutine fms_io_unstructured_save_restart(fileObj, time_stamp, directory, append, time_level)
Write out metadata and data for axes and fields to a restart file associated with an unstructured mpp...
integer function mpp_pe()
Returns processor ID.