Skip to content

Commit

Permalink
update to fix temporal subsetting
Browse files Browse the repository at this point in the history
  • Loading branch information
sliu008 committed Jul 8, 2024
1 parent 20e0eee commit e0917a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
15 changes: 12 additions & 3 deletions podaac/subsetter/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,12 @@ def build_cond(str_timestamp, compare):
epoch_datetime = dataset[epoch_time_var_name].values[0]
timestamp = np.datetime64(timestamp) - epoch_datetime

return compare(dataset[time_var_name], timestamp)
time_data = dataset[time_var_name]
if dataset[time_var_name].long_name == "reference time of sst file":
timedelta_seconds = dataset['sst_dtime'].astype('timedelta64[s]')
time_data = dataset[time_var_name] + timedelta_seconds

return compare(time_data, timestamp)

temporal_conds = []
if min_time:
Expand Down Expand Up @@ -1094,7 +1099,6 @@ def open_as_nc_dataset(filepath: str) -> Tuple[nc.Dataset, bool]:
try:
nc_dataset = nc.Dataset(filepath, mode='r')
has_groups = bool(nc_dataset.groups)

# If dataset has groups, transform to work with xarray
if has_groups:
nc_dataset = transform_grouped_dataset(nc_dataset, filepath)
Expand Down Expand Up @@ -1212,7 +1216,6 @@ def subset(file_to_subset: str, bbox: np.ndarray, output_file: str,
if 'ScanTime' in [var.split('__')[-2] for var in list(nc_dataset.variables.keys())]:
gc.change_var_dims(nc_dataset, variables)
hdf_type = 'GPM'

args = {
'decode_coords': False,
'mask_and_scale': False,
Expand All @@ -1236,6 +1239,12 @@ def subset(file_to_subset: str, bbox: np.ndarray, output_file: str,
except AttributeError:
pass

try:
if nc_dataset[time_variable].long_name == "reference time of sst file":
args['mask_and_scale'] = True
except AttributeError:
pass

if hdf_type == 'GPM':
args['decode_times'] = False

Expand Down
16 changes: 9 additions & 7 deletions tests/test_subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,15 @@ def test_subset_empty_bbox(test_file, data_dir, subset_output_dir, request):

# Ensure all variables are present but empty.
for _, variable in empty_dataset.data_vars.items():
try:
assert np.all(variable.data == variable.attrs.get('_FillValue', np.nan) or np.isnan(variable.data))
except Exception as ex:
# if there is no fill value and type is integer then don't raise exception
fill_value = variable.attrs.get('_FillValue', np.nan)
if not (np.isnan(fill_value) and np.issubdtype(variable.dtype, np.integer)):
raise(ex)
fill_value = variable.attrs.get('_FillValue', np.nan)
data = variable.data

# Perform the main check
condition = np.all(data == fill_value) or np.all(np.isnan(data))

# Handle the specific integer dtype case
if not condition and not (np.isnan(fill_value) and np.issubdtype(variable.dtype, np.integer)):
assert condition, f"Data does not match fill value for variable: {variable}"

assert test_input_dataset.dims.keys() == empty_dataset.dims.keys()

Expand Down

0 comments on commit e0917a4

Please sign in to comment.