Skip to content

Commit

Permalink
Feature/bugfix smap rss l2 sss v6 (#305)
Browse files Browse the repository at this point in the history
* fix SMAP_RSS_L2_SSS_V6 subsetting

* code update

* code clean up

* update changelog

* update function and update changelog

* update harmony service lib

* update libraries

* add test for indexer

* more tests
  • Loading branch information
sliu008 authored Dec 19, 2024
1 parent b71931d commit 6960ea8
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 114 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated
### Removed
### Fixed
- Updated xarray enhancement get_indexers_from_nd function for SMAP_RSS_L2_SSS_V6.
- Fix minor bug in checking for empty indexers and same data bounds.
### Security


Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
'sphinx.ext.coverage',
'sphinx.ext.viewcode',
'sphinx.ext.napoleon',
'sphinx_rtd_theme',
'm2r2'
'sphinx_rtd_theme'
]

# Add any paths that contain templates here, relative to this directory.
Expand Down Expand Up @@ -77,3 +76,4 @@
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

77 changes: 35 additions & 42 deletions podaac/subsetter/xarray_enhancements.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,57 +69,50 @@ def get_indexers_from_nd(cond: xr.Dataset, cut: bool) -> dict:
Indexer dictionary for the provided condition.
"""
# check if the lat/lon coordinate numpy array has 2 or more dimensions
transpose = False
if cond.values.squeeze().ndim == 2:
x_axis = 1
y_axis = 0
transpose = dim_grid = False
ndim = cond.values.squeeze().ndim

# Determine axes and flags
if ndim == 2:
x_axis, y_axis = 1, 0
else:
if any('xtrack' in dim for dim in list(cond.dims)) and\
any('atrack' in dim for dim in list(cond.dims)):
x_axis = list(cond.dims).index('xtrack')
y_axis = list(cond.dims).index('atrack')
if 'xtrack' in cond.dims and 'atrack' in cond.dims:
x_axis, y_axis = cond.dims.index('xtrack'), cond.dims.index('atrack')
transpose = True
elif 'xdim_grid' in cond.dims and 'ydim_grid' in cond.dims:
x_axis, y_axis = cond.dims.index('xdim_grid'), cond.dims.index('ydim_grid')
dim_grid = x_axis == 1 and y_axis == 0
else:
x_axis = 2
y_axis = 1
x_axis, y_axis = 2, 1

rows = np.any(cond.values.squeeze(), axis=x_axis)
if cut:
cols = np.any(cond.values.squeeze(), axis=y_axis)
else:
cols = np.ones(len(cond.values[0]))
# Compute rows and columns
squeezed_values = cond.values.squeeze()
rows = np.any(squeezed_values, axis=x_axis)
cols = np.any(squeezed_values, axis=y_axis) if cut else np.ones(len(squeezed_values[0]))

# If the subsetted area is equal to the original area
if np.all(rows) & np.all(cols):
# Log information about subsetted area
if np.all(rows) and np.all(cols):
logging.info("Subsetted area equal to the original granule.")

# If the subsetted area is empty
if not np.any(rows) | np.any(cols):
if not np.any(rows) or not np.any(cols):
logging.info("No data within the given bounding box.")

cond_shape_list = list(cond.shape)
cond_list = list(cond.dims)
output = [idx for idx, element in enumerate(cond_shape_list) if element == 1]
for i in output:
cond_list.pop(i)

if rows.ndim == 1:
indexers = {
cond_list[0]: np.where(rows)[0],
cond_list[1]: np.where(cols)[0]
}
else:
# if the lat/lon had 3 dimensions the conditional array was identical in the z direction - taking the first
# Determine dimensions and clean them up
cond_dims = list(cond.dims)
cond_shape = list(cond.shape)
cond_dims = [dim for dim, size in zip(cond_dims, cond_shape) if size > 1]

# Adjust for 3D data
if rows.ndim > 1:
if transpose:
rows = rows.transpose()[0]
cols = cols.transpose()[0]
else:
rows = rows[0]
cols = cols[0]
indexers = {
cond_list[y_axis]: np.where(rows)[0],
cond_list[x_axis]: np.where(cols)[0]
}
rows, cols = rows.transpose()[0], cols.transpose()[0]
elif not dim_grid:
rows, cols = rows[0], cols[0]

# Generate indexers
indexers = {
cond_dims[y_axis]: np.where(rows)[0],
cond_dims[x_axis]: np.where(cols)[0]
}

return indexers

Expand Down
Loading

0 comments on commit 6960ea8

Please sign in to comment.