Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 267 #268

Merged
merged 18 commits into from
May 31, 2024
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- [issue/267](https://github.com/podaac/l2ss-py/pull/261): Add xtrack and atrack dimension options for get_nd_indexers when bounding box subsetting is performed on SNDR.
### Changed
### Deprecated
### Removed
Expand Down
2 changes: 1 addition & 1 deletion podaac/subsetter/gpm_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def compute_new_time_data(time_group, nc_dataset):
return new_time_list, time_unit_out


def change_var_dims(nc_dataset, variables=None, time_name="_timeMidScan"):
def change_var_dims(nc_dataset, variables=None, time_name="__timeMidScan"):
"""
Go through each variable and get the dimension names from attribute "DimensionNames
If the name is unique, add it as a dimension to the netCDF4 dataset. Then change the
Expand Down
24 changes: 18 additions & 6 deletions podaac/subsetter/xarray_enhancements.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,20 @@ 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
else:
x_axis = 2
y_axis = 1
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')
transpose = True
else:
x_axis = 2
y_axis = 1

rows = np.any(cond.values.squeeze(), axis=x_axis)
if cut:
cols = np.any(cond.values.squeeze(), axis=y_axis)
Expand Down Expand Up @@ -102,11 +110,15 @@ def get_indexers_from_nd(cond: xr.Dataset, cut: bool) -> dict:
}
else:
# if the lat/lon had 3 dimensions the conditional array was identical in the z direction - taking the first
rows = rows[0]
cols = cols[0]
if transpose:
rows = rows.transpose()[0]
cols = cols.transpose()[0]
else:
rows = rows[0]
cols = cols[0]
indexers = {
cond_list[1]: np.where(rows)[0],
cond_list[2]: np.where(cols)[0]
cond_list[y_axis]: np.where(rows)[0],
cond_list[x_axis]: np.where(cols)[0]
}

return indexers
Expand Down
14 changes: 12 additions & 2 deletions tests/test_subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,8 +1311,10 @@ def test_cf_decode_times_sndr(data_dir, subset_output_dir, request):
sndr_files = ['SNDR.J1.CRIMSS.20210224T0100.m06.g011.L2_CLIMCAPS_RET.std.v02_28.G.210331064430.nc',
'SNDR.AQUA.AIRS.20140110T0305.m06.g031.L2_CLIMCAPS_RET.std.v02_39.G.210131015806.nc',
'SNDR.SNPP.CRIMSS.20200118T0024.m06.g005.L2_CLIMCAPS_RET.std.v02_28.G.200314032326_subset.nc']
bbox = np.array(((-180, 180), (-90, 90)))
for sndr_file in sndr_files:
# do a longitude subset on these files that doesn't alter the resulting shape
sndr_spatial = [(-180,-150), (-15,180), (-180,30)]
for sndr_file, box in zip(sndr_files, sndr_spatial):
bbox = np.array(((box[0], box[1]), (-90, 90)))
output_file = "{}_{}".format(request.node.name, sndr_file)
shutil.copyfile(
os.path.join(SNDR_dir, sndr_file),
Expand All @@ -1327,6 +1329,14 @@ def test_cf_decode_times_sndr(data_dir, subset_output_dir, request):
max_time='2021-02-24T01:09:55Z'
)

out_ds = xr.open_dataset(join(subset_output_dir, output_file),
decode_coords=False)
in_ds = xr.open_dataset(join(SNDR_dir, sndr_file),
decode_coords=False)

# do a longitude subset that cuts down on the file but the shape should remain the same
assert out_ds['lon'].shape == in_ds['lon'].shape

if not isinstance(box_test, np.ndarray):
raise ValueError('Subset for SNDR not returned properly')

Expand Down
Loading