From 20e0eeec0507a77e942f8abead376cf114b221a9 Mon Sep 17 00:00:00 2001 From: Simon Liu Date: Mon, 1 Jul 2024 13:01:40 -0700 Subject: [PATCH] update tests for empty datasets --- podaac/subsetter/xarray_enhancements.py | 2 +- tests/test_subset.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/podaac/subsetter/xarray_enhancements.py b/podaac/subsetter/xarray_enhancements.py index 032af613..522eebc3 100644 --- a/podaac/subsetter/xarray_enhancements.py +++ b/podaac/subsetter/xarray_enhancements.py @@ -145,7 +145,7 @@ def copy_empty_dataset(dataset: xr.Dataset) -> xr.Dataset: # Create a dict object where each key is a variable in the dataset and the value is an # array initialized to the fill value for that variable or NaN if there is no fill value # attribute for the variable - print("COPy EMPTY Dataset") + empty_data = {k: np.full(v.shape, dataset.variables[k].attrs.get('_FillValue', np.nan), dtype=v.dtype) for k, v in dataset.items()} # Create a copy of the dataset filled with the empty data. Then select the first index along each diff --git a/tests/test_subset.py b/tests/test_subset.py index 72b52420..28dceb2a 100644 --- a/tests/test_subset.py +++ b/tests/test_subset.py @@ -350,7 +350,13 @@ 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(): - assert np.all(variable.data == variable.attrs.get('_FillValue', np.nan) or np.isnan(variable.data)) + 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) assert test_input_dataset.dims.keys() == empty_dataset.dims.keys()