Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't access variables in the subgroup #2731

Closed
zxdawn opened this issue Jan 31, 2019 · 1 comment
Closed

Can't access variables in the subgroup #2731

zxdawn opened this issue Jan 31, 2019 · 1 comment

Comments

@zxdawn
Copy link

zxdawn commented Jan 31, 2019

Code Sample

import xarray as xr 
from netCDF4 import Dataset

rootgrp = Dataset("test.nc", "w", format="NETCDF4")
fcstgrp = rootgrp.createGroup("forecasts")

lat = rootgrp.createDimension("lat", 73)
lon = rootgrp.createDimension("lon", 144)
latitudes = rootgrp.createVariable("lat","f4",("lat",))
longitudes = rootgrp.createVariable("lon","f4",("lon",))

temp = rootgrp.createVariable("temp","f4",("lat","lon",))
ftemp = rootgrp.createVariable("/forecasts/temp","f4","lat","lon",)
rootgrp.close()

ds = xr.open_dataset('test.nc')
print (ds['temp'])
print (ds['/forecasts/temp'])

Problem description

The temp variable in the / group can be accessed successfully, but temp in the /forecasts group not.
It says:

KeyError: '/forecasts/temp'

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.8.final.0 python-bits: 64 OS: Linux OS-release: 4.9.0-8-amd64 machine: x86_64 processor: byteorder: little LC_ALL: None LANG: en_US.utf8 LOCALE: en_US.UTF-8

xarray: 0.11.0
pandas: 0.23.4
numpy: 1.15.4
scipy: 1.1.0
netCDF4: 1.4.2
h5netcdf: 0.5.1
h5py: 2.8.0
Nio: None
zarr: None
cftime: 1.0.0
PseudonetCDF: None
rasterio: None
iris: 2.2.0
bottleneck: 1.2.1
cyordereddict: None
dask: 0.16.1
distributed: 1.20.2
matplotlib: 3.0.2
cartopy: 0.17.0
seaborn: 0.8.1
setuptools: 38.4.0
pip: 18.1
conda: 4.6.1
pytest: 3.3.2
IPython: 6.2.1
sphinx: 1.6.6

@jhamman
Copy link
Member

jhamman commented Jan 31, 2019

@zxdawn -

Xarray datasets can only represent a single group. To open a group in a netCDF file, you would supply the group kwarg. For example:

ds = xr.open_dataset('test.nc', group='/forecasts')

There is a lot of discussion as to why this is and potential avenues for moving forward in #1092. The behavior and usage is also described in the xarray documentation here. I'm going to close this issue and encourage you to weigh in on #1092 if this issue interests you.

@jhamman jhamman closed this as completed Jan 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants