Skip to content

Commit

Permalink
Convert routine 'write_netcdf' to use f90 netcdf routines.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGayno-NOAA committed Jan 10, 2025
1 parent 8a15fc3 commit 99a2e4f
Showing 1 changed file with 69 additions and 68 deletions.
137 changes: 69 additions & 68 deletions sorc/orog_mask_tools.fd/orog.fd/io_utils.F90
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module io_utils
!! @param[in] lat Latitude of the first column of the model grid tile.
!! @author Jordan Alpert NOAA/EMC GFDL Programmer
subroutine write_netcdf(im, jm, slm, land_frac, oro, hprime, ntiles, tile, geolon, geolat, lon, lat)
use netcdf
implicit none
integer, intent(in):: im, jm, ntiles, tile
real, intent(in) :: lon(im), lat(jm)
Expand All @@ -56,7 +57,6 @@ subroutine write_netcdf(im, jm, slm, land_frac, oro, hprime, ntiles, tile, geolo
integer :: id_oa1,id_oa2,id_oa3,id_oa4
integer :: id_ol1,id_ol2,id_ol3,id_ol4
integer :: id_theta,id_gamma,id_sigma,id_elvmax
include "netcdf.inc"

if(ntiles > 1) then
write(outfile, '(a,i4.4,a)') 'out.oro.tile', tile, '.nc'
Expand All @@ -68,164 +68,165 @@ subroutine write_netcdf(im, jm, slm, land_frac, oro, hprime, ntiles, tile, geolo
dim2=size(lat,1)

!--- open the file
error = NF__CREATE(outfile, IOR(NF_NETCDF4,NF_CLASSIC_MODEL), inital, fsize, ncid)
error = nf90_create(outfile, IOR(NF90_NETCDF4,NF90_CLASSIC_MODEL), ncid, &
initialsize=inital, chunksize=fsize)
call netcdf_err(error, 'Creating file '//trim(outfile) )
!--- define dimension
error = nf_def_dim(ncid, 'lon', dim1, dim_lon)
error = nf90_def_dim(ncid, 'lon', dim1, dim_lon)
call netcdf_err(error, 'define dimension lon for file='//trim(outfile) )
error = nf_def_dim(ncid, 'lat', dim2, dim_lat)
error = nf90_def_dim(ncid, 'lat', dim2, dim_lat)
call netcdf_err(error, 'define dimension lat for file='//trim(outfile) )

!--- define field
!---geolon
error = nf_def_var(ncid, 'geolon', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_geolon)
error = nf90_def_var(ncid, 'geolon', NF90_FLOAT, (/dim_lon,dim_lat/), id_geolon)
call netcdf_err(error, 'define var geolon for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_geolon, "long_name", 9, "Longitude")
error = nf90_put_att(ncid, id_geolon, "long_name", "Longitude")
call netcdf_err(error, 'define geolon name for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_geolon, "units", 12, "degrees_east")
error = nf90_put_att(ncid, id_geolon, "units", "degrees_east")
call netcdf_err(error, 'define geolon units for file='//trim(outfile) )
!---geolat
error = nf_def_var(ncid, 'geolat', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_geolat)
error = nf90_def_var(ncid, 'geolat', NF90_FLOAT, (/dim_lon,dim_lat/), id_geolat)
call netcdf_err(error, 'define var geolat for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_geolat, "long_name", 8, "Latitude")
error = nf90_put_att(ncid, id_geolat, "long_name", "Latitude")
call netcdf_err(error, 'define geolat name for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_geolat, "units", 13, "degrees_north")
error = nf90_put_att(ncid, id_geolat, "units", "degrees_north")
call netcdf_err(error, 'define geolat units for file='//trim(outfile) )
!---slmsk
error = nf_def_var(ncid, 'slmsk', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_slmsk)
error = nf90_def_var(ncid, 'slmsk', NF90_FLOAT,(/dim_lon,dim_lat/), id_slmsk)
call netcdf_err(error, 'define var slmsk for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_slmsk, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_slmsk, "coordinates", "geolon geolat")
call netcdf_err(error, 'define slmsk coordinates for file='//trim(outfile) )
!--- land_frac
error = nf_def_var(ncid, 'land_frac', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_land_frac)
error = nf90_def_var(ncid, 'land_frac', NF90_FLOAT, (/dim_lon,dim_lat/), id_land_frac)
call netcdf_err(error, 'define var land_frac for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_land_frac, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_land_frac, "coordinates", "geolon geolat")
call netcdf_err(error, 'define land_frac coordinates for file='//trim(outfile) )
!---orography - raw
error = nf_def_var(ncid, 'orog_raw', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_orog_raw)
error = nf90_def_var(ncid, 'orog_raw', NF90_FLOAT, (/dim_lon,dim_lat/), id_orog_raw)
call netcdf_err(error, 'define var orog_raw for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_orog_raw, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_orog_raw, "coordinates", "geolon geolat")
call netcdf_err(error, 'define orog_raw coordinates for file='//trim(outfile) )
!---orography - filtered
error = nf_def_var(ncid, 'orog_filt', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_orog_filt)
error = nf90_def_var(ncid, 'orog_filt', NF90_FLOAT, (/dim_lon,dim_lat/), id_orog_filt)
call netcdf_err(error, 'define var orog_filt for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_orog_filt, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_orog_filt, "coordinates", "geolon geolat")
call netcdf_err(error, 'define orog_filt coordinates for file='//trim(outfile) )
!---stddev
error = nf_def_var(ncid, 'stddev', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_stddev)
error = nf90_def_var(ncid, 'stddev', NF90_FLOAT, (/dim_lon,dim_lat/), id_stddev)
call netcdf_err(error, 'define var stddev for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_stddev, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_stddev, "coordinates", "geolon geolat")
call netcdf_err(error, 'define stddev coordinates for file='//trim(outfile) )
!---convexity
error = nf_def_var(ncid, 'convexity', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_convex)
error = nf90_def_var(ncid, 'convexity', NF90_FLOAT, (/dim_lon,dim_lat/), id_convex)
call netcdf_err(error, 'define var convexity for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_convex, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_convex, "coordinates", "geolon geolat")
call netcdf_err(error, 'define convexity coordinates for file='//trim(outfile) )
!---oa1 -> oa4
error = nf_def_var(ncid, 'oa1', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_oa1)
error = nf90_def_var(ncid, 'oa1', NF90_FLOAT, (/dim_lon,dim_lat/), id_oa1)
call netcdf_err(error, 'define var oa1 for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_oa1, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_oa1, "coordinates", "geolon geolat")
call netcdf_err(error, 'define oa1 coordinates for file='//trim(outfile) )
error = nf_def_var(ncid, 'oa2', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_oa2)
error = nf90_def_var(ncid, 'oa2', NF90_FLOAT, (/dim_lon,dim_lat/), id_oa2)
call netcdf_err(error, 'define var oa2 for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_oa2, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_oa2, "coordinates", "geolon geolat")
call netcdf_err(error, 'define oa2 coordinates for file='//trim(outfile) )
error = nf_def_var(ncid, 'oa3', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_oa3)
error = nf90_def_var(ncid, 'oa3', NF90_FLOAT, (/dim_lon,dim_lat/), id_oa3)
call netcdf_err(error, 'define var oa3 for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_oa3, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_oa3, "coordinates", "geolon geolat")
call netcdf_err(error, 'define oa3 coordinates for file='//trim(outfile) )
error = nf_def_var(ncid, 'oa4', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_oa4)
error = nf90_def_var(ncid, 'oa4', NF90_FLOAT, (/dim_lon,dim_lat/), id_oa4)
call netcdf_err(error, 'define var oa4 for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_oa4, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_oa4, "coordinates", "geolon geolat")
call netcdf_err(error, 'define oa4 coordinates for file='//trim(outfile) )
!---ol1 -> ol4
error = nf_def_var(ncid, 'ol1', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_ol1)
error = nf90_def_var(ncid, 'ol1', NF90_FLOAT, (/dim_lon,dim_lat/), id_ol1)
call netcdf_err(error, 'define var ol1 for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_ol1, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_ol1, "coordinates", "geolon geolat")
call netcdf_err(error, 'define ol1 coordinates for file='//trim(outfile) )
error = nf_def_var(ncid, 'ol2', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_ol2)
error = nf90_def_var(ncid, 'ol2', NF90_FLOAT, (/dim_lon,dim_lat/), id_ol2)
call netcdf_err(error, 'define var ol2 for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_ol2, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_ol2, "coordinates", "geolon geolat")
call netcdf_err(error, 'define ol2 coordinates for file='//trim(outfile) )
error = nf_def_var(ncid, 'ol3', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_ol3)
error = nf90_def_var(ncid, 'ol3', NF90_FLOAT, (/dim_lon,dim_lat/), id_ol3)
call netcdf_err(error, 'define var ol3 for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_ol3, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_ol3, "coordinates", "geolon geolat")
call netcdf_err(error, 'define ol3 coordinates for file='//trim(outfile) )
error = nf_def_var(ncid, 'ol4', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_ol4)
error = nf90_def_var(ncid, 'ol4', NF90_FLOAT, (/dim_lon,dim_lat/), id_ol4)
call netcdf_err(error, 'define var ol4 for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_ol4, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_ol4, "coordinates", "geolon geolat")
call netcdf_err(error, 'define ol4 coordinates for file='//trim(outfile) )
!---theta gamma sigma elvmax
error = nf_def_var(ncid, 'theta', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_theta)
error = nf90_def_var(ncid, 'theta', NF90_FLOAT, (/dim_lon,dim_lat/), id_theta)
call netcdf_err(error, 'define var theta for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_theta, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_theta, "coordinates", "geolon geolat")
call netcdf_err(error, 'define theta coordinates for file='//trim(outfile) )
error = nf_def_var(ncid, 'gamma', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_gamma)
error = nf90_def_var(ncid, 'gamma', NF90_FLOAT, (/dim_lon,dim_lat/), id_gamma)
call netcdf_err(error, 'define var gamma for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_gamma, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_gamma, "coordinates", "geolon geolat")
call netcdf_err(error, 'define gamma coordinates for file='//trim(outfile) )
error = nf_def_var(ncid, 'sigma', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_sigma)
error = nf90_def_var(ncid, 'sigma', NF90_FLOAT, (/dim_lon,dim_lat/), id_sigma)
call netcdf_err(error, 'define var sigma for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_sigma, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_sigma, "coordinates", "geolon geolat")
call netcdf_err(error, 'define sigma coordinates for file='//trim(outfile) )
error = nf_def_var(ncid, 'elvmax', NF_FLOAT, 2, (/dim_lon,dim_lat/), id_elvmax)
error = nf90_def_var(ncid, 'elvmax', NF90_FLOAT, (/dim_lon,dim_lat/), id_elvmax)
call netcdf_err(error, 'define var elvmax for file='//trim(outfile) )
error = nf_put_att_text(ncid, id_elvmax, "coordinates", 13, "geolon geolat")
error = nf90_put_att(ncid, id_elvmax, "coordinates", "geolon geolat")
call netcdf_err(error, 'define elvmax coordinates for file='//trim(outfile) )

error = nf__enddef(ncid, header_buffer_val,4,0,4)
error = nf90_enddef(ncid, header_buffer_val,4,0,4)
call netcdf_err(error, 'end meta define for file='//trim(outfile) )

!--- write out data
error = nf_put_var_double( ncid, id_geolon, geolon(:dim1,:dim2))
error = nf90_put_var( ncid, id_geolon, geolon(:dim1,:dim2))
call netcdf_err(error, 'write var geolon for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_geolat, geolat(:dim1,:dim2))
error = nf90_put_var( ncid, id_geolat, geolat(:dim1,:dim2))
call netcdf_err(error, 'write var geolat for file='//trim(outfile) )

error = nf_put_var_double( ncid, id_slmsk, slm(:dim1,:dim2))
error = nf90_put_var( ncid, id_slmsk, slm(:dim1,:dim2))
call netcdf_err(error, 'write var slmsk for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_land_frac, land_frac(:dim1,:dim2))
error = nf90_put_var( ncid, id_land_frac, land_frac(:dim1,:dim2))
call netcdf_err(error, 'write var land_frac for file='//trim(outfile) )

error = nf_put_var_double( ncid, id_orog_raw, oro(:dim1,:dim2))
error = nf90_put_var( ncid, id_orog_raw, oro(:dim1,:dim2))
call netcdf_err(error, 'write var orog_raw for file='//trim(outfile) )
! We no longer filter the orog, so the raw and filtered records are the same.
error = nf_put_var_double( ncid, id_orog_filt, oro(:dim1,:dim2))
error = nf90_put_var( ncid, id_orog_filt, oro(:dim1,:dim2))
call netcdf_err(error, 'write var orog_filt for file='//trim(outfile) )

error = nf_put_var_double( ncid, id_stddev, hprime(:dim1,:dim2,1))
error = nf90_put_var( ncid, id_stddev, hprime(:dim1,:dim2,1))
call netcdf_err(error, 'write var stddev for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_convex, hprime(:dim1,:dim2,2))
error = nf90_put_var( ncid, id_convex, hprime(:dim1,:dim2,2))
call netcdf_err(error, 'write var convex for file='//trim(outfile) )

error = nf_put_var_double( ncid, id_oa1, hprime(:dim1,:dim2,3))
error = nf90_put_var( ncid, id_oa1, hprime(:dim1,:dim2,3))
call netcdf_err(error, 'write var oa1 for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_oa2, hprime(:dim1,:dim2,4))
error = nf90_put_var( ncid, id_oa2, hprime(:dim1,:dim2,4))
call netcdf_err(error, 'write var oa2 for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_oa3, hprime(:dim1,:dim2,5))
error = nf90_put_var( ncid, id_oa3, hprime(:dim1,:dim2,5))
call netcdf_err(error, 'write var oa3 for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_oa4, hprime(:dim1,:dim2,6))
error = nf90_put_var( ncid, id_oa4, hprime(:dim1,:dim2,6))
call netcdf_err(error, 'write var oa4 for file='//trim(outfile) )

error = nf_put_var_double( ncid, id_ol1, hprime(:dim1,:dim2,7))
error = nf90_put_var( ncid, id_ol1, hprime(:dim1,:dim2,7))
call netcdf_err(error, 'write var ol1 for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_ol2, hprime(:dim1,:dim2,8))
error = nf90_put_var( ncid, id_ol2, hprime(:dim1,:dim2,8))
call netcdf_err(error, 'write var ol2 for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_ol3, hprime(:dim1,:dim2,9))
error = nf90_put_var( ncid, id_ol3, hprime(:dim1,:dim2,9))
call netcdf_err(error, 'write var ol3 for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_ol4, hprime(:dim1,:dim2,10))
error = nf90_put_var( ncid, id_ol4, hprime(:dim1,:dim2,10))
call netcdf_err(error, 'write var ol4 for file='//trim(outfile) )

error = nf_put_var_double( ncid, id_theta, hprime(:dim1,:dim2,11))
error = nf90_put_var( ncid, id_theta, hprime(:dim1,:dim2,11))
call netcdf_err(error, 'write var theta for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_gamma, hprime(:dim1,:dim2,12))
error = nf90_put_var( ncid, id_gamma, hprime(:dim1,:dim2,12))
call netcdf_err(error, 'write var gamma for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_sigma, hprime(:dim1,:dim2,13))
error = nf90_put_var( ncid, id_sigma, hprime(:dim1,:dim2,13))
call netcdf_err(error, 'write var sigma for file='//trim(outfile) )
error = nf_put_var_double( ncid, id_elvmax, hprime(:dim1,:dim2,14))
error = nf90_put_var( ncid, id_elvmax, hprime(:dim1,:dim2,14))
call netcdf_err(error, 'write var elvmax for file='//trim(outfile) )

error = nf_close(ncid)
error = nf90_close(ncid)
call netcdf_err(error, 'close file='//trim(outfile) )

end subroutine write_netcdf
Expand Down

0 comments on commit 99a2e4f

Please sign in to comment.