Skip to content

Commit

Permalink
Move write of output file to its own routine.
Browse files Browse the repository at this point in the history
  • Loading branch information
George Gayno committed Oct 1, 2024
1 parent da2fd09 commit f013a59
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
2 changes: 1 addition & 1 deletion sorc/ocean_merge.fd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(lib_src merge.F90 utils.F90 namelist.F90)
set(lib_src read_write.F90 merge.F90 utils.F90 namelist.F90)
set(exe_src merge_lake_ocnmsk.F90)

if(CMAKE_Fortran_COMPILER_ID MATCHES "^(Intel)$")
Expand Down
23 changes: 3 additions & 20 deletions sorc/ocean_merge.fd/merge_lake_ocnmsk.F90
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ program merge_lake_ocnmsk
integer :: binary_lake

character(len=250) :: flnm
integer :: ncid,ndims,nvars,natts,lat,lon,v1id,v2id,v3id,v4id,start(2),count(2),latid,lonid,ncid4, dims(2),tile
integer :: ncid,ndims,nvars,natts,lat,lon,v1id,v2id,v3id,start(2),count(2),latid,lonid,tile
integer :: vlat
real, allocatable :: lake_frac(:,:),lake_depth(:,:),land_frac(:,:),ocn_frac(:,:),slmsk(:,:),lat2d(:,:)

Expand Down Expand Up @@ -69,25 +69,8 @@ program merge_lake_ocnmsk

call merge(lon, lat, binary_lake, lat2d, ocn_frac, lake_frac, lake_depth, land_frac, slmsk)

write(flnm,'(4a,i1,a)') trim(atmres),'.',trim(ocnres),'.tile',tile,'.nc'
print *,'output=',trim(flnm)
call handle_err (nf90_create (path=trim(pth3)//trim(flnm), &
cmode=or(NF90_CLOBBER, NF90_64BIT_OFFSET), ncid=ncid4)) ! netcdf3

call handle_err (nf90_def_dim (ncid4,'lon', lon, dims(1)))
call handle_err (nf90_def_dim (ncid4,'lat', lat, dims(2)))
call handle_err (nf90_def_var (ncid4,'land_frac', nf90_float, dims(1:2), v1id))
call handle_err (nf90_def_var (ncid4,'lake_frac', nf90_float, dims(1:2), v2id))
call handle_err (nf90_def_var (ncid4,'lake_depth',nf90_float, dims(1:2), v3id))
call handle_err (nf90_def_var (ncid4,'slmsk', nf90_float, dims(1:2), v4id))

call handle_err (nf90_enddef (ncid4))

call handle_err (nf90_put_var (ncid4, v1id,land_frac))
call handle_err (nf90_put_var (ncid4, v2id,lake_frac))
call handle_err (nf90_put_var (ncid4, v3id,lake_depth))
call handle_err (nf90_put_var (ncid4, v4id,slmsk))
call handle_err (nf90_close(ncid4))
call write_data(atmres,ocnres,pth3,tile,lon,lat,land_frac, &
lake_frac,lake_depth,slmsk)

end do ! tile

Expand Down
56 changes: 56 additions & 0 deletions sorc/ocean_merge.fd/read_write.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
!> Write the merged data to a NetCDF file. Each tile is in
!! its own file.
!!
!! @param[in] atmres Atmospheric resolution.
!! @param[in] ocnres Ocean resolution.
!! @param[in] pth3 Diretory path to output file.
!! @param[in] tile Tile number.
!! @param[in] lon E/W dimension of tile.
!! @param[in] lat N/S dimension of tile.
!! @param[in] land_frac Land fraction.
!! @param[in] lake_frac Lake fraction.
!! @param[in] lake_depth Lake depth.
!! @param[in] slmsk Land/sea mask.
!!
!! @author Shan Sun
!! @author Rahul Mahajan
!! @author Sanath Kumar
subroutine write_data(atmres,ocnres,pth3,tile,lon,lat,land_frac, &
lake_frac,lake_depth,slmsk)

use netcdf

implicit none

character(len=*), intent(in) :: atmres, ocnres, pth3

integer, intent(in) :: tile, lon, lat

real, intent(in) :: land_frac(lon,lat), lake_frac(lon,lat)
real, intent(in) :: lake_depth(lon,lat), slmsk(lon,lat)

character(len=250) :: flnm

integer :: ncid4, dims(2), v1id, v2id, v3id, v4id

write(flnm,'(4a,i1,a)') trim(atmres),'.',trim(ocnres),'.tile',tile,'.nc'
print *,'output data to file= ',trim(flnm)
call handle_err (nf90_create (path=trim(pth3)//trim(flnm), &
cmode=or(NF90_CLOBBER, NF90_64BIT_OFFSET), ncid=ncid4)) ! netcdf3

call handle_err (nf90_def_dim (ncid4,'lon', lon, dims(1)))
call handle_err (nf90_def_dim (ncid4,'lat', lat, dims(2)))
call handle_err (nf90_def_var (ncid4,'land_frac', nf90_float, dims(1:2), v1id))
call handle_err (nf90_def_var (ncid4,'lake_frac', nf90_float, dims(1:2), v2id))
call handle_err (nf90_def_var (ncid4,'lake_depth',nf90_float, dims(1:2), v3id))
call handle_err (nf90_def_var (ncid4,'slmsk', nf90_float, dims(1:2), v4id))

call handle_err (nf90_enddef (ncid4))

call handle_err (nf90_put_var (ncid4, v1id,land_frac))
call handle_err (nf90_put_var (ncid4, v2id,lake_frac))
call handle_err (nf90_put_var (ncid4, v3id,lake_depth))
call handle_err (nf90_put_var (ncid4, v4id,slmsk))
call handle_err (nf90_close(ncid4))

end subroutine write_data

0 comments on commit f013a59

Please sign in to comment.