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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
<!-- Generated by Harlequin WebMaker 2.2.3 (24-Apr-1996)
LispWorks 3.2.2 -->
<HTML> <HEAD>
<TITLE>5 Datasets</TITLE>
</HEAD>
<BODY bgcolor="#ffffff">
<A NAME=HEADING10></A>
<A HREF="guidef-11.html">[Next] </A><A HREF="guidef-9.html">[Previous] </A><A HREF="guidef-1.html">[Top] </A><A HREF="guidef-3.html">[Contents] </A><A HREF="guidef-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 Fortran<P>
<A NAME=HEADING10-0></A>
<H1>5 Datasets</H1>
<HR>
This chapter presents the interfaces of the netCDF functions that deal with a netCDF dataset or the whole netCDF library. <P>
A <A NAME=MARKER-2-2048></A><A NAME=MARKER-2-2049></A><A NAME=MARKER-2-2052></A><A NAME=MARKER-2-2053></A><A NAME=MARKER-2-2054></A>netCDF dataset that has not yet been opened can only be referred to by its dataset name. Once a netCDF dataset is opened, it is referred to by a <I>netCDF ID</I>, which is a small nonnegative integer returned when you create or open the dataset. A netCDF ID is much like a file descriptor in C or a logical unit number in FORTRAN. In any single program, the netCDF IDs of distinct open netCDF datasets are distinct. A single netCDF dataset may be opened multiple times and will then have multiple distinct netCDF IDs; however at most one of the open instances of a single netCDF dataset should permit writing. When an open netCDF dataset is closed, the ID is no longer associated with a netCDF dataset. <P>
Functions that deal with the netCDF library include:<P>
<UL>
<LI>Get version of library.<P>
<LI>Get error message corresponding to a returned error code.<P>
</UL>
The <A NAME=MARKER-2-2055></A><A NAME=MARKER-2-2056></A>operations supported on a netCDF dataset as a single object are:<P>
<UL>
<LI>Create, given dataset name and whether to overwrite or not.<P>
<LI>Open for access, given dataset name and read or write intent.<P>
<LI>Put into define mode, to add dimensions, variables, or attributes.<P>
<LI>Take out of define mode, checking consistency of additions.<P>
<LI>Close, writing to disk if required.<P>
<LI>Inquire about the number of dimensions, number of variables, number of global attributes, and ID of the unlimited dimension, if any.<P>
<LI>Synchronize to disk to make sure it is current.<P>
<LI>Set and unset <I>nofill</I> mode for optimized sequential writes.<P>
</UL>
After a summary of conventions used in describing the netCDF interfaces, the rest of this chapter presents a detailed description of the interfaces for these operations.<P>
<A NAME=HEADING10-16></A>
<H2>5.1 NetCDF Library Interface Descriptions</H2>
<HR>
Each <A NAME=MARKER-2-2058></A><A NAME=MARKER-2-2059></A><A NAME=MARKER-2-2060></A><A NAME=MARKER-2-2061></A><A NAME=MARKER-2-2062></A><A NAME=MARKER-2-2063></A>interface description for a particular netCDF function in this and later chapters contains: <P>
<UL>
<LI>a description of the purpose of the function;<P>
<LI>a <A NAME=MARKER-10-2064></A>FORTRAN <A NAME=MARKER-2-2065></A><A NAME=MARKER-2-2066></A>function prototype that presents the type and order of the formal parameters to the function; <P>
<LI>a description of each formal parameter in the <A NAME=MARKER-10-2067></A>FORTRAN interface;<P>
<LI>a list of possible error conditions; and<P>
<LI>an example of a <A NAME=MARKER-10-2068></A>FORTRAN program fragment calling the netCDF function (and perhaps other netCDF functions).<P>
</UL>
In the FORTRAN function prototype and formal parameter descriptions, output parameters (in which returned values will be stored) have lower case names, to distinguish them from input parameters named in upper case.<P>
The examples follow a simple convention for error handling, always checking the error status returned from each netCDF function call and calling a <A NAME=MARKER-10-2069></A><CODE>HANDLE_ERR</CODE> subroutine in case an error was detected. For an example of such a <A NAME=MARKER-10-2070></A>subroutine, <A HREF=#MARKER-9-2071>see Section 5.2 "Get error message corresponding to error status: NF_STRERROR," page 30</A>.<P>
<A NAME=HEADING10-25></A>
<H2>5.2 <A NAME=MARKER-9-2071></A>Get error message corresponding to error status: <A NAME=MARKER-10-2072></A> <CODE><A NAME=MARKER-2-2073></A>NF_STRERROR </CODE></H2>
<HR>
The <A NAME=MARKER-2-2074></A><A NAME=MARKER-2-2075></A><A NAME=MARKER-2-2076></A>function <A NAME=MARKER-10-2077></A><CODE>NF_STRERROR</CODE> returns a static reference to an error message string corresponding to an integer netCDF error status or to a system error number, presumably returned by a previous call to some other netCDF function. The list of netCDF error status codes is available in the appropriate include file for each language binding.<P>
<A NAME=HEADING10-27></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2078></A>CHARACTER*80 FUNCTION NF_STRERROR(INTEGER NCERR)
<TABLE BORDER="1"><TD><CODE>NCERR</CODE><TD>An error status that might have been returned from a previous call to some netCDF function.</TABLE>
</PRE>
Errors <P>
If you provide an invalid integer error status that does not correspond to any netCDF error message or or to any system error message (as understood by the system <CODE>strerror</CODE> function), <CODE>nc_strerror</CODE> returns a string indicating that there is no such error status.<P>
<A NAME=HEADING10-31></A>
<H4>Example <A NAME=MARKER-9-2079></A></H4>
Here is an example of a simple error handling <A NAME=MARKER-10-2080></A>subroutine that uses <A NAME=MARKER-10-2081></A><CODE>NF_STRERROR</CODE> to print the error message corresponding to the netCDF error status returned from any netCDF function call and then exit:<P>
<PRE>
<A NAME=MARKER-10-2082></A>INCLUDE 'netcdf.inc'
...
SUBROUTINE HANDLE_ERR(STATUS)
INTEGER STATUS
IF (STATUS .NE. NF_NOERR) THEN
PRINT *, NF_STRERROR(STATUS)
STOP 'Stopped'
ENDIF
END
</PRE>
<A NAME=HEADING10-42></A>
<H2>5.3 Get netCDF library version: <A NAME=MARKER-10-2083></A><CODE><A NAME=MARKER-2-2084></A>NF_INQ_LIBVERS </CODE></H2>
<HR>
The <A NAME=MARKER-2-2085></A><A NAME=MARKER-2-2086></A><A NAME=MARKER-2-2087></A>function <A NAME=MARKER-10-2088></A><CODE>NF_INQ_LIBVERS</CODE> returns a string identifying the version of the netCDF library, and when it was built.<P>
<A NAME=HEADING10-44></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2089></A>CHARACTER*80 FUNCTION NF_INQ_LIBVERS()
</PRE>
Errors <P>
This function takes no arguments, and thus no errors are possible in its invocation. <P>
<A NAME=HEADING10-48></A>
<H4>Example </H4>
Here <A NAME=MARKER-10-2090></A>is an example using <A NAME=MARKER-10-2091></A><CODE>NF_INQ_LIBVERS</CODE> to print the version of the netCDF library with which the program is linked: <P>
<PRE>
<A NAME=MARKER-10-2092></A>INCLUDE 'netcdf.inc'
...
PRINT *, NF_INQ_LIBVERS()
</PRE>
<A NAME=HEADING10-53></A>
<H2>5.4 Create a NetCDF dataset: <A NAME=MARKER-10-2094></A><CODE><A NAME=MARKER-2-2095></A>NF_CREATE </CODE></H2>
<HR>
This function creates a new netCDF dataset, returning a netCDF ID that can subsequently be used to refer to the netCDF dataset in other netCDF function calls. The new netCDF dataset opened for write access and placed in define mode, ready for you to add dimensions, variables, and attributes.<P>
A creation mode flag specifies whether to overwrite any existing dataset with the same name and whether access to the dataset is shared.<P>
<A NAME=HEADING10-56></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2096></A>INTEGER FUNCTION NF_CREATE (CHARACTER*(*) PATH, INTEGER CMODE,
INTEGER ncid)<CODE>
<TABLE BORDER="1"><TD><CODE>PATH</CODE><TD>The file name of the new netCDF dataset.<TR>
<TD><CODE>CMODE</CODE><TD>The creation mode. A zero value (or <CODE><A NAME=MARKER-2-150></A>NF_CLOBBER</CODE>) specifies the default behavior: overwrite any existing dataset with the same file name and buffer and cache accesses for efficiency.<P>Otherwise, the creation mode is <CODE>NF_NOCLOBBER</CODE>, <CODE>NF_SHARE</CODE>, or <CODE>IOR(NF_NOCLOBBER, NF_SHARE)</CODE>. Setting the <CODE>NF_NOCLOBBER</CODE> flag means you do not want to clobber (overwrite) an existing dataset; an error (<CODE><A NAME=MARKER-2-153></A>NF_EEXIST</CODE>) is returned if the specified dataset already exists. The <CODE>NF_SHARE</CODE> flag is appropriate when one process may be writing the dataset and one or more other processes reading the dataset concurrently; it means that dataset accesses are not buffered and caching is limited. Since the buffering scheme is optimised for sequential access, programs that do not access data sequentially may see some performance improvement by setting the <CODE>NF_SHARE</CODE> flag.<TR>
<TD><CODE>ncid</CODE><TD>Returned netCDF ID.</TABLE>
</CODE>
</PRE>
Errors <P>
<CODE><A NAME=MARKER-10-2097></A>NF_CREATE</CODE> returns the value <A NAME=MARKER-10-2098></A><CODE>NF_NOERR</CODE> if no errors occurred. Possible causes of errors include: <P>
<UL>
<LI>Passing a dataset name that includes a directory that does not exist.<P>
<LI>Specifying a dataset name of a file that exists and also specifying <CODE><A NAME=MARKER-10-2100></A><A NAME=MARKER-2-2101></A>NF_NOCLOBBER</CODE>.<P>
<LI>Specifying a meaningless value for the creation mode.<P>
<LI>Attempting to create a netCDF dataset in a directory where you don't have permission to create files.<P>
</UL>
<A NAME=HEADING10-65></A>
<H4>Example </H4>
In <A NAME=MARKER-10-2102></A>this example we create a netCDF dataset named <CODE>foo.nc</CODE>; we want the dataset to be created in the current directory only if a dataset with that name does not already exist: <P>
<PRE>
<A NAME=MARKER-10-2103></A>INCLUDE 'netcdf.inc'
...
INTEGER NCID, STATUS
...
STATUS = NF_CREATE('foo.nc', NF_NOCLOBBER, NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
</PRE>
<A NAME=HEADING10-73></A>
<H2>5.5 Open a NetCDF Dataset for Access: <A NAME=MARKER-10-2104></A><CODE><A NAME=MARKER-2-2105></A>NF_OPEN </CODE></H2>
<HR>
The <A NAME=MARKER-2-2106></A><A NAME=MARKER-2-2107></A><A NAME=MARKER-2-2108></A>function <A NAME=MARKER-10-2109></A><CODE>NF_OPEN</CODE> opens an existing netCDF dataset for access. <P>
<A NAME=HEADING10-75></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2110></A>INTEGER FUNCTION NF_OPEN(CHARACTER*(*) PATH, INTEGER OMODE, INTEGER ncid)<CODE>
<TABLE BORDER="1"><TD><CODE>PATH</CODE><TD>File name for netCDF dataset to be opened.<TR>
<TD><CODE>OMODE</CODE><TD>A zero value (or <CODE><A NAME=MARKER-2-154></A>NF_NOWRITE</CODE>) specifies the default behavior: open the dataset with read-only access, buffering and caching accesses for efficiency<P>Otherwise, the creation mode is <CODE><A NAME=MARKER-2-155></A>NF_WRITE</CODE>, <CODE>NF_SHARE</CODE>, or <CODE>IOR(NF_WRITE, NF_SHARE)</CODE>. Setting the <CODE>NF_WRITE</CODE> flag opens the dataset with read-write access. ("Writing" means any kind of change to the dataset, including appending or changing data, adding or renaming dimensions, variables, and attributes, or deleting attributes.) The <CODE>NF_SHARE</CODE> flag is appropriate when one process may be writing the dataset and one or more other processes reading the dataset concurrently; it means that dataset accesses are not buffered and caching is limited. Since the buffering scheme is optimised for sequential access, programs that do not access data sequentially may see some performance improvement by setting the <CODE>NF_SHARE</CODE> flag.<P><CODE></CODE><TR>
<TD><CODE>ncid</CODE><TD>Returned netCDF ID.</TABLE>
</CODE>
</PRE>
Errors <P>
<CODE><A NAME=MARKER-10-2111></A>NF_OPEN</CODE> returns the value <A NAME=MARKER-10-2112></A><CODE>NF_NOERR</CODE> if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: <P>
<UL>
<LI>The specified netCDF dataset does not exist.<P>
<LI>A meaningless mode was specified.<P>
</UL>
<A NAME=HEADING10-81></A>
<H4>Example </H4>
Here <A NAME=MARKER-10-2114></A>is an example using <A NAME=MARKER-10-2115></A><CODE>NF_OPEN</CODE> to open an existing netCDF dataset named <CODE>foo.nc</CODE> for read-only, non-shared access:<P>
<PRE>
<A NAME=MARKER-10-2116></A>INCLUDE 'netcdf.inc'
...
INTEGER NCID, STATUS
...
STATUS = NF_OPEN('foo.nc', 0, NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
</PRE>
<A NAME=HEADING10-89></A>
<H2>5.6 Put Open NetCDF Dataset into Define Mode: <A NAME=MARKER-10-2117></A><CODE><A NAME=MARKER-2-2118></A>NF_REDEF </CODE></H2>
<HR>
The <A NAME=MARKER-2-2127></A><A NAME=MARKER-2-2128></A><A NAME=MARKER-2-2129></A><A NAME=MARKER-2-2130></A><A NAME=MARKER-2-2131></A><A NAME=MARKER-2-2132></A><A NAME=MARKER-2-2133></A><A NAME=MARKER-2-2134></A><A NAME=MARKER-2-2135></A>function <A NAME=MARKER-10-2136></A><CODE>NF_REDEF</CODE> puts an open netCDF dataset into define mode, so dimensions, variables, and attributes can be added or renamed and attributes can be deleted. <P>
<A NAME=HEADING10-91></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2137></A>INTEGER FUNCTION NF_REDEF(INTEGER NCID)
<TABLE BORDER="1"><TD>NCID<TD>NetCDF ID, from a previous call to <CODE>NF_OPEN</CODE> or <CODE>NF_CREATE</CODE>.</TABLE>
</PRE>
Errors <P>
<CODE><A NAME=MARKER-10-2138></A>NF_REDEF</CODE> returns the value <A NAME=MARKER-10-2139></A><CODE>NF_NOERR</CODE> if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: <P>
<UL>
<LI>The specified netCDF dataset is already in define mode.<P>
<LI>The specified netCDF dataset was opened for read-only.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING10-98></A>
<H4>Example </H4>
Here <A NAME=MARKER-10-2141></A>is an example using <A NAME=MARKER-10-2142></A><CODE>NF_REDEF</CODE> to open an existing netCDF dataset named <CODE>foo.nc</CODE> and put it into define mode:<P>
<PRE>
<A NAME=MARKER-10-2143></A>INCLUDE 'netcdf.inc'
...
INTEGER NCID, STATUS
...
STATUS = NF_OPEN('foo.nc', NF_WRITE, NCID) ! open dataset
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
...
STATUS = NF_REDEF(NCID) ! put in define mode
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
</PRE>
<A NAME=HEADING10-109></A>
<H2>5.7 Leave Define Mode: <A NAME=MARKER-10-2144></A><CODE><A NAME=MARKER-2-2145></A>NF_ENDDEF </CODE></H2>
<HR>
The <A NAME=MARKER-2-2148></A><A NAME=MARKER-2-2149></A><A NAME=MARKER-2-2150></A>function <A NAME=MARKER-10-2151></A><CODE>NF_ENDDEF</CODE> takes an open netCDF dataset out of define mode. The changes made to the netCDF dataset while it was in define mode are checked and committed to disk if no problems occurred. Non-record variables may be initialized to a "fill value" as well (<A HREF=#MARKER-9-2270>see Section 5.12 "Set Fill Mode for Writes: NF_SET_FILL," page 39</A>). The netCDF dataset is then placed in data mode, so variable data can be read or written. <P>
This call may involve copying data under some circumstances. <A HREF=guidef-14.html#MARKER-9-3034>See Chapter 9 "NetCDF File Structure and Performance," page 95</A>, for a more extensive discussion. <P>
<A NAME=HEADING10-112></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2152></A>INTEGER FUNCTION NF_ENDDEF(INTEGER NCID)
<TABLE BORDER="1"><TD><CODE>NCID</CODE><TD>NetCDF ID, from a previous call to <CODE>NF_OPEN</CODE> or <CODE>NF_CREATE</CODE>.</TABLE>
</PRE>
Errors <P>
<CODE><A NAME=MARKER-10-2153></A>NF_ENDDEF</CODE> returns the value <A NAME=MARKER-10-2154></A><CODE>NF_NOERR</CODE> if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: <P>
<UL>
<LI>The specified netCDF dataset is not in define mode.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING10-118></A>
<H4>Example </H4>
Here <A NAME=MARKER-10-2156></A>is an example using <A NAME=MARKER-10-2157></A><CODE>NF_ENDDEF<A NAME=MARKER-10-2158></A></CODE>to finish the definitions of a new netCDF dataset named <CODE>foo.nc</CODE> and put it into data mode:<P>
<PRE>
<A NAME=MARKER-10-2159></A>INCLUDE 'netcdf.inc'
...
INTEGER NCID, STATUS
...
STATUS = NF_CREATE('foo.nc', NF_NOCLOBBER, NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
... ! create dimensions, variables, attributes
STATUS = NF_ENDDEF(NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
</PRE>
<A NAME=HEADING10-131></A>
<H2>5.8 Close an Open NetCDF Dataset: <A NAME=MARKER-10-2160></A><CODE><A NAME=MARKER-2-2161></A>NF_CLOSE </CODE></H2>
<HR>
The <A NAME=MARKER-2-2162></A><A NAME=MARKER-2-2163></A>function <A NAME=MARKER-10-2166></A><CODE>NF_CLOSE</CODE> closes an open netCDF dataset. If the dataset is in define mode, <CODE><A NAME=MARKER-10-2167></A>NF_ENDDEF</CODE> will be called before closing. (In this case, if <A NAME=MARKER-10-2168></A><CODE>NF_ENDDEF</CODE> returns an error, <A NAME=MARKER-10-2169></A><CODE>NF_ABORT</CODE> will automatically be called to restore the dataset to the consistent state before define mode was last entered.) After an open netCDF dataset is closed, its netCDF ID may be reassigned to the next netCDF dataset that is opened or created.<P>
<A NAME=HEADING10-133></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2170></A>INTEGER FUNCTION NF_CLOSE(INTEGER NCID)
<TABLE BORDER="1"><TD><CODE>NCID</CODE><TD>netCDF ID, from a previous call to <CODE>NF_OPEN</CODE> or <CODE>NF_CREATE</CODE>.</TABLE>
</PRE>
Errors <P>
<CODE><A NAME=MARKER-10-2171></A>NF_CLOSE</CODE> returns the value <A NAME=MARKER-10-2172></A><CODE>NF_NOERR</CODE> if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: <P>
<UL>
<LI>Define mode was entered and the automatic call made to <A NAME=MARKER-10-2174></A><CODE>NF_ENDDEF</CODE> failed.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING10-139></A>
<H4>Example </H4>
Here <A NAME=MARKER-10-2175></A>is an example using <A NAME=MARKER-10-2176></A><CODE>NF_CLOSE</CODE> to finish the definitions of a new netCDF dataset named <CODE>foo.nc</CODE> and release its netCDF ID:<P>
<PRE>
<A NAME=MARKER-10-2177></A>INCLUDE 'netcdf.inc'
...
INTEGER NCID, STATUS
...
STATUS = NF_CREATE('foo.nc', NF_NOCLOBBER, NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
... ! create dimensions, variables, attributes
STATUS = NF_CLOSE(NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
</PRE>
<A NAME=HEADING10-152></A>
<H2>5.9 <A NAME=MARKER-2-2178></A><A NAME=MARKER-2-2179></A><A NAME=MARKER-2-2180></A><A NAME=MARKER-2-2181></A>Inquire about an Open NetCDF Dataset: <A NAME=MARKER-10-2182></A><CODE>NF_INQ </CODE>Family</H2>
<HR>
Members of the <A NAME=MARKER-10-2184></A><CODE>NF_INQ </CODE>family of functions return information about an open netCDF dataset, given its netCDF ID. Dataset inquire functions may be called from either define mode or data mode. The first function, <A NAME=MARKER-10-2185></A><CODE>NF_INQ</CODE>, returns values for the number of dimensions, the number of variables, the number of global attributes, and the dimension ID of the dimension defined with unlimited length, if any. The other functions in the family each return just one of these items of information. <P>
<A NAME=MARKER-10-2186></A>For FORTRAN, these functions include <CODE><A NAME=MARKER-2-2187></A>NF_INQ</CODE>, <A NAME=MARKER-2-2188></A><CODE>NF_INQ_NDIMS</CODE>, <CODE><A NAME=MARKER-2-2189></A>NF_INQ_NVARS</CODE>,<A NAME=MARKER-2-2190></A> <CODE>NF_INQ_NATTS</CODE>, and <CODE><A NAME=MARKER-2-2191></A>NF_INQ_UNLIMDIM</CODE>. <P>
No I/O is performed when these functions are called, since the required information is available in memory for each open netCDF dataset. <P>
<A NAME=HEADING10-156></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2192></A>INTEGER FUNCTION NF_INQ (INTEGER NCID, INTEGER ndims,
INTEGER nvars,INTEGER ngatts,
INTEGER unlimdimid)
INTEGER FUNCTION NF_INQ_NDIMS (INTEGER NCID, INTEGER ndims)
INTEGER FUNCTION NF_INQ_NVARS (INTEGER NCID, INTEGER nvars)
INTEGER FUNCTION NF_INQ_NATTS (INTEGER NCID, INTEGER ngatts)
INTEGER FUNCTION NF_INQ_UNLIMDIM (INTEGER NCID, INTEGER unlimdimid)
<TABLE BORDER="1"><TD><CODE>NCID</CODE><TD>NetCDF ID, from a previous call to <CODE>NF_OPEN</CODE> or <CODE>NF_CREATE</CODE>.<TR>
<TD><CODE>ndims</CODE><TD>Returned number of dimensions defined for this netCDF dataset.<TR>
<TD><CODE>nvars</CODE><TD>Returned number of variables defined for this netCDF dataset.<TR>
<TD><CODE>ngatts</CODE><TD>Returned number of global attributes defined for this netCDF dataset.<TR>
<TD><CODE>unlimdimid</CODE><TD>Returned <A NAME=MARKER-2-157></A><A NAME=MARKER-2-158></A><A NAME=MARKER-2-159></A>ID of the unlimited dimension, if there is one for this netCDF dataset. If no unlimited length dimension has been defined, -1 is returned.</TABLE>
</PRE>
Errors <P>
All members of the <A NAME=MARKER-10-2193></A>NF_INQ family return the value <A NAME=MARKER-10-2194></A><CODE>NF_NOERR </CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:<P>
<UL>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING10-167></A>
<H4>Example </H4>
Here <A NAME=MARKER-10-2196></A>is an example using <A NAME=MARKER-10-2197></A><CODE>NF_INQ</CODE> to find out about a netCDF dataset named <CODE>foo.nc</CODE>: <P>
<PRE>
<A NAME=MARKER-10-2198></A>INCLUDE 'netcdf.inc'
...
INTEGER STATUS, NCID, NDIMS, NVARS, NGATTS, UNLIMDIMID
...
STATUS = NF_OPEN('foo.nc', NF_NOWRITE, NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
...
STATUS = NF_INQ(NCID, NDIMS, NVARS, NGATTS, UNLIMDIMID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
</PRE>
<A NAME=HEADING10-178></A>
<H2>5.10 <A NAME=MARKER-2-2199></A><A NAME=MARKER-2-2200></A>Synchronize an Open NetCDF Dataset to Disk: <A NAME=MARKER-10-2201></A><CODE><A NAME=MARKER-2-2202></A>NF_SYNC </CODE></H2>
<HR>
The function <A NAME=MARKER-10-2205></A><CODE>NF_SYNC</CODE> offers a way to synchronize the disk copy of a netCDF dataset with in-memory buffers. There are two reasons you might want to synchronize after writes:<P>
<UL>
<LI>To minimize data loss in case of abnormal termination, or<P>
<LI>To make data available to other processes for reading immediately after it is written. But note that a process that already had the dataset open for reading would not see the number of records increase when the writing process calls <A NAME=MARKER-10-2209></A>NF_SYNC; to accomplish this, the reading process must call <A NAME=MARKER-10-2210></A>NF_SYNC.<P>
</UL>
This function is backward-compatible with previous versions of the netCDF library. The intent was to allow sharing of a netCDF dataset among multiple readers and one writer, by having the writer call <A NAME=MARKER-10-2211></A><CODE>NF_SYNC</CODE> after writing and the readers call <A NAME=MARKER-10-2212></A><CODE>NF_SYNC</CODE> before each read. For a writer, this flushes buffers to disk. For a reader, it makes sure that the next read will be from disk rather than from previously cached buffers, so that the reader will see changes made by the writing process (e.g., the number of records written) without having to close and reopen the dataset. If you are only accessing a small amount of data, it can be expensive in computer resources to always synchronize to disk after every write, since you are giving up the benefits of buffering.<P>
An easier way to accomplish sharing (and what is now recommended) is to have the writer and readers open the dataset with the <A NAME=MARKER-10-2213></A>NF_SHARE flag, and then it will not be necessary to call <A NAME=MARKER-10-2215></A><CODE>NF_SYNC</CODE> at all. However, the <A NAME=MARKER-10-2216></A><CODE>NF_SYNC</CODE> function still provides finer granularity than the <A NAME=MARKER-10-2217></A>NF_SHARE flag, if only a few netCDF accesses need to be synchronized among processes.<P>
It is important to note that changes to the ancillary data, such as attribute values, are <I>not</I> propagated automatically by use of the <A NAME=MARKER-10-2218></A>NF_SHARE flag. Use of the <A NAME=MARKER-10-2219></A><CODE>NF_SYNC</CODE> function is still required for this purpose. <P>
Sharing datasets when the writer enters define mode to change the data schema requires extra care. In previous releases, after the writer left define mode, the readers were left looking at an old copy of the dataset, since the changes were made to a new copy. The only way readers could see the changes was by closing and reopening the dataset. Now the changes are made in place, but readers have no knowledge that their internal tables are now inconsistent with the new dataset schema. If netCDF datasets are shared across redefinition, some mechanism external to the netCDF library must be provided that prevents access by readers during redefinition and causes the readers to call <A NAME=MARKER-10-2220></A><CODE>NF_SYNC</CODE> before any subsequent access. <P>
When calling <A NAME=MARKER-10-2221></A><CODE>NF_SYNC</CODE>, the netCDF dataset must be in data mode. A netCDF dataset in define mode is synchronized to disk only when <A NAME=MARKER-10-2224></A><CODE>NF_ENDDEF</CODE> is called. A process that is reading a netCDF dataset that another process is writing may call <A NAME=MARKER-10-2225></A><CODE>NF_SYNC</CODE> to get updated with the changes made to the data by the writing process (e.g., the number of records written), without having to close and reopen the dataset.<P>
Data is automatically synchronized to disk when a netCDF dataset is closed, or whenever you leave define mode.<P>
<A NAME=HEADING10-188></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2232></A>INTEGER FUNCTION NF_SYNC(INTEGER NCID)
<TABLE BORDER="1"><TD><CODE>NCID</CODE><TD>NetCDF ID, from a previous call to <CODE>NF_OPEN</CODE> or <CODE>NF_CREATE</CODE>.</TABLE>
</PRE>
Errors <P>
<CODE><A NAME=MARKER-10-2233></A>NF_SYNC</CODE> returns the value <A NAME=MARKER-10-2234></A><CODE>NF_NOERR</CODE> if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: <P>
<UL>
<LI>The netCDF dataset is in define mode.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING10-194></A>
<H4>Example </H4>
Here <A NAME=MARKER-10-2236></A>is an example using <A NAME=MARKER-10-2237></A><CODE>NF_SYNC</CODE> to synchronize the disk writes of a netCDF dataset named <CODE>foo.nc</CODE>:<P>
<PRE>
<A NAME=MARKER-10-2238></A>INCLUDE 'netcdf.inc'
...
INTEGER STATUS, NCID
...
STATUS = NF_OPEN('foo.nc', NF_WRITE, NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
...
! write data or change attributes
...
STATUS = NF_SYNC(NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
</PRE>
<A NAME=HEADING10-207></A>
<H2>5.11 Back Out of Recent Definitions: <A NAME=MARKER-10-2239></A><CODE><A NAME=MARKER-2-2240></A>NF_ABORT </CODE></H2>
<HR>
You no longer need to call this function, since it is called automatically by <A NAME=MARKER-10-2241></A><CODE>NF_CLOSE</CODE> in case the dataset is in define mode and something goes wrong with committing the changes. The <A NAME=MARKER-2-2242></A><A NAME=MARKER-2-2243></A><A NAME=MARKER-2-2244></A><A NAME=MARKER-2-2245></A><A NAME=MARKER-2-2246></A><A NAME=MARKER-2-2247></A><A NAME=MARKER-2-2252></A><A NAME=MARKER-2-2253></A><A NAME=MARKER-2-2254></A><A NAME=MARKER-2-2255></A><A NAME=MARKER-2-2256></A>function <A NAME=MARKER-10-2257></A><CODE>NF_ABORT</CODE> <A NAME=MARKER-2-2258></A><A NAME=MARKER-2-2259></A>just closes the netCDF dataset, if not in define mode. If the dataset is being created and is still in define mode, the dataset is deleted. If define mode was entered by a call to <A NAME=MARKER-10-2262></A><CODE>NF_REDEF</CODE>, the netCDF dataset is restored to its state before definition mode was entered and the dataset is closed.<P>
<A NAME=HEADING10-209></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2263></A>INTEGER FUNCTION NF_ABORT(INTEGER NCID)
<TABLE BORDER="1"><TD><CODE>NCID</CODE><TD>NetCDF ID, from a previous call to <CODE>NF_OPEN</CODE> or <CODE>NF_CREATE</CODE>.</TABLE>
</PRE>
Errors <P>
<CODE><A NAME=MARKER-10-2264></A>NF_ABORT</CODE> returns the value <A NAME=MARKER-10-2265></A><CODE><A NAME=MARKER-2-3029></A>NF_NOERR</CODE> if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include: <P>
<UL>
<LI>When called from define mode while creating a netCDF dataset, deletion of the dataset failed.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING10-215></A>
<H4>Example </H4>
Here <A NAME=MARKER-10-2267></A>is an example using <A NAME=MARKER-10-2268></A><CODE>NF_ABORT</CODE> to back out of redefinitions of a dataset named <CODE>foo.nc</CODE>:<P>
<PRE>
<A NAME=MARKER-10-2269></A>INCLUDE 'netcdf.inc'
...
INTEGER STATUS, NCID, LATID
...
STATUS = NF_OPEN('foo.nc', NF_WRITE, NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
...
STATUS = NF_REDEF(NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
...
STATUS = NF_DEF_DIM(NCID, 'LAT', 18, LATID)
IF (STATUS .NE. NF_NOERR) THEN ! dimension definition failed
CALL HANDLE_ERR(STATUS)
STATUS = NF_ABORT(NCID) ! abort redefinitions
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
ENDIF
...
</PRE>
<A NAME=HEADING10-234></A>
<H2>5.12 <A NAME=MARKER-9-2270></A><A NAME=MARKER-2-2271></A><A NAME=MARKER-2-2272></A><A NAME=MARKER-2-2273></A><A NAME=MARKER-2-2274></A>Set Fill Mode for Writes: <A NAME=MARKER-10-2275></A><CODE>NF_SET_FILL </CODE></H2>
<HR>
This function is intended for advanced usage, to optimize writes under some circumstances described below. The function <A NAME=MARKER-10-2277></A><CODE>NF_SET_FILL</CODE> sets the <I>fill mode</I> for a netCDF dataset open for writing and returns the current fill mode in a return parameter. The fill mode can be specified as either <A NAME=MARKER-10-2278></A><CODE><A NAME=MARKER-2-2279></A>NF_FILL </CODE>or <A NAME=MARKER-10-2280></A><CODE>NF_NOFILL</CODE>. The default behavior corresponding to <A NAME=MARKER-10-2282></A>NF_FILL is that data is pre-filled with fill values, that is fill values are written when you create non-record variables or when you write a value beyond data that has not yet been written. This makes it possible to detect attempts to read data before it was written. <A HREF=guidef-12.html#MARKER-9-2867>See Section 7.16 "Fill Values," page 78</A>, for more information on the use of fill values. <A HREF=guidef-13.html#MARKER-9-2936>See Section 8.1 "Attribute Conventions," page 81</A>, for information about how to define your own fill values. <P>
The behavior corresponding to <A NAME=MARKER-10-2283></A><CODE><A NAME=MARKER-2-2284></A>NF_NOFILL</CODE> overrides the default behavior of prefilling data with fill values. This can be used to enhance performance, because it avoids the duplicate writes that occur when the netCDF library writes fill values that are later overwritten with data. <P>
A value indicating which mode the netCDF dataset was already in is returned. You can use this value to temporarily change the fill mode of an open netCDF dataset and then restore it to the previous mode. <P>
After you turn on <A NAME=MARKER-10-2285></A><CODE>NF_NOFILL</CODE> mode for an open netCDF dataset, you must be certain to write valid data in all the positions that will later be read. Note that nofill mode is only a transient property of a netCDF dataset open for writing: if you close and reopen the dataset, it will revert to the default behavior. You can also revert to the default behavior by calling <A NAME=MARKER-10-2286></A><CODE>NF_SET_FILL</CODE> again to explicitly set the fill mode to <A NAME=MARKER-10-2287></A><CODE>NF_NOFILL</CODE>.<P>
There are three situations where it is advantageous to set nofill mode:<P>
<DL>
<DL>
<DT><DD>1. Creating and initializing a netCDF dataset. In this case, you should set nofill mode before calling <A NAME=MARKER-10-2288></A><CODE>NF_ENDDEF</CODE> and then write <I>completely</I> all non-record variables and the initial records of all the record variables you want to initialize. <P>
<DT><DD>2. Extending an existing record-oriented netCDF dataset. Set nofill mode after opening the dataset for writing, then append the additional records to the dataset completely, leaving no intervening unwritten records. <P>
<DT><DD>3. Adding new variables that you are going to initialize to an existing netCDF dataset. Set nofill mode before calling <A NAME=MARKER-10-2289></A><CODE>NF_ENDDEF</CODE> then write all the new variables completely.<P>
</DL>
</DL>
If the netCDF dataset has an unlimited dimension and the last record was written while in nofill mode, then the dataset may be shorter than if nofill mode was not set, but this will be completely transparent if you access the data only through the netCDF interfaces.<P>
The use of this feature may not be available (or even needed) in future releases. Programmers are cautioned against heavy reliance upon this feature.<P>
<A NAME=HEADING10-245></A>
<H4>Usage </H4>
<PRE>
<A NAME=MARKER-10-2290></A>INTEGER FUNCTION NF_SET_FILL(INTEGER NCID, INTEGER FILLMODE,
INTEGER old_mode)
<TABLE BORDER="1"><TD><CODE>NCID</CODE><TD>NetCDF ID, from a previous call to <CODE>NF_OPEN</CODE> or <CODE>NF_CREATE</CODE>.<TR>
<TD><CODE>FILLMODE</CODE><TD>Desired fill mode for the dataset, either <CODE>NF_NOFILL</CODE> or <CODE><A NAME=MARKER-2-2279></A>NF_FILL</CODE>.<TR>
<TD>old_mode<TD>Returned current fill mode of the dataset before this call, either <CODE>NF_NOFILL</CODE> or <CODE>NF_FILL</CODE>.</TABLE>
</PRE>
Errors <P>
<CODE><A NAME=MARKER-10-2291></A>NF_SET_FILL</CODE> returns the value <A NAME=MARKER-10-2292></A><CODE>NF_NOERR</CODE> if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:<P>
<UL>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
<LI>The specified netCDF ID refers to a dataset open for read-only access.<P>
<LI>The fill mode argument is neither <A NAME=MARKER-10-2294></A><CODE>NF_NOFILL</CODE> nor <A NAME=MARKER-10-2295></A><CODE>NF_FILL</CODE>.<P>
</UL>
<A NAME=HEADING10-253></A>
<H4>Example </H4>
Here <A NAME=MARKER-10-2296></A>is an example using <A NAME=MARKER-10-2297></A><CODE>NF_SET_FILL</CODE> to set nofill mode for subsequent writes of a netCDF dataset named <CODE>foo.nc</CODE>:<P>
<PRE>
<A NAME=MARKER-10-2298></A>INCLUDE 'netcdf.inc'
...
INTEGER NCID, STATUS, OMODE
...
STATUS = NF_OPEN('foo.nc', NF_WRITE, NCID)
IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
...
! write data with default prefilling behavior
...
OMODE = NF_SET_FILL(NCID, NF_NOFILL)
...
! write data with no prefilling
...
</PRE>
<!-- TOC --><DL>
<DT><A HREF="guidef-10.html#HEADING10-16"><B>5.1 </B> - NetCDF Library Interface Descriptions</A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-25"><B>5.2 </B> - Get error message corresponding to error status: NF_STRERROR </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-27"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-31"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-42"><B>5.3 </B> - Get netCDF library version: NF_INQ_LIBVERS </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-44"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-48"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-53"><B>5.4 </B> - Create a NetCDF dataset: NF_CREATE </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-56"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-65"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-73"><B>5.5 </B> - Open a NetCDF Dataset for Access: NF_OPEN </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-75"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-81"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-89"><B>5.6 </B> - Put Open NetCDF Dataset into Define Mode: NF_REDEF </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-91"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-98"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-109"><B>5.7 </B> - Leave Define Mode: NF_ENDDEF </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-112"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-118"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-131"><B>5.8 </B> - Close an Open NetCDF Dataset: NF_CLOSE </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-133"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-139"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-152"><B>5.9 </B> - Inquire about an Open NetCDF Dataset: NF_INQ Family</A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-156"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-167"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-178"><B>5.10 </B> - Synchronize an Open NetCDF Dataset to Disk: NF_SYNC </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-188"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-194"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-207"><B>5.11 </B> - Back Out of Recent Definitions: NF_ABORT </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-209"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-215"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-234"><B>5.12 </B> - Set Fill Mode for Writes: NF_SET_FILL </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-245"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidef-10.html#HEADING10-253"><B>Example</B> - </A>
<DD>
</DL>
<HR>
<ADDRESS>NetCDF User's Guide for Fortran - 4 JUN 1997</ADDRESS>
<A HREF="guidef-11.html">[Next] </A><A HREF="guidef-9.html">[Previous] </A><A HREF="guidef-1.html">[Top] </A><A HREF="guidef-3.html">[Contents] </A><A HREF="guidef-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>
|