Skip to content

Commit

Permalink
DAS-2232 - added unit test for the new method - get_x_y_index_ranges_…
Browse files Browse the repository at this point in the history
…from_coordinates
  • Loading branch information
sudha-murthy committed Oct 7, 2024
1 parent 5c5eb85 commit 80c2fb2
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hoss/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def get_x_y_index_ranges_from_coordinates(
points.
"""

crs = get_variable_crs(non_spatial_variable, varinfo)
projected_x = 'projected_x'
projected_y = 'projected_y'
Expand All @@ -267,11 +268,13 @@ def get_x_y_index_ranges_from_coordinates(
override_dimension_datasets[projected_x][:],
x_y_extents['x_min'],
x_y_extents['x_max'],
bounds_values=None,
)
y_index_ranges = get_dimension_index_range(
override_dimension_datasets[projected_y][:],
x_y_extents['y_min'],
x_y_extents['y_max'],
bounds_values=None,
)
x_y_index_ranges = {projected_x: x_index_ranges, projected_y: y_index_ranges}
else:
Expand Down
119 changes: 119 additions & 0 deletions tests/unit/test_spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from varinfo import VarInfoFromDmr

from hoss.bbox_utilities import BBox
from hoss.coordinate_utilities import update_dimension_variables
from hoss.spatial import (
get_bounding_box_longitudes,
get_geographic_index_range,
Expand Down Expand Up @@ -183,6 +184,124 @@ def test_get_spatial_index_ranges_geographic(self):
{'/latitude': (5, 44), '/longitude': (160, 199)},
)

@patch('hoss.spatial.get_dimension_index_range')
@patch('hoss.spatial.get_projected_x_y_extents')
def test_get_x_y_index_ranges_from_coordinates(
self,
mock_get_x_y_extents,
mock_get_dimension_index_range,
):
"""Ensure that x and y index ranges are only requested only when there are
no projected dimensions and when there are coordinate datasets,
and the values have not already been calculated.
The example used in this test is for the SMAP SPL3SMP collection,
(SMAP L3 Radiometer Global Daily 36 km EASE-Grid Soil Moisture)
which has a Equal-Area Scalable Earth Grid (EASE-Grid 2.0) CRS for
a projected grid which is lambert_cylindrical_equal_area projection
"""
smap_varinfo = VarInfoFromDmr(
'tests/data/SC_SPL3SMP_009.dmr',
'SPL3SMP',
'hoss/hoss_config.json',
)
smap_file_path = 'tests/data/SC_SPL3SMP_009_prefetch.nc4'
expected_index_ranges = {'projected_x': (487, 595), 'projected_y': (9, 38)}
bbox = BBox(2, 54, 42, 72)
smap_variable_name = '/Soil_Moisture_Retrieval_Data_AM/surface_flag'

latitude_coordinate = smap_varinfo.get_variable(
'/Soil_Moisture_Retrieval_Data_AM/latitude'
)
longitude_coordinate = smap_varinfo.get_variable(
'/Soil_Moisture_Retrieval_Data_AM/longitude'
)

crs = CRS.from_cf(
{
'false_easting': 0.0,
'false_northing': 0.0,
'longitude_of_central_meridian': 0.0,
'standard_parallel': 30.0,
'grid_mapping_name': 'lambert_cylindrical_equal_area',
}
)

x_y_extents = {
'x_min': 192972.56050179302,
'x_max': 4052423.7705376535,
'y_min': 5930779.396449475,
'y_max': 6979878.9118312765,
}

mock_get_x_y_extents.return_value = x_y_extents

# When ranges are derived, they are first calculated for x, then y:
mock_get_dimension_index_range.side_effect = [(487, 595), (9, 38)]

with self.subTest(
'Projected grid from coordinates gets expected dimension ranges'
):
with Dataset(smap_file_path, 'r') as smap_prefetch:
self.assertDictEqual(
get_x_y_index_ranges_from_coordinates(
'/Soil_Moisture_Retrieval_Data_AM/surface_flag',
smap_varinfo,
smap_prefetch,
latitude_coordinate,
longitude_coordinate,
{},
bounding_box=bbox,
shape_file_path=None,
),
expected_index_ranges,
)

mock_get_x_y_extents.assert_called_once_with(
ANY, ANY, crs, shape_file=None, bounding_box=bbox
)

self.assertEqual(mock_get_dimension_index_range.call_count, 2)
mock_get_dimension_index_range.assert_has_calls(
[
call(
ANY,
x_y_extents['x_min'],
x_y_extents['x_max'],
bounds_values=None,
),
call(
ANY,
x_y_extents['y_min'],
x_y_extents['y_max'],
bounds_values=None,
),
]
)

mock_get_x_y_extents.reset_mock()
mock_get_dimension_index_range.reset_mock()

with self.subTest('Function does not rederive known index ranges'):
with Dataset(smap_file_path, 'r') as smap_prefetch:
self.assertDictEqual(
get_x_y_index_ranges_from_coordinates(
'/Soil_Moisture_Retrieval_Data_AM/surface_flag',
smap_varinfo,
smap_prefetch,
latitude_coordinate,
longitude_coordinate,
expected_index_ranges,
bounding_box=bbox,
shape_file_path=None,
),
{},
)

mock_get_x_y_extents.assert_not_called()
mock_get_dimension_index_range.assert_not_called()

@patch('hoss.spatial.get_dimension_index_range')
@patch('hoss.spatial.get_projected_x_y_extents')
def test_get_projected_x_y_index_ranges(
Expand Down

0 comments on commit 80c2fb2

Please sign in to comment.