MCPcopy
hub / github.com/pydata/xarray / test_mask_and_scale

Method test_mask_and_scale

xarray/tests/test_backends.py:2075–2099  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2073 assert_equal(ds, actual)
2074
2075 def test_mask_and_scale(self) -> None:
2076 with create_tmp_file() as tmp_file:
2077 with nc4.Dataset(tmp_file, mode="w") as nc:
2078 nc.createDimension("t", 5)
2079 nc.createVariable("x", "int16", ("t",), fill_value=-1)
2080 v = nc.variables["x"]
2081 v.set_auto_maskandscale(False)
2082 v.add_offset = 10
2083 v.scale_factor = 0.1
2084 v[:] = np.array([-1, -1, 0, 1, 2])
2085 dtype = type(v.scale_factor)
2086
2087 # first make sure netCDF4 reads the masked and scaled data
2088 # correctly
2089 with nc4.Dataset(tmp_file, mode="r") as nc:
2090 expected = np.ma.array(
2091 [-1, -1, 10, 10.1, 10.2], mask=[True, True, False, False, False]
2092 )
2093 actual = nc.variables["x"][:]
2094 assert_array_equal(expected, actual)
2095
2096 # now check xarray
2097 with open_dataset(tmp_file) as ds:
2098 expected_ds = create_masked_and_scaled_data(np.dtype(dtype))
2099 assert_identical(expected_ds, ds)
2100
2101 def test_0dimensional_variable(self) -> None:
2102 # This fix verifies our work-around to this netCDF4-python bug:

Callers

nothing calls this directly

Calls 6

open_datasetFunction · 0.90
assert_identicalFunction · 0.90
create_tmp_fileFunction · 0.85
typeFunction · 0.85
dtypeMethod · 0.45

Tested by

no test coverage detected