Skip to content

Commit

Permalink
Feature/fix temporal subsetting ghrsst (#280)
Browse files Browse the repository at this point in the history
* add more changes so that we can temporally subset ghrsst dataset collections

* update with keyerror exception when getting sst_dtime

* update subset to loop through values makes sure its accessible

* update changelog
  • Loading branch information
sliu008 authored Jul 18, 2024
1 parent 219a141 commit d6f20b2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### 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.
- Fix temporal subsetting ghrsst dataset by adding time delta to time variable.
- Add a function to test ghrsst dataset ability to access variables when mask_and_scale is true.
### Changed
### Deprecated
### Removed
Expand Down
34 changes: 34 additions & 0 deletions podaac/subsetter/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,38 @@ def decode_cf_datetime(num_dates, units, calendar=None, use_cftime=None):
xarray.coding.times.decode_cf_datetime = decode_cf_datetime


def test_access_sst_dtime_values(datafile):
"""
Test accessing values of 'sst_dtime' variable in a NetCDF file.
Parameters
----------
datafile (str): Path to the NetCDF file.
Returns
-------
access_successful (bool): True if 'sst_dtime' values are accessible, False otherwise.
"""

nc_dataset, _, _ = open_as_nc_dataset(datafile)
args = {
'decode_coords': False,
'mask_and_scale': True,
'decode_times': True
}
try:
with xr.open_dataset(
xr.backends.NetCDF4DataStore(nc_dataset),
**args
) as dataset:
# pylint: disable=pointless-statement
for var_name in dataset.variables:
dataset[var_name].values
except (TypeError, ValueError, KeyError):
return False
return True


def subset(file_to_subset: str, bbox: np.ndarray, output_file: str,
variables: Union[List[str], str, None] = (),
# pylint: disable=too-many-branches, disable=too-many-statements
Expand Down Expand Up @@ -1241,6 +1273,8 @@ def subset(file_to_subset: str, bbox: np.ndarray, output_file: str,
if (getattr(time_var, '_FillValue', None) == fill_value_f8 and time_var.dtype in float_dtypes) or \
(getattr(time_var, 'long_name', None) == "reference time of sst file"):
args['mask_and_scale'] = True
if getattr(time_var, 'long_name', None) == "reference time of sst file":
args['mask_and_scale'] = test_access_sst_dtime_values(file_to_subset)
break

if hdf_type == 'GPM':
Expand Down

0 comments on commit d6f20b2

Please sign in to comment.