Go to the previous, next section.

Copy Attribute from One NetCDF to Another

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:

ncattcopy: C Interface

int ncattcopy(int incdf, int invar, const char* name, int outcdf, int outvar);

incdf
The netCDF ID of an input netCDF file from which the attribute will be copied, returned from a previous call to ncopen or nccreate.

invar
ID of the variable in the input netCDF file from which the attribute will be copied, or NC_GLOBAL for a global attribute.

name
Name of the attribute in the input netCDF file to be copied.

outcdf
The netCDF ID of the output netCDF file to which the attribute will be copied, returned from a previous call to 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
ID of the variable in the output netCDF file to which the attribute will be copied, or 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 */

NCACPY: FORTRAN Interface

      SUBROUTINE NCACPY (INTEGER INCDF, INTEGER INVAR,
     +                   CHARACTER*(*) ATTNAM, INTEGER OUTCDF,
     +                   INTEGER OUTVAR, INTEGER RCODE)

INCDF
The netCDF ID of an input netCDF file from which the attribute will be copied, returned from a previous call to NCOPN or NCCRE.

INVAR
ID of the variable in the input netCDF file from which the attribute will be copied, or NCGLOBAL for a global attribute.

ATTNAM
Name of the attribute in the input netCDF file to be copied.

OUTCDF
The netCDF ID of the output netCDF file to which the attribute will be copied, returned from a previous call to 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
ID of the variable in the output netCDF file to which the attribute will be copied, or 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.