[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[netCDF #AOB-147341]: Sample C++ program to write variables in to an existing (empty) NetCDF file
- Subject: [netCDF #AOB-147341]: Sample C++ program to write variables in to an existing (empty) NetCDF file
- Date: Tue, 06 Jul 2010 10:38:16 -0600
Hi Lyon,
> I have looked at your sample programs C++ at :
> http://www.unidata.ucar.edu/software/netcdf/examples/programs/
> and have found them to be quite useful. However, I found that in all
> the examples associated with writing variables
> in to a NetCDF file, the program always creates a new NetCDF file first
> and then writes the variables in to it.
>
> Could you please send me a sample C++ program which writes variables
> (their values) in to a currently existing NetCDF
> file? - e.g. create a dummy NetCDF file and then close it. Thereafter,
> re-open it and then write some variables in
> to it. To be more specific, I am looking for a routine to write a time
> value and vector values for x, y, z as my
> code runs i.e. where the time dimension will be unlimited and I will be
> writing one time record of info at a time.
You can close an existing file with the close() method, as in
// First create a file with no data.
NcFile file0("simple.nc", NcFile::Replace);
// Check to see if the file was created.
if(!file0.is_valid())
return NC_ERR;
// close the empty file
file0.close();
and then reopen it for writing using the same or a different variable, as in
NcFile file1("simple.nc", FileMode=NcFile::Write);
if(!file1.is_valid())
return NC_ERR;
Now you can define dimensions, variables, and atributes and add data to file1
using the C++ methods as in the examples.
If you want to append data to an existing file that already has N records of
a four-dimensional variable dimensioned with (time, x, y, z), where time is
the unlimited dimension, just call setcur with
if(!file1.setcur(N, 0, 0, 0))
return NC_ERR;
then call put with the data for record N+1, as in
NcVar var = file1.get_var("varname");
if(!var)
return NC_ERR;
if(!var.put(values, 1, NX, NY, NZ))
return NC_ERR;
where "values" is a block of data values for one record.
--Russ
Russ Rew UCAR Unidata Program
address@hidden http://www.unidata.ucar.edu
Ticket Details
===================
Ticket ID: AOB-147341
Department: Support netCDF
Priority: Normal
Status: Closed