Skip to content

Commit

Permalink
refcator tests, replace xr load_dataset with open_dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
SarahAlidoost committed Nov 25, 2024
1 parent ea34a42 commit 933b053
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 47 deletions.
6 changes: 3 additions & 3 deletions tests/test_cds_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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(
Expand All @@ -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(
Expand Down
21 changes: 10 additions & 11 deletions tests/test_datasets/test_era5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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),
Expand All @@ -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.
21 changes: 10 additions & 11 deletions tests/test_datasets/test_era5_land.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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),
Expand All @@ -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.
22 changes: 11 additions & 11 deletions tests/test_datasets/test_eth_canopy_height.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.

Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tests/test_datasets/test_land_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 10 additions & 10 deletions tests/test_datasets/test_prism_dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.

0 comments on commit 933b053

Please sign in to comment.