Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-release > by-pkgid > 99fb92fc7affdd6c9038a90e3b6f002c > files > 16

netcdf-3.6.2-4mdv2008.1.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<!-- Generated by Harlequin WebMaker 3.0.2 ( 8-May-1977 experimental) -->
<HEAD>
<LINK REL=TOP HREF=guidec-1.html>
<LINK REL=PREV HREF=guidec-9.html>
<LINK REL=NEXT HREF=guidec-11.html>
<TITLE>5	 Datasets</TITLE>
<META NAME=GENERATOR CONTENT="Harlequin WebMaker 3.0.2 ( 8-May-1977 experimental)">
</HEAD>
<BODY bgcolor="#ffffff">

<DIV>
<A NAME=HEADING10></A>

<DIV>
<P><A HREF=guidec-11.html>[Next] </A><A HREF=guidec-9.html>[Previous] </A><A HREF=guidec-1.html>[Top] </A><A HREF=guidec-3.html>[Contents] </A><A HREF=guidec-21.html>[Index] </A><A HREF="http://www.unidata.ucar.edu/packages/netcdf/">[netCDF Home Page] </A><A HREF="http://www.unidata.ucar.edu/">[Unidata Home Page]</A></P>
<P>NetCDF User's Guide for C</P>

</DIV>
<A NAME=HEADING10-0></A>
<A NAME=UID-guide.fm-1018091></A>
<H1>5  <A NAME=MARKER-10-2217></A>Datasets</H1>
<HR>
<P>This chapter presents the interfaces of the netCDF functions that deal with a netCDF dataset or the whole netCDF library. </P>
<P>A <A NAME=MARKER-2-2218></A><A NAME=MARKER-2-2219></A><A NAME=MARKER-2-2220></A><A NAME=MARKER-2-2221></A><A NAME=MARKER-2-2222></A><A NAME=MARKER-2-2223></A><A NAME=MARKER-2-2224></A>netCDF dataset that has not yet been opened can only be referred to by its dataset name. Once a netCDF dataset is opened, it is referred to by a <I>netCDF ID</I>, which is a small nonnegative integer returned when you create or open the dataset. A netCDF ID is much like a file descriptor in C or a logical unit number in FORTRAN. In any single program, the netCDF IDs of distinct open netCDF datasets are distinct. A single netCDF dataset may be opened multiple times and will then have multiple distinct netCDF IDs; however at most one of the open instances of a single netCDF dataset should permit writing. When an open netCDF dataset is closed, the ID is no longer associated with a netCDF dataset. </P>
<P>Functions that deal with the netCDF library include:</P>
<UL>
<LI>Get version of library.
<LI>Get error message corresponding to a returned error code.
</UL>
<P>The <A NAME=MARKER-2-2225></A><A NAME=MARKER-2-2226></A>operations supported on a netCDF dataset as a single object are:</P>
<UL>
<LI>Create, given dataset name and whether to overwrite or not.
<LI>Open for access, given dataset name and read or write intent.
<LI>Put into define mode, to add dimensions, variables, or attributes.
<LI>Take out of define mode, checking consistency of additions.
<LI>Close, writing to disk if required.
<LI>Inquire about the number of dimensions, number of variables, number of global attributes, and ID of the unlimited dimension, if any.
<LI>Synchronize to disk to make sure it is current.
<LI>Set and unset <I>nofill</I> mode for optimized sequential writes.
</UL>
<P>After a summary of conventions used in describing the netCDF interfaces, the rest of this chapter presents a detailed description of the interfaces for these operations.</P>
<A NAME=HEADING10-16></A>
<A NAME=UID-guide.fm-1018121></A>
<P><BR><BR></P>
<H2>5.1  NetCDF Library Interface Descriptions</H2>
<HR>
<P>Each <A NAME=MARKER-2-2227></A><A NAME=MARKER-2-2228></A><A NAME=MARKER-2-2229></A><A NAME=MARKER-2-2230></A><A NAME=MARKER-2-2231></A><A NAME=MARKER-2-2232></A><A NAME=MARKER-2-2233></A>interface description for a particular netCDF function in this and later chapters contains: </P>
<UL>
<LI>a description of the purpose of the function;
<LI>a C <A NAME=MARKER-10-2234></A>function prototype that presents the type and order of the formal parameters to the function; 
<LI>a description of each formal parameter in the C <A NAME=MARKER-10-2235></A>interface;
<LI>a list of possible error conditions; and
<LI>an example of a C <A NAME=MARKER-10-2236></A>program fragment calling the netCDF function (and perhaps other netCDF functions).
</UL>
<P><A NAME=MARKER-10-2237></A>The examples follow a simple convention for error handling, always checking the error status returned from each netCDF function call and calling a <CODE>handle_error</CODE> function <A NAME=MARKER-10-2238></A>in case an error was detected. For an example of such a function,<A NAME=MARKER-10-2239></A> <A HREF="#MARKER-9-2240">see Section 5.2 "Get error message corresponding to error status: nc_strerror,"  page&nbsp;30</A>.</P>
<A NAME=HEADING10-24></A>
<A NAME=UID-guide.fm-1231168></A>
<P><BR><BR></P>
<H2>5.2  <A NAME=MARKER-9-2240></A>Get error message corresponding to error status: <CODE><A NAME=MARKER-2-2241></A>nc_strerror</CODE>  <CODE><A NAME=MARKER-10-2242></A></CODE></H2>
<HR>
<P>The <A NAME=MARKER-2-2243></A><A NAME=MARKER-2-2244></A><A NAME=MARKER-2-2245></A>function <CODE>nc_strerror</CODE> <A NAME=MARKER-10-2246></A>returns a static reference to an error message string corresponding to an integer netCDF error status or to a system error number, presumably returned by a previous call to some other netCDF function. The list of netCDF error status codes is available in the appropriate include file for each language binding.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
const char * nc_strerror(int ncerr);
</PRE>
<P>  </P>
<TABLE BORDER=1><TR VALIGN=TOP><TD><P> <CODE>ncerr</CODE></P>
</TD><TD><P> An error status that might have been returned from a previous call to some netCDF function.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2247></A></H4>
<P>If you provide an invalid integer error status that does not correspond to any netCDF error message or or to any system error message (as understood by the system <CODE>strerror</CODE> function), <CODE>nc_strerror</CODE> returns a string indicating that there is no such error status.</P>
<P><BR><BR></P>
<H4>Example <A NAME=MARKER-9-2248></A></H4>
<P>Here is an example of a simple error handling function <A NAME=MARKER-10-2249></A>that uses <CODE>nc_strerror</CODE> <CODE><A NAME=MARKER-10-2250></A></CODE>to print the error message corresponding to the netCDF error status returned from any netCDF function call and then exit:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
void handle_error(int status) {
if (status != NC_NOERR) {
   fprintf(stderr, &quot;%s\n&quot;, nc_strerror(status));
   exit(-1);
   }
}
</PRE>
<A NAME=HEADING10-51></A>
<A NAME=UID-guide.fm-1231208></A>
<P><BR><BR></P>
<H2>5.3  <A NAME=MARKER-10-2251></A>Get netCDF library version: <CODE><A NAME=MARKER-2-2252></A>nc_inq_libvers</CODE> <A NAME=MARKER-10-2253></A><CODE></CODE></H2>
<HR>
<P>The <A NAME=MARKER-2-2254></A><A NAME=MARKER-2-2255></A><A NAME=MARKER-2-2256></A>function <CODE>nc_inq_libvers</CODE> <A NAME=MARKER-10-2257></A>returns a string identifying the version of the netCDF library, and when it was built.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
const char * nc_inq_libvers(void);
</PRE>
<P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2258></A></H4>
<P>This function takes no arguments, and thus no errors are possible in its invocation. </P>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here <A NAME=MARKER-2-2259></A>is an example using <CODE>nc_inq_libvers</CODE> <A NAME=MARKER-10-2260></A>to print the version of the netCDF library with which the program is linked: </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
   printf(&quot;%s\n&quot;, nc_inq_libvers());
</PRE>
<A NAME=HEADING10-62></A>
<A NAME=UID-guide.fm-1034427></A>
<P><BR><BR></P>
<H2>5.4  <A NAME=MARKER-10-2261></A> <A NAME=MARKER-2-2262></A>Create a NetCDF dataset: <CODE><A NAME=MARKER-2-2263></A>nc_create <A NAME=MARKER-10-2264></A></CODE></H2>
<HR>
<P>This function creates a new netCDF dataset, returning a netCDF ID that can subsequently be used to refer to the netCDF dataset in other netCDF function calls. The new netCDF dataset opened for write access and placed in define mode, ready for you to add dimensions, variables, and attributes.</P>
<P>A creation mode flag specifies whether to overwrite any existing dataset with the same name and whether access to the dataset is shared.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_create (const char* path, int cmode, int *ncidp);
</PRE>
<P> </P>
<TABLE BORDER=1><TR VALIGN=TOP><TD><P> <CODE>path</CODE></P>
</TD><TD><P> The file name of the new netCDF dataset.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>cmode</CODE></P>
</TD><TD><P> The creation mode. A zero value (or <CODE><A NAME=MARKER-2-275></A>NC_CLOBBER</CODE>) specifies the default behavior: overwrite any existing dataset with the same file name and buffer and cache accesses for efficiency.</P>
<P> Otherwise, the creation mode is <CODE>NC_NOCLOBBER</CODE>, <CODE><A NAME=MARKER-2-276></A>NC_SHARE</CODE>, or <CODE>NC_NOCLOBBER|NC_SHARE</CODE>. Setting the <CODE>NC_NOCLOBBER</CODE> flag means you do not want to clobber (overwrite) an existing dataset; an error (<CODE><A NAME=MARKER-2-277></A>NC_EEXIST</CODE>) is returned if the specified dataset already exists. The <CODE>NC_SHARE</CODE> flag is appropriate when one process may be writing the dataset and one or more other processes reading the dataset concurrently; it means that dataset accesses are not buffered and caching is limited. Since the buffering scheme is optimised for sequential access, programs that do not access data sequentially may see some performance improvement by setting the <CODE>NC_SHARE</CODE> flag.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>ncidp</CODE></P>
</TD><TD><P> Pointer to location where returned netCDF ID is to be stored.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2265></A></H4>
<P><CODE>nc_create</CODE> <A NAME=MARKER-10-2266></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2267></A>if no errors occurred. Possible causes of errors include: </P>
<UL>
<LI>Passing a dataset name that includes a directory that does not exist.
<LI>Specifying a dataset name of a file that exists and also specifying <CODE>NC_NOCLOBBER</CODE>.<A NAME=MARKER-10-2268></A>
<LI>Specifying a meaningless value for the creation mode.
<LI>Attempting to create a netCDF dataset in a directory where you don't have permission to create files.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>In <A NAME=MARKER-2-2269></A>this example we create a netCDF dataset named <CODE>foo.nc</CODE>; we want the dataset to be created in the current directory only if a dataset with that name does not already exist: </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status;
int ncid;
   ... 
status = nc_create(&quot;foo.nc&quot;, NC_NOCLOBBER, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING10-110></A>
<A NAME=UID-guide.fm-1034444></A>
<P><BR><BR></P>
<H2>5.5  <A NAME=MARKER-10-2270></A>Open a NetCDF Dataset for Access: <CODE><A NAME=MARKER-2-2271></A>nc_open</CODE> <A NAME=MARKER-10-2272></A><CODE></CODE></H2>
<HR>
<P>The <A NAME=MARKER-2-2273></A><A NAME=MARKER-2-2274></A><A NAME=MARKER-2-2275></A>function <CODE>nc_open</CODE> <A NAME=MARKER-10-2276></A>opens an existing netCDF dataset for access. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_open (const char *path, int omode, int *ncidp);
</PRE>
<P> </P>
<TABLE BORDER=1><TR VALIGN=TOP><TD><P> <CODE>path</CODE></P>
</TD><TD><P> File name for netCDF dataset to be opened.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>omode</CODE></P>
</TD><TD><P> A zero value (or <CODE><A NAME=MARKER-2-278></A>NC_NOWRITE</CODE>) specifies the default behavior: open the dataset with read-only access, buffering and caching accesses for efficiency</P>
<P> Otherwise, the creation mode is <CODE><A NAME=MARKER-2-279></A>NC_WRITE</CODE>, <CODE><A NAME=MARKER-2-280></A>NC_SHARE</CODE>, or <CODE>NC_WRITE|NC_SHARE</CODE>. Setting the <CODE>NC_WRITE</CODE> flag opens the dataset with read-write access. ("Writing" means any kind of change to the dataset, including appending or changing data, adding or renaming dimensions, variables, and attributes, or deleting attributes.) The <CODE>NC_SHARE</CODE> flag is appropriate when one process may be writing the dataset and one or more other processes reading the dataset concurrently; it means that dataset accesses are not buffered and caching is limited. Since the buffering scheme is optimised for sequential access, programs that do not access data sequentially may see some performance improvement by setting the <CODE>NC_SHARE</CODE> flag.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>ncidp</CODE></P>
</TD><TD><P> Pointer to location where returned netCDF ID is to be stored.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2277></A></H4>
<P><CODE>nc_open</CODE> <A NAME=MARKER-10-2278></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2279></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: </P>
<UL>
<LI>The specified netCDF dataset does not exist.
<LI>A meaningless mode was specified.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here <A NAME=MARKER-2-2280></A>is an example using <CODE>nc_open</CODE> <A NAME=MARKER-10-2281></A>to open an existing netCDF dataset named <CODE>foo.nc</CODE> for read-only, non-shared access:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status;
int ncid;
   ... 
status = nc_open(&quot;foo.nc&quot;, 0, &amp;ncid);
if (status != NC_NOERR) hendle_error(status);
</PRE>
<A NAME=HEADING10-155></A>
<A NAME=UID-guide.fm-1018263></A>
<P><BR><BR></P>
<H2>5.6  <A NAME=MARKER-10-2282></A>Put Open NetCDF Dataset into Define Mode: <CODE><A NAME=MARKER-2-2283></A>nc_redef</CODE> <A NAME=MARKER-10-2284></A><CODE></CODE></H2>
<HR>
<P>The <A NAME=MARKER-2-2285></A><A NAME=MARKER-2-2286></A><A NAME=MARKER-2-2287></A><A NAME=MARKER-2-2288></A><A NAME=MARKER-2-2289></A><A NAME=MARKER-2-2290></A><A NAME=MARKER-2-2291></A><A NAME=MARKER-2-2292></A><A NAME=MARKER-2-2293></A><A NAME=MARKER-2-2294></A><A NAME=MARKER-2-2295></A><A NAME=MARKER-2-2296></A><A NAME=MARKER-2-2297></A><A NAME=MARKER-2-2298></A><A NAME=MARKER-2-2299></A><A NAME=MARKER-2-2300></A><A NAME=MARKER-2-2301></A>function <CODE>nc_redef</CODE> <A NAME=MARKER-10-2302></A>puts an open netCDF dataset into define mode, so dimensions, variables, and attributes can be added or renamed and attributes can be deleted. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_redef(int ncid);
</PRE>
<P> </P>
<TABLE BORDER=1><TR VALIGN=TOP><TD><P> <CODE>ncid</CODE></P>
</TD><TD><P> netCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2303></A></H4>
<P><CODE>nc_redef</CODE> <A NAME=MARKER-10-2304></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2305></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: </P>
<UL>
<LI>The specified netCDF dataset is already in define mode.
<LI>The specified netCDF dataset was opened for read-only.
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here <A NAME=MARKER-2-2306></A>is an example using <CODE>nc_redef</CODE> <A NAME=MARKER-10-2307></A>to open an existing netCDF dataset named <CODE>foo.nc</CODE> and put it into define mode:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status;
int ncid;
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);  /* open dataset */
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_redef(ncid);                      /* put in define mode */
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING10-187></A>
<A NAME=UID-guide.fm-1018311></A>
<P><BR><BR></P>
<H2>5.7  <A NAME=MARKER-10-2308></A>Leave Define Mode: <CODE><A NAME=MARKER-2-2309></A>nc_enddef</CODE> <A NAME=MARKER-10-2310></A><CODE></CODE></H2>
<HR>
<P>The <A NAME=MARKER-2-2311></A><A NAME=MARKER-2-2312></A><A NAME=MARKER-2-2313></A><A NAME=MARKER-2-2314></A><A NAME=MARKER-2-2315></A>function <CODE>nc_enddef</CODE> <A NAME=MARKER-10-2316></A>takes an open netCDF dataset out of define mode. The changes made to the netCDF dataset while it was in define mode are checked and committed to disk if no problems occurred. Non-record variables may be initialized to a "fill value" as well (<A HREF="#MARKER-9-2431">see Section 5.12 "Set Fill Mode for Writes: nc_set_fill,"  page&nbsp;39</A>). The netCDF dataset is then placed in data mode, so variable data can be read or written. </P>
<P>This call may involve copying data under some circumstances. <A HREF="guidec-14.html#MARKER-9-3081">See Chapter 9 "NetCDF File Structure and Performance,"  page&nbsp;95</A>, for a more extensive discussion. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_enddef(int ncid);
</PRE>
<P> </P>
<TABLE BORDER=1><TR VALIGN=TOP><TD><P> <CODE>ncid</CODE></P>
</TD><TD><P> NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2317></A></H4>
<P><CODE>nc_enddef</CODE> <A NAME=MARKER-10-2318></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2319></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: </P>
<UL>
<LI>The specified netCDF dataset is not in define mode.
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here <A NAME=MARKER-2-2320></A>is an example using <CODE>nc_enddef</CODE> <A NAME=MARKER-10-2321></A> to finish the definitions of a new netCDF dataset named <CODE>foo.nc</CODE> and put it into data mode:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status;
int ncid;
   ... 
status = nc_create(&quot;foo.nc&quot;, <A NAME=MARKER-2-2322></A>NC_NOCLOBBER, &amp;ncid);
if (status != NC_NOERR) handle_error(status);

   ...       /* create dimensions, variables, attributes */

status = nc_enddef(ncid);  /*leave define mode*/
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING10-221></A>
<A NAME=UID-guide.fm-1034639></A>
<P><BR><BR></P>
<H2>5.8  <A NAME=MARKER-10-2323></A>Close an Open NetCDF Dataset: <CODE><A NAME=MARKER-2-2324></A>nc_close</CODE> <A NAME=MARKER-10-2325></A><CODE></CODE></H2>
<HR>
<P>The <A NAME=MARKER-2-2326></A><A NAME=MARKER-2-2327></A><A NAME=MARKER-2-2328></A><A NAME=MARKER-2-2329></A>function <CODE>nc_close</CODE> <A NAME=MARKER-10-2330></A>closes an open netCDF dataset. If the dataset is in define mode, <CODE>nc_enddef</CODE> <A NAME=MARKER-10-2331></A>will be called before closing. (In this case, if <CODE>nc_enddef</CODE> <A NAME=MARKER-10-2332></A>returns an error, <CODE>nc_abort</CODE> <A NAME=MARKER-10-2333></A>will automatically be called to restore the dataset to the consistent state before define mode was last entered.) After an open netCDF dataset is closed, its netCDF ID may be reassigned to the next netCDF dataset that is opened or created.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_close(int ncid);
</PRE>
<P> </P>
<TABLE BORDER=1><TR VALIGN=TOP><TD><P> <CODE>ncid</CODE></P>
</TD><TD><P> NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2334></A></H4>
<P><CODE>nc_close</CODE> <A NAME=MARKER-10-2335></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2336></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: </P>
<UL>
<LI>Define mode was entered and the automatic call made to <CODE>nc_enddef</CODE> <A NAME=MARKER-10-2337></A>failed.
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here <A NAME=MARKER-2-2338></A>is an example using <CODE>nc_close</CODE> <A NAME=MARKER-10-2339></A>to finish the definitions of a new netCDF dataset named <CODE>foo.nc</CODE> and release its netCDF ID:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status;
int ncid;
   ... 
status = nc_create(&quot;foo.nc&quot;, NC_NOCLOBBER, &amp;ncid);
if (status != NC_NOERR) handle_error(status);

   ...       /* create dimensions, variables, attributes */

status = nc_close(ncid);       /* close netCDF dataset */
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING10-254></A>
<A NAME=UID-guide.fm-1018414></A>
<P><BR><BR></P>
<H2>5.9  <A NAME=MARKER-10-2340></A><A NAME=MARKER-2-2341></A><A NAME=MARKER-2-2342></A><A NAME=MARKER-2-2343></A><A NAME=MARKER-2-2344></A>Inquire about an Open NetCDF Dataset: <CODE><A NAME=MARKER-2-2345></A>nc_inq <A NAME=MARKER-10-2346></A></CODE>Family</H2>
<HR>
<P>Members of the <CODE>nc_inq <A NAME=MARKER-10-2347></A></CODE>family of functions return information about an open netCDF dataset, given its netCDF ID. Dataset inquire functions may be called from either define mode or data mode. The first function, <CODE>nc_inq</CODE>,<A NAME=MARKER-10-2348></A> returns values for the number of dimensions, the number of variables, the number of global attributes, and the dimension ID of the dimension defined with unlimited length, if any. The other functions in the family each return just one of these items of information. </P>
<P>For C, these functions include <CODE><A NAME=MARKER-2-2349></A>nc_inq</CODE>, <CODE><A NAME=MARKER-2-2350></A>nc_inq_ndims</CODE>,<A NAME=MARKER-2-2351></A> <CODE>nc_inq_nvars</CODE><A NAME=MARKER-2-2352></A>, <CODE>nc_inq_natts</CODE>, and <CODE><A NAME=MARKER-2-2353></A>nc_inq_unlimdim</CODE>. </P>
<P><A NAME=MARKER-10-2354></A>No I/O is performed when these functions are called, since the required information is available in memory for each open netCDF dataset. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_inq          (int ncid, int *ndimsp, int *nvarsp, int *ngattsp,
                     int *unlimdimidp);
int nc_inq_ndims    (int ncid, int *ndimsp);
int nc_inq_nvars    (int ncid, int *nvarsp);
int nc_inq_natts    (int ncid, int *ngattsp);
int nc_inq_unlimdim (int ncid, int *unlimdimidp);
</PRE>
<P> </P>
<TABLE BORDER=1><TR VALIGN=TOP><TD><P> <CODE>ncid</CODE></P>
</TD><TD><P> NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>ndimsp</CODE></P>
</TD><TD><P> Pointer <A NAME=MARKER-2-132></A><A NAME=MARKER-2-133></A>to location for returned number of dimensions defined for this netCDF dataset.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>nvarsp</CODE></P>
</TD><TD><P> Pointer <A NAME=MARKER-2-134></A><A NAME=MARKER-2-135></A>to location for returned number of variables defined for this netCDF dataset.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>ngattsp</CODE></P>
</TD><TD><P> Pointer <A NAME=MARKER-2-136></A><A NAME=MARKER-2-137></A><A NAME=MARKER-2-138></A>to location for returned number of global attributes defined for this netCDF dataset.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>unlimdimidp</CODE></P>
</TD><TD><P> Pointer <A NAME=MARKER-2-139></A><A NAME=MARKER-2-140></A><A NAME=MARKER-2-141></A>to location for returned ID of the unlimited dimension, if there is one for this netCDF dataset. If no unlimited length dimension has been defined, -1 is returned.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2355></A></H4>
<P>All members of the <CODE>nc_inq</CODE> <A NAME=MARKER-10-2356></A>family return the value <CODE>NC_NOERR <A NAME=MARKER-10-2357></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here <A NAME=MARKER-2-2358></A>is an example using <CODE>nc_inq</CODE> <A NAME=MARKER-10-2359></A>to find out about a netCDF dataset named <CODE>foo.nc</CODE>: </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status, ncid, ndims, nvars, ngatts, unlimdimid;
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_NOWRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq(ncid, &amp;ndims, &amp;nvars, &amp;ngatts, &amp;unlimdimid);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING10-322></A>
<A NAME=UID-guide.fm-1018588></A>
<P><BR><BR></P>
<H2>5.10  <A NAME=MARKER-10-2360></A><A NAME=MARKER-2-2361></A><A NAME=MARKER-2-2362></A>Synchronize an Open NetCDF Dataset to Disk: <CODE><A NAME=MARKER-2-2363></A>nc_sync</CODE> <A NAME=MARKER-10-2364></A><CODE></CODE></H2>
<HR>
<P>The <A NAME=MARKER-2-2365></A><A NAME=MARKER-2-2366></A>function <CODE>nc_sync</CODE> <A NAME=MARKER-10-2367></A>offers a way to synchronize the disk copy of a netCDF dataset with in-memory buffers. There are two reasons you might want to synchronize after writes:</P>
<UL>
<LI>To minimize data loss in case of abnormal termination, or
<LI>To <A NAME=MARKER-2-2368></A><A NAME=MARKER-2-2369></A><A NAME=MARKER-2-2370></A>make data available to other processes for reading immediately after it is written. But note that a process that already had the dataset open for reading would not see the number of records increase when the writing process calls <CODE>nc_sync</CODE>;<A NAME=MARKER-10-2371></A> to accomplish this, the reading process must call <CODE>nc_sync</CODE>.<A NAME=MARKER-10-2372></A>
</UL>
<P>This function is backward-compatible with previous versions of the netCDF library. The intent was to allow sharing of a netCDF dataset among multiple readers and one writer, by having the writer call <CODE>nc_sync</CODE> <A NAME=MARKER-10-2373></A>after writing and the readers call <CODE>nc_sync</CODE> <A NAME=MARKER-10-2374></A>before each read. For a writer, this flushes buffers to disk. For a reader, it makes sure that the next read will be from disk rather than from previously cached buffers, so that the reader will see changes made by the writing process (e.g., the number of records written) without having to close and reopen the dataset. If you are only accessing a small amount of data, it can be expensive in computer resources to always synchronize to disk after every write, since you are giving up the benefits of buffering.</P>
<P>An easier way to accomplish sharing (and what is now recommended) is to have the writer and readers open the dataset with the <A NAME=MARKER-2-2375></A>NC_SHARE <A NAME=MARKER-10-2376></A>flag, and then it will not be necessary to call <CODE>nc_sync</CODE> <A NAME=MARKER-10-2377></A>at all. However, the <CODE>nc_sync</CODE> <A NAME=MARKER-10-2378></A>function still provides finer granularity than the NC_SHARE <A NAME=MARKER-10-2379></A>flag, if only a few netCDF accesses need to be synchronized among processes.</P>
<P>It is important to note that changes to the ancillary data, such as attribute values, are <I>not</I> propagated automatically by use of the NC_SHARE <A NAME=MARKER-10-2380></A>flag. Use of the <CODE>nc_sync</CODE> <A NAME=MARKER-10-2381></A>function is still required for this purpose. </P>
<P>Sharing datasets when the writer enters define mode to change the data schema requires extra care. In previous releases, after the writer left define mode, the readers were left looking at an old copy of the dataset, since the changes were made to a new copy. The only way readers could see the changes was by closing and reopening the dataset. Now the changes are made in place, but readers have no knowledge that their internal tables are now inconsistent with the new dataset schema. If netCDF datasets are shared across redefinition, some mechanism external to the netCDF library must be provided that prevents access by readers during redefinition and causes the readers to call <CODE>nc_sync</CODE> <A NAME=MARKER-10-2382></A>before any subsequent access. </P>
<P>When calling <CODE>nc_sync</CODE>,<A NAME=MARKER-10-2383></A> the netCDF dataset must be in <A NAME=MARKER-2-2384></A><A NAME=MARKER-2-2385></A>data mode. A netCDF dataset in define mode is synchronized to disk only when <CODE>nc_enddef</CODE> <A NAME=MARKER-10-2386></A>is called. A process that is reading a netCDF dataset that another process is writing may call <CODE>nc_sync</CODE> <A NAME=MARKER-10-2387></A>to get updated with the changes made to the data by the writing process (e.g., the number of records written), without having to close and reopen the dataset.</P>
<P>Data <A NAME=MARKER-2-2388></A><A NAME=MARKER-2-2389></A><A NAME=MARKER-2-2390></A><A NAME=MARKER-2-2391></A><A NAME=MARKER-2-2392></A><A NAME=MARKER-2-2393></A>is automatically synchronized to disk when a netCDF dataset is closed, or whenever you leave define mode.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_sync(int ncid);
</PRE>
<P> </P>
<TABLE BORDER=1><TR VALIGN=TOP><TD><P> <CODE>ncid</CODE></P>
</TD><TD><P> NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2394></A></H4>
<P><CODE>nc_sync</CODE> <A NAME=MARKER-10-2395></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2396></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: </P>
<UL>
<LI>The netCDF dataset is in define mode.
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here <A NAME=MARKER-2-2397></A>is an example using <CODE>nc_sync</CODE> <A NAME=MARKER-10-2398></A>to synchronize the disk writes of a netCDF dataset named <CODE>foo.nc</CODE>:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status;
int ncid;
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);  /* open for writing */
if (status != NC_NOERR) handle_error(status);

   ...           /* write data or change attributes */

status = nc_sync(ncid);      /* synchronize to disk */
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING10-363></A>
<A NAME=UID-guide.fm-1231265></A>
<P><BR><BR></P>
<H2>5.11  <A NAME=MARKER-10-2399></A>Back Out of Recent Definitions: <CODE><A NAME=MARKER-2-2400></A>nc_abort</CODE> <A NAME=MARKER-10-2401></A><CODE></CODE></H2>
<HR>
<P>You no longer need to call this function, since it is called automatically by <CODE>nc_close</CODE> <A NAME=MARKER-10-2402></A>in case the dataset is in define mode and something goes wrong with committing the changes. The <A NAME=MARKER-2-2403></A><A NAME=MARKER-2-2404></A><A NAME=MARKER-2-2405></A><A NAME=MARKER-2-2406></A><A NAME=MARKER-2-2407></A><A NAME=MARKER-2-2408></A><A NAME=MARKER-2-2409></A><A NAME=MARKER-2-2410></A><A NAME=MARKER-2-2411></A><A NAME=MARKER-2-2412></A><A NAME=MARKER-2-2413></A><A NAME=MARKER-2-2414></A><A NAME=MARKER-2-2415></A><A NAME=MARKER-2-2416></A><A NAME=MARKER-2-2417></A>function <CODE>nc_abort</CODE> <A NAME=MARKER-10-2418></A><A NAME=MARKER-2-2419></A><A NAME=MARKER-2-2420></A>just closes the netCDF dataset, if not in <A NAME=MARKER-2-2421></A><A NAME=MARKER-2-2422></A>define mode. If the dataset is being created and is still in define mode, the dataset is deleted. If define mode was entered by a call to <CODE>nc_redef</CODE>,<A NAME=MARKER-10-2423></A> the netCDF dataset is restored to its state before definition mode was entered and the dataset is closed.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_abort(int ncid);
</PRE>
<P> </P>
<TABLE BORDER=1><TR VALIGN=TOP><TD><P> <CODE>ncid</CODE></P>
</TD><TD><P> NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2424></A></H4>
<P><CODE>nc_abort</CODE> <A NAME=MARKER-10-2425></A>returns the value <CODE>NC_NOERR</CODE> <CODE><A NAME=MARKER-10-2426></A><A NAME=MARKER-10-2427></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: </P>
<UL>
<LI>When called from define mode while creating a netCDF dataset, deletion of the dataset failed.
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here <A NAME=MARKER-2-2428></A>is an example using <CODE>nc_abort</CODE> <A NAME=MARKER-10-2429></A>to back out of redefinitions of a dataset named <CODE>foo.nc</CODE>:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int ncid, status, latid;
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);/* open for writing */
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_redef(ncid);                    /* enter define mode */
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_def_dim(ncid, &quot;lat&quot;, 18L, &amp;latid);
if (status != NC_NOERR) {
   handle_error(status);
   status = nc_abort(ncid);                 /* define failed, abort */
   if (status != NC_NOERR) handle_error(status);
}
</PRE>
<A NAME=HEADING10-400></A>
<A NAME=UID-guide.fm-1018706></A>
<P><BR><BR></P>
<H2>5.12  <A NAME=MARKER-10-2430></A><A NAME=MARKER-9-2431></A><A NAME=MARKER-2-2432></A><A NAME=MARKER-2-2433></A><A NAME=MARKER-2-2434></A><A NAME=MARKER-2-2435></A>Set Fill Mode for Writes: <CODE><A NAME=MARKER-2-2436></A>nc_set_fill</CODE> <A NAME=MARKER-10-2437></A><CODE></CODE></H2>
<HR>
<P>This function is intended for advanced usage, to optimize writes under some circumstances described below. The function <CODE>nc_set_fill</CODE> <A NAME=MARKER-10-2438></A>sets the <I>fill mode</I> for a netCDF dataset open for writing and returns the current fill mode in a return parameter. The fill mode can be specified as either <CODE>NC_FILL</CODE> <A NAME=MARKER-10-2439></A>or <CODE><A NAME=MARKER-2-2440></A>NC_NOFILL</CODE>.<A NAME=MARKER-10-2441></A> The default behavior corresponding to <CODE>NC_FILL</CODE> <A NAME=MARKER-10-2442></A>is that data is pre-filled with fill values, that is fill values are written when you create non-record variables or when you write a value beyond data that has not yet been written. This makes it possible to detect attempts to read data before it was written. <A HREF="guidec-12.html#MARKER-9-2925">See Section 7.16 "Fill Values,"  page&nbsp;78</A>, for more information on the use of fill values. <A HREF="guidec-13.html#MARKER-9-2991">See Section 8.1 "Attribute Conventions,"  page&nbsp;81</A>, for information about how to define your own fill values. </P>
<P>The behavior corresponding to <CODE>NC_NOFILL</CODE> <A NAME=MARKER-10-2443></A>overrides the default behavior of prefilling data with fill values. This can be used to enhance performance, because it avoids the duplicate writes that occur when the netCDF library writes fill values that are later overwritten with data. </P>
<P>A value indicating which mode the netCDF dataset was already in is returned. You can use this value to temporarily change the fill mode of an open netCDF dataset and then restore it to the previous mode. </P>
<P>After you turn on <CODE>NC_NOFILL</CODE> <A NAME=MARKER-10-2444></A>mode for an open netCDF dataset, you must be certain to write valid data in all the positions that will later be read. Note that nofill mode is only a transient property of a netCDF dataset open for writing: if you close and reopen the dataset, it will revert to the default behavior. You can also revert to the default behavior by calling <CODE>nc_set_fill</CODE> <CODE><A NAME=MARKER-10-2445></A></CODE>again to explicitly set the fill mode to <CODE>NC_FILL</CODE>.<A NAME=MARKER-10-2446></A></P>
<P>There are three situations where it is advantageous to set nofill mode:</P>
<DL>
<DL>
<DT><DD>1.  Creating and initializing a netCDF dataset. In this case, you should set nofill mode before calling <CODE>nc_enddef</CODE> <A NAME=MARKER-10-2447></A>and then write <I>completely</I> all non-record variables and the initial records of all the record variables you want to initialize. 
<DT><DD>2.  Extending an existing record-oriented netCDF dataset. Set nofill mode after opening the dataset for writing, then append the additional records to the dataset completely, leaving no intervening unwritten records. 
<DT><DD>3.  Adding new variables that you are going to initialize to an existing netCDF dataset. Set nofill mode before calling <CODE>nc_enddef</CODE> <A NAME=MARKER-10-2448></A>then write all the new variables completely.
</DL>
</DL>
<P>If the netCDF dataset has an unlimited dimension and the last record was written while in nofill mode, then the dataset may be shorter than if nofill mode was not set, but this will be completely transparent if you access the data only through the netCDF interfaces.</P>
<P>The use of this feature may not be available (or even needed) in future releases. Programmers are cautioned against heavy reliance upon this feature.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_set_fill (int ncid, int fillmode, int *old_modep];
</PRE>
<P> </P>
<TABLE BORDER=1><TR VALIGN=TOP><TD><P> <CODE>ncid</CODE></P>
</TD><TD><P> NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>fillmode</CODE></P>
</TD><TD><P> Desired fill mode for the dataset, either <CODE><A NAME=MARKER-2-281></A>NC_NOFILL</CODE> or <CODE><A NAME=MARKER-2-282></A>NC_FILL</CODE>.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>old_modep</CODE></P>
</TD><TD><P> Pointer to location for returned current fill mode of the dataset before this call, either <CODE>NC_NOFILL</CODE> or <CODE>NC_FILL</CODE>.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2449></A></H4>
<P><CODE>nc_set_fill</CODE> <A NAME=MARKER-10-2450></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2451></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
<LI>The specified netCDF ID refers to a dataset open for read-only access.
<LI>The fill mode argument is neither <CODE>NC_NOFILL</CODE> <A NAME=MARKER-10-2452></A>nor <CODE>NC_FILL</CODE>.<A NAME=MARKER-10-2453></A>.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here <A NAME=MARKER-2-2454></A>is an example using <CODE>nc_set_fill</CODE> <A NAME=MARKER-10-2455></A>to set nofill mode for subsequent writes of a netCDF dataset named <CODE>foo.nc</CODE>:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int ncid, status, old_fill_mode;
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);  /* open for writing */
if (status != NC_NOERR) handle_error(status);

   ...           /* write data with default prefilling behavior */

status = nc_set_fill(ncid, NC_NOFILL, &amp;old_fill_mode); /* set nofill */
if (status != NC_NOERR) handle_error(status);

   ...           /* write data with no prefilling */
</PRE>

</DIV>

<DIV>

<DIV>

<!-- TOC --><DL>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-16"><B>5.1 </B> - NetCDF Library Interface Descriptions</A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-24"><B>5.2 </B> - Get error message corresponding to error status: nc_strerror  </A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-51"><B>5.3 </B> - Get netCDF library version: nc_inq_libvers </A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-62"><B>5.4 </B> -  Create a NetCDF dataset: nc_create </A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-110"><B>5.5 </B> - Open a NetCDF Dataset for Access: nc_open </A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-155"><B>5.6 </B> - Put Open NetCDF Dataset into Define Mode: nc_redef </A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-187"><B>5.7 </B> - Leave Define Mode: nc_enddef </A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-221"><B>5.8 </B> - Close an Open NetCDF Dataset: nc_close </A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-254"><B>5.9 </B> - Inquire about an Open NetCDF Dataset: nc_inq Family</A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-322"><B>5.10 </B> - Synchronize an Open NetCDF Dataset to Disk: nc_sync </A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-363"><B>5.11 </B> - Back Out of Recent Definitions: nc_abort </A>
<DD>
<DT><A REL=BELOW HREF="guidec-10.html#HEADING10-400"><B>5.12 </B> - Set Fill Mode for Writes: nc_set_fill </A>
<DD>
</DL>


</DIV>

<HR>
<ADDRESS>NetCDF User's Guide for C - 6 Nov 1997</ADDRESS>
<P><A HREF=guidec-11.html>[Next] </A><A HREF=guidec-9.html>[Previous] </A><A HREF=guidec-1.html>[Top] </A><A HREF=guidec-3.html>[Contents] </A><A HREF=guidec-21.html>[Index] </A><A HREF="http://www.unidata.ucar.edu/packages/netcdf/">[netCDF Home Page] </A><A HREF="http://www.unidata.ucar.edu/">[Unidata Home Page]</A></P>
<P><H6>&lt;http://www.unidata.ucar.edu<!--#echo var="DOCUMENT_URI"-->&gt;</H6></P>

</DIV>
</BODY>
</HTML>