Skip to content

Commit 06ec16a

Browse files
committed
add test which checks add_offset being not conforming to cf standards
1 parent b6310e4 commit 06ec16a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

xarray/tests/test_backends.py

+40
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,46 @@ def test_mask_and_scale(self, dtype) -> None:
15841584
expected = create_masked_and_scaled_data(dtype)
15851585
assert_identical(expected, ds)
15861586

1587+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
1588+
@pytest.mark.parametrize("offset_cf_conforming", [True, False])
1589+
def test_mask_and_scale_non_cf_conforming(self, dtype, offset_cf_conforming) -> None:
1590+
with create_tmp_file() as tmp_file:
1591+
with nc4.Dataset(tmp_file, mode="w") as nc:
1592+
nc.createDimension("t", 5)
1593+
nc.createVariable("x", "int16", ("t",), fill_value=-1)
1594+
v = nc.variables["x"]
1595+
v.set_auto_maskandscale(False)
1596+
if offset_cf_conforming:
1597+
v.add_offset = dtype(10)
1598+
else:
1599+
v.add_offset = 10
1600+
v.scale_factor = dtype(0.1)
1601+
v[:] = np.array([-1, -1, 0, 1, 2])
1602+
1603+
# first make sure netCDF4 reads the masked and scaled data
1604+
# correctly
1605+
with nc4.Dataset(tmp_file, mode="r") as nc:
1606+
expected = np.ma.array(
1607+
[-1, -1, 10, 10.1, 10.2],
1608+
mask=[True, True, False, False, False],
1609+
dtype=dtype,
1610+
)
1611+
actual = nc.variables["x"][:]
1612+
assert_array_equal(expected, actual)
1613+
1614+
# now check xarray
1615+
with open_dataset(tmp_file) as ds:
1616+
if not offset_cf_conforming:
1617+
# if not conforming, this get's promoted to float64 in any case
1618+
# if add_offset is available
1619+
expected = create_masked_and_scaled_data(np.float64)
1620+
# here we have slight differences possibly
1621+
# due to using float32 first and casting to float64 later
1622+
assert_allclose(expected, ds)
1623+
else:
1624+
expected = create_masked_and_scaled_data(dtype)
1625+
assert_identical(expected, ds)
1626+
15871627
def test_0dimensional_variable(self) -> None:
15881628
# This fix verifies our work-around to this netCDF4-python bug:
15891629
# https://github.com/Unidata/netcdf4-python/pull/220

0 commit comments

Comments
 (0)