Reputation: 705
My NetCDF file has a nested structure, how do i access a nested group or variable?
from netCDF4 import Dataset
source_dataset = Dataset('/path/to/file.nc')
source_geo_group = source_dataset.groups['/PRODUCT/SUPPORT_DATA/GEOLOCATIONS/']
This will throw the following error:
"KeyError: '/PRODUCT/SUPPORT_DATA/GEOLOCATIONS/'"
My goal is to get the values of the variables in the nested group.
Upvotes: 0
Views: 4135
Reputation: 91
That works for me:
import netCDF4 as nc
fn = '/path/to/file.nc'
ds = nc.Dataset(fn)
print('\n --> READ GROUPS')
print(ds.groups)
print('\n --> GET GROUP')
print(ds.groups['GROUP_NAME'])
print('\n --> READ GROUPS VARIABLES')
print(ds.groups['GROUP_NAME'].variables['VARIABLE_NAME'])
print('\n --> GET VARIABLE VALUE')
print(ds.groups['GROUP_NAME'].variables['VARIABLE_NAME'][:])
Upvotes: 0
Reputation: 705
The syntax for getting nested groups is:
source_dataset = Dataset('/path/to/file.nc')
source_geo_group = source_dataset['/PRODUCT/SUPPORT_DATA/GEOLOCATIONS/']
Upvotes: 1