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

[netCDF #YKA-965314]: reading group attributes



Hi Kevin,

A great question! You're correct that attributes are typically associated with 
variables, but it is possible to hav `Global Attributes` associated either with 
the entire netCDF dataset, or with a group therein.  The key is that, when 
using nc_inq_att* functions, you use `NC_GLOBAL` as the `varid` parameter.  

Consider the example for `nc_inq_attname()`, found at the following 
awkwardly-named link:

https://docs.unidata.ucar.edu/netcdf-c/current/group__attributes.html#gae20821a75d209e296205c4812e91be68

At the bottom of the example, you can see a loop where we are incrementing `j` 
from 0 to a pre-defined number of attributes.  For your application, you can 
use the result of `nc_inq_natts` to figure out the number of attributes.

In the example here, we are using `ncid` because we are querying across the 
entire netCDF dataset.  If you were to replace that with the value of the group 
you are working with, then you should be able to get the name of the attribute. 
 Similarly, you can use `nc_inq_atttype` to get the attribute type, 
`nc_inq_attid` to get the attribute ID, etc.  

With the group ID and attribute ID, and the knowledge of the attribute type, 
you can use `nc_inq_att()` to query the values of the attribute.  

See 
https://docs.unidata.ucar.edu/netcdf-c/current/group__attributes.html#ga9757db7370e0e2e8c23ed1139a6ce6eb

I hope this helps!

-Ward

> Hi,
> How do you read group attributes using the NetCDF C library? All the 
> attribute functions seem oriented towards variables. I found that the 
> "nc_inq_natts" function will give the number of attributes of a group, but I 
> can't figure out how to retrieve any other information about them. I keep 
> getting an error saying "Not a valid ID" when trying to use the attribute 
> reading functions for group attributes.
> thanks,
> Kevin
> 
> Kevin Vermeesch
> MODIS Characterization Support Team (MCST)
> Global Science & Technology, Inc.
> Email: address@hidden
> Phone: (301) 867-2124
> 
> 
> 


Ticket Details
===================
Ticket ID: YKA-965314
Department: Support netCDF
Priority: Normal
Status: Closed
===================
NOTE: All email exchanges with NSF 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.