Go to the previous, next section.
The function ncattcopy (or NCACPY for FORTRAN) copies an
attribute from one open netCDF file to another. It can also be used to
copy an attribute from one variable to another within the same netCDF.
In case of an error, ncattcopy returns -1; NCACPY returns a
nonzero value in rcode. Possible causes of errors include:
int ncattcopy(int incdf, int invar, const char* name, int outcdf, int outvar);
incdf
ncopen or nccreate.
invar
NC_GLOBAL for a global attribute.
name
outcdf
ncopen or nccreate. It
is permissible for the input and output netCDF IDs to be the same. The
output netCDF file 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.
outvar
NC_GLOBAL to copy to a global attribute.
Here is an example using ncattcopy to copy the variable attribute
units from the variable rh in an existing netCDF file
named `foo.nc' to the variable avgrh in another existing
netCDF file named `bar.nc', assuming that the variable
avgrh already exists, but does not yet have a units
attribute:
#include "netcdf.h"
...
int ncid1, ncid2; /* netCDF IDs */
int rh_id, avgrh_id; /* variable IDs */
...
ncid1 = ncopen("foo.nc", NC_NOWRITE);
ncid2 = ncopen("bar.nc", NC_WRITE);
...
rh_id = ncvarid (ncid1, "rh");
avgrh_id = ncvarid (ncid2, "avgrh");
...
ncredef(ncid2); /* enter define mode */
/* copy variable attribute from "rh" to "avgrh" */
ncattcopy(ncid1, rh_id, "units", ncid2, avgrh_id);
...
ncendef(ncid2); /* leave define mode */
SUBROUTINE NCACPY (INTEGER INCDF, INTEGER INVAR,
+ CHARACTER*(*) ATTNAM, INTEGER OUTCDF,
+ INTEGER OUTVAR, INTEGER RCODE)
INCDF
NCOPN or NCCRE.
INVAR
NCGLOBAL for a global attribute.
ATTNAM
OUTCDF
NCOPN or NCCRE. It
is permissible for the input and output netCDF IDs to be the same. The
output netCDF file 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.
OUTVAR
NCGLOBAL to copy to a global
attribute.
Here is an example using NCACPY to copy the variable attribute
units from the variable rh in an existing netCDF file
named `foo.nc' to the variable avgrh in another existing
netCDF file named `bar.nc', assuming that the variable
avgrh already exists, but does not yet have a units
attribute:
INCLUDE 'netcdf.inc'
...
INTEGER NCID1, NCID2 ! netCDF IDs
INTEGER RHID, AVRHID ! variable IDs
...
NCID1 = NCOPN ('foo.nc', NCNOWRIT, RCODE)
NCID2 = NCOPN ('bar.nc', NCWRITE, RCODE)
...
RHID = NCVID (NCID1, 'rh', RCODE)
AVRHID = NCVID (NCID2, 'avgrh', RCODE)
...
CALL NCREDF (NCID2, RCODE) ! enter define mode
* copy variable attribute from "rh" to "avgrh"
CALL NCACPY (NCID1, RHID, 'units', NCID2, AVRHID, RCODE)
...
CALL NCENDF (NCID2, RCODE) ! leave define mode
Go to the previous, next section.