From 6f28c272ab5e1eb264fee0ec393da05c08857147 Mon Sep 17 00:00:00 2001 From: "denise.worthen" Date: Tue, 15 Oct 2024 15:37:31 -0500 Subject: [PATCH] add error checks for dimensions in restart file * tested by using nco to alter the nth,nk values in a restart file. The model correctly aborted after finding nth did not match the expected value --- model/src/wav_restart_mod.F90 | 84 +++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/model/src/wav_restart_mod.F90 b/model/src/wav_restart_mod.F90 index 8076cd0ec..9d1c67780 100644 --- a/model/src/wav_restart_mod.F90 +++ b/model/src/wav_restart_mod.F90 @@ -61,7 +61,7 @@ subroutine write_restart (fname, va, mapsta) character(len=*), intent(in) :: fname ! local variables - integer :: timid, xtid, ytid, ztid + integer :: timid, xtid, ytid integer :: nseal_cpl, nmode integer :: dimid(3) real , allocatable :: lva(:,:) @@ -92,10 +92,9 @@ subroutine write_restart (fname, va, mapsta) ierr = pio_def_dim(pioid, 'nx', nx, xtid) ierr = pio_def_dim(pioid, 'ny', ny, ytid) - ierr = pio_def_dim(pioid, 'nspec', nspec, ztid) ierr = pio_def_dim(pioid, 'time', PIO_UNLIMITED, timid) - !define the time variable + ! define the time variable ierr = pio_def_var(pioid, 'time', PIO_DOUBLE, (/timid/), varid) call handle_err(ierr,'def_timevar') ierr = pio_put_att(pioid, varid, 'units', trim(time_origin)) @@ -103,6 +102,14 @@ subroutine write_restart (fname, va, mapsta) ierr = pio_put_att(pioid, varid, 'calendar', trim(calendar_name)) call handle_err(ierr,'def_time_calendar') + ! define the nth,nk sizes + ierr = pio_def_var(pioid, 'nth', PIO_INT, varid) + call handle_err(ierr,'def_nth') + ierr = pio_put_att(pioid, varid, 'long_name', 'number of direction bins') + ierr = pio_def_var(pioid, 'nk', PIO_INT, varid) + call handle_err(ierr,'def_nk') + ierr = pio_put_att(pioid, varid, 'long_name', 'number of frequencies') + ! write each nspec as separate variable do kk = 1,nspec write(cspec,'(i4.4)')kk @@ -134,6 +141,16 @@ subroutine write_restart (fname, va, mapsta) ierr = pio_enddef(pioid) call handle_err(ierr, 'end variable definition') + ! write the freq and direction sizes + ierr = pio_inq_varid(pioid, 'nth', varid) + call handle_err(ierr, 'inquire variable nth ') + ierr = pio_put_var(pioid, varid, nth) + call handle_err(ierr, 'put nth') + ierr = pio_inq_varid(pioid, 'nk', varid) + call handle_err(ierr, 'inquire variable nk ') + ierr = pio_put_var(pioid, varid, nk) + call handle_err(ierr, 'put nk') + ! initialize the decomp call wav_pio_initdecomp(iodesc2dint, use_int=.true.) call wav_pio_initdecomp(iodesc2d) @@ -283,6 +300,9 @@ subroutine read_restart (fname, va, mapsta, mapst2) call handle_err(ierr, 'open file '//trim(fname)) if (iaproc == 1) write(ndso,'(a)')' Reading restart file '//trim(fname) + ! check the field dimensions and sizes against the current values + call checkfile() + ! initialize the decomp call wav_pio_initdecomp(iodesc2dint, use_int=.true.) call wav_pio_initdecomp(iodesc2d) @@ -457,4 +477,62 @@ subroutine read_globalfield(wave_communicator, vname, nseal_cpl, global_output, end do end subroutine read_globalfield + + !=============================================================================== + !> Check that a restart file has the expected dimensions and sizes + !! + !> author DeniseWorthen@noaa.gov + !> @date 10-15-2024 + subroutine checkfile() + + use w3odatmd , only : ndse + use w3servmd , only : extcde + + integer :: dimid, ivar + integer(kind=PIO_OFFSET_KIND) :: dimlen + + ! check dimension nx + vname = 'nx' + ierr = pio_inq_dimid(pioid, vname, dimid) + call handle_err(ierr, 'inquire dimension '//trim(vname)) + ierr = pio_inq_dimlen(pioid, dimid, dimlen) + if (dimlen /= int(nx,PIO_OFFSET_KIND)) then + write(ndse,*) '*** WAVEWATCH III restart error: '//trim(vname)//' does not match expected value' + call extcde ( 49 ) + end if + + ! check dimension ny + vname = 'ny' + ierr = pio_inq_dimid(pioid, vname, dimid) + call handle_err(ierr, 'inquire dimension '//trim(vname)) + ierr = pio_inq_dimlen(pioid, dimid, dimlen) + if (dimlen /= int(ny,PIO_OFFSET_KIND)) then + write(ndse,*) '*** WAVEWATCH III restart error: '//trim(vname)//' does not match expected value' + call extcde ( 49 ) + end if + + ! check number of directions + vname = 'nth' + ierr = pio_inq_varid(pioid, vname, varid) + call handle_err(ierr, 'inquire variable '//trim(vname)) + ierr = pio_get_var(pioid, varid, ivar) + call handle_err(ierr, 'get variable '//trim(vname)) + if (ivar .ne. nth) then + write(ndse,*) '*** WAVEWATCH III restart error: '//trim(vname)//' does not match expected value' + call extcde ( 49 ) + end if + + ! check number of frequencies + vname = 'nk' + ierr = pio_inq_varid(pioid, vname, varid) + call handle_err(ierr, 'inquire variable '//trim(vname)) + ierr = pio_get_var(pioid, varid, ivar) + call handle_err(ierr, 'get variable '//trim(vname)) + if (ivar .ne. nk) then + write(ndse,*) '*** WAVEWATCH III restart error: '//trim(vname)//' does not match expected value' + call extcde ( 49 ) + end if + + end subroutine checkfile + end module wav_restart_mod