From 21ec8e85d37e3c60bdb33e7e6eccfa8b44c14bf2 Mon Sep 17 00:00:00 2001 From: Iain Russell Date: Tue, 17 Dec 2024 16:13:26 +0000 Subject: [PATCH] Add values_dtype backend option to load values at full precision - add tests --- tests/test_30_dataset.py | 14 ++++++++++++++ tests/test_50_xarray_plugin.py | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/test_30_dataset.py b/tests/test_30_dataset.py index 9914e482..ae9d22f9 100644 --- a/tests/test_30_dataset.py +++ b/tests/test_30_dataset.py @@ -380,3 +380,17 @@ def test_missing_field_values() -> None: t2 = res.variables["t2m"] assert np.isclose(np.nanmean(t2.data[0, :, :]), 268.375) assert np.isclose(np.nanmean(t2.data[1, :, :]), 270.716) + + +def test_default_values_dtype() -> None: + res = dataset.open_file(TEST_DATA_MISSING_VALS) + assert res.variables["t2m"].data.dtype == np.dtype("float32") + assert res.variables["latitude"].data.dtype == np.dtype("float64") + assert res.variables["longitude"].data.dtype == np.dtype("float64") + + +def test_float64_values_dtype() -> None: + res = dataset.open_file(TEST_DATA_MISSING_VALS, values_dtype=np.dtype("float64")) + assert res.variables["t2m"].data.dtype == np.dtype("float64") + assert res.variables["latitude"].data.dtype == np.dtype("float64") + assert res.variables["longitude"].data.dtype == np.dtype("float64") diff --git a/tests/test_50_xarray_plugin.py b/tests/test_50_xarray_plugin.py index 2009198a..3a9ecc6f 100644 --- a/tests/test_50_xarray_plugin.py +++ b/tests/test_50_xarray_plugin.py @@ -175,4 +175,18 @@ def test_xr_open_dataset_coords_to_attributes() -> None: assert "depthBelowLandLayer" not in ds.coords assert "GRIB_surface" in ds["t2m"].attrs - assert "GRIB_depthBelowLandLayer" in ds["stl1"].attrs \ No newline at end of file + assert "GRIB_depthBelowLandLayer" in ds["stl1"].attrs + + +def test_xr_open_dataset_default_values_dtype() -> None: + ds = xr.open_dataset(TEST_DATA_MISSING_VALS, engine="cfgrib") + assert ds["t2m"].data.dtype == np.dtype("float32") + assert ds["latitude"].data.dtype == np.dtype("float64") + assert ds["longitude"].data.dtype == np.dtype("float64") + + +def test_xr_open_dataset_float64_values_dtype() -> None: + ds = xr.open_dataset(TEST_DATA_MISSING_VALS, engine="cfgrib", values_dtype=np.dtype("float64")) + assert ds["t2m"].data.dtype == np.dtype("float64") + assert ds["latitude"].data.dtype == np.dtype("float64") + assert ds["longitude"].data.dtype == np.dtype("float64")