xCDAT / xcdat

An extension of xarray for climate data analysis on structured grids.
https://xcdat.readthedocs.io/en/latest/
Apache License 2.0
101 stars 11 forks source link

[Bug]: Regrid2 error when interpolates a spatial field without time axis #581

Closed lee1043 closed 4 months ago

lee1043 commented 7 months ago

What happened?

regrid2 returns error when input field for interpolation does not have time axis. In below example, I am interpolating a 2d spatial field, a time snapshat. xesmf works fine for the case.

What did you expect to happen? Are there are possible answers you came across?

When time axis does not exist, regrid process should only regrid that field (single time step).

Minimal Complete Verifiable Example (MVCE)

import xcdat as xc

!wget https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/v2.1/access/avhrr/198109/oisst-avhrr-v02r01.19810901.nc

input_data = "oisst-avhrr-v02r01.19810901.nc"
ds = xc.open_dataset(input_data)

# Make a target grid to interpolate to
target_grid = xc.create_uniform_grid(-90, 90, 5, 2.5, 357.5, 5)
target_grid

ds_regridded_regrid2 = ds.regridder.horizontal("sst", target_grid, tool="regrid2")

Relevant log output

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/mambaforge/envs/enso_metrics_dev_20231204/lib/python3.10/site-packages/xcdat/regridder/regrid2.py:134, in Regrid2Regridder._output_axis_sizes(self, da)
    133 try:
--> 134     axis_name = axis_name_map[standard_name]
    135 except KeyError:

KeyError: 'time'

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
/Users/lee1043/Documents/Research/xcdat/test_regrid2_nan_issue/test_regrid2_coordinate.ipynb Cell 6 line 1
----> 1 ds_regridded_regrid2 = ds.regridder.horizontal("sst", target_grid, tool="regrid2") # unmapped_to_nan=True is not available for regrid2

File ~/mambaforge/envs/enso_metrics_dev_20231204/lib/python3.10/site-packages/xcdat/regridder/accessor.py:324, in RegridderAccessor.horizontal(self, data_var, output_grid, tool, **options)
    322 input_grid = _get_input_grid(self._ds, data_var, ["X", "Y"])
    323 regridder = regrid_tool(input_grid, output_grid, **options)
--> 324 output_ds = regridder.horizontal(data_var, self._ds)
    326 return output_ds

File ~/mambaforge/envs/enso_metrics_dev_20231204/lib/python3.10/site-packages/xcdat/regridder/regrid2.py:96, in Regrid2Regridder.horizontal(self, data_var, ds)
     92 input_data = input_data_var.values
     94 axis_variable_name_map = {x: y[0] for x, y in input_data_var.cf.axes.items()}
---> 96 output_axis_sizes = self._output_axis_sizes(input_data_var)
     98 ordered_axis_names = list(output_axis_sizes)
    100 output_data = self._regrid(input_data, output_axis_sizes, ordered_axis_names)

File ~/mambaforge/envs/enso_metrics_dev_20231204/lib/python3.10/site-packages/xcdat/regridder/regrid2.py:136, in Regrid2Regridder._output_axis_sizes(self, da)
    134     axis_name = axis_name_map[standard_name]
    135 except KeyError:
--> 136     raise RuntimeError(
    137         f"Could not find axis {standard_name!r}, ensure {standard_name!r} "
    138         "exists and the attributes are correct."
    139     )
    141 if standard_name in self._output_grid:
    142     output_sizes[axis_name] = self._output_grid.sizes[standard_name]

RuntimeError: Could not find axis 'time', ensure 'time' exists and the attributes are correct.

Anything else we need to know?

No response

Environment

xcdat: 0.6.1

xr.show_versions() ``` INSTALLED VERSIONS ------------------ commit: None python: 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:17:34) [Clang 14.0.6 ] python-bits: 64 OS: Darwin OS-release: 22.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: None LOCALE: (None, 'UTF-8') libhdf5: 1.14.2 libnetcdf: 4.9.2 xarray: 2023.11.0 pandas: 2.1.3 numpy: 1.23.5 scipy: 1.11.4 netCDF4: 1.6.5 pydap: None h5netcdf: None h5py: None Nio: None zarr: None cftime: 1.6.3 nc_time_axis: 1.4.1 iris: None bottleneck: None dask: 2023.12.0 distributed: 2023.12.0 matplotlib: 3.7.1 cartopy: 0.22.0 seaborn: 0.12.2 numbagg: None fsspec: 2023.12.0 cupy: None pint: None sparse: 0.14.0 flox: None numpy_groupies: None setuptools: 67.7.2 pip: 23.1.2 conda: None pytest: None mypy: None IPython: 8.18.1 sphinx: None [/Users/lee1043/mambaforge/envs/enso_metrics_dev_20231204/lib/python3.10/site-packages/_distutils_hack/__init__.py:33](https://file+.vscode-resource.vscode-cdn.net/Users/lee1043/mambaforge/envs/enso_metrics_dev_20231204/lib/python3.10/site-packages/_distutils_hack/__init__.py:33): UserWarning: Setuptools is replacing distutils. warnings.warn("Setuptools is replacing distutils.") ```
lee1043 commented 7 months ago

I don't know if this was already considered in #533. @jasonb5 @tomvothecoder could you please remind me?

jasonb5 commented 6 months ago

@lee1043 I will check, this may be resolved with #533.

tomvothecoder commented 4 months ago

Hey @jasonb5, did you confirm whether or not this was fixed in #533?

jasonb5 commented 4 months ago

@tomvothecoder @lee1043 With #533 I had to adjust the example to

import xcdat as xc

!wget https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/v2.1/access/avhrr/198109/oisst-avhrr-v02r01.19810901.nc

input_data = "oisst-avhrr-v02r01.19810901.nc"
ds = xc.open_dataset(input_data)
# Needs axis attribute for cf_xarray
ds["lat"].attrs["axis"] = "Y"
ds["lon"].attrs["axis"] = "X"
ds["time"].attrs["axis"] = "T"

# Make a target grid to interpolate to
target_grid = xc.create_uniform_grid(-90, 90, 5, 2.5, 357.5, 5)
target_grid

ds_regridded_regrid2 = ds.regridder.horizontal("sst", target_grid, tool="regrid2")

and it worked correctly.

lee1043 commented 4 months ago

The above @jasonb5's code is working with #533. Thank you @jasonb5

tomvothecoder commented 4 months ago

From my understanding, this issue is related to missing CF attributes for this specific dataset.

Is that correct? If so, we can probably close this issue.

lee1043 commented 4 months ago

@tomvothecoder agreed.