I have this NetCDF file.
it has 2 groups in it! I want to select one group and get the desired variable. I tried this in multiple ways but I failed. I could use simply
print(ncfile.variables)
but it's not working!
the file structure looks like this, i want to chose group /O4/ and variable 'aerosol_optical_depth'
filename =
'output.nc'
Source:
C:\Users\IITM\Desktop\work\output.nc
Format:
netcdf4
Global Attributes:
_NCProperties = 'version=2,netcdf=4.6.3,hdf5=1.10.2'
version = 1
Dimensions:
DIM_SCAN_NAME = 3
DIM_ANGLE_NAME = 10
DIM_LAYERS = 20
Groups:
/O4/
Variables:
vertical_column_density
Size: 3x1
Dimensions: /DIM_SCAN_NAME
Datatype: double
Attributes:
_FillValue = NaN
description = 'o4 vertical column density'
units = 'molec/cm5'
aerosol_optical_depth
Size: 3x1
Dimensions: /DIM_SCAN_NAME
Datatype: single
Attributes:
_FillValue = NaN
description = 'Total aerosol optical depth'
units = '1'
/TG/
Variables:
vertical_column_density
Size: 3x1
Dimensions: /DIM_SCAN_NAME
Datatype: single
Attributes:
_FillValue = NaN
description = 'gas vertical column density'
units = 'molec/cm2'
vertical_column_density_error
Size: 3x1
Dimensions: /DIM_SCAN_NAME
Datatype: single
Attributes:
_FillValue = NaN
description = 'gas vertical column density error calculated from covariance smoothing error matrix, covariance measurement noise error matrix and systematic error as a fixed fraction of vcd'
units = 'molec/cm2'
you should be able to access the variable as follows:
from netCDF4 import Dataset
file = 'C:\Users\IITM\Desktop\work\output.nc'
with Dataset(file) as f:
O4 = f.groups['O4'] # variable O4 references to group 'O4'...
# extract everything (could also be just one variable)
data_O4 = {}
for attr in O4.ncattrs():
data_O4[attr] = O4.getncattr(attr) # put everything from the group to a dict
AOD = data_O4['aerosol_optical_depth']
or more general, to import all groups/variables to a dict
with nested dicts for each group:
nc_dct = {}
with Dataset(file) as f:
for g in f.groups:
tmp_grp = f.groups[g]
nc_dct[g] = {}
for attr in tmp_grp.ncattrs():
nc_dct[g][attr] = tmp_grp.getncattr(attr)
Note: my 'general' solution won't work properly if you have nested groups.