Go to the previous, next section.

Rename a Variable

The function ncvarrename (or NCVREN for FORTRAN) changes the name of a netCDF variable in an open netCDF. If the new name is longer than the old name, the netCDF must be in define mode. You cannot rename a variable to have the name of any existing variable.

In case of an error, ncvarrename returns -1; NCVREN returns a nonzero value in rcode. Possible causes of errors include:

ncvarrename: C Interface

int ncvarrename(int ncid, int varid, const char* name);

ncid
NetCDF ID, returned from a previous call to ncopen or nccreate.

varid
Variable ID, returned from a previous call to ncvardef or ncvarid.

name
New name for the specified variable.

Here is an example using ncvarrename to rename the variable rh to rel_hum in an existing netCDF file named `foo.nc':

#include "netcdf.h"
   ...
int  ncid;                         /* netCDF ID */
int  rh_id;                        /* variable ID */
   ...
ncid = ncopen("foo.nc", NC_WRITE);
   ...
ncredef(ncid);  /* put in define mode to rename variable */
rh_id = ncvarid (ncid, "rh");
ncvarrename (ncid, rh_id, "rel_hum");
ncendef(ncid);  /* leave define mode */

NCVREN: FORTRAN Interface

      SUBROUTINE NCVREN (INTEGER NCID, INTEGER VARID,
     +                   CHARACTER*(*) NEWNAM, INTEGER RCODE)

NCID
NetCDF ID, returned from a previous call to NCOPN or NCCRE.

VARID
Variable ID, returned from a previous call to NCVDEF or NCVID.

NEWNAM
New name for the specified variable.

Here is an example using NCVREN to rename the variable rh to rel_hum in an existing netCDF file named `foo.nc':

      INCLUDE 'netcdf.inc'
         ...
      INTEGER  NCID, RCODE
      INTEGER  RHID               ! variable ID
         ...
      NCID = NCOPN ('foo.nc', NCWRITE, RCODE)
         ...
      CALL NCREDF (CDFFID, RCODE)  ! enter definition mode
      RHID = NCVID (NCID, 'rh', RCODE)  ! get ID
      CALL NCVREN (NCID, RHID, 'rel_hum', RCODE)
      CALL NCENDF (CDFFID, RCODE)  ! leave definition mode

Go to the previous, next section.