NetCDF  4.3.2
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
ddim.c File Reference

Dimension functions. More...

#include "ncdispatch.h"

Go to the source code of this file.

Functions

int nc_def_dim (int ncid, const char *name, size_t len, int *idp)
 Define a new dimension. More...
 
int nc_inq_dimid (int ncid, const char *name, int *idp)
 Find the ID of a dimension from the name. More...
 
int nc_inq_dim (int ncid, int dimid, char *name, size_t *lenp)
 Find the name and length of a dimension. More...
 
int nc_rename_dim (int ncid, int dimid, const char *name)
 Rename a dimension. More...
 
int nc_inq_ndims (int ncid, int *ndimsp)
 Find the number of dimensions. More...
 
int nc_inq_unlimdim (int ncid, int *unlimdimidp)
 Find the ID of the unlimited dimension. More...
 
int nc_inq_dimname (int ncid, int dimid, char *name)
 Find out the name of a dimension. More...
 
int nc_inq_dimlen (int ncid, int dimid, size_t *lenp)
 Find the length of a dimension. More...
 

Detailed Description

Dimension functions.

These functions define and inquire about dimensions.

Copyright 2010 University Corporation for Atmospheric Research/Unidata. See COPYRIGHT file for more info.

Definition in file ddim.c.

Function Documentation

int nc_def_dim ( int  ncid,
const char *  name,
size_t  len,
int *  idp 
)

Define a new dimension.

The function nc_def_dim 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 classic or 64-bit offset netCDF dataset. NetCDF-4 datasets may have multiple unlimited dimensions.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
nameName of the dimension to be created.
lenLength of the dimension to be created. Use NC_UNLIMITED for unlimited dimensions.
idpPointer where dimension ID will be stored.
Return values
NC_NOERRNo error.
Returns
NC_EBADID Not a valid ID.
NC_ENOTINDEFINE Not in define mode.
NC_EDIMSIZE Invalid dimension size.
NC_EUNLIMIT NC_UNLIMITED size already in use
NC_EMAXDIMS NC_MAX_DIMS exceeded
NC_ENAMEINUSE String match to name in use
NC_ENOMEM Memory allocation (malloc) failure
NC_EPERM Write to read only

Example

Here is an example using nc_def_dim() to create a dimension named lat of length 18 and a unlimited dimension named rec in a new netCDF dataset named foo.nc:

1 #include <netcdf.h>
2  ...
3 int status, ncid, latid, recid;
4  ...
5 status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
6 if (status != NC_NOERR) handle_error(status);
7  ...
8 status = nc_def_dim(ncid, "lat", 18L, &latid);
9 if (status != NC_NOERR) handle_error(status);
10 status = nc_def_dim(ncid, "rec", NC_UNLIMITED, &recid);
11 if (status != NC_NOERR) handle_error(status);

Definition at line 67 of file ddim.c.

int nc_inq_dim ( int  ncid,
int  dimid,
char *  name,
size_t *  lenp 
)

Find the name and length of a dimension.

The length for the unlimited dimension, if any, is the number of records written so far.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
dimidDimension ID, from a previous call to nc_inq_dimid() or nc_def_dim().
nameReturned 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 NC_MAX_NAME. (This doesn't include the null terminator, so declare your array to be size NC_MAX_NAME+1). The returned character array will be null-terminated.
lenpPointer to location for returned length of dimension. For the unlimited dimension, this is the number of records written so far.
Returns
NC_NOERR No error.
NC_EBADID Not a valid ID.
NC_EBADDIM Invalid dimension ID or name.

Example

Here is an example using nc_inq_dim() to determine the length of a dimension named lat, and the name and current maximum length of the unlimited dimension for an existing netCDF dataset named foo.nc:

1 #include <netcdf.h>
2  ...
3 int status, ncid, latid, recid;
4 size_t latlength, recs;
5 char recname[NC_MAX_NAME+1];
6  ...
7 status = nc_open("foo.nc", NC_NOWRITE, &ncid);
8 if (status != NC_NOERR) handle_error(status);
9 status = nc_inq_unlimdim(ncid, &recid);
10 if (status != NC_NOERR) handle_error(status);
11  ...
12 status = nc_inq_dimid(ncid, "lat", &latid);
13 if (status != NC_NOERR) handle_error(status);
14 status = nc_inq_dimlen(ncid, latid, &latlength);
15 if (status != NC_NOERR) handle_error(status);
16 
17 status = nc_inq_dim(ncid, recid, recname, &recs);
18 if (status != NC_NOERR) handle_error(status);

Definition at line 159 of file ddim.c.

int nc_inq_dimid ( int  ncid,
const char *  name,
int *  idp 
)

Find the ID of a dimension from the name.

The function nc_inq_dimid returns (as an argument) the ID of a netCDF dimension, given the name of the dimension. If ndims is the number of dimensions defined for a netCDF dataset, each dimension has an ID between 0 and ndims-1.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
nameName of the dimension.
idpPointer where dimension ID will be stored.
Returns
NC_NOERR No error.
NC_EBADID Not a valid ID.
NC_EBADDIM Invalid dimension ID or name.

Definition at line 96 of file ddim.c.

int nc_inq_dimlen ( int  ncid,
int  dimid,
size_t *  lenp 
)

Find the length of a dimension.

The length for the unlimited dimension, if any, is the number of records written so far.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
dimidDimension ID, from a previous call to nc_inq_dimid() or nc_def_dim().
lenpPointer where the length will be stored.
Returns
NC_NOERR No error.
NC_EBADID Not a valid ID.
NC_EBADDIM Invalid dimension ID or name.

Example

Here is an example using nc_inq_dim() to determine the length of a dimension named lat, and the name and current maximum length of the unlimited dimension for an existing netCDF dataset named foo.nc:

1 #include <netcdf.h>
2  ...
3 int status, ncid, latid, recid;
4 size_t latlength, recs;
5 char recname[NC_MAX_NAME+1];
6  ...
7 status = nc_open("foo.nc", NC_NOWRITE, &ncid);
8 if (status != NC_NOERR) handle_error(status);
9 status = nc_inq_unlimdim(ncid, &recid);
10 if (status != NC_NOERR) handle_error(status);
11  ...
12 status = nc_inq_dimid(ncid, "lat", &latid);
13 if (status != NC_NOERR) handle_error(status);
14 status = nc_inq_dimlen(ncid, latid, &latlength);
15 if (status != NC_NOERR) handle_error(status);
16 
17 status = nc_inq_dim(ncid, recid, recname, &recs);
18 if (status != NC_NOERR) handle_error(status);

Definition at line 394 of file ddim.c.

int nc_inq_dimname ( int  ncid,
int  dimid,
char *  name 
)

Find out the name of a dimension.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
dimidDimension ID, from a previous call to nc_inq_dimid() or nc_def_dim().
nameReturned 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 NC_MAX_NAME. (This doesn't include the null terminator, so declare your array to be size NC_MAX_NAME+1). The returned character array will be null-terminated. Ignored if NULL.
Returns
NC_NOERR No error.
NC_EBADID Not a valid ID.
NC_EBADDIM Invalid dimension ID or name.

Example

Here is an example using nc_inq_dim() to determine the length of a dimension named lat, and the name and current maximum length of the unlimited dimension for an existing netCDF dataset named foo.nc:

1 #include <netcdf.h>
2  ...
3 int status, ncid, latid, recid;
4 size_t latlength, recs;
5 char recname[NC_MAX_NAME+1];
6  ...
7 status = nc_open("foo.nc", NC_NOWRITE, &ncid);
8 if (status != NC_NOERR) handle_error(status);
9 status = nc_inq_unlimdim(ncid, &recid);
10 if (status != NC_NOERR) handle_error(status);
11  ...
12 status = nc_inq_dimid(ncid, "lat", &latid);
13 if (status != NC_NOERR) handle_error(status);
14 status = nc_inq_dimlen(ncid, latid, &latlength);
15 if (status != NC_NOERR) handle_error(status);
16 
17 status = nc_inq_dim(ncid, recid, recname, &recs);
18 if (status != NC_NOERR) handle_error(status);

Definition at line 338 of file ddim.c.

int nc_inq_ndims ( int  ncid,
int *  ndimsp 
)

Find the number of dimensions.

In a classic model netCDF file, this funtion returns the number of defined dimensions. In a netCDF-4/HDF5 file, this function returns the number of dimensions available in the group specified by ncid, which may be less than the total number of dimensions in a file. In a netCDF-4/HDF5 file, dimensions are in all sub-groups, sub-sub-groups, etc.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
ndimspPointer where number of dimensions will be written. Ignored if NULL.
Returns
NC_NOERR No error.
NC_EBADID Not a valid ID.

Definition at line 250 of file ddim.c.

int nc_inq_unlimdim ( int  ncid,
int *  unlimdimidp 
)

Find the ID of the unlimited dimension.

This function finds the ID of the unlimited dimension. For netCDF-4/HDF5 files (which may have more than one unlimited dimension), the ID of the first unlimited dimesnion is returned. For these files, nc_inq_unlimdims() will return all the unlimited dimension IDs.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
unlimdimidpPointer where unlimited dimension ID will be stored. If there is no unlimited dimension, -1 will be stored here. Ignored if NULL.
Returns
NC_NOERR No error.
NC_EBADID Not a valid ID.

Definition at line 280 of file ddim.c.

int nc_rename_dim ( int  ncid,
int  dimid,
const char *  name 
)

Rename a dimension.

This function renames an existing dimension in a netCDF dataset open for writing. You cannot rename a dimension to have the same name as another dimension.

For netCDF classic and 64-bit offset files, if the new name is longer than the old name, the netCDF dataset must be in define mode.

For netCDF-4 files the dataset is switched to define more for the rename, regardless of the name length.

Parameters
ncidNetCDF or group ID, from a previous call to nc_open(), nc_create(), nc_def_grp(), or associated inquiry functions such as nc_inq_ncid().
dimidDimension ID, from a previous call to nc_inq_dimid() or nc_def_dim().
nameNew name for dimension. Must be a null-terminated string with length less than NC_MAX_NAME.
Returns
NC_NOERR No error.
NC_EBADID Not a valid ID.
NC_EBADDIM Invalid dimension ID or name.
NC_ENAMEINUSE String match to name in use
NC_ENOMEM Memory allocation (malloc) failure
NC_EPERM Write to read only

Example

Here is an example using nc_rename_dim to rename the dimension lat to latitude in an existing netCDF dataset named foo.nc:

1 #include <netcdf.h>
2  ...
3 int status, ncid, latid;
4  ...
5 status = nc_open("foo.nc", NC_WRITE, &ncid);
6 if (status != NC_NOERR) handle_error(status);
7  ...
8 status = nc_redef(ncid);
9 if (status != NC_NOERR) handle_error(status);
10 status = nc_inq_dimid(ncid, "lat", &latid);
11 if (status != NC_NOERR) handle_error(status);
12 status = nc_rename_dim(ncid, latid, "latitude");
13 if (status != NC_NOERR) handle_error(status);
14 status = nc_enddef(ncid);
15 if (status != NC_NOERR) handle_error(status);

Definition at line 220 of file ddim.c.


Return to the Main Unidata NetCDF page.
Generated on Sun Nov 23 2014 16:20:09 for NetCDF. NetCDF is a Unidata library.