Next: , Previous: nc_get_att_ type, Up: Attributes


5.5 Copy Attribute from One NetCDF to Another: nc_copy_att

The function nc_copy_att copies an attribute from one open netCDF dataset to another. It can also be used to copy an attribute from one variable to another within the same netCDF.

Usage

     int nc_copy_att (int ncid_in, int varid_in, const char *name,
                      int ncid_out, int varid_out);
ncid_in
The netCDF ID of an input netCDF dataset from which the attribute will be copied, from a previous call to nc_open or nc_create.
varid_in
ID of the variable in the input netCDF dataset from which the attribute will be copied, or NC_GLOBAL for a global attribute.
name
Name of the attribute in the input netCDF dataset to be copied.
ncid_out
The netCDF ID of the output netCDF dataset to which the attribute will be copied, from a previous call to nc_open or nc_create. It is permissible for the input and output netCDF IDs to be the same. The output netCDF dataset should be in define mode if the attribute to be copied does not already exist for the target variable, or if it would cause an existing target attribute to grow.
varid_out
ID of the variable in the output netCDF dataset to which the attribute will be copied, or NC_GLOBAL to copy to a global attribute.

Errors

nc_copy_att returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using nc_copy_att to copy the variable attribute units from the variable rh in an existing netCDF dataset named foo.nc to the variable avgrh in another existing netCDF dataset named bar.nc, assuming that the variable avgrh already exists, but does not yet have a units attribute:

     #include <netcdf.h>
        ...
     int  status;               /* error status */
     int  ncid1, ncid2;         /* netCDF IDs */
     int  rh_id, avgrh_id;      /* variable IDs */
        ...
     status = nc_open("foo.nc", NC_NOWRITE, ncid1);
     if (status != NC_NOERR) handle_error(status);
     status = nc_open("bar.nc", NC_WRITE, ncid2);
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_inq_varid (ncid1, "rh", &rh_id);
     if (status != NC_NOERR) handle_error(status);
     status = nc_inq_varid (ncid2, "avgrh", &avgrh_id);
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_redef(ncid2);  /* enter define mode */
     if (status != NC_NOERR) handle_error(status);
     /* copy variable attribute from "rh" to "avgrh" */
     status = nc_copy_att(ncid1, rh_id, "units", ncid2, avgrh_id);
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_enddef(ncid2); /* leave define mode */
     if (status != NC_NOERR) handle_error(status);