File: guidec-11.html

package info (click to toggle)
netcdf-doc 1%3A3a-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,052 kB
  • ctags: 1,463
  • sloc: makefile: 58; sh: 25
file content (215 lines) | stat: -rw-r--r-- 15,891 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!-- Generated by Harlequin WebMaker 2.2.3 (24-Apr-1996)
LispWorks 3.2.2 -->
<HTML> <HEAD>
<TITLE>6	 Dimensions</TITLE>
</HEAD>
<BODY bgcolor="#ffffff">
<A NAME=HEADING11></A>
<A HREF="guidec-12.html">[Next] </A><A HREF="guidec-10.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>
NetCDF User's Guide for C<P>
<A NAME=HEADING11-0></A>
<H1>6  <A NAME=MARKER-10-2325></A>Dimensions</H1>
<HR>
 Dimensions <A NAME=MARKER-2-2335></A>for a netCDF dataset are defined when it is created, while the netCDF dataset is in define mode. Additional dimensions may be added later by reentering define mode. A netCDF dimension has a name and a length. At most one dimension in a netCDF dataset can have the <CODE>unlimited</CODE> length, which means variables using this dimension can grow along this dimension. <P>
 There <A NAME=MARKER-2-2336></A><A NAME=MARKER-2-2337></A>is a suggested limit (100) to the number of dimensions that can be defined in a single netCDF dataset. The limit is the value of the predefined macro <CODE><A NAME=MARKER-2-2343></A>NC_MAX_DIMS.<A NAME=MARKER-10-2344></A></CODE> The purpose of the limit is to make writing generic applications simpler. They need only provide an array of <CODE>NC_MAX_DIMS <A NAME=MARKER-10-2345></A></CODE>dimensions to handle any netCDF dataset. The implementation of the netCDF library does not enforce this advisory maximum, so it is possible to use more dimensions, if necessary, but netCDF utilities that assume the advisory maximums may not be able to handle the resulting netCDF datasets. <P>
 Ordinarily, the name and length of a dimension are fixed when the dimension is first defined. The name may be changed later, but the length of a dimension (other than the unlimited dimension) cannot be changed without copying all the data to a new netCDF dataset with a redefined dimension length. <P>
 Dimension lengths in the C interface are type <CODE>size_t</CODE> rather than type <CODE>int</CODE> to make it possible to access all the data in a netCDF dataset on a platform that only supports a 16-bit <CODE>int</CODE> data type, for example MSDOS. If dimension lengths were type <CODE>int</CODE> instead, it would not be possible to access data from variables with a dimension length greater than a 16-bit <CODE>int</CODE> can accommodate. <P>
 A netCDF dimension in an open netCDF dataset is referred to by a small integer called a <I>dimension ID</I>. In the C <A NAME=MARKER-10-2352></A>interface, dimension IDs are 0,<A NAME=MARKER-10-2353></A> 1,<A NAME=MARKER-10-2354></A> 2,<A NAME=MARKER-10-2355></A> ..., in the order in which the dimensions were defined. <P>
 Operations supported on dimensions are:<P>
<UL>
<LI>Create a dimension, given its name and length.<P>
<LI>Get a dimension ID from its name.<P>
<LI>Get a dimension's name and length from its ID.<P>
<LI>Rename a dimension.<P>
</UL>
<A NAME=HEADING11-11></A>
<H2>6.1  Create a Dimension: <CODE><A NAME=MARKER-2-2356></A>nc_def_dim</CODE> <A NAME=MARKER-10-2357></A><CODE></CODE></H2>
<HR>
 The <A NAME=MARKER-2-2368></A><A NAME=MARKER-2-2369></A><A NAME=MARKER-2-2370></A>function <CODE>nc_def_dim</CODE> <A NAME=MARKER-10-2371></A>adds a new dimension to an open netCDF dataset in define mode. It returns (as an argument) a dimension ID, given the netCDF ID, the dimension name, and the dimension length. At most one unlimited length dimension, called the record dimension, may be defined for each netCDF dataset.<P>
<A NAME=HEADING11-13></A>
<H4>Usage </H4>
<PRE>
int nc_def_dim (int ncid, const char *name, size_t len, int *dimidp);
<TABLE BORDER="1"><TD><CODE>ncid</CODE><TD>NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.<TR>
<TD><CODE>name</CODE><TD>Dimension name. Must begin with an alphabetic character, followed by zero or more alphanumeric characters including the underscore ('<CODE>_</CODE>'). Case is significant.<TR>
<TD><CODE>len</CODE><TD>Length of dimension; that is, number of values for this dimension as an index to variables that use it. This should be either a positive integer (of type <CODE>size_t</CODE>) or the predefined constant <CODE><A NAME=MARKER-2-152></A>NC_UNLIMITED</CODE>.<TR>
<TD><CODE>dimidp</CODE><TD>Pointer to location for returned dimension ID.</TABLE>

</PRE>
Errors <A NAME=MARKER-10-2372></A><P>
 <CODE>nc_def_dim</CODE> <A NAME=MARKER-10-2373></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2374></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: <P>
<UL>
<LI>The netCDF dataset is not in definition mode.<P>
<LI>The specified dimension name is the name of another existing dimension.<P>
<LI>The specified length is not greater than zero.<P>
<LI>The specified length is unlimited, but there is already an unlimited length dimension defined for this netCDF dataset.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING11-22></A>
<H4>Example </H4>
 Here <A NAME=MARKER-2-2375></A>is an example using <CODE>nc_def_dim</CODE> <A NAME=MARKER-10-2376></A>to create a dimension named <CODE>lat</CODE> of length 18 and a unlimited dimension named <CODE>rec</CODE> in a new netCDF dataset named <CODE>foo.nc</CODE>:<P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status, ncid, latid, recid;
   ... 
status = nc_create(&quot;foo.nc&quot;, NC_NOCLOBBER, &amp;ncid);
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_def_dim(ncid, &quot;lat&quot;, 18L, &amp;latid);
if (status != NC_NOERR) handle_error(status);
status = nc_def_dim(ncid, &quot;rec&quot;, NC_UNLIMITED, &amp;recid);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING11-35></A>
<H2>6.2  <A NAME=MARKER-10-2377></A>Get a Dimension ID from Its Name: <CODE><A NAME=MARKER-2-2378></A>nc_inq_dimid</CODE> <A NAME=MARKER-10-2379></A><CODE></CODE></H2>
<HR>
 The <A NAME=MARKER-2-2384></A><A NAME=MARKER-2-2385></A><A NAME=MARKER-2-2386></A>function <CODE>nc_inq_dimid</CODE> <A NAME=MARKER-10-2387></A>returns (as an argument) the ID of a netCDF dimension, given the name of the dimension. If <CODE>ndims</CODE> is the number of dimensions defined for a netCDF dataset, each dimension has an ID between <CODE>0</CODE> <A NAME=MARKER-10-2388></A>and <CODE>ndims-1.<A NAME=MARKER-10-2389></A></CODE><P>
<A NAME=HEADING11-37></A>
<H4>Usage </H4>
<PRE>
int nc_inq_dimid (int ncid, const char *name, int *dimidp);
<TABLE BORDER="1"><TD><CODE>ncid</CODE><TD>NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.<TR>
<TD><CODE>name</CODE><TD>Dimension name, a character string beginning with a letter and followed by any sequence of letters, digits, or underscore ('<CODE>_</CODE>') characters. Case is significant in dimension names.<TR>
<TD><CODE>dimidp</CODE><TD>Pointer to location for the returned dimension ID.</TABLE>

<A NAME=MARKER-10-2390></A>
</PRE>
Errors <P>
 <CODE>nc_inq_dimid</CODE> <A NAME=MARKER-10-2391></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2392></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: <P>
<UL>
<LI>The name that was specified is not the name of a dimension in the netCDF dataset.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING11-44></A>
<H4>Example </H4>
 Here <A NAME=MARKER-2-2393></A>is an example using <CODE>nc_inq_dimid</CODE> <CODE><A NAME=MARKER-10-2394></A><A NAME=MARKER-10-2395></A></CODE>to determine the dimension ID of a dimension named <CODE>lat</CODE>, assumed to have been defined previously in an existing netCDF dataset named <CODE>foo.nc</CODE>:<P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status, ncid, latid;
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_NOWRITE, &amp;ncid);  /* open for reading */
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_dimid(ncid, &quot;lat&quot;, &amp;latid);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING11-55></A>
<H2>6.3  <A NAME=MARKER-10-2396></A>Inquire about a Dimension: <CODE><A NAME=MARKER-2-2397></A>nc_inq_dim</CODE> <A NAME=MARKER-10-2398></A>Family</H2>
<HR>
 This <A NAME=MARKER-2-2403></A><A NAME=MARKER-2-2404></A><A NAME=MARKER-2-2405></A><A NAME=MARKER-2-2406></A><A NAME=MARKER-2-2407></A><A NAME=MARKER-2-2408></A><A NAME=MARKER-2-2409></A><A NAME=MARKER-2-2410></A><A NAME=MARKER-2-2411></A><A NAME=MARKER-2-2412></A><A NAME=MARKER-2-2413></A><A NAME=MARKER-2-2414></A><A NAME=MARKER-2-2415></A><A NAME=MARKER-2-2420></A><A NAME=MARKER-2-2421></A><A NAME=MARKER-2-2422></A><A NAME=MARKER-2-2423></A><A NAME=MARKER-2-2424></A><A NAME=MARKER-2-2425></A>family of functions returns information about a netCDF dimension. Information about a dimension includes its name and its length. The length for the unlimited dimension, if any, is the number of records written so far. <P>
 The functions in this family include <CODE>nc_inq_dim</CODE>,<A NAME=MARKER-10-2426></A> <CODE><A NAME=MARKER-2-2427></A>nc_inq_dimname</CODE>,<A NAME=MARKER-10-2428></A> and<A NAME=MARKER-2-2429></A> <CODE>nc_inq_dimlen</CODE>.<A NAME=MARKER-10-2430></A> The function <CODE>nc_inq_dim</CODE> <A NAME=MARKER-10-2431></A>returns all the information about a dimension; the other functions each return just one item of information. <P>
<A NAME=HEADING11-58></A>
<H4>Usage </H4>
<PRE>
int nc_inq_dim     (int ncid, int dimid, char* name, size_t* lengthp);
int nc_inq_dimname (int ncid, int dimid, char *name);
int nc_inq_dimlen  (int ncid, int dimid, size_t *lengthp);
<TABLE BORDER="1"><TD><CODE>ncid</CODE><TD>NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>. <TR>
<TD><CODE>dimid</CODE><TD>Dimension ID, from a previous call to <CODE>nc_inq_dimid</CODE> or <CODE>nc_def_dim</CODE>.<TR>
<TD><CODE>name</CODE><TD>Returned dimension name. The caller must allocate space for the returned name. The maximum possible length, in characters, of a dimension name is given by the predefined constant <CODE><A NAME=MARKER-2-155></A>NC_MAX_NAME</CODE>.<TR>
<TD><CODE>lengthp</CODE><TD>Pointer to location for returned length of dimension. For the unlimited dimension, this is the number of records written so far.</TABLE>

</PRE>
Errors <A NAME=MARKER-10-2432></A><P>
 These functions return the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2433></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:<P>
<UL>
<LI>The dimension ID is invalid for the specified netCDF dataset.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING11-66></A>
<H4>Example </H4>
 Here <A NAME=MARKER-2-2434></A>is an example using <CODE>nc_inq_dim</CODE> <A NAME=MARKER-10-2435></A>to determine the length of a dimension named <CODE>lat</CODE>, and the name and current maximum length of the unlimited dimension for an existing netCDF dataset named <CODE>foo.nc</CODE>:<P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status, ncid, latid, recid;
size_t latlength, recs;
char recname[NC_MAX_NAME];
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_NOWRITE, &amp;ncid);  /* open for reading */
if (status != NC_NOERR) handle_error(status);
status = nc_inq_unlimdim(ncid, &amp;recid); /* get ID of unlimited dimension */
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_inq_dimid(ncid, &quot;lat&quot;, &amp;latid);  /* get ID for lat dimension */
if (status != NC_NOERR) handle_error(status);
status = nc_inq_dimlen(ncid, latid, &amp;latlength); /* get lat length */
if (status != NC_NOERR) handle_error(status);
/* get unlimited dimension name and current length */
status = nc_inq_dim(ncid, recid, recname, &amp;recs);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING11-86></A>
<H2>6.4  <A NAME=MARKER-10-2436></A>Rename a Dimension: <CODE><A NAME=MARKER-2-2437></A>nc_rename_dim</CODE> <A NAME=MARKER-10-2438></A><CODE></CODE></H2>
<HR>
 The <A NAME=MARKER-2-2439></A><A NAME=MARKER-2-2440></A><A NAME=MARKER-2-2441></A><A NAME=MARKER-2-2442></A>function <CODE>nc_rename_dim</CODE> <A NAME=MARKER-10-2443></A>renames an existing dimension in a netCDF dataset open for writing. If the new name is longer than the old name, the netCDF dataset must be in define mode. You cannot rename a dimension to have the same name as another dimension. <P>
<A NAME=HEADING11-88></A>
<H4>Usage </H4>
<PRE>
int nc_rename_dim(int ncid, int dimid, const char* name);
<TABLE BORDER="1"><TD><CODE>ncid</CODE><TD>NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.<TR>
<TD><CODE>dimid</CODE><TD>Dimension ID, from a previous call to <CODE>nc_inq_dimid</CODE> or <CODE>nc_def_dim</CODE>.<TR>
<TD><CODE>name</CODE><TD>New dimension name.</TABLE>

</PRE>
Errors <A NAME=MARKER-10-2444></A><P>
 <CODE>nc_rename_dim</CODE> <A NAME=MARKER-10-2445></A>returns the value <CODE>NC_NOERR</CODE> <A NAME=MARKER-10-2446></A>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: <P>
<UL>
<LI>The new name is the name of another dimension.<P>
<LI>The dimension ID is invalid for the specified netCDF dataset.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
<LI>The new name is longer than the old name and the netCDF dataset is not in define mode.<P>
</UL>
<A NAME=HEADING11-96></A>
<H4>Example </H4>
 Here <A NAME=MARKER-2-2447></A>is an example using <CODE>nc_rename_dim</CODE> <A NAME=MARKER-10-2448></A>to rename the dimension <CODE>lat</CODE> to <CODE>latitude</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>:<P>
<PRE>
#include &lt;netcdf.h&gt;
   ... 
int status, ncid, latid;
   ... 
status = nc_open(&quot;foo.nc&quot;, NC_WRITE, &amp;ncid);  /* open for writing */
if (status != NC_NOERR) handle_error(status);
   ... 
status = nc_redef(ncid);  /* put in define mode to rename dimension */
if (status != NC_NOERR) handle_error(status);
status = nc_inq_dimid(ncid, &quot;lat&quot;, &amp;latid);
if (status != NC_NOERR) handle_error(status);
status = nc_rename_dim(ncid, latid, &quot;latitude&quot;);
if (status != NC_NOERR) handle_error(status);
status = nc_enddef(ncid); /* leave define mode */
if (status != NC_NOERR) handle_error(status);
</PRE>
<!-- TOC --><DL>
<DT><A HREF="guidec-11.html#HEADING11-11"><B>6.1 </B> - Create a Dimension: nc_def_dim </A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-13"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-22"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-35"><B>6.2 </B> - Get a Dimension ID from Its Name: nc_inq_dimid </A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-37"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-44"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-55"><B>6.3 </B> - Inquire about a Dimension: nc_inq_dim Family</A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-58"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-66"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-86"><B>6.4 </B> - Rename a Dimension: nc_rename_dim </A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-88"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidec-11.html#HEADING11-96"><B>Example</B> - </A>
<DD>
</DL>

<HR>
<ADDRESS>NetCDF User's Guide for C - 5 JUN 1997</ADDRESS>
<A HREF="guidec-12.html">[Next] </A><A HREF="guidec-10.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>
</BODY>