Unidata - To provide the data services, tools, and cyberinfrastructure leadership that advance Earth system science, enhance educational opportunities, and broaden participation. Unidata
         
  advanced  
 

Re: C++ and record variables

> Organization: ERL
> Keywords: 199408312007.AA13009

Hi Tom,

> Is it possible with the current C++ NetCDF interface to write record
> (Unlimited) variables? I know the ncrecput function does not exist, but
> the manual for the C interface says that it's not necessary (accomplish
> the same by repeated calls to ncvarput; in C++, put). I've tried this
> method and I can never get more than one record in a variable.

Writing record variables is supported with the current C++ netCDF interface.
The netcdf/c++/nctst.cc program that comes with the current distribution
shows how this is done.  Briefly, you just need to declare the unlimited
dimension by *not* providing a size argument to NcFile::add_dim(), for
example:

    NcDim* frtimed = nc.add_dim("frtime"); // unlimited dimension

then just declare record variables to use this dimension as their first
dimension, for example: 

    NcVar* P = nc.add_var("P", ncFloat, frtimed, latd, lond);

and write data to the variable.  

    // We could write all P data at once with P->put(P_data, P->edges()),
    // but instead we write one record at a time, to show use of setcur().
    long rec = 0;                                      // start at zero-th
    const long nrecs = 1;                              // # records to write
    P->put(&P_data[0], nrecs, NLATS, NLONS);           // write zero-th record
    P->set_cur(++rec);                                 // set to next record
    P->put(&P_data[NLATS*NLONS], nrecs, NLATS, NLONS); // write next record

--
Russ Rew                                                   UCAR Unidata Program
russ@xxxxxxxxxxxxxxxx                                             P.O. Box 3000
http://www.unidata.ucar.edu/                             Boulder, CO 80307-3000


 
 
  Contact Us     Site Map     Search     Terms and Conditions     Privacy Policy     Participation Policy
 
National Science Foundation (NSF) UCAR Community Programs   Unidata is a member of the UCAR Community Programs, is managed by the University Corporation for Atmospheric Research, and is sponsored by the National Science Foundation.
P.O. Box 3000     Boulder, CO 80307-3000 USA     Tel: 303-497-8643     Fax: 303-497-8690