From 5f3aa34cf62a8a4fd6faefdbc469d656fdd822d4 Mon Sep 17 00:00:00 2001 From: Abel Aoun Date: Tue, 11 Jul 2023 15:02:38 +0200 Subject: [PATCH 1/2] FIX: Avoid resampling for SPI indices This fixes #273 --- icclim/main.py | 13 ++++++++++++- icclim/tests/test_spi6.py | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 icclim/tests/test_spi6.py diff --git a/icclim/main.py b/icclim/main.py index a92ec938..1e5f98c0 100644 --- a/icclim/main.py +++ b/icclim/main.py @@ -21,6 +21,7 @@ from xarray.core.dataset import Dataset from icclim.ecad.ecad_indices import EcadIndexRegistry +from icclim.ecad.xclim_binding import XCLIM_BINDING from icclim.generic_indices.generic_indicators import ( GenericIndicator, GenericIndicatorRegistry, @@ -569,7 +570,17 @@ def _compute_climate_index( else: result_da = result_da.rename(climate_index.name) result_da.attrs[UNITS_KEY] = _get_unit(config.out_unit, result_da) - if config.frequency.post_processing is not None and "time" in result_da.dims: + if ( + config.frequency.post_processing is not None + and "time" in result_da.dims + and not isinstance( + climate_index, + ( + XCLIM_BINDING.StandardizedPrecipitationIndex6, + XCLIM_BINDING.StandardizedPrecipitationIndex3, + ), + ) + ): resampled_da, time_bounds = config.frequency.post_processing(result_da) result_ds = resampled_da.to_dataset() if time_bounds is not None: diff --git a/icclim/tests/test_spi6.py b/icclim/tests/test_spi6.py new file mode 100644 index 00000000..4ca1e585 --- /dev/null +++ b/icclim/tests/test_spi6.py @@ -0,0 +1,14 @@ +import xarray as xr + +import icclim + + +def test_spi6(): + dataset = xr.open_dataset("./icclim/pr.nc") + dataset = icclim.index( + index_name="spi6", + in_files=dataset, + var_name="pr", + base_period_time_range=["2015-01-01", "2015-06-01"], + ).load() + assert "time_bounds" not in dataset.coords From 5013b2cada47f9a31bad27edd96dd60a96b84748 Mon Sep 17 00:00:00 2001 From: Abel Aoun Date: Tue, 5 Dec 2023 10:10:05 +0100 Subject: [PATCH 2/2] Fix test --- icclim/tests/test_input_parsing.py | 8 ++++---- icclim/tests/test_main.py | 9 +++++++++ icclim/tests/test_spi6.py | 14 -------------- 3 files changed, 13 insertions(+), 18 deletions(-) delete mode 100644 icclim/tests/test_spi6.py diff --git a/icclim/tests/test_input_parsing.py b/icclim/tests/test_input_parsing.py index 424dcc6f..4a9a66b6 100644 --- a/icclim/tests/test_input_parsing.py +++ b/icclim/tests/test_input_parsing.py @@ -108,7 +108,7 @@ def test_read_dataset_xr_ds__simple(self): # THEN xr.testing.assert_equal(ds_res.pouet, ds.pouet) - def test_read_dataset_netcdf_success(self): + def test_read_dataset__netcdf_success(self): # GIVEN ds = xr.Dataset({"pouet": self.pr_da}) ds.to_netcdf(self.OUTPUT_NC_FILE) @@ -117,7 +117,7 @@ def test_read_dataset_netcdf_success(self): # THEN xr.testing.assert_equal(ds_res.pouet, ds.pouet) - def test_read_dataset_multi_netcdf_success(self): + def test_read_dataset__multi_netcdf_success(self): # GIVEN ds = xr.Dataset({"pouet": self.pr_da}) ds.to_netcdf(self.OUTPUT_NC_FILE) @@ -128,7 +128,7 @@ def test_read_dataset_multi_netcdf_success(self): xr.testing.assert_equal(ds_res.pouet, ds.pouet) xr.testing.assert_equal(ds_res.patapouet, ds.pouet) - def test_read_dataset_zarr_store_success(self): + def test_read_dataset__zarr_store_success(self): # GIVEN ds = xr.Dataset({"pouet": self.pr_da}) ds.to_zarr(self.OUTPUT_ZARR_STORE) @@ -137,7 +137,7 @@ def test_read_dataset_zarr_store_success(self): # THEN xr.testing.assert_equal(ds_res.pouet, ds.pouet) - def test_read_dataset_not_implemented_error(self): + def test_read_dataset__not_implemented_error(self): # THEN with pytest.raises(NotImplementedError): # WHEN diff --git a/icclim/tests/test_main.py b/icclim/tests/test_main.py index 3e712087..3ee2e770 100644 --- a/icclim/tests/test_main.py +++ b/icclim/tests/test_main.py @@ -400,6 +400,15 @@ def test_indices_all_from_Dataset__seasonal_SPI_error(self): base_period_time_range=("2042-01-01", "2042-12-31"), ) + def test_spi6__no_time_bounds(self): + dataset = icclim.index( + index_name="spi6", + in_files=self.full_data, + var_name="pr", + base_period_time_range=["2042-01-01", "2042-06-01"], + ).load() + assert "time_bounds" not in dataset.coords + def test_indices_all_from_Dataset__seasonal(self): res = icclim.indices( index_group="all", diff --git a/icclim/tests/test_spi6.py b/icclim/tests/test_spi6.py deleted file mode 100644 index 4ca1e585..00000000 --- a/icclim/tests/test_spi6.py +++ /dev/null @@ -1,14 +0,0 @@ -import xarray as xr - -import icclim - - -def test_spi6(): - dataset = xr.open_dataset("./icclim/pr.nc") - dataset = icclim.index( - index_name="spi6", - in_files=dataset, - var_name="pr", - base_period_time_range=["2015-01-01", "2015-06-01"], - ).load() - assert "time_bounds" not in dataset.coords