[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDF #ZMF-465105]: can not append data to an exist NETCDF file on cygwin platform



> I think that I have used the syntax as the statement what you have
> mentioned. Because I just write a single value to the record variable,
> not an array or vector.
> 
> I do have gfortran compile also. But it can not compile the fortran code
> with nf90 netcdf interface. I have to use g95.

OK, I suspect the real reason you are not seeing new records added to the 
file is that you are not calling nf90_close(ncid).  It's necessary to 
call nf90_close to get all the records that you have written flushed to
disk.

I just tried a similar program that didn't work with the nf90_close() call
commented out, but works fine when it is uncommented.  It assumes that I
originally start with a file that has only one record, and with the 
nf90_close call it ends up with 12 records:

program zmf
  use netcdf
  integer :: ncid, varid
  integer :: start(1), count(1)
  integer :: times(1)
  call check( nf90_open('zmf.nc',nf90_write,ncid) )
  do m=1,12
     call check( nf90_inq_varid(ncid,'ocean_time',varid) )
     times(1) = m
     call check( nf90_put_var(ncid,varid,times,start=(/ m /), count=(/ 1 /)) )
  enddo
  call check( nf90_close(ncid) )
contains
  subroutine check(status)
    integer, intent ( in) :: status
    
    if(status /= nf90_noerr) then 
       print *, trim(nf90_strerror(status))
       stop "Stopped"
    end if
  end subroutine check
end program zmf

Before I compiled the program, I generated the test file "zmf.nc"
by running "ncgen -b zmf.cdl" using this CDL file as zmf.cdl:

netcdf zmf {
dimensions:
        ocean_time = UNLIMITED;
variables:
        int ocean_time(ocean_time) ;
data:
        ocean_time = 0;
}

Please let us know if this doesn't work for you.

--Russ


> Regards.
> Yours sincerely,
> Xueming Zhu
> 
> 2013/8/23 3:17, Unidata netCDF Support ?:
> >> Oh, Sorry, the '*' is not in my code, I just set ',count=(/1/)' as bold
> >> font, so it could be the displaying 's problem. Please don't take care
> >> about it. I use the g95 compiler on my cygwin platform.
> > Maybe the g95 compiler has a bug in array initialization.  Are you able to
> > compile the program at
> >
> >    http://www.unidata.ucar.edu/netcdf/examples/programs/pres_temp_4D_wr.f90
> >
> > which uses the syntax in the statements
> >
> >    count = (/ NLONS, NLATS, NLVLS, 1 /)
> >      ...
> >    call check( nf90_put_var(ncid, pres_varid, pres_out, start = start, &
> >                                count = count) )
> >
> > That compiles fine here with gfortran (we don't have g95 installed).
> >
> > --Russ
> >
> >> 2013/8/23 2:41, Unidata netCDF Support ?:
> >>>> But it looks like that I have to just provide 'start=...' when I write a
> >>>> record value (not a array or vector), such as
> >>>>
> >>>> ierr=nf90_put_var(ncid,varid,dnum,start=(/ m /))
> >>>>
> >>>> Once I provided the 'count=...' argument, such as
> >>>>
> >>>> ierr=nf90_put_var(ncid,varid,dnum,start=(/ m /)*, count=(/1/)*)
> >>>>
> >>>> I can not compile it successfully. Can you tell me why?
> >>> I think the syntax of the above statement is wrong.  Try leaving out
> >>> the "*" characters, as in:
> >>>
> >>>     ierr=nf90_put_var(ncid,varid,dnum,start=(/ m /), count=(/ 1 /))
> >>>
> >>> Also, Fortran-2003 compilers now accept using "count=[ 1 ]" instead of
> >>> "count=(/ 1 /)" notation for array initialization, which might also
> >>> work.netcdf zmf {
dimensions:
        ocean_time = UNLIMITED;
variables:
        int ocean_time(ocean_time) ;
data:
        ocean_time = 0;
}

> >>>
> >>> --Russ
> >>>
> >>>> Regards.
> >>>> Yours sincerely,
> >>>> Xueming Zhu
> >>>>
> >>>> 2013/8/23 1:28, Unidata netCDF Support ?:
> >>>>> Xueming Zhu,
> >>>>>
> >>>>>> Thanks for your replying. But if in that case, I have a question why
> >>>>>> I can not find any introduction of the nf90_put_vara from your link or
> >>>>>> the help file of netcdf interface( attached by this e-mail)? I just can
> >>>>>> find the interface nf90_put_var, and it looks like can be used to 
> >>>>>> wroten
> >>>>>> an array to the netcdf variable. I know that there are an interface
> >>>>>> nf_put_vara for nf77. But never heard about the nf90_put_vara.
> >>>>> Oops, sorry, I forgot that the nf90_put_var interface already includes 
> >>>>> the
> >>>>> put_vara behavior if you provide the optional keyword arguments 
> >>>>> "start=..."
> >>>>> and "count=...". You're right, that there is no separate function for 
> >>>>> using
> >>>>> those optional arguments, as there is in C, which didn't have optional
> >>>>> keyword arguments.
> >>>>>
> >>>>> A Fortran-90 example using those arguments to write a record variable 
> >>>>> is here:
> >>>>>
> >>>>>      
> >>>>> http://www.unidata.ucar.edu/software/netcdf/examples/programs/pres_temp_4D_wr.f90
> >>>>>
> >>>>> --Russ
> >>>>>
> >>>>>> 2013/8/23 0:34, Unidata netCDF Support ?:
> >>>>>>> Hi Xueming Zhu,
> >>>>>>>
> >>>>>>>> I tried to write data to an exist netCDF file with record variables
> >>>>>>>> on cygwin platform. My fortran codes as follow,
> >>>>>>>>
> >>>>>>>> clm  =trim(direc)//trim(casename)//'_monthly_'//year//'.nc'
> >>>>>>>> ierr=nf90_open(trim(clm),nf90_write,ncid)
> >>>>>>>> CALL HANDLE_ERR(ierr)
> >>>>>>>> do m=1,12
> >>>>>>>> ierr=nf90_inq_varid(ncid,'ocean_time',varid)
> >>>>>>>> CALL HANDLE_ERR(ierr)
> >>>>>>>> ierr=nf90_put_var(ncid,varid,dnum,start=(/ m /))
> >>>>>>>> CALL HANDLE_ERR(ierr)
> >>>>>>>> enddo
> >>>>>>>>
> >>>>>>>> the variable ocean_time's dimension is ocean_time, which is an
> >>>>>>>> unlimited dimension. At beginning, ocean_time's dimension is 1. But
> >>>>>>>> after the loop, it is 1 still. Some ncdump results as follow,
> >>>>>>>>
> >>>>>>>> $ ncdump.exe -v ocean_time np8_monthly_2002.nc
> >>>>>>>> netcdf np8_monthly_2002 {
> >>>>>>>> dimensions:
> >>>>>>>> tracer = 2 ;
> >>>>>>>> boundary = 4 ;
> >>>>>>>> s_rho = 22 ;
> >>>>>>>> s_w = 23 ;
> >>>>>>>> ocean_time = UNLIMITED ; // (1 currently)
> >>>>>>>> eta_rho = 633 ;
> >>>>>>>> xi_rho = 1545 ;
> >>>>>>>> eta_u = 633 ;
> >>>>>>>> xi_u = 1544 ;
> >>>>>>>> eta_v = 632 ;
> >>>>>>>> xi_v = 1545 ;
> >>>>>>>> variables:
> >>>>>>>>
> >>>>>>>> double ocean_time(ocean_time) ;
> >>>>>>>> ocean_time:long_name = "averaged time since
> >>>>>>>> initialization" ;
> >>>>>>>> ocean_time:units = "seconds since 1990-01-01 00:00:00" ;
> >>>>>>>> ocean_time:calendar = "gregorian" ;
> >>>>>>>> ocean_time:field = "time, scalar, series" ;
> >>>>>>>>
> >>>>>>>> I found the file size changed, but had not got any error when run the
> >>>>>>>> fortran code. It is weird for me.
> >>>>>>>>
> >>>>>>>> Can you give me any suggestions to fix the problem? Thanks a lot.
> >>>>>>> I think the problem is that you should be using nf90_put_vara
> >>>>>>> instead of nf90_put_var.  For an explanation why, see this section
> >>>>>>> of the nf90_put_var documentation:
> >>>>>>>
> >>>>>>>       
> >>>>>>> http://www.unidata.ucar.edu/netcdf/docs/netcdf-f90.html#NF90_005fPUT_005fVAR
> >>>>>>>
> >>>>>>> where it says:
> >>>>>>>
> >>>>>>>       Take care when using the simplest forms of this interface with
> >>>>>>>       record variables (variables that use the NF90_UNLIMITED 
> >>>>>>> dimension)
> >>>>>>>       when you don't specify how many records are to be written. If 
> >>>>>>> you
> >>>>>>>       try to write all the values of a record variable into a netCDF 
> >>>>>>> file
> >>>>>>>       that has no record data yet (hence has 0 records), nothing will 
> >>>>>>> be
> >>>>>>>       written. Similarly, if you try to write all the values of a 
> >>>>>>> record
> >>>>>>>       variable from an array but there are more records in the file 
> >>>>>>> than
> >>>>>>>       you assume, more in-memory data will be accessed than you 
> >>>>>>> expect,
> >>>>>>>       which may cause a segmentation violation. To avoid such 
> >>>>>>> problems, it
> >>>>>>>       is better to specify start and count arguments for variables 
> >>>>>>> that
> >>>>>>>       use the NF90_UNLIMITED dimension.
> >>>>>>>
> >>>>>>> --Russ
> >>>>>>>
> >>>>>>> Russ Rew                                         UCAR Unidata Program
> >>>>>>> address@hidden                      http://www.unidata.ucar.edu
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>>> Ticket Details
> >>>>>>> ===================
> >>>>>>> Ticket ID: ZMF-465105
> >>>>>>> Department: Support netCDF
> >>>>>>> Priority: Normal
> >>>>>>> Status: Closed
> >>>>>>>
> >>>>>>>
> >>>>> Russ Rew                                         UCAR Unidata Program
> >>>>> address@hidden                      http://www.unidata.ucar.edu
> >>>>>
> >>>>>
> >>>>>
> >>>>> Ticket Details
> >>>>> ===================
> >>>>> Ticket ID: ZMF-465105
> >>>>> Department: Support netCDF
> >>>>> Priority: Normal
> >>>>> Status: Closed
> >>>>>
> >>>>>
> >>>>
> >>> Russ Rew                                         UCAR Unidata Program
> >>> address@hidden                      http://www.unidata.ucar.edu
> >>>
> >>>
> >>>
> >>> Ticket Details
> >>> ===================
> >>> Ticket ID: ZMF-465105
> >>> Department: Support netCDF
> >>> Priority: Normal
> >>> Status: Closed
> >>>
> >>>
> >>
> > Russ Rew                                         UCAR Unidata Program
> > address@hidden                      http://www.unidata.ucar.edu
> >
> >
> >
> > Ticket Details
> > ===================
> > Ticket ID: ZMF-465105
> > Department: Support netCDF
> > Priority: Normal
> > Status: Closed
> >
> >
> 
> 
Russ Rew                                         UCAR Unidata Program
address@hidden                      http://www.unidata.ucar.edu



Ticket Details
===================
Ticket ID: ZMF-465105
Department: Support netCDF
Priority: Normal
Status: Closed