Skip to content

Commit 1de5f82

Browse files
committed
Add more area slice caching tests
1 parent 2f9df8b commit 1de5f82

2 files changed

Lines changed: 51 additions & 14 deletions

File tree

pyresample/future/geometry/_subset.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
from __future__ import annotations
33

44
import math
5+
from typing import TYPE_CHECKING, Any
56

67
import numpy as np
78

8-
from pyresample import AreaDefinition
9-
109
# this caching module imports the geometries so this subset module
1110
# must be imported inside functions in the geometry modules if needed
1211
# to avoid circular dependencies
@@ -15,6 +14,9 @@
1514
from pyresample.geometry import get_geostationary_bounding_box_in_lonlats, logger
1615
from pyresample.utils import check_slice_orientation
1716

17+
if TYPE_CHECKING:
18+
from pyresample import AreaDefinition
19+
1820

1921
@cache_to_json_if("cache_geom_slices")
2022
def get_area_slices(
@@ -23,8 +25,10 @@ def get_area_slices(
2325
shape_divisible_by: int | None,
2426
) -> tuple[slice, slice]:
2527
"""Compute the slice to read based on an `area_to_cover`."""
26-
if not isinstance(area_to_cover, AreaDefinition):
27-
raise NotImplementedError('Only AreaDefinitions can be used')
28+
if not _is_area_like(src_area):
29+
raise NotImplementedError(f"Only AreaDefinitions are supported, not {type(src_area)}")
30+
if not _is_area_like(area_to_cover):
31+
raise NotImplementedError(f"Only AreaDefinitions are supported, not {type(area_to_cover)}")
2832

2933
# Intersection only required for two different projections
3034
proj_def_to_cover = area_to_cover.crs
@@ -65,6 +69,10 @@ def get_area_slices(
6569
check_slice_orientation(y_slice))
6670

6771

72+
def _is_area_like(area_obj: Any) -> bool:
73+
return hasattr(area_obj, "crs") and hasattr(area_obj, "area_extent")
74+
75+
6876
def _get_slice_starts_stops(src_area, area_to_cover):
6977
"""Get x and y start and stop points for slicing."""
7078
llx, lly, urx, ury = area_to_cover.area_extent

pyresample/test/test_geometry/test_area.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,8 @@ def test_area_to_cover_all_nan_bounds(self, geos_src_area, create_test_area):
18231823
with pytest.raises(NotImplementedError):
18241824
area_def.get_area_slices(area_to_cover)
18251825

1826-
def test_area_slices_caching(self, create_test_area, tmp_path):
1826+
@pytest.mark.parametrize("cache_slices", [False, True])
1827+
def test_area_slices_caching(self, create_test_area, tmp_path, cache_slices):
18271828
"""Check that area slices can be cached."""
18281829
src_area = create_test_area(dict(proj="utm", zone=33),
18291830
10980, 10980,
@@ -1832,21 +1833,49 @@ def test_area_slices_caching(self, create_test_area, tmp_path):
18321833
100, 100,
18331834
(15.9689, 58.5284, 16.4346, 58.6995))
18341835
cache_glob = str(tmp_path / "geometry_slices_v1" / "*.json")
1835-
with pyresample.config.set(cache_dir=tmp_path, cache_geom_slices=False):
1836+
with pyresample.config.set(cache_dir=tmp_path, cache_geom_slices=cache_slices):
18361837
assert len(glob(cache_glob)) == 0
18371838
slice_x, slice_y = src_area.get_area_slices(crop_area)
1838-
assert len(glob(cache_glob)) == 0
1839-
with pyresample.config.set(cache_dir=tmp_path, cache_geom_slices=True):
1840-
assert len(glob(cache_glob)) == 0
1841-
slice_x, slice_y = src_area.get_area_slices(crop_area)
1842-
assert len(glob(cache_glob)) == 1
1839+
assert len(glob(cache_glob)) == int(cache_slices)
18431840
assert slice_x == slice(5630, 8339)
18441841
assert slice_y == slice(9261, 10980)
18451842

1843+
if cache_slices:
1844+
from pyresample.future.geometry._subset import get_area_slices
1845+
with pyresample.config.set(cache_dir=tmp_path):
1846+
get_area_slices.cache_clear()
1847+
assert len(glob(cache_glob)) == 0
1848+
1849+
def test_area_slices_caching_no_swaths(self, tmp_path, create_test_area, create_test_swath):
1850+
"""Test that swath inputs produce a warning when tried to use in caching."""
1851+
from pyresample.future.geometry._subset import get_area_slices
1852+
from pyresample.test.utils import create_test_latitude, create_test_longitude
1853+
area = create_test_area(dict(proj="utm", zone=33),
1854+
10980, 10980,
1855+
(499980.0, 6490200.0, 609780.0, 6600000.0))
1856+
lons = create_test_longitude(-95.0, -75.0, shape=(1000, 500))
1857+
lats = create_test_latitude(25.0, 35.0, shape=(1000, 500))
1858+
swath = create_test_swath(lons, lats)
1859+
1860+
with pyresample.config.set(cache_dir=tmp_path, cache_geom_slices=True), pytest.raises(NotImplementedError):
1861+
with pytest.warns(UserWarning, match="unhashable"):
1862+
get_area_slices(swath, area, None)
1863+
1864+
@pytest.mark.parametrize("swath_as_src", [False, True])
1865+
def test_unsupported_slice_inputs(self, create_test_area, create_test_swath, swath_as_src):
1866+
"""Test that swath inputs produce an error."""
18461867
from pyresample.future.geometry._subset import get_area_slices
1847-
with pyresample.config.set(cache_dir=tmp_path):
1848-
get_area_slices.cache_clear()
1849-
assert len(glob(cache_glob)) == 0
1868+
from pyresample.test.utils import create_test_latitude, create_test_longitude
1869+
area = create_test_area(dict(proj="utm", zone=33),
1870+
10980, 10980,
1871+
(499980.0, 6490200.0, 609780.0, 6600000.0))
1872+
lons = create_test_longitude(-95.0, -75.0, shape=(1000, 500))
1873+
lats = create_test_latitude(25.0, 35.0, shape=(1000, 500))
1874+
swath = create_test_swath(lons, lats)
1875+
1876+
with pytest.raises(NotImplementedError):
1877+
args = (swath, area) if swath_as_src else (area, swath)
1878+
get_area_slices(*args, None)
18501879

18511880

18521881
class TestBoundary:

0 commit comments

Comments
 (0)