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
|
## The CDL Header API Reference Guide
Starting from PnetCDF 1.14.1, a set of C APIs have been added that read and
parse the header section of a CDL file.
([Common Data Language](https://docs.unidata.ucar.edu/nug/2.0-draft/cdl.html))
is a text notation for representing the structure and data of a binary netCDF
file. As CDL files are in a plain text format, they can can be read by a human.
The new APIs allow users to query the metadata defined in the CDL file. They
can be useful for programs that want to read only the file header of a netCDF
file produced by other applications. A CDL file can be generated by running the
PnetCDF utility program
[ncmpidump](https://github.com/Parallel-NetCDF/PnetCDF/tree/master/src/utils#ncmpidump)
or NetCDF's
[ncdump](https://docs.unidata.ucar.edu/nug/current/netcdf_utilities_guide.html#ncdump_guide).
Both utility programs take an command-line option "-h" to dump only the file
header in a plain text format.
Example programs can be found in
[examples/create_from_cdl.c](../examples/C/create_from_cdl.c) and
[benchmarks/WRF-IO/wrf_io.c.c](../benchmarks/WRF-IO/wrf_io.c).
---
### Table of contents
* [cdl_hdr_open](#cdl_hdr_open) opens and parses the CDL file's header
* [cdl_hdr_inq_format](#cdl_hdr_inq_format) returns the file format version
* [cdl_hdr_inq_ndims](#cdl_hdr_inq_ndims) returns the number of dimensions
defined in CDL file
* [cdl_hdr_inq_dim](#cdl_hdr_inq_dim) returns the metadata of a dimension
* [cdl_hdr_inq_nvars](#cdl_hdr_inq_nvars) returns the number of variables
* [cdl_hdr_inq_var](#cdl_hdr_inq_var) returns the metadata of a variable
defined in CDL file
* [cdl_hdr_inq_nattrs](#cdl_hdr_inq_nattrs) returns the number of attributes of
a given variable
* [cdl_hdr_inq_attr](#cdl_hdr_inq_attr) returns the metadata of an attribute
* [cdl_hdr_close](#cdl_hdr_close) closes the CDL file
---
### cdl_hdr_open()
This API opens a file that is in the CDL format.
#### Usage
```
int cdl_hdr_open(const char *filename,
int *hidp);
```
+ **path** - [input] File name for a CDL file to be opened.
+ **hidp** - [output] Pointer to location where returned file ID is to be
stored.
#### Return Error Codes
This API returns the value `NC_NOERR` if no errors occurred. Otherwise, the
returned value of type `int` indicates an error. Possible error codes and
causes of errors include:
+ `NC_ENFILE` - too many files opened.
+ `NC_ENOENT` - the specified input file does not exist.
+ `NC_EFILE` - other unknown I/O error, including unable to open the file.
+ `NC_ENOMEM` - unable to allocate memory (out of memory).
+ `NC_ENOTNC` - input file is not in the CDL format.
#### Example
```
#include <pnetcdf.h>
int err, hid;
/* open the input file in CDL format */
err = cdl_hdr_open("cdl_header.txt", &hid);
if (err != NC_NOERR)
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));
```
---
### cdl_hdr_inq_format()
This API returns the file format version of a given file ID.
#### Usage
```
int cdl_hdr_inq_format(int hid,
int *formatp);
```
+ **hid** - [input] file ID, from a previous call to `cdl_hdr_open`.
+ **formatp** - [output] Pointer to location for returned format version, one
of the following constants define in the header file pnetcdf.h. When this
argument is NULL, this API takes no effect simply returns `NC_NOERR`.
* 1 - the classic CDF-1 format.
* 2 - the classic CDF-2 format (NC_64BIT_OFFSET).
* 5 - the classic CDF-5 format (NC_64BIT_DATA).
#### Return Error Codes
This API returns the value `NC_NOERR` if no errors occurred. Otherwise, the
returned value of type `int` indicates an error. Possible error codes and
causes of errors include:
+ `NC_EBADID` - the specified file ID, `hid`, does not refer to an opened CDL
file.
#### Example
```
#include <pnetcdf.h>
int err, hid, format;
/* open the input file in CDL format */
err = cdl_hdr_open("cdl_header.txt", &hid);
/* retrieve file format information of the input file */
err = cdl_hdr_inq_format(hid, &format);
if (err != NC_NOERR)
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));
```
---
### cdl_hdr_inq_ndims()
This API returns number of dimensions defined in the CDL file.
#### Usage
```
int cdl_hdr_inq_ndims(int *hid,
int *ndimsp);
```
+ **hid** - [input] file ID, from a previous call to `cdl_hdr_open`.
+ **ndimsp** - [output] Pointer to location for returned number of dimensions
defined in the CDL file. When this argument is NULL, this API takes no
effect simply returns `NC_NOERR`.
#### Return Error Codes
This API returns the value `NC_NOERR` if no errors occurred. Otherwise, the
returned value of type `int` indicates an error. Possible error codes and
causes of errors include:
+ `NC_EBADID` - the specified file ID, `hid`, does not refer to an opened CDL
file.
#### Example
```
#include <pnetcdf.h>
int err, hid, ndims;
/* open the input file in CDL format */
err = cdl_hdr_open("cdl_header.txt", &hid);
/* retrieve the number of dimensions defined in the CDL file */
err = cdl_hdr_inq_ndims(hid, &ndims);
if (err != NC_NOERR)
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));
```
---
### cdl_hdr_inq_dim()
This API returns metadata of a dimension.
#### Usage
```
int cdl_hdr_inq_dim(int hid,
int dimid,
char **namep,
MPI_Offset *sizep);
```
+ **hid** - [input] file ID, from a previous call to `cdl_hdr_open`.
+ **dimid** - [input] dimension ID.
+ **namep** - [output] returned the name of dimension specified by its ID,
'dimid'. Note users should not alter the contents of character string
pointed by 'namep', as it is an internal allocated data structure. When
`NULL` is passed in, this argument is ignored.
+ **sizep** - [output] size of the dimension. When `NULL` is passed in, this
argument is ignored.
#### Return Error Codes
This API returns the value `NC_NOERR` if no errors occurred. Otherwise, the
returned value of type `int` indicates an error. Possible error codes and
causes of errors include:
+ `NC_EBADID` - the specified file ID, `hid`, does not refer to an opened CDL
file, or or the value of `dimid` is not within the range of number of
dimension defined in the CDL file.
#### Example
```
#include <pnetcdf.h>
int i, err, hid, ndims,
/* open the input file in CDL format */
err = cdl_hdr_open("cdl_header.txt", &hid);
/* retrieve the number of dimensions defined in the CDL file */
err = cdl_hdr_inq_ndims(hid, &ndims);
for (i=0; i<ndims; i++) {
char *name;
MPI_Offset dimlen;
/* retrieve metadata of dimension i */
err = cdl_hdr_inq_dim(hid, i, &name, &dimlen);
if (err != NC_NOERR)
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));
}
```
---
### cdl_hdr_inq_nvars()
This API returns the number of variables defined in the CDL file.
#### Usage
```
int cdl_hdr_inq_nvars(int hid,
int *nvarsp);
```
+ **hid** - [input] file ID, from a previous call to `cdl_hdr_open`.
+ **nvarsp** - [output] Pointer to location for returned number of variables
defined in the CDFL file. When this argument is NULL, this API takes no
effect simply returns `NC_NOERR`.
#### Return Error Codes
This API returns the value `NC_NOERR` if no errors occurred. Otherwise, the
returned value of type `int` indicates an error. Possible error codes and
causes of errors include:
+ `NC_EBADID` - the specified file ID, `hid`, does not refer to an opened CDL
file.
#### Example
```
#include <pnetcdf.h>
int err, hid, nvars;
/* open the input file in CDL format */
err = cdl_hdr_open("cdl_header.txt", &hid);
/* retrieve number of variables defined in the CDL file */
err = cdl_hdr_inq_nvars(hid, &nvars);
if (err != NC_NOERR)
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));
```
---
### cdl_hdr_inq_var()
This API returns metadata of a variable defined in CDL file.
#### Usage
```
int cdl_hdr_inq_var(int hid,
int varid,
char **namep,
nc_type *xtypep,
int *ndimsp,
int **dimidsp);
```
+ **hid** - [input] file ID, from a previous call to `cdl_hdr_open`.
+ **varid** - [input] variable ID.
+ **namep** - [output] returned the name of variable specified by its ID,
'varid'. Note users should not alter the contents of character string
pointed by 'namep', as it is an internal allocated data structure. When
`NULL` is passed in, this argument is ignored.
+ **xtypep** - [output] pointer to location for returned variable's data
type, one of the set of predefined netCDF external data types. The type of
this parameter, `nc_type`, is defined in the PnetCDF header file. The valid
data types are `NC_BYTE`, `NC_CHAR`, `NC_SHORT`, `NC_INT`, `NC_FLOAT`,
`NC_DOUBLE`, `NC_UBYTE`, `NC _USHORT`, `NC_UINT`, `NC_INT64`, and
`NC_UINT64`. When `NULL` is passed in, this argument is ignored.
+ **ndimsp** - [output] pointer to location for returned number of dimensions
the variable was defined as using. When `NULL` is passed in, this argument
is ignored.
+ **dimidsp** - [output] returned vector of dimension IDs corresponding to
the variable dimensions. Note users should not alter the contents pointed
by argument `dimidsp`, as it is an internal allocated data structure. When
`NULL` is passed in, this argument is ignored.
#### Return Error Codes
This API returns the value `NC_NOERR` if no errors occurred. Otherwise, the
returned value of type `int` indicates an error. Possible error codes and
causes of errors include:
+ `NC_EBADID` - the specified file ID, `hid`, does not refer to an opened CDL
file, or the value of `varid` is not within the range of number of
variables defined in the CDL file.
#### Example
```
#include <pnetcdf.h>
int i, err, hid, nvars;
/* open the input file in CDL format */
err = cdl_hdr_open("cdl_header.txt", &hid);
/* retrieve number of variables defined in the CDL file */
err = cdl_hdr_inq_nvars(hid, &nvars);
for (i=0; i<nvars; i++) {
char *name;
int ndims, *dimids;
nc_type xtype;
/* retrieve metadata of variable i defined in the CDL file */
err = cdl_hdr_inq_var(hid, i, &name, &xtype, &ndims, &dimids);
if (err != NC_NOERR)
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));
}
```
---
### cdl_hdr_inq_nattrs()
This API returns number of attributes associated with a given variable.
#### Usage
```
int cdl_hdr_inq_nattrs(int hid,
int varid,
int *nattrsp);
```
+ **hid** - [input] file ID, from a previous call to `cdl_hdr_open`.
+ **varid** - [input] variable ID.
+ **nattrsp** - [output] pointer to location for returned number of
attributes associated to the variable was defined in the CDL file. When
`NULL` is passed in, this argument is ignored.
#### Return Error Codes
This API returns the value `NC_NOERR` if no errors occurred. Otherwise, the
returned value of type `int` indicates an error. Possible error codes and
causes of errors include:
+ `NC_EBADID` - the specified file ID, `hid`, does not refer to an opened CDL
file, or the value of `varid` is not within the range of number of
variables defined in the CDL file.
#### Example
```
#include <pnetcdf.h>
int err, hid;
/* open the input file in CDL format */
err = cdl_hdr_open("cdl_header.txt", &hid);
/* retrieve the number of global attributes */
err = cdl_hdr_inq_nattrs(hid, NC_GLOBAL, &nattrs);
if (err != NC_NOERR)
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));
for (i=0; i<nvars; i++) {
int nattrs;
/* retrieve the number of attributes associated with variable i */
err = cdl_hdr_inq_nattrs(hid, i, &nattrs);
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));
}
```
---
### cdl_hdr_inq_attr()
This API returns metadata of an attribute.
#### Usage
```
int cdl_hdr_inq_attr(int hid,
int varid,
int attrid,
char **namep,
nc_type *xtypep,
MPI_Offset *nelemsp,
void **valuep);
```
+ **hid** - [input] file ID, from a previous call to `cdl_hdr_open`.
+ **varid** - [input] variable ID.
+ **attrid** - [input] attribute ID.
+ **namep** - [output] returned the name of attribute specified by its ID,
'attrid'. Note users should not alter the contents of character string
pointed by 'namep', as it is an internal allocated data structure. When
`NULL` is passed in, this argument is ignored.
+ **xtypep** - [output] pointer to location for returned attribute's data
type, one of the set of predefined netCDF external data types. The type of
this parameter, `nc_type`, is defined in the PnetCDF header file. The valid
data types are `NC_BYTE`, `NC_CHAR`, `NC_SHORT`, `NC_INT`, `NC_FLOAT`,
`NC_DOUBLE`, `NC_UBYTE`, `NC _USHORT`, `NC_UINT`, `NC_INT64`, and
`NC_UINT64`. When `NULL` is passed in, this argument is ignored.
+ **nelemsp** - [output] pointer to location for returned number of elements
the attribute was defined as using. When `NULL` is passed in, this argument
is ignored.
+ **valuep** - [output] pointer to location for returned attribute value(s).
Note users should not alter the contents pointed by argument `valuep`, as
it is an internal allocated data structure. When `NULL` is passed in, this
argument is ignored.
#### Return Error Codes
This API returns the value `NC_NOERR` if no errors occurred. Otherwise, the
returned value of type `int` indicates an error. Possible error codes and
causes of errors include:
+ `NC_EBADID` - the specified file ID, `hid`, does not refer to an opened CDL
file, the value of `varid` is not within the range of number of variables
defined in the CDL file, or the value of `attrid` is not within the range
of number of attributes associated to the variable.
#### Example
```
#include <pnetcdf.h>
int err, hid, nattrs, nvars, nelems;
char *name;
void *value;
nc_type xtype;
/* open the input file in CDL format */
err = cdl_hdr_open("cdl_header.txt", &hid);
/* retrieve the number of global attributes */
err = cdl_hdr_inq_nattrs(hid, NC_GLOBAL, &nattrs); ERR
for (i=0; i<nattrs; i++) {
/* retrieve metadata of global attribute i */
err = cdl_hdr_inq_attr(hid, NC_GLOBAL, i, &name, &xtype, &nelems, &value);
ERR
}
/* retrieve number of variables defined in the CDL file */
err = cdl_hdr_inq_nvars(hid, &nvars);
for (i=0; i<nvars; i++) {
/* retrieve the number of attributes associated with variable i */
err = cdl_hdr_inq_nattrs(hid, i, &nattrs); ERR
for (j=0; j<nattrs; j++) {
/* retrieve metadata of attribute j associated with variable i */
err = cdl_hdr_inq_attr(hid, i, j, &name, &xtype, &nelems, &value);
if (err != NC_NOERR)
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));
}
}
```
---
### cdl_hdr_close()
This API closes the CDL file.
#### Usage
```
int cdl_hdr_close(int hid);
```
+ **hid** - [input] file ID, from a previous call to `cdl_hdr_open`.
#### Return Error Codes
This API returns the value `NC_NOERR` if no errors occurred. Otherwise, the
returned value of type `int` indicates an error. Possible error codes and
causes of errors include:
+ `NC_EBADID` - the specified file ID, `hid`, does not refer to an opened CDL
file.
#### Example
```
#include <pnetcdf.h>
int err, hid;
/* open the input file in CDL format */
err = cdl_hdr_open("cdl_header.txt", &hid);
/* close CDL file */
err = cdl_hdr_close(&hid);
if (err != NC_NOERR)
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));
```
|