Skip to content

Commit

Permalink
Merge pull request NASA-LIS#734 from emkemp/fix/read_CDFdata_memory
Browse files Browse the repository at this point in the history
Reorganized read_CDFdata_month to reduce memory usage.
  • Loading branch information
jvgeiger authored Mar 10, 2021
2 parents f67633e + a706541 commit 2fea888
Showing 1 changed file with 60 additions and 47 deletions.
107 changes: 60 additions & 47 deletions lis/core/LIS_dataAssimMod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module LIS_dataAssimMod
! 27 Feb 2005 Sujay Kumar; Initial Specification
! 7 Sep 2017 Mahdi Navari; set a condition to deal the skewness of the both model and obs CDF
! 1 Apr 2019 Yonghwan Kwon; include an option to read soil moisture CDF information for each month separately
! 5 Mar 2021 Eric Kemp; reduced memory usage in read_CDFdata_month
!
! !USES:
use ESMF
Expand Down Expand Up @@ -998,15 +999,15 @@ subroutine read_CDFdata_all(n, k, nbins, ntimes, ngrid, &
end subroutine read_CDFdata_all

!BOP
! !ROUTINE: read_CDFdata_month
! !ROUTINE: read_CDFdata_month
! \label{read_CDFdata_month}
!
! !INTERFACE:
! !INTERFACE:
subroutine read_CDFdata_month(n, k, nbins, ntimes, ngrid, &
filename, varname, xrange, cdf, imonth)

implicit none
! !ARGUMENTS:
! !ARGUMENTS:
integer, intent(in) :: n
integer, intent(in) :: k
integer, intent(in) :: nbins
Expand All @@ -1015,16 +1016,16 @@ subroutine read_CDFdata_month(n, k, nbins, ntimes, ngrid, &
integer, intent(in) :: imonth
character(len=*) :: filename
character(len=*) :: varname
real :: xrange(ngrid,1, nbins)
real :: cdf(ngrid,1, nbins)
real :: xrange(ngrid, 1, nbins)
real :: cdf(ngrid, 1, nbins)

!
! !DESCRIPTION:
!
! !DESCRIPTION:
! This routine reads the input CDF file (generated by LDT in NETCDF format)
! The xrange values and the corresponding CDFs are read for each grid point.
! Both these fields are expected to be in the 1-d grid vector dimension.
!
! The arguments are:
! The xrange values and the corresponding CDFs are read for each grid point.
! Both these fields are expected to be in the 1-d grid vector dimension.
!
! The arguments are:
! \begin{description}
! \item[n] index of the nest
! \item[nbins] number of bins used to compute the model and obs CDFs
Expand All @@ -1044,74 +1045,86 @@ subroutine read_CDFdata_month(n, k, nbins, ntimes, ngrid, &

TRACE_ENTER("DA_readCDF")
#if(defined USE_NETCDF3 || defined USE_NETCDF4)
write(LIS_logunit,*) '[INFO] Reading CDF file ',trim(filename)
write(LIS_logunit,*) '[INFO] Reading monthly CDF file ', trim(filename)
if(ngrid.gt.0) then
call LIS_verify(nf90_open(path=trim(filename),mode=NF90_NOWRITE,&
ncid=nid),'failed to open file '//trim(filename))
call LIS_verify(nf90_open(path=trim(filename), mode=NF90_NOWRITE, &
ncid=nid), 'failed to open file '//trim(filename))

call LIS_verify(nf90_inq_dimid(nid,"ngrid",ngridId), &
call LIS_verify(nf90_inq_dimid(nid, "ngrid", ngridId), &
'nf90_inq_dimid failed for ngrid')
call LIS_verify(nf90_inq_dimid(nid,"nbins",nbinsId), &
call LIS_verify(nf90_inq_dimid(nid, "nbins", nbinsId), &
'nf90_inq_dimid failed for nbins')
call LIS_verify(nf90_inq_dimid(nid,trim(varname)//"_levels",nlevsId), &
call LIS_verify(nf90_inq_dimid(nid, &
trim(varname)//"_levels", nlevsId), &
'nf90_inq_dimid failed for '//trim(varname)//"_levels")

call LIS_verify(nf90_inquire_dimension(nid,ngridId, len=ngrid_file),&
call LIS_verify(nf90_inquire_dimension(nid, ngridId, len=ngrid_file),&
'nf90_inquire_dimension failed for ngrid')
call LIS_verify(nf90_inquire_dimension(nid,nbinsId, len=nbins_file),&
call LIS_verify(nf90_inquire_dimension(nid, nbinsId, len=nbins_file),&
'nf90_inquire_dimension failed for nbins')
call LIS_verify(nf90_inquire_dimension(nid,nlevsId, len=nlevs_file),&
call LIS_verify(nf90_inquire_dimension(nid, nlevsId, len=nlevs_file),&
'nf90_inquire_dimension failed for nbins')

if(nbins.ne.nbins_file) then
write(LIS_logunit,*) '[ERR] The number of bins specified in the file '//&
if (nbins .ne. nbins_file) then
write(LIS_logunit,*) &
'[ERR] The number of bins specified in the file '//&
trim(filename)
write(LIS_logunit,*) '[ERR] (',nbins_file, &
write(LIS_logunit,*) '[ERR] (', nbins_file, &
') is different from the number of bins specified'
write(LIS_logunit,*) '[ERR] in the lis.config file (',nbins,')'
write(LIS_logunit,*) '[ERR] in the lis.config file (', nbins, ')'
call LIS_endrun()
endif

allocate(xrange_file(ngrid_file,nlevs_file, nbins))
allocate(cdf_file(ngrid_file,nlevs_file, nbins))
! EMK...Reorganized code to reduce memory usage. The _xrange file data
! is handled first, then the _CDF file data.

call LIS_verify(nf90_inq_varid(nid,trim(varname)//'_xrange',xid),&
! First, handle _xrange
call LIS_verify(nf90_inq_varid(nid,trim(varname)//'_xrange', xid),&
'nf90_inq_varid failed for for '//trim(varname)//'_xrange')
call LIS_verify(nf90_inq_varid(nid,trim(varname)//'_CDF',cdfid),&
'nf90_inq_varid failed for '//trim(varname)//'_CDF')

call LIS_verify(nf90_get_var(nid,xid,xrange_file, &
start=(/1,imonth,1,1/), count=(/ngrid_file,1,nlevs_file,nbins/)),&
allocate(xrange_file(ngrid_file, nlevs_file, nbins))
xrange_file = 0
call LIS_verify(nf90_get_var(nid, xid, xrange_file, &
start=(/1, imonth, 1, 1/), &
count=(/ngrid_file, 1, nlevs_file,nbins/)),&
'nf90_get_var failed for '//trim(varname)//'_xrange')
call LIS_verify(nf90_get_var(nid,cdfid,cdf_file,&
start=(/1,imonth,1,1/), count=(/ngrid_file,1,nlevs_file,nbins/)),&
'nf90_get_var failed for '//trim(varname)//'_CDF')

if(ngrid.gt.0) then
do kk=1,nbins
call LIS_convertObsVarToLocalSpace(n,k,xrange_file(:,1,kk), &
if (ngrid .gt. 0) then
do kk = 1, nbins
call LIS_convertObsVarToLocalSpace(n, k, &
xrange_file(:,1,kk), &
xrange(:,1,kk))
call LIS_convertObsVarToLocalSpace(n,k,cdf_file(:,1,kk), &
cdf(:,1,kk))
enddo
endif

! xrange = xrange_file
! cdf = cdf_file

deallocate(xrange_file)

! Next, handle _CDF
call LIS_verify(nf90_inq_varid(nid, trim(varname)//'_CDF', cdfid),&
'nf90_inq_varid failed for '//trim(varname)//'_CDF')
allocate(cdf_file(ngrid_file, nlevs_file, nbins))
cdf_file = 0
call LIS_verify(nf90_get_var(nid, cdfid, cdf_file,&
start=(/1, imonth, 1, 1/), &
count=(/ngrid_file, 1, nlevs_file, nbins/)),&
'nf90_get_var failed for '//trim(varname)//'_CDF')
if (ngrid .gt. 0) then
do kk = 1, nbins
call LIS_convertObsVarToLocalSpace(n, k, cdf_file(:,1,kk), &
cdf(:,1,kk))
enddo
endif
deallocate(cdf_file)

! Finish with the file
call LIS_verify(nf90_close(nid),&
'failed to close file '//trim(filename))
write(LIS_logunit,*) '[INFO] Successfully read CDF file ',trim(filename)
write(LIS_logunit,*) '[INFO] Successfully read CDF file ', &
trim(filename)
endif
#endif
TRACE_EXIT("DA_readCDF")
end subroutine read_CDFdata_month

!BOP
! !ROUTINE: read_MeanSigmaData_all
! !ROUTINE: read_MeanSigmaData_all
! \label{read_MeanSigmaData_all}
!
! !INTERFACE:
Expand Down

0 comments on commit 2fea888

Please sign in to comment.