Sophie

Sophie

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

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-11.html>
<LINK REL=NEXT HREF=guidec-13.html>
<TITLE>7	 Variables</TITLE>
<META NAME=GENERATOR CONTENT="Harlequin WebMaker 3.0.2 ( 8-May-1977 experimental)">
</HEAD>
<BODY bgcolor="#ffffff">

<DIV>
<A NAME=HEADING12></A>

<DIV>
<P><A HREF=guidec-13.html>[Next] </A><A HREF=guidec-11.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=HEADING12-0></A>
<A NAME=UID-guide.fm-1019131></A>
<H1>7  <A NAME=MARKER-10-2580></A>Variables</H1>
<HR>
<P>Variables <A NAME=MARKER-2-2581></A><A NAME=MARKER-2-2582></A><A NAME=MARKER-2-2583></A><A NAME=MARKER-2-2584></A><A NAME=MARKER-2-2585></A><A NAME=MARKER-2-2586></A><A NAME=MARKER-2-2587></A><A NAME=MARKER-2-2588></A><A NAME=MARKER-2-2589></A>for a netCDF dataset are defined when the dataset is created, while the netCDF dataset is in define mode. Other variables may be added later by reentering define mode. A netCDF variable has a name, a type, and a shape, which are specified when it is defined. A variable may also have values, which are established later in data mode. </P>
<P>Ordinarily, <A NAME=MARKER-2-2590></A><A NAME=MARKER-2-2591></A><A NAME=MARKER-2-2592></A><A NAME=MARKER-2-2593></A><A NAME=MARKER-2-2594></A><A NAME=MARKER-2-2595></A>the name, type, and shape are fixed when the variable is first defined. The name may be changed, but the type and shape of a variable cannot be changed. However, a variable defined in terms of the unlimited dimension can grow without bound in that dimension. </P>
<P>A <A NAME=MARKER-2-2596></A><A NAME=MARKER-2-2597></A>netCDF variable in an open netCDF dataset is referred to by a small integer called a <I>variable ID</I>.</P>
<P>Variable IDs reflect the order in which variables were defined within a netCDF dataset. Variable IDs are 0,<A NAME=MARKER-10-2598></A> 1,<A NAME=MARKER-10-2599></A> 2,<A NAME=MARKER-10-2600></A>..., in the order in which the variables were defined. A function is available for getting the variable ID from the variable name and vice-versa. </P>
<P>Attributes (<A HREF="guidec-13.html#MARKER-9-2957">see Chapter 8 "Attributes,"  page&nbsp;81</A>) may be associated with a variable to specify such properties as units.</P>
<P>Operations supported on variables are:</P>
<UL>
<LI>Create a variable, given its name, data type, and shape.
<LI>Get a variable ID from its name.
<LI>Get a variable's name, data type, shape, and number of attributes from its ID.
<LI>Put a data value into a variable, given variable ID, indices, and value.
<LI>Put an array of values into a variable, given variable ID, corner indices, edge lengths, and a block of values.
<LI>Put a subsampled or mapped array-section of values into a variable, given variable ID, corner indices, edge lengths, <A NAME=MARKER-2-2601></A><A NAME=MARKER-2-2602></A>stride vector, <A NAME=MARKER-2-2603></A><A NAME=MARKER-2-2604></A><A NAME=MARKER-2-2605></A>index mapping vector, and a block of values.
<LI>Get a data value from a variable, given variable ID and indices.
<LI>Get an array of values from a variable, given variable ID, corner indices, and edge lengths.
<LI>Get a subsampled or mapped array-section of values from a variable, given variable ID, corner indices, edge lengths, <A NAME=MARKER-2-2606></A><A NAME=MARKER-2-2607></A>stride vector, and <A NAME=MARKER-2-2608></A><A NAME=MARKER-2-2609></A><A NAME=MARKER-2-2610></A>index mapping vector.
<LI>Rename a variable.
</UL>
<A NAME=HEADING12-17></A>
<A NAME=UID-guide.fm-1019164></A>
<P><BR><BR></P>
<H2>7.1  Language Types Corresponding to netCDF external data types</H2>
<HR>
<P>The <A NAME=MARKER-2-2611></A><A NAME=MARKER-2-2612></A><A NAME=MARKER-2-2613></A><A NAME=MARKER-2-2614></A><A NAME=MARKER-2-2615></A><A NAME=MARKER-2-2616></A><A NAME=MARKER-2-2617></A><A NAME=MARKER-2-2618></A>following table gives the netCDF external data types and the corresponding type constants for defining variables in the C <A NAME=MARKER-10-2619></A>interface:</P>
<P></P>
<TABLE BORDER=1><TR VALIGN=TOP><TH><P> <B>netCDF/CDL Data Type</B></P>
</TH><TH><P> <B>C API Mnemonic</B></P>
</TH><TH><P> <B>Bits</B></P>
</TH></TR><TR VALIGN=TOP><TD><P> <CODE>byte</CODE></P>
</TD><TD><P> <CODE><A NAME=MARKER-2-249></A>NC_BYTE <A NAME=MARKER-10-250></A></CODE></P>
</TD><TD><P> 8</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>char</CODE></P>
</TD><TD><P> <CODE><A NAME=MARKER-2-251></A>NC_CHAR <A NAME=MARKER-10-252></A></CODE></P>
</TD><TD><P> 8</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>short</CODE></P>
</TD><TD><P> <CODE><A NAME=MARKER-2-253></A>NC_SHORT <A NAME=MARKER-10-254></A></CODE></P>
</TD><TD><P> 16</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>int</CODE></P>
</TD><TD><P> <CODE><A NAME=MARKER-2-255></A>NC_INT <A NAME=MARKER-10-256></A></CODE></P>
</TD><TD><P> 32</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>float</CODE></P>
</TD><TD><P> <CODE><A NAME=MARKER-2-257></A>NC_FLOAT <A NAME=MARKER-10-258></A></CODE></P>
</TD><TD><P> 32</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>double</CODE></P>
</TD><TD><P> <CODE><A NAME=MARKER-2-259></A>NC_DOUBLE <A NAME=MARKER-10-260></A></CODE></P>
</TD><TD><P> 64</P>
</TD></TR></TABLE><P>The first column gives the netCDF external data type, which is the same as the CDL data type. The next column gives the corresponding C preprocessor macro <A NAME=MARKER-10-2620></A>for use in netCDF functions (the preprocessor macros are defined in the netCDF C header-file <CODE>netcdf.h</CODE>)<A NAME=MARKER-10-2621></A>. The last column gives the number of bits used in the external representation of values of the corresponding type.</P>
<P>Note that there are no netCDF types corresponding to 64-bit integers or to characters wider than 8 bits in the current version of the netCDF library. </P>
<A NAME=HEADING12-101></A>
<A NAME=UID-guide.fm-1036555></A>
<P><BR><BR></P>
<H2>7.2  Create a Variable: <CODE><A NAME=MARKER-2-2622></A>nc_def_var</CODE> <A NAME=MARKER-10-2623></A><CODE></CODE></H2>
<HR>
<P>The <A NAME=MARKER-2-2624></A><A NAME=MARKER-2-2625></A><A NAME=MARKER-2-2626></A>function <CODE>nc_def_var</CODE> <A NAME=MARKER-10-2627></A>adds a new variable to an open netCDF dataset in define mode. It returns (as an argument) a variable ID, given the netCDF ID, the variable name, the variable type, the number of dimensions, and a list of the dimension IDs. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_def_var (int ncid, const char *name, nc_type xtype,
                int ndims, const int dimids[], int *varidp);
</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>name</CODE></P>
</TD><TD><P> Variable name. Must begin with an alphabetic character, followed by zero or more alphanumeric characters including the underscore ('<CODE>_</CODE>'). Case is significant.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>xtype</CODE></P>
</TD><TD><P> One <A NAME=MARKER-2-288></A>of the set of predefined netCDF external data types. The type of this parameter, <CODE><A NAME=MARKER-2-289></A>nc_type</CODE>, is defined in the netCDF header file. The valid netCDF external data types are <CODE>NC_BYTE</CODE>, <CODE>NC_CHAR</CODE>, <CODE>NC_SHORT</CODE>, <CODE>NC_INT</CODE>, <CODE>NC_FLOAT</CODE>, and <CODE>NC_DOUBLE</CODE>.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>ndims</CODE></P>
</TD><TD><P> Number <A NAME=MARKER-2-290></A><A NAME=MARKER-2-291></A><A NAME=MARKER-2-292></A><A NAME=MARKER-2-293></A>of dimensions for the variable. For example, <CODE>2</CODE> specifies a matrix, <CODE>1</CODE> specifies a vector, and <CODE>0</CODE> means the variable is a scalar with no dimensions. Must not be negative or greater than the predefined constant <CODE><A NAME=MARKER-2-294></A>NC_MAX_VAR_DIMS</CODE>.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>dimids</CODE></P>
</TD><TD><P> Vector of <CODE>ndims</CODE> dimension IDs corresponding to the variable dimensions. If the ID of the unlimited dimension is included, it must be first. This argument is ignored if <CODE>ndims</CODE> is <CODE>0</CODE>.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>varidp</CODE></P>
</TD><TD><P> Pointer to location for the returned variable ID.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2628></A></H4>
<P><CODE>nc_def_var</CODE> <A NAME=MARKER-10-2629></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2630></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: </P>
<UL>
<LI>The <A NAME=MARKER-2-2631></A><A NAME=MARKER-2-2632></A>netCDF dataset is not in define mode.
<LI>The specified variable name is the name of another existing variable.
<LI>The specified type is not a valid netCDF type.
<LI>The <A NAME=MARKER-2-2633></A><A NAME=MARKER-2-2634></A><A NAME=MARKER-2-2635></A><A NAME=MARKER-2-2636></A><A NAME=MARKER-2-2637></A><A NAME=MARKER-2-2638></A><A NAME=MARKER-2-2639></A>specified number of dimensions is negative or more than the constant <CODE>NC_MAX_VAR_DIMS</CODE>,<A NAME=MARKER-10-2640></A> the maximum number of dimensions permitted for a netCDF variable. 
<LI>One <A NAME=MARKER-2-2641></A><A NAME=MARKER-2-2642></A>or more of the dimension IDs in the list of dimensions is not a valid dimension ID for the netCDF dataset.
<LI>The <A NAME=MARKER-2-2643></A><A NAME=MARKER-2-2644></A>number of variables would exceed the constant <CODE><A NAME=MARKER-2-2645></A>NC_MAX_VARS</CODE>,<A NAME=MARKER-10-2646></A> the maximum number of variables permitted in a netCDF dataset. 
<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-2647></A>is an example using <CODE>nc_def_var</CODE> <A NAME=MARKER-10-2648></A>to create a variable named <CODE>rh</CODE> of type <CODE>double</CODE> with three dimensions, <CODE>time</CODE>, <CODE>lat</CODE>, and <CODE>lon</CODE> in a new netCDF dataset named <CODE>foo.nc</CODE>:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int  status;                       /* error status */
int  ncid;                         /* netCDF ID */
int  lat_dim, lon_dim, time_dim;   /* dimension IDs */
int  rh_id;                        /* variable ID */
int  rh_dimids[3];                 /* variable shape */
   ... 
status = nc_create(&quot;foo.nc&quot;, NC_NOCLOBBER, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
                                   /* define dimensions */
status = nc_def_dim(ncid, &quot;lat&quot;, 5L, &amp;lat_dim);
if (status != NC_NOERR) handle_error(status);
status = nc_def_dim(ncid, &quot;lon&quot;, 10L, &amp;lon_dim);
if (status != NC_NOERR) handle_error(status);
status = nc_def_dim(ncid, &quot;time&quot;, NC_UNLIMITED, &amp;time_dim);
if (status != NC_NOERR) handle_error(status);
   ... 
                                   /* define variable */
rh_dimids[0] = time_dim;
rh_dimids[1] = lat_dim;
rh_dimids[2] = lon_dim;
status = nc_def_var (ncid, &quot;rh&quot;, NC_DOUBLE, 3, rh_dimids, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-193></A>
<A NAME=UID-guide.fm-1036975></A>
<P><BR><BR></P>
<H2>7.3  <A NAME=MARKER-10-2649></A><A NAME=MARKER-2-2650></A>Get a Variable ID from Its Name: <CODE><A NAME=MARKER-2-2651></A>nc_inq_varid</CODE> <A NAME=MARKER-10-2652></A><CODE></CODE></H2>
<HR>
<P>The function <CODE>nc_inq_varid</CODE> <A NAME=MARKER-10-2653></A>returns the ID of a netCDF variable, given its name.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_inq_varid (int ncid, const char *name, int *varidp);
</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>name</CODE></P>
</TD><TD><P> Variable name for which ID is desired.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>varidp</CODE></P>
</TD><TD><P> Pointer to location for returned variable ID.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2654></A></H4>
<P><CODE>nc_inq_varid</CODE> <A NAME=MARKER-10-2655></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2656></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The specified variable name is not a valid name for a variable in the specified netCDF dataset.
<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-2657></A>is an example using <CODE>nc_inq_varid</CODE> <A NAME=MARKER-10-2658></A>to find out the ID of a variable named <CODE>rh</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int  status, ncid, rh_id;
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_NOWRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid (ncid, &quot;rh&quot;, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-239></A>
<A NAME=UID-guide.fm-1025332></A>
<P><BR><BR></P>
<H2>7.4  <A NAME=MARKER-10-2659></A>Get Information about a Variable from Its ID: <CODE><A NAME=MARKER-2-2660></A>nc_inq_var</CODE> <CODE><A NAME=MARKER-10-2661></A></CODE>family</H2>
<HR>
<P>A <A NAME=MARKER-2-2662></A><A NAME=MARKER-2-2663></A><A NAME=MARKER-2-2664></A><A NAME=MARKER-2-2665></A><A NAME=MARKER-2-2666></A><A NAME=MARKER-2-2667></A><A NAME=MARKER-2-2668></A><A NAME=MARKER-2-2669></A><A NAME=MARKER-2-2670></A><A NAME=MARKER-2-2671></A><A NAME=MARKER-2-2672></A><A NAME=MARKER-2-2673></A><A NAME=MARKER-2-2674></A><A NAME=MARKER-2-2675></A>family of functions that returns information about a netCDF variable, given its ID. Information about a variable includes its name, type, number of dimensions, a list of dimension IDs describing the shape of the variable, and the number of variable attributes that have been assigned to the variable. </P>
<P>The function <CODE>nc_inq_var <A NAME=MARKER-10-2676></A></CODE>returns all the information about a netCDF variable, given its ID. The other functions each return just one item of information about a variable. </P>
<P>These other functions include <CODE><A NAME=MARKER-2-2677></A>nc_inq_varname</CODE>,<A NAME=MARKER-10-2678></A><A NAME=MARKER-2-2679></A> <CODE>nc_inq_vartype</CODE>,<A NAME=MARKER-10-2680></A> <CODE><A NAME=MARKER-2-2681></A>nc_inq_varndims</CODE>,<A NAME=MARKER-2-2682></A><A NAME=MARKER-10-2683></A> <CODE>nc_inq_vardimid</CODE>,<A NAME=MARKER-10-2684></A> and<A NAME=MARKER-2-2685></A> <CODE>nc_inq_varnatts</CODE>.<A NAME=MARKER-10-2686></A> </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_inq_var      (int ncid, int varid, char *name, nc_type *xtypep,
                     int *ndimsp, int dimids[], int *nattsp);
int nc_inq_varname  (int ncid, int varid, char *name);
int nc_inq_vartype  (int ncid, int varid, nc_type *xtypep);
int nc_inq_varndims (int ncid, int varid, int *ndimsp);
int nc_inq_vardimid (int ncid, int varid, int dimids[]);
int nc_inq_varnatts (int ncid, int varid, int *nattsp);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>name</CODE></P>
</TD><TD><P> Returned <A NAME=MARKER-2-295></A><A NAME=MARKER-2-296></A><A NAME=MARKER-2-297></A>variable name. The caller must allocate space for the returned name. The maximum possible length, in characters, of a variable name is given by the predefined constant <CODE><A NAME=MARKER-2-298></A>NC_MAX_NAME</CODE>. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>xtypep</CODE></P>
</TD><TD><P> Pointer to location for returned variable type, one of the set of predefined netCDF external data types. The type of this parameter, <CODE>nc_type</CODE>, is defined in the netCDF header file. The valid netCDF external data types are <CODE>NC_BYTE</CODE>, <CODE>NC_CHAR</CODE>, <CODE>NC_SHORT</CODE>, <CODE>NC_INT</CODE>, <CODE>NC_FLOAT</CODE>, and <CODE>NC_DOUBLE</CODE>. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>ndimsp</CODE></P>
</TD><TD><P> Pointer to location for returned number of dimensions the variable was defined as using. For example, <CODE>2</CODE> indicates a matrix, <CODE>1 </CODE>indicates a vector, and <CODE>0</CODE> means the variable is a scalar with no dimensions. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>dimids</CODE></P>
</TD><TD><P> Returned <A NAME=MARKER-2-299></A><A NAME=MARKER-2-300></A><A NAME=MARKER-2-301></A><A NAME=MARKER-2-302></A>vector of <CODE>*ndimsp</CODE> dimension IDs corresponding to the variable dimensions. The caller must allocate enough space for a vector of at least <CODE>*ndimsp</CODE> integers to be returned. The maximum possible number of dimensions for a variable is given by the predefined constant <CODE><A NAME=MARKER-2-303></A>NC_MAX_VAR_DIMS</CODE>. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>nattsp</CODE></P>
</TD><TD><P> Pointer to location for returned number of variable attributes assigned to this variable.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2687></A></H4>
<P>These functions return the value <CODE>NC_NOERR <A NAME=MARKER-10-2688></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<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-2689></A>is an example using <CODE>nc_inq_var</CODE> <A NAME=MARKER-10-2690></A>to find out about a variable named <CODE>rh</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int  status                        /* error status */
int  ncid;                         /* netCDF ID */
int  rh_id;                        /* variable ID */
nc_type rh_type;                   /* variable type */
int rh_ndims;                      /* number of dims */
int  rh_dims[NC_MAX_VAR_DIMS];     /* variable shape */
int rh_natts                       /* number of attributes */
   ... 
status = nc_open (&quot;foo.nc&quot;, NC_NOWRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid (ncid, &quot;rh&quot;, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
/* we don't need name, since we already know it */
status = nc_inq_var (ncid, rh_id, 0, &amp;rh_type, &amp;rh_ndims, rh_dims,
                     &amp;rh_natts);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-333></A>
<A NAME=UID-guide.fm-1027388></A>
<P><BR><BR></P>
<H2>7.5  <A NAME=MARKER-10-2691></A>Write a Single Data Value: <CODE><A NAME=MARKER-2-2692></A>nc_put_var1_</CODE> <I>type</I>  <A NAME=MARKER-10-2693></A><I></I></H2>
<HR>
<P>The <A NAME=MARKER-2-2694></A><A NAME=MARKER-2-2695></A><A NAME=MARKER-2-2696></A><A NAME=MARKER-2-2697></A>functions <CODE>nc_put_var1_</CODE> <I>type</I>  <A NAME=MARKER-10-2698></A>put a single data value of the specified <VAR>type</VAR> into a variable of an open netCDF dataset that is in data mode. Inputs are the netCDF ID, the variable ID, an index that specifies which value to add or alter, and the data value. The value is converted to the external data type of the variable, if necessary.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_put_var1_text  (int ncid, int varid, const size_t index[],
                       const char *tp);
int nc_put_var1_uchar (int ncid, int varid, const size_t index[],
                       const unsigned char *up);
int nc_put_var1_schar (int ncid, int varid, const size_t index[],
                       const signed char *cp);
int nc_put_var1_short (int ncid, int varid, const size_t index[],
                       const short *sp);
int nc_put_var1_int   (int ncid, int varid, const size_t index[],
                       const int *ip);
int nc_put_var1_long  (int ncid, int varid, const size_t index[],
                       const long *lp);
int nc_put_var1_float (int ncid, int varid, const size_t index[],
                       const float *fp); 
int nc_put_var1_double(int ncid, int varid, const size_t index[],
                       const double *dp);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>index[]</CODE></P>
</TD><TD><P> The index of the data value to be written. The indices are relative to 0, so for example, the first data value of a two-dimensional variable would have index <CODE>(0,0)</CODE>. The elements of <CODE>index</CODE> must correspond to the variable's dimensions. Hence, if the variable uses the unlimited dimension, the first index would correspond to the unlimited dimension.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>tp, up, cp, sp, ip, lp, fp, </CODE>or<CODE> dp</CODE></P>
</TD><TD><P> Pointer to the data value to be written. If the type of data values differs from the netCDF variable type, type conversion will occur. <A HREF="guidec-8.html#MARKER-9-2044">See Section 3.3 "Type Conversion,"  page&nbsp;20</A>, for details.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2699></A></H4>
<P><CODE>nc_put_var1_</CODE> <I>type</I>  <A NAME=MARKER-10-2700></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2701></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>The specified indices were out of range for the rank of the specified variable. For example, a negative index or an index that is larger than the corresponding dimension length will cause an error. 
<LI>The specified value is out of the range of values representable by the external data type of the variable.
<LI>The specified netCDF is in define mode rather than data 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-2702></A>is an example using <CODE>nc_put_var1_double</CODE> to set the <CODE>(1,2,3)</CODE> element of the variable named <CODE>rh</CODE> to <CODE>0.5</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>. For simplicity in this example, we assume that we know that <CODE>rh</CODE> is dimensioned with <CODE>time</CODE>, <CODE>lat</CODE>, and <CODE>lon</CODE>, so we want to set the value of <CODE>rh</CODE> that corresponds to the second <CODE>time</CODE> value, the third <CODE>lat</CODE> value, and the fourth <CODE>lon</CODE> value: </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int  status;                          /* error status */
int  ncid;                            /* netCDF ID */
int  rh_id;                           /* variable ID */
static size_t rh_index[] = {1, 2, 3}; /* where to put value */
static double rh_val = 0.5;           /* value to put */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid (ncid, &quot;rh&quot;, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_put_var1_double(ncid, rh_id, rh_index, &amp;rh_val);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-411></A>
<A NAME=UID-guide.fm-1028203></A>
<P><BR><BR></P>
<H2>7.6  <A NAME=MARKER-10-2703></A><A NAME=MARKER-2-2704></A><A NAME=MARKER-2-2705></A><A NAME=MARKER-2-2706></A><A NAME=MARKER-2-2707></A>Write an Entire Variable: <CODE><A NAME=MARKER-2-2708></A>nc_put_var_</CODE> <I>type</I> <A NAME=MARKER-10-2709></A><CODE></CODE></H2>
<HR>
<P>The <CODE>nc_put_var_</CODE> <I>type</I>  <A NAME=MARKER-10-2710></A>family of functions write all the values of a variable into a netCDF variable of an open netCDF dataset. This is the simplest interface to use for writing a value in a scalar variable or whenever all the values of a multidimensional variable can all be written at once. The values to be written are associated with the netCDF variable by assuming that the last <A NAME=MARKER-10-2711></A>dimension of the netCDF variable varies fastest in the C <A NAME=MARKER-10-2712></A>interface. The values are converted to the external data type of the variable, if necessary.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_put_var_text  (int ncid, int varid, const char *tp);
int nc_put_var_uchar (int ncid, int varid, const unsigned char *up);
int nc_put_var_schar (int ncid, int varid, const signed char *cp);
int nc_put_var_short (int ncid, int varid, const short *sp);
int nc_put_var_int   (int ncid, int varid, const int *ip);
int nc_put_var_long  (int ncid, int varid, const long *lp);
int nc_put_var_float (int ncid, int varid, const float *fp);
int nc_put_var_double(int ncid, int varid, const double *dp);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>tp, up, cp, sp, ip, lp, fp, or dp</CODE></P>
</TD><TD><P> Pointer to a block of data values to be written. The order in which the data will be written to the netCDF variable is with the last dimension of the specified variable varying fastest. If the type of data values differs from the netCDF variable type, type conversion will occur. <A HREF="guidec-8.html#MARKER-9-2044">See Section 3.3 "Type Conversion,"  page&nbsp;20</A>, for details.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2713></A></H4>
<P>Members of the <CODE>nc_put_var_</CODE> <I>type</I>  <A NAME=MARKER-10-2714></A>family return the value <CODE>NC_NOERR <A NAME=MARKER-10-2715></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>One or more of the specified values are out of the range of values representable by the external data type of the variable.
<LI>One or more of the specified values are out of the range of values representable by the external data type of the variable.
<LI>The specified netCDF dataset is in define mode rather than data 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-2716></A>is an example using <CODE>nc_put_var_double</CODE> to add or change all the values of the variable named <CODE>rh</CODE> to <CODE>0.5</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>. For simplicity in this example, we assume that we know that <CODE>rh</CODE> is dimensioned with <CODE>time</CODE>, <CODE>lat</CODE>, and <CODE>lon</CODE>, and that there are three <CODE>time</CODE> values, five <CODE>lat</CODE> values, and ten <CODE>lon</CODE> values. </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
#define TIMES 3
#define LATS  5
#define LONS  10
int  status;                       /* error status */
int  ncid;                         /* netCDF ID */
int  rh_id;                        /* variable ID */
double rh_vals[TIMES*LATS*LONS];   /* array to hold values */
int i;
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid (ncid, &quot;rh&quot;, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
   ... 
for (i = 0; i &lt; TIMES*LATS*LONS; i++)
    rh_vals[i] = 0.5;
/* write values into netCDF variable */
status = nc_put_var_double(ncid, rh_id, rh_vals);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-480></A>
<A NAME=UID-guide.fm-1028234></A>
<P><BR><BR></P>
<H2>7.7  <A NAME=MARKER-10-2717></A><A NAME=MARKER-2-2718></A><A NAME=MARKER-2-2719></A><A NAME=MARKER-2-2720></A><A NAME=MARKER-2-2721></A>Write an Array of Values: <CODE><A NAME=MARKER-2-2722></A>nc_put_vara_</CODE> <I>type</I>  <A NAME=MARKER-10-2723></A><I></I></H2>
<HR>
<P>The function <CODE>nc_put_vara_</CODE> <I>type</I>  <A NAME=MARKER-10-2724></A>writes values into a netCDF variable of an open netCDF dataset. The part of the netCDF variable to write is specified by giving a corner and a vector of edge lengths that refer to an array section of the netCDF variable. The values to be written are associated with the netCDF variable by assuming that the last <A NAME=MARKER-10-2725></A>dimension of the netCDF variable varies fastest in the C <A NAME=MARKER-10-2726></A>interface. The netCDF dataset must be in data mode. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_put_vara<CODE>_ <I>type </I></CODE>  (int ncid, int varid, const size_t start[],
                       const size_t count[], const <I>type</I> *valuesp);
int nc_put_vara_text  (int ncid, int varid, const size_t start[],
                       const size_t count[], const char *tp);
int nc_put_vara_uchar (int ncid, int varid, const size_t start[],
                       const size_t count[], const unsigned char *up);
int nc_put_vara_schar (int ncid, int varid, const size_t start[],
                       const size_t count[], const signed char *cp);
int nc_put_vara_short (int ncid, int varid, const size_t start[],
                       const size_t count[], const short *sp);
int nc_put_vara_int   (int ncid, int varid, const size_t start[],
                       const size_t count[], const int *ip);
int nc_put_vara_long  (int ncid, int varid, const size_t start[],
                       const size_t count[], const long *lp);
int nc_put_vara_float (int ncid, int varid, const size_t start[],
                       const size_t count[], const float *fp);
int nc_put_vara_double(int ncid, int varid, const size_t start[],
                       const size_t count[], const double *dp);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>start</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the index in the variable where the first of the data values will be written. The indices are relative to 0, so for example, the first data value of a variable would have index <CODE>(0, 0, ... , 0)</CODE>. The size of <CODE>start</CODE> must be the same as the number of dimensions of the specified variable. The elements of <CODE>start</CODE> must correspond to the variable's dimensions in order. Hence, if the variable is a record variable, the first index would correspond to the starting record number for writing the data values. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>count</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the edge lengths along each dimension of the block of data values to be written. To write a single value, for example, specify <CODE>count</CODE> as <CODE>(1, 1, ... , 1)</CODE>. The length of <CODE>count</CODE> is the number of dimensions of the specified variable. The elements of <CODE>count </CODE>correspond to the variable's dimensions. Hence, if the variable is a record variable, the first element of <CODE>count</CODE> corresponds to a count of the number of records to write. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>tp, up, cp, sp, ip, lp, fp, or dp</CODE></P>
</TD><TD><P> Pointer to a block of data values to be written. The order in which the data will be written to the netCDF variable is with the last dimension of the specified variable varying fastest. If the type of data values differs from the netCDF variable type, type conversion will occur. <A HREF="guidec-8.html#MARKER-9-2044">See Section 3.3 "Type Conversion,"  page&nbsp;20</A>, for details.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2727></A></H4>
<P><CODE>nc_put_vara_</CODE> <I>type</I>  <A NAME=MARKER-10-2728></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2729></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>The specified corner indices were out of range for the rank of the specified variable. For example, a negative index, or an index that is larger than the corresponding dimension length will cause an error. 
<LI>The specified edge lengths added to the specified corner would have referenced data out of range for the rank of the specified variable. For example, an edge length that is larger than the corresponding dimension length minus the corner index will cause an error. 
<LI>One or more of the specified values are out of the range of values representable by the external data type of the variable.
<LI>The specified netCDF dataset is in define mode rather than data 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-2730></A>is an example using <CODE>nc_put_vara_double</CODE> to add or change all the values of the variable named <CODE>rh</CODE> to <CODE>0.5</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>. For simplicity in this example, we assume that we know that <CODE>rh</CODE> is dimensioned with <CODE>time</CODE>, <CODE>lat</CODE>, and <CODE>lon</CODE>, and that there are three <CODE>time</CODE> values, five <CODE>lat</CODE> values, and ten <CODE>lon</CODE> values. </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
#define TIMES 3
#define LATS  5
#define LONS  10
int  status;                       /* error status */
int  ncid;                         /* netCDF ID */
int  rh_id;                        /* variable ID */
static size_t start[] = {0, 0, 0}; /* start at first value */
static size_t count[] = {TIMES, LATS, LONS};
double rh_vals[TIMES*LATS*LONS];   /* array to hold values */
int i;
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid (ncid, &quot;rh&quot;, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
   ... 
for (i = 0; i &lt; TIMES*LATS*LONS; i++)
    rh_vals[i] = 0.5;
/* write values into netCDF variable */
status = nc_put_vara_double(ncid, rh_id, start, count, rh_vals);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-578></A>
<A NAME=UID-guide.fm-1036339></A>
<P><BR><BR></P>
<H2>7.8  <A NAME=MARKER-10-2731></A><A NAME=MARKER-2-2732></A><A NAME=MARKER-2-2733></A><A NAME=MARKER-2-2734></A><A NAME=MARKER-2-2735></A>Write a Subsampled Array of Values: <CODE><A NAME=MARKER-2-2736></A>nc_put_vars_</CODE> <I>type</I>  <CODE><A NAME=MARKER-10-2737></A><I></I></CODE></H2>
<HR>
<P>Each member of the family of functions <CODE>nc_put_vars_</CODE> <I>type</I>  <A NAME=MARKER-10-2738></A>writes a subsampled (strided) array section of values into a netCDF variable of an open netCDF dataset. The subsampled array section is specified by giving a corner, a vector of counts, and a <A NAME=MARKER-2-2739></A><A NAME=MARKER-2-2740></A>stride vector. The netCDF dataset must be in data mode. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_put_vars_text  (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const char *tp);
int nc_put_vars_uchar (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const unsigned char *up);
int nc_put_vars_schar (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const signed char *cp);
int nc_put_vars_short (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const short *sp);
int nc_put_vars_int   (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const int *ip);
int nc_put_vars_long  (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const long *lp);
int nc_put_vars_float (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const float *fp);
int nc_put_vars_double(int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const double *dp);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>start</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the index in the variable where the first of the data values will be written. The indices are relative to 0, so for example, the first data value of a variable would have index <CODE>(0, 0, ... , 0)</CODE>. The elements of <CODE>start</CODE> correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first index corresponds to the starting record number for writing the data values. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>count</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the number of indices selected along each dimension. To write a single value, for example, specify <CODE>count</CODE> as <CODE>(1, 1, ... , 1)</CODE>. The elements of <CODE>count</CODE> correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first element of <CODE>count</CODE> corresponds to a count of the number of records to write. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>stride</CODE></P>
</TD><TD><P> A vector of ptrdiff_t integers that specifies the sampling interval along each dimension of the netCDF variable. The elements of the <A NAME=MARKER-2-304></A><A NAME=MARKER-2-305></A>stride vector correspond, in order, to the netCDF variable's dimensions (stride[0] gives the sampling interval along the most slowly varying dimension of the netCDF variable). Sampling intervals are specified in type-independent units of elements (a value of 1 selects consecutive elements of the netCDF variable along the corresponding dimension, a value of 2 selects every other element, etc.). A <CODE>NULL</CODE> stride argument is treated as <CODE>(1, 1, ... , 1)</CODE>. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>tp, up, cp, sp, ip, lp, fp, or dp</CODE></P>
</TD><TD><P> Pointer to a block of data values to be written. The order in which the data will be written to the netCDF variable is with the last dimension of the specified variable varying fastest. If the type of data values differs from the netCDF variable type, type conversion will occur. <A HREF="guidec-8.html#MARKER-9-2044">See Section 3.3 "Type Conversion,"  page&nbsp;20</A>, for details.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2741></A></H4>
<P><CODE>nc_put_vars_</CODE> <I>type</I>  <A NAME=MARKER-10-2742></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2743></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>The specified start, count and stride generate an index which is out of range.
<LI>One or more of the specified values are out of the range of values representable by the external data type of the variable.
<LI>The specified netCDF is in define mode rather than data mode.
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here is an example of using <CODE>nc_put_vars_float</CODE> <A NAME=MARKER-10-2744></A>to write -- from an internal array -- every other point of a netCDF variable named <CODE>rh</CODE> which is described by the C <A NAME=MARKER-10-2745></A>declaration <CODE>float rh[4][6]</CODE> <A NAME=MARKER-10-2746></A>(note the size of the dimensions):</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
#define NDIM 2                /* rank of netCDF variable */
int ncid;                     /* netCDF ID */
int status;                   /* error status */
int rhid;                     /* variable ID */
static size_t start[NDIM]     /* netCDF variable start point: */
                 = {0, 0};    /* first element */
static size_t count[NDIM]     /* size of internal array: entire */
                   = {2, 3};  /* (subsampled) netCDF variable */
static ptrdiff_t stride[NDIM] /* variable subsampling intervals: */
                 = {2, 2};    /* access every other netCDF element */
float rh[2][3];               /* note subsampled sizes for */
                              /* netCDF variable dimensions */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid(ncid, &quot;rh&quot;, &amp;rhid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_put_vars_float(ncid, rhid, start, count, stride, rh);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-688></A>
<A NAME=UID-guide.fm-1019781></A>
<P><BR><BR></P>
<H2>7.9  <A NAME=MARKER-10-2747></A><A NAME=MARKER-9-2748></A>Write a Mapped Array of Values: <CODE><A NAME=MARKER-2-2749></A>nc_put_varm_</CODE> <I>type</I> <CODE> <A NAME=MARKER-10-2750></A><I></I></CODE></H2>
<HR>
<P>The <CODE>nc_put_varm_</CODE> <I>type</I> <B> <A NAME=MARKER-10-2751></A></B><A NAME=MARKER-2-2752></A><A NAME=MARKER-2-2753></A><A NAME=MARKER-2-2754></A><A NAME=MARKER-2-2755></A>family of functions writes a mapped array section of values into a netCDF variable of an open netCDF dataset. The mapped array section is specified by giving a corner, a vector of counts, a <A NAME=MARKER-2-2756></A><A NAME=MARKER-2-2757></A>stride vector, and an <A NAME=MARKER-2-2758></A><A NAME=MARKER-2-2759></A><A NAME=MARKER-2-2760></A>index mapping vector. The <A NAME=MARKER-2-2761></A><A NAME=MARKER-2-2762></A><A NAME=MARKER-2-2763></A>index mapping vector is a vector of integers that specifies the mapping between the dimensions of a netCDF variable and the in-memory structure of the internal data array. No assumptions are made about the ordering or length of the dimensions of the data array. The netCDF dataset must be in data mode. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_put_varm_text  (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], const char *tp);
int nc_put_varm_uchar (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], const unsigned char *up);
int nc_put_varm_schar (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], const signed char *cp);
int nc_put_varm_short (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], const short *sp);
int nc_put_varm_int   (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], const int *ip);
int nc_put_varm_long  (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], const long *lp);
int nc_put_varm_float (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], const float *fp);
int nc_put_varm_double(int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], const double *dp);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>start</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the index in the variable where the first of the data values will be written. The indices are relative to 0, so for example, the first data value of a variable would have index <CODE>(0, 0, ... , 0)</CODE>. The elements of <CODE>start</CODE> correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first index corresponds to the starting record number for writing the data values. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>count</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the number of indices selected along each dimension. To write a single value, for example, specify <CODE>count</CODE> as <CODE>(1, 1, ... , 1)</CODE>. The elements of <CODE>count</CODE> correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first element of <CODE>count</CODE> corresponds to a count of the number of records to write. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>stride</CODE></P>
</TD><TD><P> A vector of ptrdiff_t integers that specifies the sampling interval along each dimension of the netCDF variable. The elements of the <A NAME=MARKER-2-306></A><A NAME=MARKER-2-307></A>stride vector correspond, in order, to the netCDF variable's dimensions (stride[0] gives the sampling interval along the most slowly varying dimension of the netCDF variable). Sampling intervals are specified in type-independent units of elements (a value of 1 selects consecutive elements of the netCDF variable along the corresponding dimension, a value of 2 selects every other element, etc.). A <CODE>NULL</CODE> stride argument is treated as <CODE>(1, 1, ... , 1)</CODE>. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>imap</CODE></P>
</TD><TD><P> A vector of ptrdiff_t integers that specifies the mapping between the dimensions of a netCDF variable and the in-memory structure of the internal data array. The elements of the <A NAME=MARKER-2-308></A><A NAME=MARKER-2-309></A><A NAME=MARKER-2-310></A>index mapping vector correspond, in order, to the netCDF variable's dimensions (imap[0] gives the distance between elements of the internal array corresponding to the most slowly varying dimension of the netCDF variable). Distances between elements are specified in type-independent units of elements (the distance between internal elements that occupy adjacent memory locations is 1 and not the element's byte-length as in netCDF 2). A <CODE>NULL</CODE> argument means the memory-resident values have the same structure as the associated netCDF variable. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>tp, up, cp, sp, ip, lp, fp, or dp</CODE></P>
</TD><TD><P> Pointer to the location used for computing where the data values will be found; the data should be of the type appropriate for the function called. If the type of data values differs from the netCDF variable type, type conversion will occur. <A HREF="guidec-8.html#MARKER-9-2044">See Section 3.3 "Type Conversion,"  page&nbsp;20</A>, for details.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2764></A></H4>
<P><CODE>nc_put_varm_</CODE> <I>type</I> <B> <A NAME=MARKER-10-2765></A></B>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2766></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>The specified <CODE>start</CODE>,<A NAME=MARKER-10-2767></A> <CODE>count</CODE>,<A NAME=MARKER-10-2768></A> and <CODE>stride</CODE> <A NAME=MARKER-10-2769></A>generate an index which is out of range. Note that no error checking is possible on the <CODE>imap</CODE> <A NAME=MARKER-10-2770></A>vector.
<LI>One or more of the specified values are out of the range of values representable by the external data type of the variable.
<LI>The specified netCDF is in define mode rather than data mode.
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>The following <CODE>imap <A NAME=MARKER-10-2771></A></CODE>vector maps in the trivial way a 4x3x2 <A NAME=MARKER-10-2772></A>netCDF variable and an internal array of the same shape: </P>
<PRE>
float a[4][3][2];       /* same shape as netCDF variable */
int   imap[3] = {6, 2, 1};
                        /* netCDF dimension       inter-element distance */
                        /* ----------------       ---------------------- */
                        /* most rapidly varying       1                  */
                        /* intermediate               2 (=imap[2]*2)     */
                        /* most slowly varying        6 (=imap[1]*3)     */
</PRE>
<P><A NAME=MARKER-10-2773></A>Using the <CODE>imap</CODE> <A NAME=MARKER-10-2774></A>vector above with <CODE>nc_put_varm_float</CODE> <A NAME=MARKER-10-2775></A>obtains the same result as simply using <CODE>nc_put_var_float</CODE>.<A NAME=MARKER-10-2776></A></P>
<P>Here is an example of using <CODE>nc_put_varm_float</CODE> <A NAME=MARKER-10-2777></A>to write -- from a transposed, internal array -- a netCDF variable named <CODE>rh</CODE> which is described by the C <A NAME=MARKER-10-2778></A>declaration <CODE>float rh[6][4]</CODE> <A NAME=MARKER-10-2779></A>(note the size and order of the dimensions):</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
#define NDIM 2               /* rank of netCDF variable */
int ncid;                    /* netCDF ID */
int status;                  /* error status */
int rhid;                    /* variable ID */
static size_t start[NDIM]    /* netCDF variable start point: */
                 = {0, 0};   /* first element */
static size_t count[NDIM]    /* size of internal array: entire netCDF */
                 = {6, 4};   /* variable; order corresponds to netCDF */
                             /* variable -- not internal array */
static ptrdiff_t stride[NDIM]/* variable subsampling intervals: */
                 = {1, 1};   /* sample every netCDF element */
static ptrdiff_t imap[NDIM]  /* internal array inter-element distances; */
                 = {1, 6};   /* would be {4, 1} if not transposing */
float rh[4][6];              /* note transposition of netCDF variable */
                             /* dimensions */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid(ncid, &quot;rh&quot;, &amp;rhid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_put_varm_float(ncid, rhid, start, count, stride, imap, rh);
if (status != NC_NOERR) handle_error(status);
</PRE>
<P><A NAME=MARKER-10-2780></A>Here is another example of using <CODE>nc_put_varm_float</CODE> <A NAME=MARKER-10-2781></A>to write -- from a transposed, internal array -- a subsample of the same netCDF variable, by writing every other point of the netCDF variable:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
#define NDIM 2                /* rank of netCDF variable */
int ncid;                     /* netCDF ID */
int status;                   /* error status */
int rhid;                     /* variable ID */
static size_t start[NDIM]     /* netCDF variable start point: */
                 = {0, 0};    /* first element */
static size_t count[NDIM]     /* size of internal array: entire */
                   = {3, 2};  /* (subsampled) netCDF variable; order of */
                              /* dimensions corresponds to netCDF */
                              /* variable -- not internal array */
static ptrdiff_t stride[NDIM] /* variable subsampling intervals: */
                 = {2, 2};    /* sample every other netCDF element */
static ptrdiff_t imap[NDIM]   /* internal array inter-element distances; */
                 = {1, 3};    /* would be {2, 1} if not transposing */
float rh[2][3];               /* note transposition of (subsampled) */
                              /* netCDF variable dimensions */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid(ncid, &quot;rh&quot;, &amp;rhid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_put_varm_float(ncid, rhid, start, count, stride, imap, rh);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-846></A>
<A NAME=UID-guide.fm-1019942></A>
<P><BR><BR></P>
<H2>7.10  <A NAME=MARKER-10-2782></A>Read a Single Data Value: <CODE><A NAME=MARKER-2-2783></A>nc_get_var1_</CODE> <I>type</I>  <A NAME=MARKER-10-2784></A><I></I></H2>
<HR>
<P>The <A NAME=MARKER-2-2785></A><A NAME=MARKER-2-2786></A><A NAME=MARKER-2-2787></A><A NAME=MARKER-2-2788></A>functions <CODE>nc_get_var1_</CODE> <I>type</I>  <A NAME=MARKER-10-2789></A>get a single data value from a variable of an open netCDF dataset that is in data mode. Inputs are the netCDF ID, the variable ID, a multidimensional index that specifies which value to get, and the address of a location into which the data value will be read. The value is converted from the external data type of the variable, if necessary.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_get_var1_text  (int ncid, int varid, const size_t index[],
                       char *tp);
int nc_get_var1_uchar (int ncid, int varid, const size_t index[],
                       unsigned char *up);
int nc_get_var1_schar (int ncid, int varid, const size_t index[],
                       signed char *cp);
int nc_get_var1_short (int ncid, int varid, const size_t index[],
                       short *sp);
int nc_get_var1_int   (int ncid, int varid, const size_t index[],
                       int *ip);
int nc_get_var1_long  (int ncid, int varid, const size_t index[],
                       long *lp);
int nc_get_var1_float (int ncid, int varid, const size_t index[],
                       float *fp); 
int nc_get_var1_double(int ncid, int varid, const size_t index[],
                       double *dp);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>index[]</CODE></P>
</TD><TD><P> The index of the data value to be read. The indices are relative to 0, so for example, the first data value of a two-dimensional variable would have index <CODE>(0,0)</CODE>. The elements of <CODE>index</CODE> must correspond to the variable's dimensions. Hence, if the variable is a record variable, the first index is the record number. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>tp, up, cp, sp, ip, lp, fp, </CODE>or<CODE>, dp</CODE></P>
</TD><TD><P> Pointer to the location into which the data value is read. If the type of data value differs from the netCDF variable type, type conversion will occur. <A HREF="guidec-8.html#MARKER-9-2044">See Section 3.3 "Type Conversion,"  page&nbsp;20</A>, for details.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2790></A></H4>
<P><CODE>nc_get_var1_</CODE> <I>type</I>  <A NAME=MARKER-10-2791></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2792></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>The specified indices were out of range for the rank of the specified variable. For example, a negative index or an index that is larger than the corresponding dimension length will cause an error.
<LI>The value is out of the range of values representable by the desired data type. 
<LI>The specified netCDF is in define mode rather than data 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-2793></A>is an example using <CODE>nc_get_var1_double</CODE> to get the <CODE>(1,2,3)</CODE> element of the variable named <CODE>rh</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>. For simplicity in this example, we assume that we know that <CODE>rh</CODE> is dimensioned with <CODE>time</CODE>, <CODE>lat</CODE>, and <CODE>lon</CODE>, so we want to get the value of <CODE>rh</CODE> that corresponds to the second <CODE>time</CODE> value, the third <CODE>lat</CODE> value, and the fourth <CODE>lon</CODE> value: </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int  status;                           /* error status */
int ncid;                              /* netCDF ID */
int rh_id;                             /* variable ID */
static size_t rh_index[] = {1, 2, 3};  /* where to get value from */
double rh_val;                         /* where to put it */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_NOWRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid (ncid, &quot;rh&quot;, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_get_var1_double(ncid, rh_id, rh_index, &amp;rh_val);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-925></A>
<A NAME=UID-guide.fm-1030255></A>
<P><BR><BR></P>
<H2>7.11  <A NAME=MARKER-10-2794></A>Read an Entire Variable <CODE><A NAME=MARKER-2-2795></A>nc_get_var_</CODE> <I>type</I> <CODE> <A NAME=MARKER-10-2796></A><I></I></CODE></H2>
<HR>
<P>The <A NAME=MARKER-2-2797></A><A NAME=MARKER-2-2798></A><A NAME=MARKER-2-2799></A>members of the <CODE>nc_get_var_</CODE> <I>type</I>  <A NAME=MARKER-10-2800></A>family of functions read all the values from a netCDF variable of an open netCDF dataset. This is the simplest interface to use for reading the value of a scalar variable or when all the values of a multidimensional variable can be read at once. The values are read into consecutive locations with the last <A NAME=MARKER-10-2801></A>dimension varying fastest. The netCDF dataset must be in data mode.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_get_var_text  (int ncid, int varid, char *tp);
int nc_get_var_uchar (int ncid, int varid, unsigned char *up);
int nc_get_var_schar (int ncid, int varid, signed char *cp);
int nc_get_var_short (int ncid, int varid, short *sp);
int nc_get_var_int   (int ncid, int varid, int *ip);
int nc_get_var_long  (int ncid, int varid, long *lp);
int nc_get_var_float (int ncid, int varid, float *fp);
int nc_get_var_double(int ncid, int varid, double *dp);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>tp, up, cp, sp, ip, lp, fp, </CODE>or<CODE> dp</CODE></P>
</TD><TD><P> Pointer to the location into which the data value is read. If the type of data value differs from the netCDF variable type, type conversion will occur. <A HREF="guidec-8.html#MARKER-9-2044">See Section 3.3 "Type Conversion,"  page&nbsp;20</A>, for details.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2802></A></H4>
<P><CODE>nc_get_var_</CODE> <I>type</I>  <A NAME=MARKER-10-2803></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2804></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>One or more of the values are out of the range of values representable by the desired type.
<LI>The specified netCDF is in define mode rather than data 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-2805></A>is an example using <CODE>nc_get_var_double</CODE> to read all the values of the variable named <CODE>rh</CODE> from an existing netCDF dataset named <CODE>foo.nc</CODE>. For simplicity in this example, we assume that we know that <CODE>rh</CODE> is dimensioned with <CODE>time</CODE>, <CODE>lat</CODE>, and <CODE>lon</CODE>, and that there are three <CODE>time</CODE> values, five <CODE>lat </CODE>values, and ten <CODE>lon</CODE> values. </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
#define TIMES 3
#define LATS 5
#define LONS 10
int  status;                       /* error status */
int ncid;                          /* netCDF ID */
int rh_id;                         /* variable ID */
double rh_vals[TIMES*LATS*LONS];   /* array to hold values */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_NOWRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid (ncid, &quot;rh&quot;, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
   ... 
/* read values from netCDF variable */
status = nc_get_var_double(ncid, rh_id, rh_vals);
if (status != NC_NOERR) handle_error(status);
</PRE>
<P><A NAME=MARKER-10-2806></A></P>
<A NAME=HEADING12-991></A>
<A NAME=UID-guide.fm-1020036></A>
<P><BR><BR></P>
<H2>7.12  Read an Array of Values: <CODE><A NAME=MARKER-2-2807></A>nc_get_vara_</CODE> <I>type</I> <CODE> <A NAME=MARKER-10-2808></A><I></I></CODE></H2>
<HR>
<P>The <A NAME=MARKER-2-2809></A><A NAME=MARKER-2-2810></A><A NAME=MARKER-2-2811></A><A NAME=MARKER-2-2812></A>members of the <CODE>nc_get_vara_</CODE> <I>type</I>  <A NAME=MARKER-10-2813></A>family of functions read an array of values from a netCDF variable of an open netCDF dataset. The array is specified by giving a corner and a vector of edge lengths. The values are read into consecutive locations with the last <A NAME=MARKER-10-2814></A>dimension varying fastest. The netCDF dataset must be in data mode.</P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_get_vara_text  (int ncid, int varid, const size_t start[],
                       const size_t count[] char *tp);
int nc_get_vara_uchar (int ncid, int varid, const size_t start[],
                       const size_t count[] unsigned char *up);
int nc_get_vara_schar (int ncid, int varid, const size_t start[],
                       const size_t count[] signed char *cp);
int nc_get_vara_short (int ncid, int varid, const size_t start[],
                       const size_t count[] short *sp);
int nc_get_vara_int   (int ncid, int varid, const size_t start[],
                       const size_t count[] int *ip);
int nc_get_vara_long  (int ncid, int varid, const size_t start[],
                       const size_t count[] long *lp);
int nc_get_vara_float (int ncid, int varid, const size_t start[],
                       const size_t count[] float *fp);
int nc_get_vara_double(int ncid, int varid, const size_t start[],
                       const size_t count[] double *dp);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>start</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the index in the variable where the first of the data values will be read. The indices are relative to 0, so for example, the first data value of a variable would have index <CODE>(0, 0, ... , 0)</CODE>. The length of <CODE>start</CODE> must be the same as the number of dimensions of the specified variable. The elements of <CODE>start</CODE> correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first index would correspond to the starting record number for reading the data values. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>count</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the edge lengths along each dimension of the block of data values to be read. To read a single value, for example, specify <CODE>count</CODE> as <CODE>(1, 1, ... , 1)</CODE>. The length of <CODE>count</CODE> is the number of dimensions of the specified variable. The elements of <CODE>count </CODE>correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first element of <CODE>count</CODE> corresponds to a count of the number of records to read. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>tp, up, cp, sp, ip, lp, fp, </CODE>or<CODE>, dp</CODE></P>
</TD><TD><P> Pointer to the location into which the data value is read. If the type of data value differs from the netCDF variable type, type conversion will occur. <A HREF="guidec-8.html#MARKER-9-2044">See Section 3.3 "Type Conversion,"  page&nbsp;20</A>, for details.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2815></A></H4>
<P><CODE>nc_get_vara_</CODE> <I>type</I>  <A NAME=MARKER-10-2816></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2817></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>The specified corner indices were out of range for the rank of the specified variable. For example, a negative index or an index that is larger than the corresponding dimension length will cause an error. 
<LI>The specified edge lengths added to the specified corner would have referenced data out of range for the rank of the specified variable. For example, an edge length that is larger than the corresponding dimension length minus the corner index will cause an error. 
<LI>One or more of the values are out of the range of values representable by the desired type.
<LI>The specified netCDF is in define mode rather than data 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-2818></A>is an example using <CODE>nc_get_vara_double</CODE> to read all the values of the variable named <CODE>rh</CODE> from an existing netCDF dataset named <CODE>foo.nc</CODE>. For simplicity in this example, we assume that we know that <CODE>rh</CODE> is dimensioned with <CODE>time</CODE>, <CODE>lat</CODE>, and <CODE>lon</CODE>, and that there are three <CODE>time</CODE> values, five <CODE>lat </CODE>values, and ten <CODE>lon</CODE> values. </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
#define TIMES 3
 #define LATS 5
#define LONS 10
int  status;                       /* error status */
int ncid;                          /* netCDF ID */
int rh_id;                         /* variable ID */
static size_t start[] = {0, 0, 0}; /* start at first value */
static size_t count[] = {TIMES, LATS, LONS};
double rh_vals[TIMES*LATS*LONS];   /* array to hold values */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_NOWRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid (ncid, &quot;rh&quot;, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
   ... 
/* read values from netCDF variable */
status = nc_get_vara_double(ncid, rh_id, start, count, rh_vals);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-1084></A>
<A NAME=UID-guide.fm-1030584></A>
<P><BR><BR></P>
<H2>7.13  <A NAME=MARKER-10-2819></A>Read a Subsampled Array of Values: <CODE><A NAME=MARKER-2-2820></A>nc_get_vars_</CODE> <I>type</I> <I> <CODE><A NAME=MARKER-10-2821></A></CODE></I></H2>
<HR>
<P><A NAME=MARKER-2-2822></A><A NAME=MARKER-2-2823></A><A NAME=MARKER-2-2824></A><A NAME=MARKER-2-2825></A><A NAME=MARKER-2-2826></A>The <CODE>nc_get_vars_</CODE> <I>type</I>  <A NAME=MARKER-10-2827></A>family of functions read a subsampled (strided) array section of values from a netCDF variable of an open netCDF dataset. The subsampled array section is specified by giving a corner, a vector of edge lengths, and a <A NAME=MARKER-2-2828></A><A NAME=MARKER-2-2829></A>stride vector. The values are read with the last <A NAME=MARKER-10-2830></A>dimension of the netCDF variable varying fastest. The netCDF dataset must be in data mode. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_get_vars_text  (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       char *tp);
int nc_get_vars_uchar (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       unsigned char *up);
int nc_get_vars_schar (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       signed char *cp);
int nc_get_vars_short (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       short *sp);
int nc_get_vars_int   (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       int *ip);
int nc_get_vars_long  (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       long *lp);
int nc_get_vars_float (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       float *fp);
int nc_get_vars_double(int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       double *dp)
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>start</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the index in the variable where the first of the data values will be read. The indices are relative to 0, so for example, the first data value of a variable would have index <CODE>(0, 0, ... , 0)</CODE>. The elements of <CODE>start </CODE>correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first index corresponds to the starting record number for reading the data values. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>count</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the number of indices selected along each dimension. To read a single value, for example, specify <CODE>count</CODE> as <CODE>(1, 1, ... , 1)</CODE>. The elements of <CODE>count</CODE> correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first element of <CODE>count</CODE> corresponds to a count of the number of records to read. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>stride</CODE></P>
</TD><TD><P> A vector of ptrdiff_t integers specifying, for each dimension, the interval between selected indices. The elements of the <A NAME=MARKER-2-311></A><A NAME=MARKER-2-312></A>stride vector correspond, in order, to the variable's dimensions. A value of 1 accesses adjacent values of the netCDF variable in the corresponding dimension; a value of 2 accesses every other value of the netCDF variable in the corresponding dimension; and so on. A <CODE>NULL</CODE> stride argument is treated as <CODE>(1, 1, ... , 1)</CODE>. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>tp, up, cp, sp, ip, lp, fp, </CODE>or<CODE>, dp</CODE></P>
</TD><TD><P> Pointer to the location into which the data value is read. If the type of data value differs from the netCDF variable type, type conversion will occur. <A HREF="guidec-8.html#MARKER-9-2044">See Section 3.3 "Type Conversion,"  page&nbsp;20</A>, for details.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2831></A></H4>
<P><CODE>nc_get_vars_</CODE> <I>type</I>  <A NAME=MARKER-10-2832></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2833></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>The specified start, count and stride generate an index which is out of range.
<LI>One or more of the values are out of the range of values representable by the desired type.
<LI>The specified netCDF is in define mode rather than data mode.
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here is an <A NAME=MARKER-2-2834></A>example that uses <CODE>nc_get_vars_double</CODE> to read every other value in each dimension of the variable named <CODE>rh</CODE> from an existing netCDF dataset named <CODE>foo.nc</CODE>. For simplicity in this example, we assume that we know that <CODE>rh</CODE> is dimensioned with <CODE>time</CODE>, <CODE>lat</CODE>, and <CODE>lon</CODE>, and that there are three <CODE>time</CODE> values, five <CODE>lat</CODE> values, and ten <CODE>lon</CODE> values. </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
#define TIMES 3
#define LATS  5
#define LONS 10
int  status;                          /* error status */
int ncid;                             /* netCDF ID */
int rh_id;                            /* variable ID */
static size_t start[] = {0, 0, 0};    /* start at first value */
static size_t count[] = {TIMES, LATS, LONS};
static ptrdiff_t stride[] = {2, 2, 2};/* every other value */
double data[TIMES][LATS][LONS];       /* array to hold values */
 ... 
status = nc_open(&quot;foo.nc&quot;, NC_NOWRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
 ... 
status = nc_inq_varid (ncid, &quot;rh&quot;, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
 ... 
/* read subsampled values from netCDF variable into array */
status = nc_get_vars_double(ncid, rh_id, start, count, stride, 
                            &amp;data[0][0][0]);
if (status != NC_NOERR) handle_error(status);
 ... 
</PRE>
<A NAME=HEADING12-1195></A>
<A NAME=UID-guide.fm-1342407></A>
<P><BR><BR></P>
<H2>7.14  <A NAME=MARKER-10-2835></A><CODE><A NAME=MARKER-10-2836></A><A NAME=MARKER-10-2837></A></CODE>Read a Mapped Array of Values: <CODE><A NAME=MARKER-2-2838></A>nc_get_varm_</CODE> <I>type</I>  <CODE><A NAME=MARKER-10-2839></A><I></I></CODE></H2>
<HR>
<P><A NAME=MARKER-2-2840></A><A NAME=MARKER-2-2841></A><A NAME=MARKER-2-2842></A><A NAME=MARKER-2-2843></A><A NAME=MARKER-2-2844></A>The <CODE>nc_get_varm_</CODE> <I>type</I>  <A NAME=MARKER-10-2845></A>family of functions reads a mapped array section of values from a netCDF variable of an open netCDF dataset. The mapped array section is specified by giving a corner, a vector of edge lengths, a <A NAME=MARKER-2-2846></A><A NAME=MARKER-2-2847></A>stride vector, and an <A NAME=MARKER-2-2848></A><A NAME=MARKER-2-2849></A><A NAME=MARKER-2-2850></A>index mapping vector. The <A NAME=MARKER-2-2851></A><A NAME=MARKER-2-2852></A><A NAME=MARKER-2-2853></A>index mapping vector is a vector of integers that specifies the mapping between the dimensions of a netCDF variable and the in-memory structure of the internal data array. No assumptions are made about the ordering or length of the dimensions of the data array. The netCDF dataset must be in data mode. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_get_varm_text  (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], char *tp);
int nc_get_varm_uchar (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], unsigned char *up);
int nc_get_varm_schar (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], signed char *cp);
int nc_get_varm_short (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], short *sp);
int nc_get_varm_int   (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], int *ip);
int nc_get_varm_long  (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], long *lp);
int nc_get_varm_float (int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], float *fp);
int nc_get_varm_double(int ncid, int varid, const size_t start[],
                       const size_t count[], const ptrdiff_t stride[],
                       const ptrdiff_t imap[], double *dp);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>start</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the index in the variable where the first of the data values will be read. The indices are relative to 0, so for example, the first data value of a variable would have index <CODE>(0, 0, ... , 0)</CODE>. The elements of <CODE>start </CODE>correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first index corresponds to the starting record number for reading the data values. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>count</CODE></P>
</TD><TD><P> A vector of size_t integers specifying the number of indices selected along each dimension. To read a single value, for example, specify <CODE>count</CODE> as <CODE>(1, 1, ... , 1)</CODE>. The elements of <CODE>count</CODE> correspond, in order, to the variable's dimensions. Hence, if the variable is a record variable, the first element of <CODE>count</CODE> corresponds to a count of the number of records to read. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>stride</CODE></P>
</TD><TD><P> A vector of ptrdiff_t integers specifying, for each dimension, the interval between selected indices. The elements of the <A NAME=MARKER-2-313></A><A NAME=MARKER-2-314></A>stride vector correspond, in order, to the variable's dimensions. A value of 1 accesses adjacent values of the netCDF variable in the corresponding dimension; a value of 2 accesses every other value of the netCDF variable in the corresponding dimension; and so on. A <CODE>NULL</CODE> stride argument is treated as <CODE>(1, 1, ... , 1)</CODE>. </P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>imap</CODE></P>
</TD><TD><P> A vector of integers that specifies the mapping between the dimensions of a netCDF variable and the in-memory structure of the internal data array. imap[0] gives the distance between elements of the internal array corresponding to the most slowly varying dimension of the netCDF variable. imap[n-1] (where n is the rank of the netCDF variable) gives the distance between elements of the internal array corresponding to the most rapidly varying dimension of the netCDF variable. Intervening imap elements correspond to other dimensions of the netCDF variable in the obvious way. Distances between elements are specified in type-independent units of elements (the distance between internal elements that occupy adjacent memory locations is 1 and not the element's byte-length as in netCDF 2).</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>tp, up, cp, sp, ip, lp, fp, </CODE>or<CODE>, dp</CODE></P>
</TD><TD><P> Pointer to the location used for computing where the data values are read; the data should be of the type appropriate for the function called. If the type of data value differs from the netCDF variable type, type conversion will occur. <A HREF="guidec-8.html#MARKER-9-2044">See Section 3.3 "Type Conversion,"  page&nbsp;20</A>, for details.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2854></A></H4>
<P><CODE>nc_get_varm_</CODE> <I>type</I>  <A NAME=MARKER-10-2855></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2856></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>The specified <CODE>start</CODE>,<A NAME=MARKER-10-2857></A> <CODE>count</CODE>,<A NAME=MARKER-10-2858></A> and <CODE>stride</CODE> <A NAME=MARKER-10-2859></A>generate an index which is out of range. Note that no error checking is possible on the <CODE>imap</CODE> <A NAME=MARKER-10-2860></A>vector.
<LI>One or more of the values are out of the range of values representable by the desired type.
<LI>The specified netCDF is in define mode rather than data mode.
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>The following <CODE>imap</CODE> <A NAME=MARKER-10-2861></A>vector maps in the trivial way a 4x3x2 <A NAME=MARKER-10-2862></A>netCDF variable and an internal array of the same shape: </P>
<PRE>
float a[4][3][2];       /* same shape as netCDF variable */
size_t imap[3] = {6, 2, 1};
                        /* netCDF dimension       inter-element distance */
                        /* ----------------       ---------------------- */
                        /* most rapidly varying       1                  */
                        /* intermediate               2 (=imap[2]*2)     */
                        /* most slowly varying        6 (=imap[1]*3)     */
</PRE>
<P><A NAME=MARKER-10-2863></A>Using the <CODE>imap</CODE> <A NAME=MARKER-10-2864></A>vector above with <CODE>nc_get_varm_float</CODE> <A NAME=MARKER-10-2865></A>obtains the same result as simply using <CODE>nc_get_var_float</CODE>.<A NAME=MARKER-10-2866></A></P>
<P>Here is an example of using <CODE>nc_get_varm_float</CODE> <A NAME=MARKER-10-2867></A>to transpose a netCDF variable named <CODE>rh</CODE> which is described by the C <A NAME=MARKER-10-2868></A>declaration <CODE>float rh[6][4]</CODE> <A NAME=MARKER-10-2869></A>(note the size and order of the dimensions):</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
#define NDIM 2                /* rank of netCDF variable */
int ncid;                     /* netCDF ID */
int status;                   /* error status */
int rhid;                     /* variable ID */
static size_t start[NDIM]     /* netCDF variable start point: */
                 = {0, 0};    /* first element */
static size_t count[NDIM]     /* size of internal array: entire netCDF */
                 = {6, 4};    /* variable; order corresponds to netCDF */
                              /* variable -- not internal array */
static ptrdiff_t stride[NDIM] /* variable subsampling intervals: */
                 = {1, 1};    /* sample every netCDF element */
static ptrdiff_t imap[NDIM]   /* internal array inter-element distances; */
                 = {1, 6};    /* would be {4, 1} if not transposing */
float rh[4][6];               /* note transposition of netCDF variable */
                              /* dimensions */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid(ncid, &quot;rh&quot;, &amp;rhid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_get_varm_float(ncid, rhid, start, count, stride, imap, rh);
if (status != NC_NOERR) handle_error(status);
</PRE>
<P><A NAME=MARKER-10-2870></A>Here is another example of using <CODE>nc_get_varm_float</CODE> <A NAME=MARKER-10-2871></A>to simultaneously transpose and subsample the same netCDF variable, by accessing every other point of the netCDF variable:</P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
#define NDIM 2               /* rank of netCDF variable */
int ncid;                    /* netCDF ID */
int status;                  /* error status */
int rhid;                    /* variable ID */
static size_t start[NDIM]    /* netCDF variable start point: */
                 = {0, 0};   /* first element */
static size_t count[NDIM]    /* size of internal array: entire */
                   = {3, 2}; /* (subsampled) netCDF variable; order of */
                             /* dimensions corresponds to netCDF */
                             /* variable -- not internal array */
static ptrdiff_t stride[NDIM]/* variable subsampling intervals: */
                 = {2, 2};   /* sample every other netCDF element */
static ptrdiff_t imap[NDIM]  /* internal array inter-element distances; */
                 = {1, 3};   /* would be {2, 1} if not transposing */
float rh[2][3];              /* note transposition of (subsampled) */
                             /* netCDF variable dimensions */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_varid(ncid, &quot;rh&quot;, &amp;rhid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_get_varm_float(ncid, rhid, start, count, stride, imap, rh);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-1353></A>
<A NAME=UID-guide.fm-1003267></A>
<P><BR><BR></P>
<H2>7.15  <A NAME=MARKER-10-2872></A>Reading and Writing Character String Values</H2>
<HR>
<P><A NAME=MARKER-2-2873></A><A NAME=MARKER-2-2874></A><A NAME=MARKER-2-2875></A><A NAME=MARKER-2-2876></A><A NAME=MARKER-2-2877></A>Character <A NAME=MARKER-2-2878></A><A NAME=MARKER-2-2879></A><A NAME=MARKER-2-2880></A><A NAME=MARKER-2-2881></A><A NAME=MARKER-2-2882></A>strings <A NAME=MARKER-2-2883></A><A NAME=MARKER-2-2884></A><A NAME=MARKER-2-2885></A><A NAME=MARKER-2-2886></A><A NAME=MARKER-2-2887></A><A NAME=MARKER-2-2888></A><A NAME=MARKER-2-2889></A>are not a <A NAME=MARKER-2-2890></A><A NAME=MARKER-2-2891></A><A NAME=MARKER-2-2892></A><A NAME=MARKER-2-2893></A><A NAME=MARKER-2-2894></A><A NAME=MARKER-2-2895></A><A NAME=MARKER-2-2896></A><A NAME=MARKER-2-2897></A><A NAME=MARKER-2-2898></A><A NAME=MARKER-2-2899></A>primitive netCDF external data type, in part because FORTRAN does not support the abstraction of variable-length character strings (the FORTRAN <CODE>LEN</CODE> function returns the static length of a character string, not its dynamic length). As a result, a character string cannot be written or read as a single object in the netCDF interface. Instead, a character string must be treated as an array of characters, and array access must be used to read and write character strings as variable data in netCDF datasets. Furthermore, variable-length strings are not supported by the netCDF interface except by convention; for example, you may treat a zero byte as terminating a character string, but you must explicitly specify the length of strings to be read from and written to netCDF variables. </P>
<P><A NAME=MARKER-2-2900></A><A NAME=MARKER-2-2901></A><A NAME=MARKER-2-2902></A><A NAME=MARKER-2-2903></A>Character strings as attribute values are easier to use, since the strings are treated as a single unit for access. However, the value of a character-string attribute is still an array of characters with an explicit length that must be specified when the attribute is defined. </P>
<P><A NAME=MARKER-2-2904></A><A NAME=MARKER-2-2905></A><A NAME=MARKER-2-2906></A><A NAME=MARKER-2-2907></A><A NAME=MARKER-2-2908></A>When you define a variable that will have character-string values, use a <I>character-position dimension</I> as the most quickly varying dimension for the variable (the last <A NAME=MARKER-10-2909></A>dimension for the variable in C)<A NAME=MARKER-10-2910></A>. The length of the character-position dimension will be the maximum string length of any value to be stored in the character-string variable. Space for maximum-length strings will be allocated in the disk representation of character-string variables whether you use the space or not. If two or more variables have the same maximum length, the same character-position dimension may be used in defining the variable shapes. </P>
<P>To write a character-string value into a character-string variable, use either entire variable access or array access. The latter requires that you specify both a corner and a vector of edge lengths. The character-position dimension at the corner should be zero <A NAME=MARKER-10-2911></A>for C.<A NAME=MARKER-10-2912></A> If the length of the string to be written is <CODE>n</CODE>, then the vector of edge lengths will specify <CODE>n</CODE> in the character-position dimension, and one for all the other dimensions:<CODE>(1, 1, ... , 1, n).<A NAME=MARKER-10-2913></A></CODE></P>
<P><A NAME=MARKER-2-2914></A><A NAME=MARKER-2-2915></A><A NAME=MARKER-2-2916></A><A NAME=MARKER-2-2917></A><A NAME=MARKER-2-2918></A><A NAME=MARKER-2-2919></A><A NAME=MARKER-2-2920></A><A NAME=MARKER-2-2921></A><A NAME=MARKER-2-2922></A>In C, fixed-length strings may be written to a netCDF dataset without the terminating zero byte, to save space. Variable-length strings should be written <I>with</I> a terminating zero byte so that the intended length of the string can be determined when it is later read. </P>
<P>Here is an example that defines a record variable, <CODE>tx</CODE>, for character strings and stores a character-string value into the third record using <CODE>nc_put_vara_text</CODE>. In this example, we assume the string variable and data are to be added to an existing netCDF dataset named <CODE>foo.nc</CODE> that already has an unlimited record dimension <CODE>time</CODE>. </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int  ncid;            /* netCDF ID */
int  chid;            /* dimension ID for char positions */
int  timeid;          /* dimension ID for record dimension */
int  tx_id;           /* variable ID */
#define TDIMS 2       /* rank of tx variable */
int tx_dims[TDIMS];   /* variable shape */
size_t tx_start[TDIMS];
size_t tx_count[TDIMS];
static char tx_val[] =
        &quot;example string&quot;; /* string to be put */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
status = nc_redef(ncid);       /* enter define mode */
if (status != NC_NOERR) handle_error(status);
   ... 
/* define character-position dimension for strings of max length 40 */
status = nc_def_dim(ncid, &quot;chid&quot;, 40L, &amp;chid);
if (status != NC_NOERR) handle_error(status);
   ... 
/* define a character-string variable */
tx_dims[0] = timeid;
tx_dims[1] = chid;    /* character-position dimension last */
status = nc_def_var (ncid, &quot;tx&quot;, NC_CHAR, TDIMS, tx_dims, &amp;tx_id);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_enddef(ncid);       /* leave define mode */
if (status != NC_NOERR) handle_error(status);
   ... 
/* write tx_val into tx netCDF variable in record 3 */
tx_start[0] = 3;      /* record number to write */
tx_start[1] = 0;      /* start at beginning of variable */
tx_count[0] = 1;      /* only write one record */
tx_count[1] = strlen(tx_val) + 1;  /* number of chars to write */
status = <A NAME=MARKER-2-2923></A>nc_put_vara_text(ncid, tx_id, tx_start, tx_count, tx_val);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING12-1398></A>
<A NAME=UID-guide.fm-1003457></A>
<P><BR><BR></P>
<H2>7.16  <A NAME=MARKER-10-2924></A><A NAME=MARKER-9-2925></A>Fill Values</H2>
<HR>
<P><A NAME=MARKER-2-2926></A><A NAME=MARKER-2-2927></A><A NAME=MARKER-2-2928></A><A NAME=MARKER-2-2929></A>What happens when you try to read a value that was never written in an open netCDF dataset? You might expect that this should always be an error, and that you should get an error message or an error status returned. You <I>do</I> get an error if you try to read data from a netCDF dataset that is not open for reading, if the variable ID is invalid for the specified netCDF dataset, or if the specified indices are not properly within the range defined by the dimension lengths of the specified variable. Otherwise, reading a value that was not written returns a special <I>fill value</I> used to fill in any undefined values when a netCDF variable is first written. </P>
<P>You may ignore fill values and use the entire range of a netCDF external data type, but in this case you should make sure you write all data values before reading them. If you know you will be writing all the data before reading it, you can specify that no prefilling of variables with fill values will occur by calling <CODE><A NAME=MARKER-2-2930></A>nc_set_fill<A NAME=MARKER-10-2931></A></CODE>before writing. This may provide a significant performance gain for netCDF writes. </P>
<P>The variable attribute <CODE>_FillValue</CODE> may be used to specify the fill value for a variable. Their are <A NAME=MARKER-2-2932></A><A NAME=MARKER-2-2933></A><A NAME=MARKER-2-2934></A>default fill values for each type, defined in the include file <CODE>netcdf.h:<A NAME=MARKER-10-2935></A></CODE>  <CODE><A NAME=MARKER-2-2936></A>NC_FILL_CHAR</CODE>, <CODE><A NAME=MARKER-2-2937></A>NC_FILL_BYTE</CODE>, <CODE><A NAME=MARKER-2-2938></A>NC_FILL_SHORT</CODE>, <CODE><A NAME=MARKER-2-2939></A>NC_FILL_INT</CODE>, <CODE><A NAME=MARKER-2-2940></A>NC_FILL_FLOAT</CODE>, and <CODE><A NAME=MARKER-2-2941></A>NC_FILL_DOUBLE</CODE>.<A NAME=MARKER-10-2942></A></P>
<P><A NAME=MARKER-2-2943></A><A NAME=MARKER-2-2944></A><A NAME=MARKER-2-2945></A>The netCDF byte and character types have different default fill values. The default fill value for characters is the zero byte, a useful value for detecting the end of variable-length C character strings. If you need a fill value for a byte variable, it is recommended that you explicitly define an appropriate <CODE>_FillValue</CODE> attribute, as generic utilities such as <CODE>ncdump</CODE> will not assume a default fill value for byte variables.</P>
<P>Type conversion for fill values is identical to type conversion for other values: attempting to convert a value from one type to another type that can't represent the value results in a range error. Such errors may occur on writing or reading values from a larger type (such as double) to a smaller type (such as float), if the fill value for the larger type cannot be represented in the smaller type.</P>
<A NAME=HEADING12-1404></A>
<A NAME=UID-guide.fm-1003497></A>
<P><BR><BR></P>
<H2>7.17  Rename a Variable: <CODE><A NAME=MARKER-2-2946></A>nc_rename_var</CODE> <A NAME=MARKER-10-2947></A><CODE></CODE></H2>
<HR>
<P>The function <CODE>nc_rename_var</CODE> <A NAME=MARKER-10-2948></A><A NAME=MARKER-2-2949></A><A NAME=MARKER-2-2950></A>changes the name of a netCDF variable in an open netCDF dataset. If the new name is longer than the old name, the netCDF dataset must be in define mode. You cannot rename a variable to have the name of any existing variable. </P>
<P><BR><BR></P>
<H4>Usage </H4>
<PRE>
int nc_rename_var(int ncid, int varid, const char* name);
</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>varid</CODE></P>
</TD><TD><P> Variable ID.</P>
</TD></TR><TR VALIGN=TOP><TD><P> <CODE>name</CODE></P>
</TD><TD><P> New name for the specified variable.</P>
</TD></TR></TABLE><P><BR><BR></P>
<H4>Errors <A NAME=MARKER-10-2951></A></H4>
<P><CODE>nc_rename_var</CODE> <A NAME=MARKER-10-2952></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2953></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:</P>
<UL>
<LI>The new name is in use as the name of another variable.
<LI>The variable ID is invalid for the specified netCDF dataset.
<LI>The specified netCDF ID does not refer to an open netCDF dataset.
</UL>
<P><BR><BR></P>
<H4>Example </H4>
<P>Here is an example using <CODE><A NAME=MARKER-2-2954></A>nc_rename_var</CODE> <A NAME=MARKER-10-2955></A>to rename the variable <CODE>rh</CODE> to <CODE>rel_hum</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>: </P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int  status;              /* error status */
int  ncid;                /* netCDF ID */
int  rh_id;               /* variable ID */
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_redef(ncid);  /* put in define mode to rename variable */
if (status != NC_NOERR) handle_error(status);
status = nc_inq_varid (ncid, &quot;rh&quot;, &amp;rh_id);
if (status != NC_NOERR) handle_error(status);
status = nc_rename_var (ncid, rh_id, &quot;rel_hum&quot;);
if (status != NC_NOERR) handle_error(status);
status = nc_enddef(ncid); /* leave define mode */
if (status != NC_NOERR) handle_error(status);
</PRE>

</DIV>

<DIV>

<DIV>

<!-- TOC --><DL>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-17"><B>7.1 </B> - Language Types Corresponding to netCDF external data types</A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-101"><B>7.2 </B> - Create a Variable: nc_def_var </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-193"><B>7.3 </B> - Get a Variable ID from Its Name: nc_inq_varid </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-239"><B>7.4 </B> - Get Information about a Variable from Its ID: nc_inq_var family</A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-333"><B>7.5 </B> - Write a Single Data Value: nc_put_var1_ type  </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-411"><B>7.6 </B> - Write an Entire Variable: nc_put_var_ type </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-480"><B>7.7 </B> - Write an Array of Values: nc_put_vara_ type  </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-578"><B>7.8 </B> - Write a Subsampled Array of Values: nc_put_vars_ type  </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-688"><B>7.9 </B> - Write a Mapped Array of Values: nc_put_varm_ type  </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-846"><B>7.10 </B> - Read a Single Data Value: nc_get_var1_ type  </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-925"><B>7.11 </B> - Read an Entire Variable nc_get_var_ type  </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-991"><B>7.12 </B> - Read an Array of Values: nc_get_vara_ type  </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-1084"><B>7.13 </B> - Read a Subsampled Array of Values: nc_get_vars_ type  </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-1195"><B>7.14 </B> - Read a Mapped Array of Values: nc_get_varm_ type  </A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-1353"><B>7.15 </B> - Reading and Writing Character String Values</A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-1398"><B>7.16 </B> - Fill Values</A>
<DD>
<DT><A REL=BELOW HREF="guidec-12.html#HEADING12-1404"><B>7.17 </B> - Rename a Variable: nc_rename_var </A>
<DD>
</DL>


</DIV>

<HR>
<ADDRESS>NetCDF User's Guide for C - 6 Nov 1997</ADDRESS>
<P><A HREF=guidec-13.html>[Next] </A><A HREF=guidec-11.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>