Skip to content

Commit 6928035

Browse files
EddyCMWFecmwf-cobarzan
authored andcommitted
Cds 265 url area selector too small selection (#194)
* handle too small area selection
1 parent 59e6144 commit 6928035

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

cads_adaptors/tools/area_selector.py

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from earthkit.transforms import tools as eka_tools
88

99
from cads_adaptors.adaptors import Context
10+
from cads_adaptors.exceptions import InvalidRequest
1011

1112

1213
def incompatible_area_error(
@@ -182,6 +183,17 @@ def area_selector(
182183

183184
ds_area = xr.concat(sub_selections, dim=lon_key)
184185
context.logger.debug(f"ds_area: {ds_area}")
186+
187+
# Ensure that there are no length zero dimensions
188+
for dim in [lat_key, lon_key]:
189+
if len(ds_area[dim]) == 0:
190+
message = (
191+
f"Area selection resulted in a dataset with zero length dimension for: {dim}.\n"
192+
"Please ensure that your area selection covers at least one point in the data."
193+
)
194+
context.add_user_visible_error(message)
195+
raise InvalidRequest(message)
196+
185197
return ds_area
186198

187199
else:

tests/test_30_area_selector.py

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
import xarray as xr
77

8+
from cads_adaptors.exceptions import InvalidRequest
89
from cads_adaptors.tools.area_selector import (
910
area_selector,
1011
get_dim_slices,
@@ -215,6 +216,14 @@ def test_area_selector_regular_partially_oob_lats(self):
215216
np.allclose(result.longitude.values, test_result.longitude.values)
216217
)
217218

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

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

0 commit comments

Comments
 (0)