7.6 Copy Attribute from One NetCDF to Another: NF90_COPY_ATT
The function NF90_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 dataset.
Usage
function nf90_copy_att(ncid_in, varid_in, name, ncid_out, varid_out)
integer, intent( in) :: ncid_in, varid_in
character (len = *), intent( in) :: name
integer, intent( in) :: ncid_out, varid_out
integer :: nf90_copy_att
ncid_in- The netCDF ID of an input netCDF dataset from which the attribute
will be copied, from a previous call to NF90_OPEN or NF90_CREATE.
varid_in- ID of the variable in the input netCDF dataset from which the
attribute will be copied, or NF90_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 NF90_OPEN or NF90_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 NF90_GLOBAL to copy to a global attribute.
Errors
NF90_COPY_ATT returns the value NF90_NOERR if no errors
occurred. Otherwise, the returned status indicates an error. Possible
causes of errors include:
- The input or output variable ID is invalid for the specified netCDF
dataset.
- The specified attribute does not exist.
- The output netCDF is not in define mode and the attribute is new for
the output dataset is larger than the existing attribute.
- The input or output netCDF ID does not refer to an open netCDF
dataset.
Example
Here is an example using NF90_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:
use netcdf
implicit none
integer :: ncid1, ncid2, status
integer :: RHVarID, avgRHVarID ! Variable ID
...
status = nf90_open("foo.nc", nf90_nowrite, ncid1)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_open("bar.nc", nf90_write, ncid2)
if (status /= nf90_noerr) call handle_err(status)
...
! Find the IDs of the variables
status = nf90_inq_varid(ncid1, "rh", RHVarID)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_varid(ncid1, "avgrh", avgRHVarID)
if (status /= nf90_noerr) call handle_err(status)
...
status = nf90_redef(ncid2) ! Enter define mode
if (status /= nf90_noerr) call handle_err(status)
! Copy variable attribute from "rh" in file 1 to "avgrh" in file 1
status = nf90_copy_att(ncid1, RHVarID, "units", ncid2, avgRHVarID)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_enddef(ncid2)
if (status /= nf90_noerr) call handle_err(status)