From 933b0531176f39232ec48eef2e9c9b0bc2481123 Mon Sep 17 00:00:00 2001 From: SarahAlidoost Date: Mon, 25 Nov 2024 10:50:19 +0100 Subject: [PATCH] refcator tests, replace xr load_dataset with open_dataset --- tests/test_cds_utils.py | 6 ++--- tests/test_datasets/test_era5.py | 21 +++++++++--------- tests/test_datasets/test_era5_land.py | 21 +++++++++--------- tests/test_datasets/test_eth_canopy_height.py | 22 +++++++++---------- tests/test_datasets/test_land_cover.py | 2 +- tests/test_datasets/test_prism_dem.py | 20 ++++++++--------- 6 files changed, 45 insertions(+), 47 deletions(-) diff --git a/tests/test_cds_utils.py b/tests/test_cds_utils.py index 15e7003..ec44293 100644 --- a/tests/test_cds_utils.py +++ b/tests/test_cds_utils.py @@ -189,7 +189,7 @@ def test_convert_to_zampy(dummy_dir): overwrite=True, ) - ds = xr.load_dataset(Path(dummy_dir, "era5_northward_component_of_wind_2020-1.nc")) + ds = xr.open_dataset(Path(dummy_dir, "era5_northward_component_of_wind_2020-1.nc")) assert list(ds.data_vars)[0] == "northward_component_of_wind" @@ -215,7 +215,7 @@ def test_parse_nc_file_radiation(self): "surface_solar_radiation_downwards": "ssrd", } for variable in variables: - ds_original = xr.load_dataset( + ds_original = xr.open_dataset( data_folder / "era5" / f"era5_{variable}_2020-1.nc" ) ds = cds_utils.parse_nc_file( @@ -232,7 +232,7 @@ def test_parse_nc_file_radiation(self): def test_parse_nc_file_precipitation(self): """Test parsing netcdf file function with precipitation.""" - ds_original = xr.load_dataset( + ds_original = xr.open_dataset( data_folder / "era5" / "era5_total_precipitation_2020-1.nc" ) ds = cds_utils.parse_nc_file( diff --git a/tests/test_datasets/test_era5.py b/tests/test_datasets/test_era5.py index b93d6a6..374ef44 100644 --- a/tests/test_datasets/test_era5.py +++ b/tests/test_datasets/test_era5.py @@ -90,19 +90,19 @@ def ingest_dummy_data(self, temp_dir): """Ingest dummy tif data to nc for other tests.""" era5_dataset = ERA5() era5_dataset.ingest(download_dir=data_folder, ingest_dir=Path(temp_dir)) - ds = xr.load_dataset( + + return era5_dataset + + def test_ingest(self, dummy_dir): + """Test ingest function.""" + _ = self.ingest_dummy_data(dummy_dir) + ds = xr.open_dataset( Path( - temp_dir, + dummy_dir, "era5", "era5_northward_component_of_wind_2020-1.nc", ) ) - - return ds, era5_dataset - - def test_ingest(self, dummy_dir): - """Test ingest function.""" - ds, _ = self.ingest_dummy_data(dummy_dir) assert isinstance(ds, xr.Dataset) def test_load(self, dummy_dir): @@ -111,8 +111,7 @@ def test_load(self, dummy_dir): bbox = SpatialBounds(60.0, 0.3, 59.7, 0.0) variable = ["northward_component_of_wind"] - era5_dataset = ERA5() - era5_dataset.ingest(download_dir=data_folder, ingest_dir=Path(dummy_dir)) + era5_dataset = self.ingest_dummy_data(dummy_dir) ds = era5_dataset.load( ingest_dir=Path(dummy_dir), @@ -134,6 +133,6 @@ def test_load(self, dummy_dir): def test_convert(self, dummy_dir): """Test convert function.""" - _, era5_dataset = self.ingest_dummy_data(dummy_dir) + era5_dataset = self.ingest_dummy_data(dummy_dir) era5_dataset.convert(ingest_dir=Path(dummy_dir), convention="ALMA") # TODO: finish this test when the function is complete. diff --git a/tests/test_datasets/test_era5_land.py b/tests/test_datasets/test_era5_land.py index 4f290a4..43179ce 100644 --- a/tests/test_datasets/test_era5_land.py +++ b/tests/test_datasets/test_era5_land.py @@ -88,19 +88,19 @@ def ingest_dummy_data(self, temp_dir): """Ingest dummy tif data to nc for other tests.""" era5_land_dataset = ERA5Land() era5_land_dataset.ingest(download_dir=data_folder, ingest_dir=Path(temp_dir)) - ds = xr.load_dataset( + + return era5_land_dataset + + def test_ingest(self, dummy_dir): + """Test ingest function.""" + _ = self.ingest_dummy_data(dummy_dir) + ds = xr.open_dataset( Path( - temp_dir, + dummy_dir, "era5-land", "era5-land_dewpoint_temperature_2020-1.nc", ) ) - - return ds, era5_land_dataset - - def test_ingest(self, dummy_dir): - """Test ingest function.""" - ds, _ = self.ingest_dummy_data(dummy_dir) assert isinstance(ds, xr.Dataset) def test_load(self, dummy_dir): @@ -109,8 +109,7 @@ def test_load(self, dummy_dir): bbox = SpatialBounds(60.0, 0.3, 59.7, 0.0) variable = ["dewpoint_temperature"] - era5_land_dataset = ERA5Land() - era5_land_dataset.ingest(download_dir=data_folder, ingest_dir=Path(dummy_dir)) + era5_land_dataset = self.ingest_dummy_data(dummy_dir) ds = era5_land_dataset.load( ingest_dir=Path(dummy_dir), @@ -132,6 +131,6 @@ def test_load(self, dummy_dir): def test_convert(self, dummy_dir): """Test convert function.""" - _, era5_land_dataset = self.ingest_dummy_data(dummy_dir) + era5_land_dataset = self.ingest_dummy_data(dummy_dir) era5_land_dataset.convert(ingest_dir=Path(dummy_dir), convention="ALMA") # TODO: finish this test when the function is complete. diff --git a/tests/test_datasets/test_eth_canopy_height.py b/tests/test_datasets/test_eth_canopy_height.py index 68ac518..9852906 100644 --- a/tests/test_datasets/test_eth_canopy_height.py +++ b/tests/test_datasets/test_eth_canopy_height.py @@ -57,25 +57,25 @@ def ingest_dummy_data(self, temp_dir): canopy_height_dataset.ingest( download_dir=data_folder, ingest_dir=Path(temp_dir) ) - ds = xr.load_dataset( + + return canopy_height_dataset + + def test_ingest(self, dummy_dir): + """Test ingest function.""" + _ = self.ingest_dummy_data(dummy_dir) + ds = xr.open_dataset( Path( - temp_dir, + dummy_dir, "eth-canopy-height", "ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.nc", ) ) - return ds, canopy_height_dataset - - def test_ingest(self, dummy_dir): - """Test ingest function.""" - ds, _ = self.ingest_dummy_data(dummy_dir) - assert isinstance(ds, xr.Dataset) def test_load(self, dummy_dir): """Test load function.""" - _, canopy_height_dataset = self.ingest_dummy_data(dummy_dir) + canopy_height_dataset = self.ingest_dummy_data(dummy_dir) times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-01-04")) bbox = SpatialBounds(60.0, 0.3, 59.7, 0.0) @@ -98,7 +98,7 @@ def test_load(self, dummy_dir): def test_convert(self, dummy_dir): """Test convert function.""" - _, canopy_height_dataset = self.ingest_dummy_data(dummy_dir) + canopy_height_dataset = self.ingest_dummy_data(dummy_dir) canopy_height_dataset.convert(ingest_dir=Path(dummy_dir), convention="ALMA") # TODO: finish this test when the function is complete. @@ -168,7 +168,7 @@ def test_convert_tiff_to_netcdf(dummy_dir): file=dummy_data, ) - ds = xr.load_dataset( + ds = xr.open_dataset( Path(dummy_dir, "ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.nc") ) assert isinstance(ds, xr.Dataset) diff --git a/tests/test_datasets/test_land_cover.py b/tests/test_datasets/test_land_cover.py index 38e4f43..8876251 100644 --- a/tests/test_datasets/test_land_cover.py +++ b/tests/test_datasets/test_land_cover.py @@ -79,7 +79,7 @@ def ingest_dummy_data(self, temp_dir): """Ingest dummy zip data to nc for other tests.""" land_cover_dataset = LandCover() land_cover_dataset.ingest(download_dir=data_folder, ingest_dir=Path(temp_dir)) - ds = xr.load_dataset( + ds = xr.open_dataset( Path( temp_dir, "land-cover", diff --git a/tests/test_datasets/test_prism_dem.py b/tests/test_datasets/test_prism_dem.py index 7993dfb..0b07b74 100644 --- a/tests/test_datasets/test_prism_dem.py +++ b/tests/test_datasets/test_prism_dem.py @@ -55,25 +55,25 @@ def ingest_dummy_data(self, temp_dir): """Ingest dummy tif data to nc for other tests.""" prism_dem_dataset = prism_dem.PrismDEM90() prism_dem_dataset.ingest(download_dir=data_folder, ingest_dir=Path(temp_dir)) - ds = xr.load_dataset( + + return prism_dem_dataset + + def test_ingest(self, dummy_dir): + """Test ingest function.""" + _ = self.ingest_dummy_data(dummy_dir) + ds = xr.open_dataset( Path( - temp_dir, + dummy_dir, "prism-dem-90", "Copernicus_DSM_30_N50_00_E000_00.nc", ) ) - return ds, prism_dem_dataset - - def test_ingest(self, dummy_dir): - """Test ingest function.""" - ds, _ = self.ingest_dummy_data(dummy_dir) - assert isinstance(ds, xr.Dataset) def test_load(self, dummy_dir): """Test load function.""" - _, prism_dem_dataset = self.ingest_dummy_data(dummy_dir) + prism_dem_dataset = self.ingest_dummy_data(dummy_dir) times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-01-04")) bbox = SpatialBounds(60.0, 0.3, 59.7, 0.0) @@ -96,6 +96,6 @@ def test_load(self, dummy_dir): def test_convert(self, dummy_dir): """Test convert function.""" - _, prism_dem_dataset = self.ingest_dummy_data(dummy_dir) + prism_dem_dataset = self.ingest_dummy_data(dummy_dir) prism_dem_dataset.convert(ingest_dir=Path(dummy_dir), convention="ALMA") # TODO: finish this test when the function is complete.