[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDF #JCY-709704]: Names of variables in netcdf file

This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.


  • Subject: [netCDF #JCY-709704]: Names of variables in netcdf file
  • Date: Sun, 03 Dec 2017 13:03:11 -0700

I assume that your file has no subgroups.

1. Get the list of variable ids
   int status;
   int nvars;
   int* varids;
   /* Get # of variables */
   status = nc_inq_varids(ncid, &nvars, NULL);
   if(nvars == 0) return status;
   varids = malloc(sizeof(int)*nvars);
   if(varids == NULL) {status = NC_ENOMEM; return status;}
   /* repeat to get the variable ids */
   status = nc_inq_varids(ncid, &nvars, varids);

2. Iterate over the variable ids to get the names
   int i;
   for(i=0;i<nvars;i++) {
        char name[NC_MAX_NAME+1];
        status = nc_inq_varname(ncid,varids[i],name);
        if(status != NC_NOERR) return status;
        fprintf("varid=%d varname=%s\n",varids[i],name);
   }


> Full Name: Ilkka Karasalo
> Email Address: address@hidden
> Organization: KTH
> Package Version: 4.5.1
> Operating System: Ubuntu 15.04
> Hardware:
> Description of problem:
> 
> How do I get a list of the names of all variables in an unknown netcdf
> file using the inq-... functions in the C interface (nc_open, nc_inq,
> etc) ?
> 
> 

=Dennis Heimbigner
  Unidata


Ticket Details
===================
Ticket ID: JCY-709704
Department: Support netCDF
Priority: Normal
Status: Open
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.