From 43677eab35540ffceb827dd45c13a1780b5ba1d6 Mon Sep 17 00:00:00 2001 From: rem1776 Date: Mon, 15 Jul 2024 11:24:01 -0400 Subject: [PATCH 1/7] rename error check macro and add syncs --- postprocessing/land_utils/combine-ncc.F90 | 61 ++++++++++--------- postprocessing/land_utils/decompress-ncc.F90 | 58 +++++++++--------- postprocessing/land_utils/scatter-ncc.F90 | 60 +++++++++--------- tools/simple_hydrog/lakes/Makefile.am | 2 +- tools/simple_hydrog/lakes/cr_lake_files.f90 | 17 ++++-- .../rmvpr/rmv_parallel_rivers.f90 | 11 +++- 6 files changed, 113 insertions(+), 96 deletions(-) diff --git a/postprocessing/land_utils/combine-ncc.F90 b/postprocessing/land_utils/combine-ncc.F90 index f40891af..0ea95a44 100644 --- a/postprocessing/land_utils/combine-ncc.F90 +++ b/postprocessing/land_utils/combine-ncc.F90 @@ -25,7 +25,7 @@ ! !----------------------------------------------------------------------- -#define __NF_ASRT__(ierr) call nfu_check_err(ierr,__FILE__,__LINE__) +#define CHECK_NF_ERRSTAT(ierr) call nfu_check_err(ierr,__FILE__,__LINE__) program combine_res use nfu_mod @@ -82,8 +82,8 @@ program combine_res ! otherwise it's 32-bit allocate(input(nfiles)) do i = 1,nfiles - __NF_ASRT__(nf_open(files(i),NF_NOWRITE,input(i))) - __NF_ASRT__(nf_inq_format(input(i),in_format)) + CHECK_NF_ERRSTAT(nf_open(files(i),NF_NOWRITE,input(i))) + CHECK_NF_ERRSTAT(nf_inq_format(input(i),in_format)) enddo if (in_format==NF_FORMAT_NETCDF4) then @@ -104,18 +104,18 @@ program combine_res ! mpp_io supports environment variables to set these. For Riga, we'll simply use the defaults` - __NF_ASRT__(nf__create(outfile,cmode,0,blksz,ncid)) + CHECK_NF_ERRSTAT(nf__create(outfile,cmode,0,blksz,ncid)) ! Create netcdf structure in the output NetCDF file, using last input file ! as a template. ! clone all dimensions; for compressed dimensions calculate the total length in all ! input files - __NF_ASRT__(nf_inq_ndims(input(nfiles),ndims)) + CHECK_NF_ERRSTAT(nf_inq_ndims(input(nfiles),ndims)) allocate(dim(ndims)) do dimid = 1,ndims associate(d => dim(dimid)) - __NF_ASRT__(nfu_inq_dim(input(nfiles),dimid,dimname=d%name)) + CHECK_NF_ERRSTAT(nfu_inq_dim(input(nfiles),dimid,dimname=d%name)) call inquire_dimension(input(:), d%name, & len=dimlen, siz=d%buflen, compressed=d%compressed, is_unlim=has_records) d%len = dimlen @@ -133,32 +133,32 @@ program combine_res endif if(verbosity>0)& write(*,'(x,a,i8)')'defining dimension "'//trim(d%name)//'" with length',d%len - __NF_ASRT__(nf_def_dim(ncid,d%name,dimlen,i)) ! i is just a dummy var for dimid, unused + CHECK_NF_ERRSTAT(nf_def_dim(ncid,d%name,dimlen,i)) ! i is just a dummy var for dimid, unused end associate enddo ! clone all variable definitions - __NF_ASRT__(nf_inq_nvars(input(nfiles),nvars)) + CHECK_NF_ERRSTAT(nf_inq_nvars(input(nfiles),nvars)) do i = 1,nvars - __NF_ASRT__(nfu_clone_var(input(nfiles),i,ncid)) + CHECK_NF_ERRSTAT(nfu_clone_var(input(nfiles),i,ncid)) ! NOTE: since cloning of variable definition relies on dimension names, ! each variable tile and compressed dimensions automaticaly get the right ! size, as defined while creating dimensions in the output file enddo ! clone all global attributes - __NF_ASRT__(nf_inq_natts(input(nfiles),ngatts)) + CHECK_NF_ERRSTAT(nf_inq_natts(input(nfiles),ngatts)) do i = 1,ngatts - __NF_ASRT__(nf_inq_attname(input(nfiles),NF_GLOBAL,i,attname)) - __NF_ASRT__(nf_copy_att(input(nfiles),NF_GLOBAL,attname,ncid,NF_GLOBAL)) + CHECK_NF_ERRSTAT(nf_inq_attname(input(nfiles),NF_GLOBAL,i,attname)) + CHECK_NF_ERRSTAT(nf_copy_att(input(nfiles),NF_GLOBAL,attname,ncid,NF_GLOBAL)) enddo ! ---- end of definition stage - __NF_ASRT__(nf__enddef(ncid,HEADERPAD,4,0,4)) + CHECK_NF_ERRSTAT(nf__enddef(ncid,HEADERPAD,4,0,4)) ! copy all uncompressed vars do varid = 1, nvars - __NF_ASRT__(nfu_inq_var(input(nfiles),varid,name=varname,ndims=ndims,dimids=dimids,dimlens=dimlens,recsize=recsize,nrec=nrec,xtype=xtype)) + CHECK_NF_ERRSTAT(nfu_inq_var(input(nfiles),varid,name=varname,ndims=ndims,dimids=dimids,dimlens=dimlens,recsize=recsize,nrec=nrec,xtype=xtype)) n = 0 do k = 1,ndims if (dim(dimids(k))%compressed) n = n+1 @@ -175,15 +175,15 @@ program combine_res ! dimension anyway, and (3) there is no convenient interface (yet) in ! nfu utilities for by-record i/o for CHAR variables. allocate(text(recsize*nrec)) - __NF_ASRT__(nf_get_var_text(input(nfiles),varid,text)) - __NF_ASRT__(nfu_inq_var(ncid,varname,id=varid1)) - __NF_ASRT__(nf_put_var_text(ncid,varid1,text)) + CHECK_NF_ERRSTAT(nf_get_var_text(input(nfiles),varid,text)) + CHECK_NF_ERRSTAT(nfu_inq_var(ncid,varname,id=varid1)) + CHECK_NF_ERRSTAT(nf_put_var_text(ncid,varid1,text)) deallocate(text) else allocate(buffer(recsize)) do rec=1,nrec - __NF_ASRT__(nfu_get_rec_r8(input(nfiles),varid,rec,buffer)) - __NF_ASRT__(nfu_put_rec_r8(ncid,varname,rec,buffer)) + CHECK_NF_ERRSTAT(nfu_get_rec_r8(input(nfiles),varid,rec,buffer)) + CHECK_NF_ERRSTAT(nfu_put_rec_r8(ncid,varname,rec,buffer)) enddo deallocate(buffer) endif @@ -217,12 +217,12 @@ program combine_res ! process all vars that depend on this compressed dimension do varid = 1, nvars - __NF_ASRT__(nfu_inq_var(input(nfiles),varid,name=varname,ndims=ndims,dimids=dimids,dimlens=dimlens)) + CHECK_NF_ERRSTAT(nfu_inq_var(input(nfiles),varid,name=varname,ndims=ndims,dimids=dimids,dimlens=dimlens)) if (.not.any(dimids(1:ndims)==dimid)) cycle ! skip variables that do not depend on our compressed dim if(verbosity>0) write(*,'(2x,a)')'copy compressed variable "'//trim(varname)//'"' ! get the output variable ID - __NF_ASRT__(nfu_inq_var(ncid,varname,id=ovarid)) + CHECK_NF_ERRSTAT(nfu_inq_var(ncid,varname,id=ovarid)) ! find index of the compressed dimension cdim = 1 @@ -247,8 +247,8 @@ program combine_res k = 1 do ifile = 1,nfiles cnt(cdim) = sizes(ifile) - __NF_ASRT__(nfu_inq_var(input(ifile),varname,id=varid1)) - __NF_ASRT__(nf_get_vara_double(input(ifile),varid1,start,cnt,buffer(k))) + CHECK_NF_ERRSTAT(nfu_inq_var(input(ifile),varname,id=varid1)) + CHECK_NF_ERRSTAT(nf_get_vara_double(input(ifile),varid1,start,cnt,buffer(k))) k = k+sizes(ifile) enddo ! reshuffle variable values in desired order @@ -257,14 +257,15 @@ program combine_res enddo ! write slice to output file cnt(cdim) = d%len - __NF_ASRT__(nf_put_vara_double(ncid,ovarid,start,cnt,obuffer)) + CHECK_NF_ERRSTAT(nf_put_vara_double(ncid,ovarid,start,cnt,obuffer)) enddo enddo deallocate(rank, buffer, obuffer) end associate enddo - i = nf_close(ncid) + CHECK_NF_ERRSTAT(nf_sync(ncid)) + CHECK_NF_ERRSTAT(nf_close(ncid)) contains ! -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- subroutine inquire_dimension(input,name,len,compressed,siz,sizes,is_unlim) @@ -288,7 +289,7 @@ subroutine inquire_dimension(input,name,len,compressed,siz,sizes,is_unlim) integer :: i nfiles = size(input) - __NF_ASRT__(nfu_inq_dim(input(nfiles), name, dimlen=len_, is_unlim=is_unlim)) + CHECK_NF_ERRSTAT(nfu_inq_dim(input(nfiles), name, dimlen=len_, is_unlim=is_unlim)) if (present(len)) len = len_ if (present(siz)) siz = len_ if (present(sizes)) sizes(:)=len_ @@ -301,7 +302,7 @@ subroutine inquire_dimension(input,name,len,compressed,siz,sizes,is_unlim) ! calculate total size of the compressed dimension across all files do i=1,nfiles - __NF_ASRT__(nfu_inq_dim(input(i),name,dimlen=sizes_(i))) + CHECK_NF_ERRSTAT(nfu_inq_dim(input(i),name,dimlen=sizes_(i))) enddo if (present(sizes)) sizes(:) = sizes_(:) if (present(siz)) siz = sum(sizes_(:)) @@ -309,7 +310,7 @@ subroutine inquire_dimension(input,name,len,compressed,siz,sizes,is_unlim) len = 0 allocate(buff(maxval(sizes_(:)))) do i=1,nfiles - __NF_ASRT__(nfu_get_var_int(input(i),name,buff)) + CHECK_NF_ERRSTAT(nfu_get_var_int(input(i),name,buff)) len = len + count(buff(1:sizes_(i))>=0) enddo deallocate(buff) @@ -328,8 +329,8 @@ subroutine reorder_compressed_index(input,name,rank) allocate(buff(size(rank))) k = 1 do i=1,nfiles - __NF_ASRT__(nfu_get_var_int(input(i),name,buff(k:))) - __NF_ASRT__(nfu_inq_dim(input(i),name,dimlen=dimlen)) + CHECK_NF_ERRSTAT(nfu_get_var_int(input(i),name,buff(k:))) + CHECK_NF_ERRSTAT(nfu_inq_dim(input(i),name,dimlen=dimlen)) k = k+dimlen enddo diff --git a/postprocessing/land_utils/decompress-ncc.F90 b/postprocessing/land_utils/decompress-ncc.F90 index 16680a15..e1ecaa3f 100644 --- a/postprocessing/land_utils/decompress-ncc.F90 +++ b/postprocessing/land_utils/decompress-ncc.F90 @@ -25,7 +25,7 @@ ! !----------------------------------------------------------------------- -#define __NF_ASRT__(ierr) call nfu_check_err(ierr,__FILE__,__LINE__) +#define CHECK_NF_ERRSTAT(ierr) call nfu_check_err(ierr,__FILE__,__LINE__) program decompress use nfu_mod @@ -67,8 +67,8 @@ program decompress ! open all input files allocate(input(nfiles)) do i = 1,nfiles - __NF_ASRT__(nf_open(files(i),NF_NOWRITE,input(i))) - __NF_ASRT__(nf_inq_format(input(i),in_format)) + CHECK_NF_ERRSTAT(nf_open(files(i),NF_NOWRITE,input(i))) + CHECK_NF_ERRSTAT(nf_inq_format(input(i),in_format)) enddo if (in_format==NF_FORMAT_NETCDF4) then @@ -86,41 +86,41 @@ program decompress endif ! create output file - __NF_ASRT__(nf__create(outfile,cmode,0,blksz,ncid)) + CHECK_NF_ERRSTAT(nf__create(outfile,cmode,0,blksz,ncid)) ! Create netcdf structure in the output NetCDF file, using last input file ! as a template. ! clone all dimensions except compressed ones; compressed are just skipped - __NF_ASRT__(nf_inq_ndims(input(nfiles),ndims)) + CHECK_NF_ERRSTAT(nf_inq_ndims(input(nfiles),ndims)) do dimid = 1,ndims - __NF_ASRT__(nfu_inq_dim(input(nfiles),dimid,dimname=dimname,dimlen=dimlen,is_unlim=has_records)) + CHECK_NF_ERRSTAT(nfu_inq_dim(input(nfiles),dimid,dimname=dimname,dimlen=dimlen,is_unlim=has_records)) if(nfu_inq_att(input(nfiles),dimname,'compress')==NF_NOERR) cycle if(has_records)& dimlen=NF_UNLIMITED if(debug>0)& write(*,*)'defining dimension "'//trim(dimname)//'" with length',dimlen - __NF_ASRT__(nf_def_dim(ncid,dimname,dimlen,i)) ! i is just a space for id + CHECK_NF_ERRSTAT(nf_def_dim(ncid,dimname,dimlen,i)) ! i is just a space for id enddo ! clone all variable definitions, replacing compressed dimensions with sets ! of uncompressed ones - __NF_ASRT__(nf_inq_nvars(input(nfiles),nvars)) + CHECK_NF_ERRSTAT(nf_inq_nvars(input(nfiles),nvars)) do varid = 1,nvars iret = nfu_inq_compressed_var(input(nfiles),varid,varname,xtype,ndims,dimids,& natts=natts,is_dim=is_dim, is_compressed=is_compressed) if(debug>0)& write(*,*)'defining variable "'//trim(varname)//'"' - __NF_ASRT__(iret) ! just because the line is going to be too long + CHECK_NF_ERRSTAT(iret) ! just because the line is going to be too long if(is_dim.and.is_compressed) cycle do i = 1,ndims - __NF_ASRT__(nf_inq_dimname(input(nfiles),dimids(i),dimname)) - __NF_ASRT__(nf_inq_dimid(ncid,dimname,dimids(i))) + CHECK_NF_ERRSTAT(nf_inq_dimname(input(nfiles),dimids(i),dimname)) + CHECK_NF_ERRSTAT(nf_inq_dimid(ncid,dimname,dimids(i))) enddo - __NF_ASRT__(nf_def_var(ncid,varname,xtype,ndims,dimids,varid1)) + CHECK_NF_ERRSTAT(nf_def_var(ncid,varname,xtype,ndims,dimids,varid1)) do i=1,natts - __NF_ASRT__(nf_inq_attname(input(nfiles),varid,i,attname)) - __NF_ASRT__(nf_copy_att(input(nfiles),varid,attname,ncid,varid1)) + CHECK_NF_ERRSTAT(nf_inq_attname(input(nfiles),varid,i,attname)) + CHECK_NF_ERRSTAT(nf_copy_att(input(nfiles),varid,attname,ncid,varid1)) enddo if(add_missing_value.and.is_compressed) then ! check if missing_value or _FillValue attributes are present @@ -136,39 +136,39 @@ program decompress missing = NF_FILL_INT end select ! and add it to the output variable - __NF_ASRT__(nf_put_att_double(ncid,varid1,'missing_value',xtype,1,missing)) + CHECK_NF_ERRSTAT(nf_put_att_double(ncid,varid1,'missing_value',xtype,1,missing)) endif endif enddo ! clone all global attributes - __NF_ASRT__(nf_inq_natts(input(nfiles),natts)) + CHECK_NF_ERRSTAT(nf_inq_natts(input(nfiles),natts)) do i = 1,natts - __NF_ASRT__(nf_inq_attname(input(nfiles),NF_GLOBAL,i,attname)) - __NF_ASRT__(nf_copy_att(input(nfiles),NF_GLOBAL,attname,ncid,NF_GLOBAL)) + CHECK_NF_ERRSTAT(nf_inq_attname(input(nfiles),NF_GLOBAL,i,attname)) + CHECK_NF_ERRSTAT(nf_copy_att(input(nfiles),NF_GLOBAL,attname,ncid,NF_GLOBAL)) enddo ! ---- end of definition stage - __NF_ASRT__(nf__enddef(ncid,HEADERPAD,4,0,4)) + CHECK_NF_ERRSTAT(nf__enddef(ncid,HEADERPAD,4,0,4)) ! grow unlimited dimension, if necessary do varid=1,nvars - __NF_ASRT__(nfu_inq_compressed_var(input(nfiles),varid,name=varname,dimlens=dimlens,has_records=has_records)) + CHECK_NF_ERRSTAT(nfu_inq_compressed_var(input(nfiles),varid,name=varname,dimlens=dimlens,has_records=has_records)) if(has_records) then ! just write an integer at the very end of the variable -- that extends the ! record dimensions as well - __NF_ASRT__(nf_inq_varid(ncid,varname,varid)) - __NF_ASRT__(nf_put_var1_int(ncid,varid,dimlens,0)) + CHECK_NF_ERRSTAT(nf_inq_varid(ncid,varname,varid)) + CHECK_NF_ERRSTAT(nf_put_var1_int(ncid,varid,dimlens,0)) exit ! this loop endif enddo ! gather and copy data - __NF_ASRT__(nf_inq_nvars(ncid,nvars)) + CHECK_NF_ERRSTAT(nf_inq_nvars(ncid,nvars)) do varid = 1,nvars -! __NF_ASRT__(nfu_inq_compressed_var(ncid,varid,name=varname,varsize=vsize)) - __NF_ASRT__(nfu_inq_var(ncid,varid,varname,xtype,varsize=vsize)) +! CHECK_NF_ERRSTAT(nfu_inq_compressed_var(ncid,varid,name=varname,varsize=vsize)) + CHECK_NF_ERRSTAT(nfu_inq_var(ncid,varid,varname,xtype,varsize=vsize)) if(debug>0) & write(*,*)'processing var "'//trim(varname)//'"' allocate(buffer(vsize),mask(vsize)) @@ -200,14 +200,16 @@ program decompress ! read the variable do i=1,nfiles - __NF_ASRT__(nfu_get_compressed_var_r8n(input(i),varname,buffer,mask,ocean=do_oceanValue,ocean_value=ocean_value)) + CHECK_NF_ERRSTAT(nfu_get_compressed_var_r8n(input(i),varname,buffer,mask,ocean=do_oceanValue,ocean_value=ocean_value)) enddo ! write the variable - __NF_ASRT__(nfu_put_var_r8(ncid,varname,buffer)) + CHECK_NF_ERRSTAT(nfu_put_var_r8(ncid,varname,buffer)) deallocate(buffer,mask) enddo - __NF_ASRT__(nf_close(ncid)) + CHECK_NF_ERRSTAT(nf_sync(ncid)) + + CHECK_NF_ERRSTAT(nf_close(ncid)) contains ! -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- diff --git a/postprocessing/land_utils/scatter-ncc.F90 b/postprocessing/land_utils/scatter-ncc.F90 index af0d5485..c4606f6d 100644 --- a/postprocessing/land_utils/scatter-ncc.F90 +++ b/postprocessing/land_utils/scatter-ncc.F90 @@ -25,7 +25,7 @@ ! !----------------------------------------------------------------------- -#define __NF_ASRT__(ierr) call nfu_check_err(ierr,__FILE__,__LINE__) +#define CHECK_NF_ERRSTAT(ierr) call nfu_check_err(ierr,__FILE__,__LINE__) program combine_res use nfu_mod @@ -74,8 +74,8 @@ program combine_res ! open input files and determine the creation mode of output file: ! if the input file is 64-bit then the output is 64-bit as well, ! otherwise it's 32-bit - __NF_ASRT__(nf_open(infile,NF_NOWRITE,in_ncid)) - __NF_ASRT__(nf_inq_format(in_ncid,in_format)) + CHECK_NF_ERRSTAT(nf_open(infile,NF_NOWRITE,in_ncid)) + CHECK_NF_ERRSTAT(nf_inq_format(in_ncid,in_format)) if (in_format==NF_FORMAT_NETCDF4) then cmode = NF_NETCDF4 @@ -98,9 +98,9 @@ program combine_res nlon = 0 nlat = 0 nz = 1 - __NF_ASRT__(nf_inq_ndims(in_ncid,ndims)) + CHECK_NF_ERRSTAT(nf_inq_ndims(in_ncid,ndims)) do dimid = 1,ndims - __NF_ASRT__(nfu_inq_dim(in_ncid,dimid,dimname=dimname,dimlen=dimlen)) + CHECK_NF_ERRSTAT(nfu_inq_dim(in_ncid,dimid,dimname=dimname,dimlen=dimlen)) if( trim(dimname) == "lon" ) then nlon = dimlen else if( trim(dimname) == "lat" ) then @@ -126,16 +126,16 @@ program combine_res do n = 1, nfiles_out ! set the outfile name write(outfile, '(a,i4.4)') trim(infile)//".", n-1 - __NF_ASRT__(nf__create(outfile,cmode,0,blksz,out_ncid(n))) + CHECK_NF_ERRSTAT(nf__create(outfile,cmode,0,blksz,out_ncid(n))) enddo ! clone all dimensions; for compressed dimensions calculate the length nrec = 1 nz_saved = nz - __NF_ASRT__(nf_inq_ndims(in_ncid,ndims)) + CHECK_NF_ERRSTAT(nf_inq_ndims(in_ncid,ndims)) do dimid = 1,ndims - __NF_ASRT__(nfu_inq_dim(in_ncid,dimid,dimname=dimname,dimlen=dimlen,is_unlim=has_records)) + CHECK_NF_ERRSTAT(nfu_inq_dim(in_ncid,dimid,dimname=dimname,dimlen=dimlen,is_unlim=has_records)) if(debug>0)& write(*,*)'defining dimension "'//trim(dimname)//'" with length',dimlen if(has_records)then @@ -146,10 +146,10 @@ program combine_res is_compressed = .false. if(nfu_inq_att(in_ncid,dimname,'compress')==NF_NOERR) then is_compressed = .true. - __NF_ASRT__(nfu_inq_compressed_var(in_ncid,dimname,varsize=vsize)) + CHECK_NF_ERRSTAT(nfu_inq_compressed_var(in_ncid,dimname,varsize=vsize)) allocate(buffer(vsize),mask(vsize)) mask(:) = .false. - __NF_ASRT__(nfu_get_compressed_var_r8n(in_ncid,dimname,buffer,mask)) + CHECK_NF_ERRSTAT(nfu_get_compressed_var_r8n(in_ncid,dimname,buffer,mask)) !--- figure out dimension length for each file do l = 1, vsize if(mask(l)) then @@ -168,15 +168,15 @@ program combine_res endif do n = 1, nfiles_out if(is_compressed) dimlen = max(dimlen_list(n),1) - __NF_ASRT__(nf_def_dim(out_ncid(n),dimname,dimlen,i)) ! i is just a space for id + CHECK_NF_ERRSTAT(nf_def_dim(out_ncid(n),dimname,dimlen,i)) ! i is just a space for id enddo enddo ! clone all variable definitions - __NF_ASRT__(nf_inq_nvars(in_ncid,nvars)) + CHECK_NF_ERRSTAT(nf_inq_nvars(in_ncid,nvars)) do i = 1,nvars do n = 1, nfiles_out - __NF_ASRT__(nfu_clone_var(in_ncid,i,out_ncid(n))) + CHECK_NF_ERRSTAT(nfu_clone_var(in_ncid,i,out_ncid(n))) ! NOTE: since cloning of variable definition relies on dimension names, ! each variable tile and compressed dimensions automaticaly get the right ! size, as defined while creating dimensions in the output file @@ -184,17 +184,17 @@ program combine_res enddo ! clone all global attributes - __NF_ASRT__(nf_inq_natts(in_ncid,ngatts)) + CHECK_NF_ERRSTAT(nf_inq_natts(in_ncid,ngatts)) do i = 1,ngatts do n = 1, nfiles_out - __NF_ASRT__(nf_inq_attname(in_ncid,NF_GLOBAL,i,attname)) - __NF_ASRT__(nf_copy_att(in_ncid,NF_GLOBAL,attname,out_ncid(n),NF_GLOBAL)) + CHECK_NF_ERRSTAT(nf_inq_attname(in_ncid,NF_GLOBAL,i,attname)) + CHECK_NF_ERRSTAT(nf_copy_att(in_ncid,NF_GLOBAL,attname,out_ncid(n),NF_GLOBAL)) enddo enddo ! ---- end of definition stage do n = 1, nfiles_out - __NF_ASRT__(nf__enddef(out_ncid(n),HEADERPAD,4,0,4)) + CHECK_NF_ERRSTAT(nf__enddef(out_ncid(n),HEADERPAD,4,0,4)) enddo @@ -207,18 +207,18 @@ program combine_res ! write out the data do varid = 1,nvars !-- make sure number of levels is no greater than 2. - __NF_ASRT__(nfu_inq_var(in_ncid,varid, ndims=ndims, dimids=dimids, dimlens=dimlens,recsize=recsize,has_records=has_records)) + CHECK_NF_ERRSTAT(nfu_inq_var(in_ncid,varid, ndims=ndims, dimids=dimids, dimlens=dimlens,recsize=recsize,has_records=has_records)) if(.not. has_records .and. tlev>1) cycle is_compressed = .false. if(ndims>0) then !--- restrict that only the first dimension could be compressed dimension. - __NF_ASRT__(nfu_inq_dim(in_ncid,dimids(1),dimname=dimname)) + CHECK_NF_ERRSTAT(nfu_inq_dim(in_ncid,dimids(1),dimname=dimname)) if(nfu_inq_att(in_ncid,dimname,'compress')==NF_NOERR) is_compressed = .true. endif - __NF_ASRT__(nfu_inq_compressed_var(in_ncid,varid,name=varname,varsize=vsize, first_dim_only=.true.)) + CHECK_NF_ERRSTAT(nfu_inq_compressed_var(in_ncid,varid,name=varname,varsize=vsize, first_dim_only=.true.)) if(debug>0) & write(*,*)'processing var "'//trim(varname)//'"' @@ -230,7 +230,7 @@ program combine_res k_id = 0 t_id = 0 do dimid = 1, ndims - __NF_ASRT__(nfu_inq_dim(in_ncid,dimids(dimid),dimname=dimname,dimlen=dimlen,is_unlim=has_records)) + CHECK_NF_ERRSTAT(nfu_inq_dim(in_ncid,dimids(dimid),dimname=dimname,dimlen=dimlen,is_unlim=has_records)) if(has_records) then t_id = dimid else if(trim(dimname) == "zfull") then @@ -254,7 +254,7 @@ program combine_res mask(:) = .false. mask_2d(:,:) = .false. buffer_2d(:,:) = 0 - __NF_ASRT__(nfu_get_compressed_var_r8n(in_ncid,varname,buffer,mask,start=start,count=nread)) + CHECK_NF_ERRSTAT(nfu_get_compressed_var_r8n(in_ncid,varname,buffer,mask,start=start,count=nread)) do l = 1, vsize if(mask(l)) then ij = mod((l-1), npts)+1 @@ -277,7 +277,7 @@ program combine_res nwrite(1) = count(mask_2d(:,n)) if(t_id>0) start(t_id) = tlev if(k_id>0) start(k_id) = k - __NF_ASRT__(nfu_put_vara_r8(out_ncid(n),varname,start, nwrite, pack(buffer_2d(:,n),mask_2d(:,n)))) + CHECK_NF_ERRSTAT(nfu_put_vara_r8(out_ncid(n),varname,start, nwrite, pack(buffer_2d(:,n),mask_2d(:,n)))) endif enddo enddo @@ -289,15 +289,15 @@ program combine_res if(has_records) then ! record variable start(:) = 1; nread(:) = 1; nwrite(:) = 1 start(1) = tlev - __NF_ASRT__(nfu_get_compressed_var_r8n(in_ncid,varname,buffer,mask,start=start,count=nread)) + CHECK_NF_ERRSTAT(nfu_get_compressed_var_r8n(in_ncid,varname,buffer,mask,start=start,count=nread)) do n = 1, nfiles_out - __NF_ASRT__(nfu_put_vara_r8(out_ncid(n),varname,start, nwrite,buffer)) + CHECK_NF_ERRSTAT(nfu_put_vara_r8(out_ncid(n),varname,start, nwrite,buffer)) enddo else - __NF_ASRT__(nfu_get_compressed_var_r8n(in_ncid,varname,buffer,mask)) + CHECK_NF_ERRSTAT(nfu_get_compressed_var_r8n(in_ncid,varname,buffer,mask)) if (count(mask)>0) then do n = 1, nfiles_out - __NF_ASRT__(nfu_put_var_r8(out_ncid(n),varname,pack(buffer,mask))) + CHECK_NF_ERRSTAT(nfu_put_var_r8(out_ncid(n),varname,pack(buffer,mask))) enddo endif endif @@ -305,10 +305,12 @@ program combine_res deallocate(buffer,mask) enddo enddo - __NF_ASRT__(nf_close(in_ncid)) + CHECK_NF_ERRSTAT(nf_sync(in_ncid)) + CHECK_NF_ERRSTAT(nf_close(in_ncid)) do n = 1, nfiles_out - __NF_ASRT__(nf_close(out_ncid(n))) + CHECK_NF_ERRSTAT(nf_sync(out_ncid(n))) + CHECK_NF_ERRSTAT(nf_close(out_ncid(n))) enddo contains ! -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- diff --git a/tools/simple_hydrog/lakes/Makefile.am b/tools/simple_hydrog/lakes/Makefile.am index e8e44c58..50f7d221 100644 --- a/tools/simple_hydrog/lakes/Makefile.am +++ b/tools/simple_hydrog/lakes/Makefile.am @@ -24,7 +24,7 @@ bin_PROGRAMS = cr_lake_files FMSROOT = $(top_builddir)/tools/simple_hydrog/libfmslite AM_CFLAGS = $(NETCDF_CFLAGS) -AM_FCFLAGS = -I$(FMSROOT) $(FC_DEFAULT_REAL_KIND8_FLAG) $(NETCDF_FCFLAGS) +AM_FCFLAGS = -I$(FMSROOT) $(FC_DEFAULT_REAL_KIND8_FLAG) $(NETCDF_FCFLAGS) -I$(top_builddir)/postprocessing/land_utils LDADD = $(NETCDF_FCLDFLAGS) $(NETCDF_LIBS) $(NETCDF_FCLIBS) $(RPATH_FLAGS) cr_lake_files_SOURCES = cr_lake_files.f90 diff --git a/tools/simple_hydrog/lakes/cr_lake_files.f90 b/tools/simple_hydrog/lakes/cr_lake_files.f90 index 9d1986e2..93fcf59e 100644 --- a/tools/simple_hydrog/lakes/cr_lake_files.f90 +++ b/tools/simple_hydrog/lakes/cr_lake_files.f90 @@ -17,10 +17,13 @@ !* License along with FRE-NCTools.. If not, see !* . !*********************************************************************** +#define CHECK_NF_ERRSTAT(ierr) call nfu_check_err(ierr,__FILE__,__LINE__) + program cr_lake_files use horiz_interp_mod - +use nfu_mod +use nfu_compress_mod implicit none integer, parameter :: maxdims= 3 @@ -268,7 +271,8 @@ program cr_lake_files enddo enddo -rcode= nf_close (ncid) +CHECK_NF_ERRSTAT(nf_sync(ncid)) +CHECK_NF_ERRSTAT(nf_close(ncid)) do j= 1,jdl do i= 1,idl @@ -426,7 +430,8 @@ program cr_lake_files start= 1 ; count(1)= id rcode= nf_get_vara_double (ncid, lonid, start, count, lon_idx) -rcode= nf_close (ncid) +CHECK_NF_ERRSTAT(nf_sync(ncid)) +CHECK_NF_ERRSTAT(nf_close(ncid)) @@ -608,7 +613,8 @@ program cr_lake_files where (travel(:,:,n) == mval_travel) travel(:,:,n)= mval_mdl - rcode= nf_close (ncid) + CHECK_NF_ERRSTAT(nf_sync(ncid)) + CHECK_NF_ERRSTAT(nf_close(ncid)) enddo @@ -932,7 +938,8 @@ program cr_lake_files rcode= NF_PUT_VARA_DOUBLE (ncid, varid9, start, count, max_slp2nxt(2:idp1,2:jdp1,n)) ! close netcdf file - rcode= NF_CLOSE (ncid) + CHECK_NF_ERRSTAT(nf_sync(ncid)) + CHECK_NF_ERRSTAT(nf_close(ncid)) enddo !close (10) diff --git a/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.f90 b/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.f90 index d4909fe5..03d06064 100644 --- a/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.f90 +++ b/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.f90 @@ -17,6 +17,8 @@ !* License along with FRE-NCTools.. If not, see !* . !*********************************************************************** +#define CHECK_NF_ERRSTAT(ierr) call nfu_check_err(ierr,__FILE__,__LINE__) + program rmv_parallel_rivers ! =============================================================================== @@ -155,7 +157,8 @@ program rmv_parallel_rivers idp4= id + 4 !write (6,*) 'id= ', id, ', idp1= ', idp1, ', idp2= ', idp2 -rcode= nf_close (ncid) +CHECK_NF_ERRSTAT(nf_sync(ncid)) +CHECK_NF_ERRSTAT(nf_close(ncid)) allocate (lat(idp4,jdp4,ntiles), lon(idp4,jdp4,ntiles), arlat(idp4,jdp4,ntiles)) @@ -368,7 +371,8 @@ program rmv_parallel_rivers where (basin(:,:,n) == mval_in) basin(:,:,n)= mval_mdl - rcode= nf_close (ncid) + CHECK_NF_ERRSTAT(nf_sync(ncid)) + CHECK_NF_ERRSTAT(nf_close(ncid)) enddo sum= 0. @@ -1260,7 +1264,8 @@ program rmv_parallel_rivers rcode= NF_PUT_VARA_DOUBLE (ncid, varid8, start, count, drn_idx(3:idp2,3:jdp2,n)) ! close netcdf file - rcode= NF_CLOSE (ncid) + CHECK_NF_ERRSTAT(nf_sync(ncid)) + CHECK_NF_ERRSTAT(nf_close(ncid)) enddo deallocate (lat, lon, sin_lat, cos_lat) From f4b6c191dd006d6dbf6b66319bede2a8ad25e08a Mon Sep 17 00:00:00 2001 From: rem1776 Date: Tue, 16 Jul 2024 14:18:18 -0400 Subject: [PATCH 2/7] change file extensions and build files in order to use macros --- tools/simple_hydrog/lakes/Makefile.am | 4 ++-- .../lakes/{cr_lake_files.f90 => cr_lake_files.F90} | 1 - tools/simple_hydrog/rmvpr/Makefile.am | 6 +++--- .../{rmv_parallel_rivers.f90 => rmv_parallel_rivers.F90} | 1 + 4 files changed, 6 insertions(+), 6 deletions(-) rename tools/simple_hydrog/lakes/{cr_lake_files.f90 => cr_lake_files.F90} (99%) rename tools/simple_hydrog/rmvpr/{rmv_parallel_rivers.f90 => rmv_parallel_rivers.F90} (99%) diff --git a/tools/simple_hydrog/lakes/Makefile.am b/tools/simple_hydrog/lakes/Makefile.am index 50f7d221..152cb97c 100644 --- a/tools/simple_hydrog/lakes/Makefile.am +++ b/tools/simple_hydrog/lakes/Makefile.am @@ -27,7 +27,7 @@ AM_CFLAGS = $(NETCDF_CFLAGS) AM_FCFLAGS = -I$(FMSROOT) $(FC_DEFAULT_REAL_KIND8_FLAG) $(NETCDF_FCFLAGS) -I$(top_builddir)/postprocessing/land_utils LDADD = $(NETCDF_FCLDFLAGS) $(NETCDF_LIBS) $(NETCDF_FCLIBS) $(RPATH_FLAGS) -cr_lake_files_SOURCES = cr_lake_files.f90 -cr_lake_files_LDADD = $(FMSROOT)/libfms.a $(LDADD) +cr_lake_files_SOURCES = cr_lake_files.F90 +cr_lake_files_LDADD = $(FMSROOT)/libfms.a $(top_builddir)/postprocessing/land_utils/libnfu.a $(LDADD) CLEANFILES = *.mod diff --git a/tools/simple_hydrog/lakes/cr_lake_files.f90 b/tools/simple_hydrog/lakes/cr_lake_files.F90 similarity index 99% rename from tools/simple_hydrog/lakes/cr_lake_files.f90 rename to tools/simple_hydrog/lakes/cr_lake_files.F90 index 93fcf59e..79da11dd 100644 --- a/tools/simple_hydrog/lakes/cr_lake_files.f90 +++ b/tools/simple_hydrog/lakes/cr_lake_files.F90 @@ -23,7 +23,6 @@ program cr_lake_files use horiz_interp_mod use nfu_mod -use nfu_compress_mod implicit none integer, parameter :: maxdims= 3 diff --git a/tools/simple_hydrog/rmvpr/Makefile.am b/tools/simple_hydrog/rmvpr/Makefile.am index 09a866fc..a9edfd58 100644 --- a/tools/simple_hydrog/rmvpr/Makefile.am +++ b/tools/simple_hydrog/rmvpr/Makefile.am @@ -19,10 +19,10 @@ #*********************************************************************** bin_PROGRAMS = rmv_parallel_rivers -AM_FCFLAGS = $(FC_DEFAULT_REAL_KIND8_FLAG) $(NETCDF_FCFLAGS) +AM_FCFLAGS = $(FC_DEFAULT_REAL_KIND8_FLAG) $(NETCDF_FCFLAGS) -I$(top_builddir)/postprocessing/land_utils/ -LDADD = $(NETCDF_FCLDFLAGS) $(NETCDF_FCLIBS) $(RPATH_FLAGS) +LDADD = $(NETCDF_FCLDFLAGS) $(NETCDF_FCLIBS) $(top_builddir)/postprocessing/land_utils/libnfu.a $(RPATH_FLAGS) -rmv_parallel_rivers_SOURCES = rmv_parallel_rivers.f90 +rmv_parallel_rivers_SOURCES = rmv_parallel_rivers.F90 diff --git a/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.f90 b/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 similarity index 99% rename from tools/simple_hydrog/rmvpr/rmv_parallel_rivers.f90 rename to tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 index 03d06064..c45d0b6b 100644 --- a/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.f90 +++ b/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 @@ -32,6 +32,7 @@ program rmv_parallel_rivers ! River fields then are updated and iteration continues until fields (subA, ! travel, basin, celllength) no longer change. ! =============================================================================== +use nfu_mod implicit none From d7eb69005d5802396a03ec462f4c6abff08effd3 Mon Sep 17 00:00:00 2001 From: rem1776 Date: Wed, 17 Jul 2024 12:20:02 -0400 Subject: [PATCH 3/7] add in error checking and syncs for cp_river_vars --- postprocessing/land_utils/decompress-ncc.F90 | 1 - tools/simple_hydrog/postp/Makefile.am | 6 +++--- .../postp/{cp_river_vars.f90 => cp_river_vars.F90} | 12 +++++++++--- 3 files changed, 12 insertions(+), 7 deletions(-) rename tools/simple_hydrog/postp/{cp_river_vars.f90 => cp_river_vars.F90} (99%) diff --git a/postprocessing/land_utils/decompress-ncc.F90 b/postprocessing/land_utils/decompress-ncc.F90 index e1ecaa3f..fc8ce101 100644 --- a/postprocessing/land_utils/decompress-ncc.F90 +++ b/postprocessing/land_utils/decompress-ncc.F90 @@ -208,7 +208,6 @@ program decompress enddo CHECK_NF_ERRSTAT(nf_sync(ncid)) - CHECK_NF_ERRSTAT(nf_close(ncid)) contains ! -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- diff --git a/tools/simple_hydrog/postp/Makefile.am b/tools/simple_hydrog/postp/Makefile.am index 8eb33248..269f14f2 100644 --- a/tools/simple_hydrog/postp/Makefile.am +++ b/tools/simple_hydrog/postp/Makefile.am @@ -19,9 +19,9 @@ #*********************************************************************** bin_PROGRAMS = cp_river_vars -AM_FCFLAGS = $(FC_DEFAULT_REAL_KIND8_FLAG) $(NETCDF_FCFLAGS) +AM_FCFLAGS = $(FC_DEFAULT_REAL_KIND8_FLAG) $(NETCDF_FCFLAGS) -I$(top_builddir)/postprocessing/land_utils -LDADD = $(NETCDF_FCLDFLAGS) $(NETCDF_FCLIBS) $(RPATH_FLAGS) +LDADD = $(NETCDF_FCLDFLAGS) $(NETCDF_FCLIBS) $(RPATH_FLAGS) $(top_builddir)/postprocessing/land_utils/libnfu.a -cp_river_vars_SOURCES = cp_river_vars.f90 +cp_river_vars_SOURCES = cp_river_vars.F90 diff --git a/tools/simple_hydrog/postp/cp_river_vars.f90 b/tools/simple_hydrog/postp/cp_river_vars.F90 similarity index 99% rename from tools/simple_hydrog/postp/cp_river_vars.f90 rename to tools/simple_hydrog/postp/cp_river_vars.F90 index 17927d71..33fd6953 100644 --- a/tools/simple_hydrog/postp/cp_river_vars.f90 +++ b/tools/simple_hydrog/postp/cp_river_vars.F90 @@ -17,12 +17,15 @@ !* License along with FRE-NCTools.. If not, see !* . !*********************************************************************** +#define CHECK_NF_ERRSTAT(ierr) call nfu_check_err(ierr,__FILE__,__LINE__) + program cp_river_vars ! ========================================================================= ! program reads lat, lon, cellarea, land_frac, and tocell fields, and ! computes the fields: subA, travel, basin, celllength, internal ! ========================================================================= +use nfu_mod implicit none @@ -163,7 +166,8 @@ program cp_river_vars idp2= id + 2 !write (6,*) 'id= ', id, ', idp1= ', idp1, ', idp2= ', idp2 -rcode= nf_close (ncid) +CHECK_NF_ERRSTAT(nf_sync(ncid)) +CHECK_NF_ERRSTAT(nf_close(ncid)) allocate (lat(idp2,jdp2,ntiles), lon(idp2,jdp2,ntiles), arlat(idp2,jdp2,ntiles)) allocate (latb(id,jdp1,ntiles), lonb(idp1,jd,ntiles)) @@ -371,7 +375,8 @@ program cp_river_vars where (tocell(:,:,n) == mval_tocell) tocell(:,:,n)= mval_mdl - rcode= nf_close (ncid) + CHECK_NF_ERRSTAT(nf_sync(ncid)) + CHECK_NF_ERRSTAT(nf_close(ncid)) enddo sum= 0. @@ -1130,7 +1135,8 @@ program cp_river_vars endif ! close netcdf file - rcode= NF_CLOSE (ncid) + CHECK_NF_ERRSTAT(nf_sync(ncid)) + CHECK_NF_ERRSTAT(nf_close(ncid)) enddo close (10) From dc74425ae59aa8f3f35c1eac3eefb4fa14d6157a Mon Sep 17 00:00:00 2001 From: rem1776 Date: Wed, 17 Jul 2024 13:45:54 -0400 Subject: [PATCH 4/7] add syncs for c calls --- postprocessing/land_utils/is-compressed.c | 15 +++++----- tools/libfrencutils/mpp_io.c | 7 +++++ tools/libfrencutils/read_mosaic.c | 35 +++++++++++++++++++++++ tools/mppncscatter/mppncscatter.c | 20 +++++++++++-- tools/ncexists/ncexists.c | 2 ++ 5 files changed, 69 insertions(+), 10 deletions(-) diff --git a/postprocessing/land_utils/is-compressed.c b/postprocessing/land_utils/is-compressed.c index 9405bc21..bb413f95 100644 --- a/postprocessing/land_utils/is-compressed.c +++ b/postprocessing/land_utils/is-compressed.c @@ -22,7 +22,7 @@ #include #include -#define __NC_ASRT__(ierr) check_error(ierr,__FILE__,__LINE__) +#define CHECK_NC_ERRSTAT(ierr) check_error(ierr,__FILE__,__LINE__) void usage(const char* name) { @@ -49,8 +49,8 @@ void usage(const char* name) void check_error(int ierr, const char* file, int line) { if (ierr!=NC_NOERR) { - fprintf(stderr,"ERROR :: %s\n", - nc_strerror(ierr)); + fprintf(stderr,"ERROR :: %s\nfile: %s, line: %d\n", + nc_strerror(ierr), file, line); exit(ierr); } } @@ -85,18 +85,19 @@ int main(int argc, char* argv[]) int optind = argc - 1; - __NC_ASRT__(nc_open(argv[optind],NC_NOWRITE,&ncid)); - __NC_ASRT__(nc_inq_ndims(ncid,&ndims)); + CHECK_NC_ERRSTAT(nc_open(argv[optind],NC_NOWRITE,&ncid)); + CHECK_NC_ERRSTAT(nc_inq_ndims(ncid,&ndims)); if (verbose) printf("Found %d dimensions\n",ndims); for (dimid=0; dimid=nfiles) mpp_error("mpp_io(mpp_close): invalid id number, id should be " "a nonnegative integer that less than nfiles"); + status = nc_sync(files[fid].ncid); + if(status != NC_NOERR) { + sprintf( errmsg, "mpp_io(mpp_close): error in syncing files %s ", files[fid].name); + netcdf_error(errmsg, status); + } status = nc_close(files[fid].ncid); if(status != NC_NOERR) { sprintf( errmsg, "mpp_io(mpp_close): error in closing files %s ", files[fid].name); @@ -1435,6 +1440,8 @@ int mpp_file_exist(const char *file) status = nc_open(file,NC_NOWRITE, &ncid); if(status == NC_NOERR) { + status = nc_sync(ncid); + if(status != NC_NOERR) netcdf_error("mpp_file_exist(mpp_io):in syncing file", status); status = nc_close(ncid); if(status != NC_NOERR) netcdf_error("mpp_file_exist(mpp_io):in closing file", status); return 1; diff --git a/tools/libfrencutils/read_mosaic.c b/tools/libfrencutils/read_mosaic.c index 00900f06..67a4d33b 100644 --- a/tools/libfrencutils/read_mosaic.c +++ b/tools/libfrencutils/read_mosaic.c @@ -80,6 +80,11 @@ int field_exist(const char* file, const char *name) else existed = 0; + status = nc_sync(ncid); + if(status != NC_NOERR) { + sprintf(msg, "field_exist: in syncing file %s.", file); + handle_netcdf_error(msg, status); + } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "field_exist: in closing file %s.", file); @@ -113,6 +118,11 @@ int get_dimlen(const char* file, const char *name) sprintf(msg, "in getting dimension size of %s from file %s.", name, file); handle_netcdf_error(msg, status); } + status = nc_sync(ncid); + if(status != NC_NOERR) { + sprintf(msg, "in syncing file %s.", file); + handle_netcdf_error(msg, status); + } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "in closing file %s.", file); @@ -153,6 +163,11 @@ void get_string_data(const char *file, const char *name, char *data) sprintf(msg, "in getting data of %s from file %s.", name, file); handle_netcdf_error(msg, status); } + status = nc_sync(ncid); + if(status != NC_NOERR) { + sprintf(msg, "in syncing file %s.", file); + handle_netcdf_error(msg, status); + } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "in closing file %s.", file); @@ -189,6 +204,11 @@ void get_string_data_level(const char *file, const char *name, char *data, const sprintf(msg, "in getting data of %s from file %s.", name, file); handle_netcdf_error(msg, status); } + status = nc_sync(ncid); + if(status != NC_NOERR) { + sprintf(msg, "in syncing file %s.", file); + handle_netcdf_error(msg, status); + } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "in closing file %s.", file); @@ -245,6 +265,11 @@ void get_var_data(const char *file, const char *name, void *data) sprintf(msg, "in getting data of %s from file %s.", name, file); handle_netcdf_error(msg, status); } + status = nc_sync(ncid); + if(status != NC_NOERR) { + sprintf(msg, "in syncing file %s.", file); + handle_netcdf_error(msg, status); + } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "in closing file %s.", file); @@ -301,6 +326,11 @@ void get_var_data_region(const char *file, const char *name, const size_t *start sprintf(msg, "get_var_data_region: in getting data of %s from file %s.", name, file); handle_netcdf_error(msg, status); } + status = nc_sync(ncid); + if(status != NC_NOERR) { + sprintf(msg, "get_var_data_region: in closing file %s.", file); + handle_netcdf_error(msg, status); + } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "get_var_data_region: in closing file %s.", file); @@ -332,6 +362,11 @@ void get_var_text_att(const char *file, const char *name, const char *attname, c sprintf(msg, "in getting attribute %s of %s from file %s.", attname, name, file); handle_netcdf_error(msg, status); } + status = nc_sync(ncid); + if(status != NC_NOERR) { + sprintf(msg, "in syncing file %s.", file); + handle_netcdf_error(msg, status); + } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "in closing file %s.", file); diff --git a/tools/mppncscatter/mppncscatter.c b/tools/mppncscatter/mppncscatter.c index 329a68b3..b2b60ede 100644 --- a/tools/mppncscatter/mppncscatter.c +++ b/tools/mppncscatter/mppncscatter.c @@ -42,6 +42,8 @@ */ #include "mppncscatter.h" +#define CHECK_NC_ERRSTAT(ierr) check_error(ierr,__FILE__,__LINE__) + /*-------------------------------------------------------------------*/ void printsizetarray(size_t *a, int n) { int i=0; @@ -1415,11 +1417,14 @@ int mppncscatter(mnsopts* opts) fflush(stdout); } - nc_close(nc); + CHECK_NC_ERRSTAT(nc_sync(nc)); + CHECK_NC_ERRSTAT(nc_close(nc)); if (!dryrun) { - for(i=0; i < nfiles; ++i) - nc_close(ncids[i]); + for(i=0; i < nfiles; ++i) { + CHECK_NC_ERRSTAT(nc_sync(ncids[i])); + CHECK_NC_ERRSTAT(nc_close(ncids[i])); + } } if (verbose) { @@ -1434,3 +1439,12 @@ int mppncscatter(mnsopts* opts) return 0; } + +void check_error(int ierr, const char* file, int line) +{ + if (ierr!=NC_NOERR) { + fprintf(stderr,"ERROR :: %s\nfile: %s, line: %d\n", + nc_strerror(ierr), file, line); + exit(ierr); + } +} diff --git a/tools/ncexists/ncexists.c b/tools/ncexists/ncexists.c index 5a3dfbab..a48aad41 100644 --- a/tools/ncexists/ncexists.c +++ b/tools/ncexists/ncexists.c @@ -137,6 +137,8 @@ int main (int argc, char **argv) } + status = nc_sync(ncid); + if (status != NC_NOERR) handle_error(status); status = nc_close(ncid); /* close netCDF dataset */ if (status != NC_NOERR) handle_error(status); From d2e702ff989214e2c194827c4e22688c006a3608 Mon Sep 17 00:00:00 2001 From: rem1776 Date: Wed, 17 Jul 2024 14:52:04 -0400 Subject: [PATCH 5/7] remove unneeded sync for mpp_file_exist routine --- tools/libfrencutils/mpp_io.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/libfrencutils/mpp_io.c b/tools/libfrencutils/mpp_io.c index 87771f04..58c4499c 100644 --- a/tools/libfrencutils/mpp_io.c +++ b/tools/libfrencutils/mpp_io.c @@ -1440,8 +1440,6 @@ int mpp_file_exist(const char *file) status = nc_open(file,NC_NOWRITE, &ncid); if(status == NC_NOERR) { - status = nc_sync(ncid); - if(status != NC_NOERR) netcdf_error("mpp_file_exist(mpp_io):in syncing file", status); status = nc_close(ncid); if(status != NC_NOERR) netcdf_error("mpp_file_exist(mpp_io):in closing file", status); return 1; From 4f2bdabd5a160f97e37bec31aab4fc768ef4b978 Mon Sep 17 00:00:00 2001 From: rem1776 Date: Wed, 17 Jul 2024 18:21:45 -0400 Subject: [PATCH 6/7] remove syncs for read only opens --- postprocessing/land_utils/is-compressed.c | 1 - postprocessing/land_utils/scatter-ncc.F90 | 1 - tools/libfrencutils/read_mosaic.c | 35 ------------------- tools/mppncscatter/mppncscatter.c | 1 - tools/ncexists/ncexists.c | 4 +-- tools/simple_hydrog/lakes/cr_lake_files.F90 | 4 --- tools/simple_hydrog/postp/cp_river_vars.F90 | 3 -- .../rmvpr/rmv_parallel_rivers.F90 | 3 -- 8 files changed, 1 insertion(+), 51 deletions(-) diff --git a/postprocessing/land_utils/is-compressed.c b/postprocessing/land_utils/is-compressed.c index bb413f95..fe67fccc 100644 --- a/postprocessing/land_utils/is-compressed.c +++ b/postprocessing/land_utils/is-compressed.c @@ -96,7 +96,6 @@ int main(int argc, char* argv[]) return 0; } } - CHECK_NC_ERRSTAT(nc_sync(ncid)); CHECK_NC_ERRSTAT(nc_close(ncid)); return -1; diff --git a/postprocessing/land_utils/scatter-ncc.F90 b/postprocessing/land_utils/scatter-ncc.F90 index c4606f6d..72d3f5d0 100644 --- a/postprocessing/land_utils/scatter-ncc.F90 +++ b/postprocessing/land_utils/scatter-ncc.F90 @@ -305,7 +305,6 @@ program combine_res deallocate(buffer,mask) enddo enddo - CHECK_NF_ERRSTAT(nf_sync(in_ncid)) CHECK_NF_ERRSTAT(nf_close(in_ncid)) do n = 1, nfiles_out diff --git a/tools/libfrencutils/read_mosaic.c b/tools/libfrencutils/read_mosaic.c index 67a4d33b..00900f06 100644 --- a/tools/libfrencutils/read_mosaic.c +++ b/tools/libfrencutils/read_mosaic.c @@ -80,11 +80,6 @@ int field_exist(const char* file, const char *name) else existed = 0; - status = nc_sync(ncid); - if(status != NC_NOERR) { - sprintf(msg, "field_exist: in syncing file %s.", file); - handle_netcdf_error(msg, status); - } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "field_exist: in closing file %s.", file); @@ -118,11 +113,6 @@ int get_dimlen(const char* file, const char *name) sprintf(msg, "in getting dimension size of %s from file %s.", name, file); handle_netcdf_error(msg, status); } - status = nc_sync(ncid); - if(status != NC_NOERR) { - sprintf(msg, "in syncing file %s.", file); - handle_netcdf_error(msg, status); - } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "in closing file %s.", file); @@ -163,11 +153,6 @@ void get_string_data(const char *file, const char *name, char *data) sprintf(msg, "in getting data of %s from file %s.", name, file); handle_netcdf_error(msg, status); } - status = nc_sync(ncid); - if(status != NC_NOERR) { - sprintf(msg, "in syncing file %s.", file); - handle_netcdf_error(msg, status); - } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "in closing file %s.", file); @@ -204,11 +189,6 @@ void get_string_data_level(const char *file, const char *name, char *data, const sprintf(msg, "in getting data of %s from file %s.", name, file); handle_netcdf_error(msg, status); } - status = nc_sync(ncid); - if(status != NC_NOERR) { - sprintf(msg, "in syncing file %s.", file); - handle_netcdf_error(msg, status); - } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "in closing file %s.", file); @@ -265,11 +245,6 @@ void get_var_data(const char *file, const char *name, void *data) sprintf(msg, "in getting data of %s from file %s.", name, file); handle_netcdf_error(msg, status); } - status = nc_sync(ncid); - if(status != NC_NOERR) { - sprintf(msg, "in syncing file %s.", file); - handle_netcdf_error(msg, status); - } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "in closing file %s.", file); @@ -326,11 +301,6 @@ void get_var_data_region(const char *file, const char *name, const size_t *start sprintf(msg, "get_var_data_region: in getting data of %s from file %s.", name, file); handle_netcdf_error(msg, status); } - status = nc_sync(ncid); - if(status != NC_NOERR) { - sprintf(msg, "get_var_data_region: in closing file %s.", file); - handle_netcdf_error(msg, status); - } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "get_var_data_region: in closing file %s.", file); @@ -362,11 +332,6 @@ void get_var_text_att(const char *file, const char *name, const char *attname, c sprintf(msg, "in getting attribute %s of %s from file %s.", attname, name, file); handle_netcdf_error(msg, status); } - status = nc_sync(ncid); - if(status != NC_NOERR) { - sprintf(msg, "in syncing file %s.", file); - handle_netcdf_error(msg, status); - } status = nc_close(ncid); if(status != NC_NOERR) { sprintf(msg, "in closing file %s.", file); diff --git a/tools/mppncscatter/mppncscatter.c b/tools/mppncscatter/mppncscatter.c index b2b60ede..dc2bb96b 100644 --- a/tools/mppncscatter/mppncscatter.c +++ b/tools/mppncscatter/mppncscatter.c @@ -1417,7 +1417,6 @@ int mppncscatter(mnsopts* opts) fflush(stdout); } - CHECK_NC_ERRSTAT(nc_sync(nc)); CHECK_NC_ERRSTAT(nc_close(nc)); if (!dryrun) { diff --git a/tools/ncexists/ncexists.c b/tools/ncexists/ncexists.c index a48aad41..7cc6610a 100644 --- a/tools/ncexists/ncexists.c +++ b/tools/ncexists/ncexists.c @@ -92,7 +92,7 @@ int main (int argc, char **argv) if (global == 1) { - status = nc_open(filename, 0, &ncid); + status = nc_open(filename, NC_NOWRITE, &ncid); if (status != NC_NOERR) handle_error(status); status = nc_inq_att (ncid, NC_GLOBAL, gattr, &t_type, &t_len); @@ -137,8 +137,6 @@ int main (int argc, char **argv) } - status = nc_sync(ncid); - if (status != NC_NOERR) handle_error(status); status = nc_close(ncid); /* close netCDF dataset */ if (status != NC_NOERR) handle_error(status); diff --git a/tools/simple_hydrog/lakes/cr_lake_files.F90 b/tools/simple_hydrog/lakes/cr_lake_files.F90 index 79da11dd..9ffd0c81 100644 --- a/tools/simple_hydrog/lakes/cr_lake_files.F90 +++ b/tools/simple_hydrog/lakes/cr_lake_files.F90 @@ -270,7 +270,6 @@ program cr_lake_files enddo enddo -CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) do j= 1,jdl @@ -429,7 +428,6 @@ program cr_lake_files start= 1 ; count(1)= id rcode= nf_get_vara_double (ncid, lonid, start, count, lon_idx) -CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) @@ -612,7 +610,6 @@ program cr_lake_files where (travel(:,:,n) == mval_travel) travel(:,:,n)= mval_mdl - CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) enddo @@ -937,7 +934,6 @@ program cr_lake_files rcode= NF_PUT_VARA_DOUBLE (ncid, varid9, start, count, max_slp2nxt(2:idp1,2:jdp1,n)) ! close netcdf file - CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) enddo diff --git a/tools/simple_hydrog/postp/cp_river_vars.F90 b/tools/simple_hydrog/postp/cp_river_vars.F90 index 33fd6953..ae0eadf1 100644 --- a/tools/simple_hydrog/postp/cp_river_vars.F90 +++ b/tools/simple_hydrog/postp/cp_river_vars.F90 @@ -166,7 +166,6 @@ program cp_river_vars idp2= id + 2 !write (6,*) 'id= ', id, ', idp1= ', idp1, ', idp2= ', idp2 -CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) allocate (lat(idp2,jdp2,ntiles), lon(idp2,jdp2,ntiles), arlat(idp2,jdp2,ntiles)) @@ -375,7 +374,6 @@ program cp_river_vars where (tocell(:,:,n) == mval_tocell) tocell(:,:,n)= mval_mdl - CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) enddo @@ -1135,7 +1133,6 @@ program cp_river_vars endif ! close netcdf file - CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) enddo diff --git a/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 b/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 index c45d0b6b..17209c07 100644 --- a/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 +++ b/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 @@ -158,7 +158,6 @@ program rmv_parallel_rivers idp4= id + 4 !write (6,*) 'id= ', id, ', idp1= ', idp1, ', idp2= ', idp2 -CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) allocate (lat(idp4,jdp4,ntiles), lon(idp4,jdp4,ntiles), arlat(idp4,jdp4,ntiles)) @@ -372,7 +371,6 @@ program rmv_parallel_rivers where (basin(:,:,n) == mval_in) basin(:,:,n)= mval_mdl - CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) enddo @@ -1265,7 +1263,6 @@ program rmv_parallel_rivers rcode= NF_PUT_VARA_DOUBLE (ncid, varid8, start, count, drn_idx(3:idp2,3:jdp2,n)) ! close netcdf file - CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) enddo From d2bb14326a9a48540a6130b21490aac2ed48c821 Mon Sep 17 00:00:00 2001 From: rem1776 Date: Wed, 17 Jul 2024 18:37:10 -0400 Subject: [PATCH 7/7] add back in syncs that got accidentally deleted --- tools/simple_hydrog/lakes/cr_lake_files.F90 | 1 + tools/simple_hydrog/postp/cp_river_vars.F90 | 1 + tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 | 1 + 3 files changed, 3 insertions(+) diff --git a/tools/simple_hydrog/lakes/cr_lake_files.F90 b/tools/simple_hydrog/lakes/cr_lake_files.F90 index 9ffd0c81..d75131a3 100644 --- a/tools/simple_hydrog/lakes/cr_lake_files.F90 +++ b/tools/simple_hydrog/lakes/cr_lake_files.F90 @@ -934,6 +934,7 @@ program cr_lake_files rcode= NF_PUT_VARA_DOUBLE (ncid, varid9, start, count, max_slp2nxt(2:idp1,2:jdp1,n)) ! close netcdf file + CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) enddo diff --git a/tools/simple_hydrog/postp/cp_river_vars.F90 b/tools/simple_hydrog/postp/cp_river_vars.F90 index ae0eadf1..6fd3fcb8 100644 --- a/tools/simple_hydrog/postp/cp_river_vars.F90 +++ b/tools/simple_hydrog/postp/cp_river_vars.F90 @@ -1133,6 +1133,7 @@ program cp_river_vars endif ! close netcdf file + CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) enddo diff --git a/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 b/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 index 17209c07..400b2375 100644 --- a/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 +++ b/tools/simple_hydrog/rmvpr/rmv_parallel_rivers.F90 @@ -1263,6 +1263,7 @@ program rmv_parallel_rivers rcode= NF_PUT_VARA_DOUBLE (ncid, varid8, start, count, drn_idx(3:idp2,3:jdp2,n)) ! close netcdf file + CHECK_NF_ERRSTAT(nf_sync(ncid)) CHECK_NF_ERRSTAT(nf_close(ncid)) enddo