Skip to content

Commit

Permalink
Cds 265 url area selector too small selection (#194)
Browse files Browse the repository at this point in the history
* handle too small area selection
  • Loading branch information
EddyCMWF authored Aug 27, 2024
1 parent d24057d commit 8d9f428
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cads_adaptors/tools/area_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from earthkit.transforms import tools as eka_tools

from cads_adaptors.adaptors import Context
from cads_adaptors.exceptions import InvalidRequest


def incompatible_area_error(
Expand Down Expand Up @@ -182,6 +183,17 @@ def area_selector(

ds_area = xr.concat(sub_selections, dim=lon_key)
context.logger.debug(f"ds_area: {ds_area}")

# Ensure that there are no length zero dimensions
for dim in [lat_key, lon_key]:
if len(ds_area[dim]) == 0:
message = (
f"Area selection resulted in a dataset with zero length dimension for: {dim}.\n"
"Please ensure that your area selection covers at least one point in the data."
)
context.add_user_visible_error(message)
raise InvalidRequest(message)

return ds_area

else:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_30_area_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
import xarray as xr

from cads_adaptors.exceptions import InvalidRequest
from cads_adaptors.tools.area_selector import (
area_selector,
get_dim_slices,
Expand Down Expand Up @@ -215,6 +216,14 @@ def test_area_selector_regular_partially_oob_lats(self):
np.allclose(result.longitude.values, test_result.longitude.values)
)

# Test that InvalidRequest exception raised when a dimension has zero length
def test_area_selector_zero_length_dim(self):
with tempfile.TemporaryDirectory() as temp_dir:
test_file = os.path.join(temp_dir, self.input_file)
self.ds.to_netcdf(test_file)
with self.assertRaises(InvalidRequest):
area_selector(test_file, area=[50.4, -10.6, 50.3, -10.5])


if __name__ == "__main__":
unittest.main()

0 comments on commit 8d9f428

Please sign in to comment.