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

[netCDF #XFU-202199]: netcdf files



Hi Amanda,

> I am using the netCDF-4 data model and a C interface. Is it possible for 
> you to send me a script where new data is written into an existing file? 
> I am a bit confused with the way i can read the existing file, its 
> dimensions, define the new dimensions and write the new data.

Yes, but first I'll show you how to create an existing file the easy
way, with the "ncgen" utility.  Basic use of ncgen is explained in
this online workshop section on netCDF utilities:

  http://www.unidata.ucar.edu/netcdf/workshops/2012/utilities/index.html

First, download the CDL text file mslp.cdl from this slide, by
clicking on the "mslp.cdl" link and saving the resulting file:

  http://www.unidata.ucar.edu/netcdf/workshops/2012/utilities/CDL.html
  
Then use ncgen, one of the netCDF utilities stored in the bin/
directory where you installed netCDF, to convert that CDL text file
into the corresponding netCDF binary file, mslp.nc, using the command:

  ncgen -b mslp.cdl

Now you can use the attached program, extend.c, to read the
mslp.nc file and add some more data to it.  You will need to compile
extend.c into a program, with something like this:

  gcc extend.c -o extend -I/usr/local/include -L/usr/local/lib -lnetcdf

or you can use the "nc-config" utility to provide the correct include
and library flags for the compiler with:

  gcc extend.c -o extend `nc-config --cflags --libs`

If you run the extend program, it will add data for an additional
time, which you can verify by looking at the mslp.nc file before
running extend and after, using the ncdump utility:

  ncdump mslp.nc
  ./extend            # You may need to set LD_LIBRARY_PATH to where libnetcdf 
is installed to run this
  ncdump mslp.nc

You can look at other example netCDF C programs to read and write
netCDF data here:

  http://www.unidata.ucar.edu/netcdf/examples/programs/index.html

You can find out more about using utilities like ncgen with netCDF-4 data in 
this section of the
workshop:

  
http://www.unidata.ucar.edu/netcdf/workshops/2012/advanced_utilities/index.html

The attached extend.c program doesn't define new dimensions, but you can do 
that by calling
the nc_def_dim function.  You can generate a C program that shows this with 
ncgen:

  ncgen -l c mslp.cdl > mslp.c

--Russ
Russ Rew                                         UCAR Unidata Program
address@hidden                      http://www.unidata.ucar.edu



Ticket Details
===================
Ticket ID: XFU-202199
Department: Support netCDF
Priority: Normal
Status: Closed

Attachment: extend.c
Description: Binary data