Skip to content

Commit

Permalink
Simplify variable transposal, update effected typehints and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyschultz committed Oct 25, 2024
1 parent b707bd2 commit 6678d5a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Changed

- [[DAS-2216](https://bugs.earthdata.nasa.gov/browse/DAS-2216)]
The Swath Projector has been updated with quick fixes to add support for TEMPO level 2 data.
The Swath Projector has been updated with quick fixes to add support for TEMPO level 2 data. These changes include optional transposing of arrays based on dimension sizes and updates to the configuration file for TEMPO_O3TOT_L2 to correctly locate coordinate variables and exclude science variables with dimensions that do no match those of the coordinate variables.

## [v1.1.1] - 2024-09-16

Expand Down
2 changes: 1 addition & 1 deletion swath_projector/earthdata_varinfo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"Value": "/geolocation/latitude, /geolocation/longitude"
}
],
"_Description": "TEMPO_O3TOT_L2 variables only contain basenames for coordinates, which are found in sibling hierarchical groups. This rule fully qualifies the paths to these coordinates."
"_Description": "TEMPO_O3TOT_L2 variables only contain basenames for coordinates, which are found in sibling hierarchical groups. This rule fully qualifies the paths to these coordinates. Some variables in these groups are excluded via 'ExcludedScienceVariables'"
}
]
}
16 changes: 7 additions & 9 deletions swath_projector/swath_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def get_projected_resolution(
projection: Proj, longitudes: Variable, latitudes: Variable
projection: Proj, longitudes: np.ma.MaskedArray, latitudes: np.ma.MaskedArray
) -> Tuple[float]:
"""Find the resolution of the target grid in the projected coordinates, x
and y. First the perimeter points are found. These are then projected
Expand All @@ -40,7 +40,7 @@ def get_projected_resolution(


def get_extents_from_perimeter(
projection: Proj, longitudes: Variable, latitudes: Variable
projection: Proj, longitudes: np.ma.MaskedArray, latitudes: np.ma.MaskedArray
) -> Tuple[float]:
"""Find the swath extents in the target CRS. First the perimeter points of
unfilled valid pixels are found. These are then projected to the target
Expand All @@ -59,19 +59,17 @@ def get_extents_from_perimeter(
def get_projected_coordinates(
coordinates_mask: np.ma.core.MaskedArray,
projection: Proj,
longitudes: Variable,
latitudes: Variable,
longitudes: np.ma.MaskedArray,
latitudes: np.ma.MaskedArray,
) -> Tuple[np.ndarray]:
"""Get the required coordinate points projected in the target Coordinate
Reference System (CRS).
"""
if len(longitudes.shape) == 1:
coordinates = get_all_coordinates(longitudes[:], latitudes[:], coordinates_mask)
coordinates = get_all_coordinates(longitudes, latitudes, coordinates_mask)
else:
coordinates = get_perimeter_coordinates(
longitudes[:], latitudes[:], coordinates_mask
)
coordinates = get_perimeter_coordinates(longitudes, latitudes, coordinates_mask)

return reproject_coordinates(coordinates, projection)

Expand Down Expand Up @@ -135,7 +133,7 @@ def get_absolute_resolution(polygon_area: float, n_pixels: int) -> float:


def get_valid_coordinates_mask(
longitudes: Variable, latitudes: Variable
longitudes: np.ma.MaskedArray, latitudes: np.ma.MaskedArray
) -> np.ma.core.MaskedArray:
"""Get a `numpy` N-d array containing boolean values (0 or 1) indicating
whether the elements of both longitude and latitude are valid at that
Expand Down
38 changes: 19 additions & 19 deletions swath_projector/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,25 @@ def get_variable_values(
return make_array_two_dimensional(variable[:])
elif 'time' in input_file.variables and 'time' in variable.dimensions:
# Assumption: Array = (time, along-track, across-track)
return transpose_if_xdim_less_than_ydim(
variable[0][:].filled(fill_value=fill_value)
return transpose_if_xdim_less_than_ydim(variable[0][:]).filled(
fill_value=fill_value
)
else:
# Assumption: Array = (along-track, across-track)
return transpose_if_xdim_less_than_ydim(
variable[:].filled(fill_value=fill_value)
return transpose_if_xdim_less_than_ydim(variable[:]).filled(
fill_value=fill_value
)


def get_coordinate_variable(
dataset: Dataset, coordinates_tuple: Tuple[str], coordinate_substring
) -> Optional[Variable]:
) -> Optional[np.ma.MaskedArray]:
"""Search the coordinate dataset names for a match to the substring,
which will be either "lat" or "lon". Return the corresponding variable
from the dataset. Only the base variable name is used, as the group
path may contain either of the strings as part of other words.
data from the dataset. Only the base variable name is used, as the group
path may contain either of the strings as part of other words. The
coordinate variables are transposed if the `along-track`dimension size is
less than the `across-track` dimension size.
"""
for coordinate in coordinates_tuple:
Expand All @@ -69,9 +71,9 @@ def get_coordinate_variable(
):
# QuickFix (DAS-2216) for short and wide swaths
if dataset[coordinate].ndim == 1:
return dataset[coordinate]
return dataset[coordinate][:]

return transpose_if_xdim_less_than_ydim(dataset[coordinate])
return transpose_if_xdim_less_than_ydim(dataset[coordinate][:])

raise MissingCoordinatesError(coordinates_tuple)

Expand Down Expand Up @@ -228,21 +230,19 @@ def make_array_two_dimensional(one_dimensional_array: np.ndarray) -> np.ndarray:
return np.expand_dims(one_dimensional_array, 1)


def transpose_if_xdim_less_than_ydim(variable: np.ndarray) -> np.ndarray:
def transpose_if_xdim_less_than_ydim(
variable_values: np.ma.MaskedArray,
) -> np.ma.MaskedArray:
"""Return transposed variable when variable is wider than tall.
QuickFix (DAS-2216): We presume that a swath has more rows than columns and
if that's not the case we transpose it so that it does.
"""
if variable.ndim != 2:
if len(variable_values.shape) != 2:
raise ValueError(
f'Input variable must be 2 dimensional, but got {variable.ndim} dimensions.'
f'Input variable must be 2 dimensional, but got {len(variable_values.shape)} dimensions.'
)
if variable.shape[0] < variable.shape[1]:
transposed_variable = np.ma.transpose(variable).copy()
if hasattr(variable, 'mask'):
transposed_variable.mask = np.ma.transpose(variable[:].mask)
if variable_values.shape[0] < variable_values.shape[1]:
return np.ma.transpose(variable_values).copy()

return transposed_variable

return variable
return variable_values
12 changes: 5 additions & 7 deletions tests/unit/test_swath_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def setUpClass(cls):

def setUp(self):
self.test_dataset = Dataset(self.test_path)
self.longitudes = self.test_dataset['lon']
self.latitudes = self.test_dataset['lat']
self.longitudes = self.test_dataset['lon'][:]
self.latitudes = self.test_dataset['lat'][:]

def tearDown(self):
self.test_dataset.close()
Expand Down Expand Up @@ -101,8 +101,8 @@ def test_get_projected_resolution_1d(self):
"""Ensure the calculated one-dimensional resolution is correct."""
resolution = get_projected_resolution(
self.geographic_projection,
self.test_dataset['lon_1d'],
self.test_dataset['lat_1d'],
self.test_dataset['lon_1d'][:],
self.test_dataset['lat_1d'][:],
)

self.assertAlmostEqual(resolution, 5.0)
Expand Down Expand Up @@ -167,9 +167,7 @@ def test_get_perimeter_coordinates(self):
np.logical_not(valid_pixels), np.ones(self.longitudes.shape)
)

coordinates = get_perimeter_coordinates(
self.longitudes[:], self.latitudes[:], mask
)
coordinates = get_perimeter_coordinates(self.longitudes, self.latitudes, mask)

self.assertCountEqual(coordinates, expected_points)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_get_coordinate_variables(self):
dataset, coordinates_tuple, coordinate
)

self.assertIsInstance(coordinates, Variable)
self.assertIsInstance(coordinates, np.ma.MaskedArray)

with self.subTest(
'Non existent coordinate variable "latitude" returns MissingCoordinatesError'
Expand Down

0 comments on commit 6678d5a

Please sign in to comment.