Skip to content

Commit fc562fd

Browse files
committed
DAS-2361 - Use np.clip to crop to granule
1 parent 42412af commit fc562fd

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

hoss/projection_utilities.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,35 @@ def get_projected_x_y_extents(
203203
grid_lats, grid_lons = get_grid_lat_lons( # pylint: disable=unpacking-non-sequence
204204
x_values, y_values, crs
205205
)
206+
max_grid_lon = np.max(grid_lons)
207+
min_grid_lon = np.min(grid_lons)
208+
max_grid_lat = np.max(grid_lats)
209+
min_grid_lat = np.min(grid_lats)
206210

207211
geographic_resolution = get_geographic_resolution(grid_lons, grid_lats)
208-
209212
resolved_points = get_resolved_geojson(
210213
geographic_resolution, shape_file=shape_file, bounding_box=bounding_box
211214
)
212-
filtered_points = [
213-
(
214-
max(min(lon, np.nanmax(grid_lons)), np.nanmin(grid_lons)),
215-
max(min(lat, np.nanmax(grid_lats)), np.nanmin(grid_lats)),
216-
)
217-
for lon, lat in resolved_points
218-
]
215+
req_lons, req_lats = zip(*resolved_points)
216+
min_req_lon = np.min(req_lons)
217+
max_req_lon = np.max(req_lons)
218+
min_req_lat = np.min(req_lats)
219+
max_req_lat = np.max(req_lats)
220+
# check if all bbox points are outside the granule.
221+
if (
222+
min_req_lon > max_grid_lon
223+
or max_req_lon < min_grid_lon
224+
or min_req_lat > max_grid_lat
225+
or max_req_lat < min_grid_lat
226+
):
227+
# do not crop. it is outside spatial area
228+
filtered_points = resolved_points
229+
else:
230+
clipped_lons = np.clip(req_lons, min_grid_lon, max_grid_lon)
231+
clipped_lats = np.clip(req_lats, min_grid_lat, max_grid_lat)
232+
clipped_points = list(zip(clipped_lons, clipped_lats))
233+
# print(f'clipped_points={clipped_points}')
234+
filtered_points = clipped_points
219235
return get_x_y_extents_from_geographic_points(filtered_points, crs)
220236

221237

tests/unit/test_projection_utilities.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -394,35 +394,60 @@ def test_get_projected_x_y_extents_whole_earth(self):
394394
'grid_mapping_name': 'lambert_azimuthal_equal_area',
395395
}
396396
)
397-
expected_output1 = {
397+
expected_output = {
398398
'x_min': -12702459.818865139,
399399
'x_max': 12702459.818865139,
400400
'y_min': -12702440.623773243,
401401
'y_max': 12702440.710450241,
402402
}
403403
with self.subTest('Whole Earth LAEA - Bounding box input'):
404-
x_y_extents1 = get_projected_x_y_extents(
405-
x_values, y_values, crs, bounding_box=whole_earth_bbox
406-
)
407-
self.assertDictEqual(x_y_extents1, expected_output1)
408404
self.assertDictEqual(
409-
x_y_extents1,
410405
get_projected_x_y_extents(
411406
x_values, y_values, crs, bounding_box=whole_earth_bbox
412407
),
413-
expected_output1,
408+
expected_output,
414409
)
415410

416411
with self.subTest('Whole Earth LAEA - Shape file input'):
417-
x_y_extents2 = get_projected_x_y_extents(
418-
x_values, y_values, crs, shape_file=polygon_path
419-
)
420412
self.assertDictEqual(
421-
x_y_extents2,
422413
get_projected_x_y_extents(
423414
x_values, y_values, crs, shape_file=polygon_path
424415
),
425-
expected_output1,
416+
expected_output,
417+
)
418+
419+
def test_get_projected_x_y_extents_invalid_bbox(self):
420+
"""Ensure that the an empty extent is returned when the bbox is totally
421+
outside the granule extent.
422+
423+
"""
424+
bbox_outside_granule = BBox(-180.0, -85.0, 180.0, -75.0)
425+
# polygon_path = 'tests/geojson_examples/polygon_whole_earth.geo.json'
426+
427+
x_values = np.linspace(-8982000, 8982000, 500)
428+
y_values = np.linspace(8982000, -8982000, 500)
429+
430+
crs = CRS.from_cf(
431+
{
432+
'false_easting': 0.0,
433+
'false_northing': 0.0,
434+
'longitude_of_central_meridian': 0.0,
435+
'latitude_of_projection_origin': 90.0,
436+
'grid_mapping_name': 'lambert_azimuthal_equal_area',
437+
}
438+
)
439+
expected_output = {
440+
'x_min': -12702459.818865139,
441+
'x_max': 12702459.818865139,
442+
'y_min': -12702440.623773243,
443+
'y_max': 12702440.710450241,
444+
}
445+
with self.subTest('Spatial area outside granule LAEA - Bounding box input'):
446+
self.assertDictEqual(
447+
get_projected_x_y_extents(
448+
x_values, y_values, crs, bounding_box=bbox_outside_granule
449+
),
450+
expected_output,
426451
)
427452

428453
def test_get_projected_x_y_variables(self):

0 commit comments

Comments
 (0)