Skip to content

[Bug]: Metadata regridding issues related to multidimensional coords #816

@pochedls

Description

@pochedls

What happened?

xcdat isn't recognizing dimensions when regridding with xesmf, but regridding does work when using xesmf directly.

Minimal Complete Verifiable Example (MVCE)

xcdat_example.tar.gz

# %% imports
import xcdat as xc
import xesmf as xe

# %% parameters
fn_source = 'source.nc'
fn_target = 'target.nc'

# %% get source / target grids
ds = xc.open_dataset('source.nc')
ngrid = xc.open_dataset('target.nc')

# %% try regridding with xcdat
dsr = ds.regridder.horizontal('tmp2m', ngrid, tool="xesmf", method="bilinear")

KeyError: "No 'X' axis dimension coordinate variables were found in the xarray object. Make sure dimension coordinate variables exist, they are one dimensional, and their CF 'axis' or 'standard_name' attrs are correctly set."

# %% inspect dataset
print(ds.tmp2m)

<xarray.DataArray 'tmp2m' (y: 1059, x: 1799)> Size: 8MB
array([[296.45013, 296.45013, 296.38763, ..., 299.76263, 299.70013, 299.70013],
...,
[282.76263, 282.76263, 282.76263, ..., 270.07513, 270.01263, 270.01263]],
shape=(1059, 1799), dtype=float32)
Coordinates:
latitude (y, x) float64 15MB ...
longitude (y, x) float64 15MB ...

print(ds.longitude)

<xarray.DataArray 'longitude' (y: 1059, x: 1799)> Size: 15MB
[1905141 values with dtype=float64]
Coordinates:
heightAboveGround float64 8B ...
latitude (y, x) float64 15MB ...
longitude (y, x) float64 15MB ...
time object 8B ...
Dimensions without coordinates: y, x
Attributes:
units: degrees_east
standard_name: longitude
long_name: longitude
axis: X

# %% try regridding with xesmf
regridder = xe.Regridder(ds, ngrid, "bilinear")
dsr = regridder(ds)

Log Output

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[4], [line 2](vscode-notebook-cell:?execution_count=4&line=2)
      1 # %% try regridding with xcdat
----> [2](vscode-notebook-cell:?execution_count=4&line=2) dsr = ds.regridder.horizontal("tmp2m", ngrid, tool="xesmf", method="bilinear")

File /global/u2/v/vo13/xCDAT/xcdat/xcdat/regridder/accessor.py:169, in RegridderAccessor.horizontal(self, data_var, output_grid, tool, **options)
    164 except KeyError as e:
    165     raise ValueError(
    166         f"Tool {e!s} does not exist, valid choices {list(HORIZONTAL_REGRID_TOOLS)}"
    167     ) from e
--> [169](https://vscode-remote+ssh-002dremote-002bperlmutter.vscode-resource.vscode-cdn.net/global/u2/v/vo13/xCDAT/xcdat/xcdat/regridder/accessor.py:169) input_grid = _get_input_grid(self._ds, data_var, ["X", "Y"])
    170 regridder = regrid_tool(input_grid, output_grid, **options)
    171 output_ds = regridder.horizontal(data_var, self._ds)

File /global/u2/v/vo13/xCDAT/xcdat/xcdat/regridder/accessor.py:377, in _get_input_grid(ds, data_var, dup_check_dims)
    374 all_coords = set(ds.coords.keys())
    376 for dimension in dup_check_dims:
--> [377](https://vscode-remote+ssh-002dremote-002bperlmutter.vscode-resource.vscode-cdn.net/global/u2/v/vo13/xCDAT/xcdat/xcdat/regridder/accessor.py:377)     coords = get_dim_coords(ds, dimension)
    379     if isinstance(coords, xr.Dataset):
    380         coord = set([get_dim_coords(ds[data_var], dimension).name])

File /global/u2/v/vo13/xCDAT/xcdat/xcdat/axis.py:141, in get_dim_coords(obj, axis)
    134     raise ValueError(
    135         f"This DataArray has more than one dimension {dim_coord_keys} mapped to the "
    136         f"'{axis}' axis, which is an unexpected behavior. Try dropping extraneous "
    137         "dimensions from the DataArray first (might affect data shape)."
    138     )
    140 if len(dim_coord_keys) == 0:
--> [141](https://vscode-remote+ssh-002dremote-002bperlmutter.vscode-resource.vscode-cdn.net/global/u2/v/vo13/xCDAT/xcdat/xcdat/axis.py:141)     raise KeyError(
    142         f"No '{axis}' axis dimension coordinate variables were found in the "
    143         f"xarray object. Make sure dimension coordinate variables exist, they are "
    144         "one dimensional, and their CF 'axis' or 'standard_name' attrs are "
    145         "correctly set."
    146     )
    148 dim_coords = obj[
    149     dim_coord_keys if len(dim_coord_keys) > 1 else dim_coord_keys[0]
    150 ].copy()
    152 return dim_coords

KeyError: "No 'X' axis dimension coordinate variables were found in the xarray object. Make sure dimension coordinate variables exist, they are one dimensional, and their CF 'axis' or 'standard_name' attrs are correctly set."

Anything else we need to know?

This may be related to #718 and #747.

Environment

xCDAT : 0.10.1
xesmf: 0.8.10

Metadata

Metadata

Assignees

Labels

type: bugInconsistencies or issues which will cause an issue or problem for users or implementors.

Type

No type
No fields configured for issues without a type.

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions