Skip to content

h5netcdf fails to decode attribute coordinates. #4822

@yt87

Description

@yt87

What happened:
The engine h5netcdf fail to decode attribute coordinates.

What you expected to happen:
It should work.

Minimal Complete Verifiable Example:

# Put your MCVE code here
import xarray as xr
ds = xr.open_dataset('/tmp/x.nc', engine='h5netcdf')

========H5 coordinates ['x y']

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-481117dce7ff> in <module>
      1 import xarray as xr
      2 
----> 3 ds = xr.open_dataset('/tmp/x.nc', engine='h5netcdf')

~/miniconda3/envs/aws/lib/python3.7/site-packages/xarray/backends/api.py in open_dataset(filename_or_obj, group, decode_cf, mask_and_scale, decode_times, autoclose, concat_characters, decode_coords, engine, chunks, lock, cache, drop_variables, backend_kwargs, use_cftime, decode_timedelta)
    572 
    573     with close_on_error(store):
--> 574         ds = maybe_decode_store(store, chunks)
    575 
    576     # Ensure source filename always stored in dataset object (GH issue #2550)

~/miniconda3/envs/aws/lib/python3.7/site-packages/xarray/backends/api.py in maybe_decode_store(store, chunks)
    476             drop_variables=drop_variables,
    477             use_cftime=use_cftime,
--> 478             decode_timedelta=decode_timedelta,
    479         )
    480 

~/miniconda3/envs/aws/lib/python3.7/site-packages/xarray/conventions.py in decode_cf(obj, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables, use_cftime, decode_timedelta)
    596         drop_variables=drop_variables,
    597         use_cftime=use_cftime,
--> 598         decode_timedelta=decode_timedelta,
    599     )
    600     ds = Dataset(vars, attrs=attrs)

~/miniconda3/envs/aws/lib/python3.7/site-packages/xarray/conventions.py in decode_cf_variables(variables, attributes, concat_characters, mask_and_scale, decode_times, decode_coords, drop_variables, use_cftime, decode_timedelta)
    504             if "coordinates" in var_attrs:
    505                 coord_str = var_attrs["coordinates"]
--> 506                 var_coord_names = coord_str.split()
    507                 if all(k in variables for k in var_coord_names):
    508                     new_vars[k].encoding["coordinates"] = coord_str

AttributeError: 'numpy.ndarray' object has no attribute 'split'

Anything else we need to know?:
The test file was created from CDL:

netcdf x {
dimensions:
	x = 1 ;
	y = 1 ;
variables:
	int foo(y, x) ;
		string foo:coordinates = "x y" ;
data:

 foo =
  0 ;
}

The line ========H5 coordinates ['x y'] comes from me adding print statement on line 56 in function _read_attributes, file api/h5netcdf.py. Obviously the problem is caused by the attribute being a list instead of a string, as it is when netcdf4 is used.

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.7 (default, Mar 23 2020, 22:36:06)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 5.9.12-200.fc33.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.4
libnetcdf: 4.6.1

xarray: 0.16.2
pandas: 1.2.0
numpy: 1.19.2
scipy: 1.5.2
netCDF4: 1.4.2
pydap: None
h5netcdf: 0.8.1
h5py: 2.10.0
Nio: None
zarr: None
cftime: 1.3.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2021.01.0
distributed: 2021.01.0
matplotlib: 3.2.1
cartopy: 0.17.0
seaborn: None
numbagg: None
pint: None
setuptools: 51.1.2.post20210112
pip: 20.3.3
conda: None
pytest: 5.4.3
IPython: 7.19.0
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions