This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
------- Forwarded Message Date: Wed, 02 Feb 2005 11:27:06 -0700 From: Russ Rew <address@hidden> To: Jiye Zeng <address@hidden> Subject: Re: 20050202: How to write to the unlimited-dimension variable >From: Jiye Zeng <address@hidden> >Organization: Center For Global Environmental Research, Japan >Keywords: 200502021439.j12Edlsi019750 netCDF C example Hi Jiya, > I am trying to use netCDF through its C interface. All work fine except > that I don't know how to wrtie data to the unlimited-dimension > variable. I can't find any example in the user's guide or reference > manual for that. Coult you give me a simple example? Thanks. Sure. First, you should not use the simplest interface for writing an entire variable at once, because that won't work with a variable that uses the unlimited dimension, as it says in the C User's Guide: Take care when using the simplest forms of this interface with record variables 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 of a record variable but there are more records in the file than you assume, more data may be written to the file than you supply, which may result in a segmentation violation. You can use the nc_put_var1_ interfaces to write one value at a time, but I assume you really want to write an array of all the values that correspond to a single "slab" of the unlimited-dimension. For example, if time is the unlimited dimension, you might want to write all the values for a single time. For this, just use the nc_put_vara_ interfaces, and treat the unlimited dimension like any other dimension. As an example, for a file with the following structure: dimensions: lat = 181; lon = 361; time = unlimited; variables: float P(time, lat, lon); If you have 181*361 values of P that you want to write at the first time, you could use C code like: static int P_start[] = {0, 0, 0}; static int P_edges[] = {1, 181, 361}; /* assume array P with 181*361 values, write it */ status = nc_put_vara_float(ncid, P_id, P_start, P_edges, P); To write at the 10th time instead, the C code might look like: static int P_start[] = {9, 0, 0}; static int P_edges[] = {1, 181, 361}; /* assume array P with 181*361 values, write 10th time */ status = nc_put_vara_float(ncid, P_id, P_start, P_edges, P); Usually you would be doing this in a loop, writing one record at a time and varying only the start[0] element to specify which time was being written. I hope this helps. --Russ ------- End of Forwarded Message