Skip to content

Commit

Permalink
Merge pull request #35 from NOAA-EMC/feature/netcdf-error
Browse files Browse the repository at this point in the history
Trap netcdf errors when they happen in ocnicepost.fd.
  • Loading branch information
aerorahul authored Nov 27, 2023
2 parents 427d467 + 2d3730b commit 65ed705
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 178 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ target_link_libraries(nemsio::nemsio INTERFACE w3emc::w3emc_d bacio::bacio_4)
if(CMAKE_Fortran_COMPILER_ID MATCHES "^(Intel)$")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -g -traceback")
set(CMAKE_Fortran_FLAGS_RELEASE "-O3")
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -check -check noarg_temp_created -check nopointer -fp-stack-check -fstack-protector-all -fpe0 -debug -ftrapuv")
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -check all -ftrapuv")
elseif(CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU)$")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -g -fbacktrace")
set(CMAKE_Fortran_FLAGS_RELEASE "-O3")
Expand Down
34 changes: 17 additions & 17 deletions src/ocnicepost.fd/masking_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module masking_mod
use init_mod , only : wgtsdir, ftype, fsrc, fdst, input_file
use init_mod , only : do_ocnpost, debug, logunit, maskvar
use arrays_mod , only : dstlat, dstlon
use utils_mod , only : getfield, dumpnc, remap
use utils_mod , only : getfield, dumpnc, remap, nf90_err

implicit none

Expand Down Expand Up @@ -41,14 +41,14 @@ subroutine remap_masks(vfill)
allocate(dstlat(nxr,nyr)); dstlat = 0.0
allocate(out1d(nxr*nyr)); out1d = 0.0

rc = nf90_open(trim(wgtsfile), nf90_nowrite, ncid)
rc = nf90_inq_varid(ncid, 'xc_b', varid)
rc = nf90_get_var(ncid, varid, out1d)
call nf90_err(nf90_open(trim(wgtsfile), nf90_nowrite, ncid), 'open: '//wgtsfile)
call nf90_err(nf90_inq_varid(ncid, 'xc_b', varid), 'get variable Id: xc_b')
call nf90_err(nf90_get_var(ncid, varid, out1d), 'get variable: xc_b')
dstlon = reshape(out1d,(/nxr,nyr/))
rc = nf90_inq_varid(ncid, 'yc_b', varid)
rc = nf90_get_var(ncid, varid, out1d)
call nf90_err(nf90_inq_varid(ncid, 'yc_b', varid), 'get variable Id: yc_b')
call nf90_err(nf90_get_var(ncid, varid, out1d), 'get variable: yc_b')
dstlat = reshape(out1d,(/nxr,nyr/))
rc = nf90_close(ncid)
call nf90_err(nf90_close(ncid), 'close: '//wgtsfile)

! --------------------------------------------------------
! mask is a 2d (ice) or 3d (ocn) array which contains 1's
Expand Down Expand Up @@ -110,12 +110,12 @@ subroutine makemask3d(vfill)

allocate(tmp3d(nxt,nyt,nlevs)); tmp3d = 0.0

rc = nf90_open(trim(input_file), nf90_nowrite, ncid)
call nf90_err(nf90_open(trim(input_file), nf90_nowrite, ncid), 'open: '//trim(input_file))
! Obtain maskvar directly from file to set fill value
rc = nf90_inq_varid(ncid, trim(maskvar), varid)
rc = nf90_get_att(ncid, varid, '_FillValue', vfill)
rc = nf90_get_var(ncid, varid, tmp3d)
rc = nf90_close(ncid)
call nf90_err(nf90_inq_varid(ncid, trim(maskvar), varid) , 'get variable Id: '// trim(maskvar))
call nf90_err(nf90_get_att(ncid, varid, '_FillValue', vfill), 'get variable attribute: FillValue '// trim(maskvar))
call nf90_err(nf90_get_var(ncid, varid, tmp3d) , 'get variable: '//trim(maskvar))
call nf90_err(nf90_close(ncid), 'close: '//trim(input_file))

mask3d = reshape(tmp3d, (/nxt*nyt,nlevs/))
! set mask3d to 0 on ocean, 1 on land on source grid
Expand All @@ -139,12 +139,12 @@ subroutine makemask2d(vfill)

allocate(tmp2d(nxt,nyt)); tmp2d = 0.0

rc = nf90_open(trim(input_file), nf90_nowrite, ncid)
call nf90_err(nf90_open(trim(input_file), nf90_nowrite, ncid), 'open: '//trim(input_file))
! Obtain maskvar directly from file to set fill value
rc = nf90_inq_varid(ncid, trim(maskvar), varid)
rc = nf90_get_att(ncid, varid, '_FillValue', vfill)
rc = nf90_get_var(ncid, varid, tmp2d)
rc = nf90_close(ncid)
call nf90_err(nf90_inq_varid(ncid, trim(maskvar), varid), 'get variable Id: '// trim(maskvar))
call nf90_err(nf90_get_att(ncid, varid, '_FillValue', vfill), 'get variable attribute: FillValue '// trim(maskvar))
call nf90_err(nf90_get_var(ncid, varid, tmp2d), 'get variable: '//trim(maskvar))
call nf90_err(nf90_close(ncid), 'close: '//trim(input_file))

mask2d = reshape(tmp2d, (/nxt*nyt/))
! set mask2d to 0 on ocean, 1 on land on source grid
Expand Down
128 changes: 64 additions & 64 deletions src/ocnicepost.fd/ocnicepost.F90
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ program ocnicepost
use arrays_mod , only : b2d, c2d, b3d, rgb2d, rgc2d, rgb3d, dstlon, dstlat, setup_packing
use arrays_mod , only : nbilin2d, nbilin3d, nconsd2d, bilin2d, bilin3d, consd2d
use masking_mod, only : mask2d, mask3d, rgmask2d, rgmask3d, remap_masks
use utils_mod , only : getfield, packarrays, remap, dumpnc
use utils_mod , only : getfield, packarrays, remap, dumpnc, nf90_err

implicit none

Expand Down Expand Up @@ -73,32 +73,32 @@ program ocnicepost
! rotation angles, vertical grid and time axis
! --------------------------------------------------------

rc = nf90_open(trim(input_file), nf90_nowrite, ncid)
call nf90_err(nf90_open(trim(input_file), nf90_nowrite, ncid), 'open: '//trim(input_file))
do n = 1,nvalid
rc = nf90_inq_varid(ncid, trim(outvars(n)%var_name), varid)
rc = nf90_get_att(ncid, varid, 'long_name', outvars(n)%long_name)
rc = nf90_get_att(ncid, varid, 'units', outvars(n)%units)
rc = nf90_get_att(ncid, varid, '_FillValue', outvars(n)%var_fillvalue)
call nf90_err(nf90_inq_varid(ncid, trim(outvars(n)%var_name), varid), 'get variable Id: '//trim(outvars(n)%var_name))
call nf90_err(nf90_get_att(ncid, varid, 'long_name', outvars(n)%long_name), 'get variable attribute: long_name '//trim(outvars(n)%var_name))
call nf90_err(nf90_get_att(ncid, varid, 'units', outvars(n)%units), 'get variable attribute: units '//trim(outvars(n)%var_name) )
call nf90_err(nf90_get_att(ncid, varid, '_FillValue', outvars(n)%var_fillvalue), 'get variable attribute: FillValue'//trim(outvars(n)%var_name))
end do

! timestamp
rc = nf90_inq_varid(ncid, 'time', varid)
rc = nf90_get_var(ncid, varid, timestamp)
rc = nf90_get_att(ncid, varid, 'units', timeunit)
rc = nf90_get_att(ncid, varid, 'calendar', timecal)
call nf90_err(nf90_inq_varid(ncid, 'time', varid), 'get variable Id: time'//trim(input_file))
call nf90_err(nf90_get_var(ncid, varid, timestamp), 'get variable: time'//trim(input_file))
call nf90_err(nf90_get_att(ncid, varid, 'units', timeunit), 'get variable attribute : units '//trim(input_file))
call nf90_err(nf90_get_att(ncid, varid, 'calendar', timecal), 'get variable attribute : calendar '//trim(input_file))
if (do_ocnpost) then
allocate(z_l(nlevs)) ; z_l = 0.0
allocate(z_i(0:nlevs)); z_i = 0.0
allocate(cosrot(nxt*nyt)); cosrot = 0.0
allocate(sinrot(nxt*nyt)); sinrot = 0.0

! cell centers
rc = nf90_inq_varid(ncid, 'z_l', varid)
rc = nf90_get_var(ncid, varid, z_l)
call nf90_err(nf90_inq_varid(ncid, 'z_l', varid), 'get variable Id: z_l '//trim(input_file))
call nf90_err(nf90_get_var(ncid, varid, z_l), 'get variable: z_l '//trim(input_file))
! cell edges
rc = nf90_inq_varid(ncid, 'z_i', varid)
rc = nf90_get_var(ncid, varid, z_i)
rc = nf90_close(ncid)
call nf90_err(nf90_inq_varid(ncid, 'z_i', varid), 'get variable Id: z_i '//trim(input_file))
call nf90_err(nf90_get_var(ncid, varid, z_i), 'get variable: z_i '//trim(input_file))
call nf90_err(nf90_close(ncid), 'close: '//trim(input_file))
! rotation angles
call getfield(trim(input_file), trim(cosvar), dims=(/nxt,nyt/), field=cosrot)
call getfield(trim(input_file), trim(sinvar), dims=(/nxt,nyt/), field=sinrot)
Expand Down Expand Up @@ -259,30 +259,30 @@ program ocnicepost
fout = trim(ftype)//'.'//trim(fdst)//'.nc'
if (debug) write(logunit, '(a)')'output file: '//trim(fout)

rc = nf90_create(trim(fout), nf90_clobber, ncid)
rc = nf90_def_dim(ncid, 'longitude', nxr, idimid)
rc = nf90_def_dim(ncid, 'latitude', nyr, jdimid)
rc = nf90_def_dim(ncid, 'time', nf90_unlimited, timid)
call nf90_err(nf90_create(trim(fout), nf90_clobber, ncid), 'create: '//trim(fout))
call nf90_err(nf90_def_dim(ncid, 'longitude', nxr, idimid), 'define dimension: longitude')
call nf90_err(nf90_def_dim(ncid, 'latitude', nyr, jdimid), 'define dimension: latitude')
call nf90_err(nf90_def_dim(ncid, 'time', nf90_unlimited, timid), 'define dimension: time')

! define the time variable
rc = nf90_def_var(ncid, 'time', nf90_double, (/timid/), varid)
rc = nf90_put_att(ncid, varid, 'units', trim(timeunit))
rc= nf90_put_att(ncid, varid, 'calendar', trim(timecal))
call nf90_err(nf90_def_var(ncid, 'time', nf90_double, (/timid/), varid), 'define variable: time')
call nf90_err(nf90_put_att(ncid, varid, 'units', trim(timeunit)), 'put variable attribute: units')
call nf90_err(nf90_put_att(ncid, varid, 'calendar', trim(timecal)), 'put variable attribute: calendar')
! spatial grid
rc = nf90_def_var(ncid, 'longitude', nf90_float, (/idimid/), varid)
rc = nf90_put_att(ncid, varid, 'units', 'degrees_east')
rc = nf90_def_var(ncid, 'latitude', nf90_float, (/jdimid/), varid)
rc = nf90_put_att(ncid, varid, 'units', 'degrees_north')
call nf90_err(nf90_def_var(ncid, 'longitude', nf90_float, (/idimid/), varid), 'define variable: longitude')
call nf90_err(nf90_put_att(ncid, varid, 'units', 'degrees_east'), 'put variable attribute: units')
call nf90_err(nf90_def_var(ncid, 'latitude', nf90_float, (/jdimid/), varid), 'define variable: latitude' )
call nf90_err(nf90_put_att(ncid, varid, 'units', 'degrees_north'), 'put variable attribute: units')
! vertical grid
if (do_ocnpost) then
rc = nf90_def_dim(ncid, 'z_l', nlevs , kdimid)
rc = nf90_def_dim(ncid, 'z_i', nlevs+1, edimid)
rc = nf90_def_var(ncid, 'z_l', nf90_float, (/kdimid/), varid)
rc = nf90_put_att(ncid, varid, 'units', 'm')
rc = nf90_put_att(ncid, varid, 'positive', 'down')
rc = nf90_def_var(ncid, 'z_i', nf90_float, (/edimid/), varid)
rc = nf90_put_att(ncid, varid, 'units', 'm')
rc = nf90_put_att(ncid, varid, 'positive', 'down')
call nf90_err(nf90_def_dim(ncid, 'z_l', nlevs , kdimid), 'define dimension: z_l')
call nf90_err(nf90_def_dim(ncid, 'z_i', nlevs+1, edimid), 'define dimension: z_i')
call nf90_err(nf90_def_var(ncid, 'z_l', nf90_float, (/kdimid/), varid), 'define variable: z_l')
call nf90_err(nf90_put_att(ncid, varid, 'units', 'm'), 'put variable attribute: units')
call nf90_err(nf90_put_att(ncid, varid, 'positive', 'down'), 'put variable attribute: positive')
call nf90_err(nf90_def_var(ncid, 'z_i', nf90_float, (/edimid/), varid), 'define variable: z_i')
call nf90_err(nf90_put_att(ncid, varid, 'units', 'm'), 'put variable attribute: units')
call nf90_err(nf90_put_att(ncid, varid, 'positive', 'down'), 'put variable attribute: positive')
end if

if (allocated(b2d)) then
Expand All @@ -291,10 +291,10 @@ program ocnicepost
vunit = trim(b2d(n)%units)
vlong = trim(b2d(n)%long_name)
vfill = b2d(n)%var_fillvalue
rc = nf90_def_var(ncid, vname, nf90_float, (/idimid,jdimid,timid/), varid)
rc = nf90_put_att(ncid, varid, 'units', vunit)
rc = nf90_put_att(ncid, varid, 'long_name', vlong)
rc = nf90_put_att(ncid, varid, '_FillValue', vfill)
call nf90_err(nf90_def_var(ncid, vname, nf90_float, (/idimid,jdimid,timid/), varid), 'define variable: '// vname)
call nf90_err(nf90_put_att(ncid, varid, 'units', vunit), 'put variable attribute: units')
call nf90_err(nf90_put_att(ncid, varid, 'long_name', vlong), 'put variable attribute: long_name')
call nf90_err(nf90_put_att(ncid, varid, '_FillValue', vfill), 'put variable attribute: FillValue')
enddo
end if
if (allocated(c2d)) then
Expand All @@ -303,10 +303,10 @@ program ocnicepost
vunit = trim(c2d(n)%units)
vlong = trim(c2d(n)%long_name)
vfill = c2d(n)%var_fillvalue
rc = nf90_def_var(ncid, vname, nf90_float, (/idimid,jdimid,timid/), varid)
rc = nf90_put_att(ncid, varid, 'units', vunit)
rc = nf90_put_att(ncid, varid, 'long_name', vlong)
rc = nf90_put_att(ncid, varid, '_FillValue', vfill)
call nf90_err(nf90_def_var(ncid, vname, nf90_float, (/idimid,jdimid,timid/), varid), 'define variable: '// vname)
call nf90_err(nf90_put_att(ncid, varid, 'units', vunit), 'put variable attribute: units' )
call nf90_err(nf90_put_att(ncid, varid, 'long_name', vlong), 'put variable attribute: long_name' )
call nf90_err(nf90_put_att(ncid, varid, '_FillValue', vfill), 'put variable attribute: FillValue' )
enddo
end if
if (allocated(b3d)) then
Expand All @@ -315,57 +315,57 @@ program ocnicepost
vunit = trim(b3d(n)%units)
vlong = trim(b3d(n)%long_name)
vfill = b3d(n)%var_fillvalue
rc = nf90_def_var(ncid, vname, nf90_float, (/idimid,jdimid,kdimid,timid/), varid)
rc = nf90_put_att(ncid, varid, 'units', vunit)
rc = nf90_put_att(ncid, varid, 'long_name', vlong)
rc = nf90_put_att(ncid, varid, '_FillValue', vfill)
call nf90_err(nf90_def_var(ncid, vname, nf90_float, (/idimid,jdimid,kdimid,timid/), varid), 'define variable: '// vname)
call nf90_err(nf90_put_att(ncid, varid, 'units', vunit), 'put variable attribute: units' )
call nf90_err(nf90_put_att(ncid, varid, 'long_name', vlong), 'put variable attribute: long_name' )
call nf90_err(nf90_put_att(ncid, varid, '_FillValue', vfill), 'put variable attribute: FillValue' )
enddo
end if
rc = nf90_enddef(ncid)
call nf90_err(nf90_enddef(ncid), 'enddef: '// trim(fout))

! dimensions
rc = nf90_inq_varid(ncid, 'longitude', varid)
rc = nf90_put_var(ncid, varid, dstlon(:,1))
rc = nf90_inq_varid(ncid, 'latitude', varid)
rc = nf90_put_var(ncid, varid, dstlat(1,:))
call nf90_err(nf90_inq_varid(ncid, 'longitude', varid), 'get variable Id: longitude')
call nf90_err(nf90_put_var(ncid, varid, dstlon(:,1)), 'put variable: longitude')
call nf90_err(nf90_inq_varid(ncid, 'latitude', varid), 'get variable Id: latitude')
call nf90_err(nf90_put_var(ncid, varid, dstlat(1,:)), 'put variable: latitude')
! time
rc = nf90_inq_varid(ncid, 'time', varid)
rc = nf90_put_var(ncid, varid, timestamp)
call nf90_err(nf90_inq_varid(ncid, 'time', varid), 'get variable Id: time')
call nf90_err(nf90_put_var(ncid, varid, timestamp), 'put variable: time')
! vertical
if (do_ocnpost) then
rc = nf90_inq_varid(ncid, 'z_l', varid)
rc = nf90_put_var(ncid, varid, z_l)
rc = nf90_inq_varid(ncid, 'z_i', varid)
rc = nf90_put_var(ncid, varid, z_i)
call nf90_err(nf90_inq_varid(ncid, 'z_l', varid), 'get variable Id: z_l')
call nf90_err(nf90_put_var(ncid, varid, z_l) , 'put variable: z_l')
call nf90_err(nf90_inq_varid(ncid, 'z_i', varid), 'get variable Id: z_i')
call nf90_err(nf90_put_var(ncid, varid, z_i) , 'put variable: z_i')
end if
if (allocated(rgb2d)) then
do n = 1,nbilin2d
out2d(:,:) = reshape(rgb2d(:,n), (/nxr,nyr/))
out2d(:,nyr) = vfill
vname = trim(b2d(n)%var_name)
rc = nf90_inq_varid(ncid, vname, varid)
rc = nf90_put_var(ncid, varid, out2d)
call nf90_err(nf90_inq_varid(ncid, vname, varid), 'get variable Id: '//vname)
call nf90_err(nf90_put_var(ncid, varid, out2d), 'put variable: '//vname)
end do
end if
if (allocated(rgc2d)) then
do n = 1,nconsd2d
out2d(:,:) = reshape(rgc2d(:,n), (/nxr,nyr/))
out2d(:,nyr) = vfill
vname = trim(c2d(n)%var_name)
rc = nf90_inq_varid(ncid, vname, varid)
rc = nf90_put_var(ncid, varid, out2d)
call nf90_err(nf90_inq_varid(ncid, vname, varid), 'get variable Id: '//vname)
call nf90_err(nf90_put_var(ncid, varid, out2d), 'put variable: '//vname)
end do
end if
if (allocated(rgb3d)) then
do n = 1,nbilin3d
out3d(:,:,:) = reshape(rgb3d(:,:,n), (/nxr,nyr,nlevs/))
out3d(:,nyr,:) = vfill
vname = trim(b3d(n)%var_name)
rc = nf90_inq_varid(ncid, vname, varid)
rc = nf90_put_var(ncid, varid, out3d)
call nf90_err(nf90_inq_varid(ncid, vname, varid), 'get variable Id: '//vname)
call nf90_err(nf90_put_var(ncid, varid, out3d), 'put variable: '//vname)
end do
end if
rc = nf90_close(ncid)
call nf90_err(nf90_close(ncid), 'close: '// trim(fout))
write(logunit,'(a)')trim(fout)//' done'

end program ocnicepost
Loading

0 comments on commit 65ed705

Please sign in to comment.