0

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.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Ivana
  • 643
  • 10
  • 27

2 Answers2

1

The syntax for getting nested groups is:

source_dataset = Dataset('/path/to/file.nc')
source_geo_group = source_dataset['/PRODUCT/SUPPORT_DATA/GEOLOCATIONS/']
Ivana
  • 643
  • 10
  • 27
0

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'][:])