Skip to content

Commit

Permalink
Refactor and test h5netcdf scalar reading
Browse files Browse the repository at this point in the history
  • Loading branch information
lahtinep committed Mar 8, 2024
1 parent c6cd647 commit 12854de
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
11 changes: 6 additions & 5 deletions satpy/readers/netcdf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,15 @@ def get_data_as_xarray(variable):
try:
attrs = variable.attrs
except AttributeError:
# netCDF4 backend requires usage of __dict__ to get the attributes
attrs = variable.__dict__
try:
arr = xr.DataArray(
variable[:], dims=variable.dimensions, attrs=attrs, name=variable.name)
except ValueError:
data = variable[:]
except (ValueError, IndexError):
# Handle scalars for h5netcdf backend
arr = xr.DataArray(
variable.__array__(), dims=variable.dimensions, attrs=attrs, name=variable.name)
data = variable.__array__()

arr = xr.DataArray(data, dims=variable.dimensions, attrs=attrs, name=variable.name)

return arr

Expand Down
47 changes: 42 additions & 5 deletions satpy/tests/reader_tests/test_netcdf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,31 @@ def test_get_data_as_xarray_netcdf4(tmp_path):
assert res.attrs == NC_ATTRS


def test_get_data_as_xarray_scalar_netcdf4(tmp_path):
"""Test getting scalar xr.DataArray from netcdf4 variable."""
import numpy as np

from satpy.readers.netcdf_utils import get_data_as_xarray

data = 1
fname = tmp_path / "test.nc"
dset = _write_test_netcdf4(fname, data)

res = get_data_as_xarray(dset["test_data"])
np.testing.assert_equal(res.data, np.array(data))
assert res.attrs == NC_ATTRS


def _write_test_netcdf4(fname, data):
import netCDF4 as nc

dset = nc.Dataset(fname, "w")
dset.createDimension("y", None)
var = dset.createVariable("test_data", "uint8", ("y",))
try:
dset.createDimension("y", data.size)
dims = ("y",)
except AttributeError:
dims = ()
var = dset.createVariable("test_data", "uint8", dims)
var[:] = data
var.setncatts(NC_ATTRS)
# Turn off automatic scale factor and offset handling
Expand Down Expand Up @@ -348,10 +367,28 @@ def _write_test_h5netcdf(fname, data):
import h5netcdf

fid = h5netcdf.File(fname, "w")
fid.dimensions = {"y": data.size}
var = fid.create_variable("test_data", ("y",), "uint8")
var[:] = data
try:
fid.dimensions = {"y": data.size}
dims = ("y",)
except AttributeError:
dims = ()
var = fid.create_variable("test_data", dims, "uint8", data=data)
for key in NC_ATTRS:
var.attrs[key] = NC_ATTRS[key]

return fid


def test_get_data_as_xarray_scalar_h5netcdf(tmp_path):
"""Test getting xr.DataArray from h5netcdf variable."""
import numpy as np

from satpy.readers.netcdf_utils import get_data_as_xarray

data = 1
fname = tmp_path / "test.nc"
fid = _write_test_h5netcdf(fname, data)

res = get_data_as_xarray(fid["test_data"])
np.testing.assert_equal(res.data, np.array(data))
assert res.attrs == NC_ATTRS

0 comments on commit 12854de

Please sign in to comment.