Skip to content

Support multidimensional coordinates for bounds and regridding#820

Open
tomvothecoder wants to merge 7 commits intomainfrom
bug/816-multidim-coords
Open

Support multidimensional coordinates for bounds and regridding#820
tomvothecoder wants to merge 7 commits intomainfrom
bug/816-multidim-coords

Conversation

@tomvothecoder
Copy link
Copy Markdown
Collaborator

@tomvothecoder tomvothecoder commented Dec 3, 2025

Description

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

If applicable:

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass with my changes (locally and CI/CD build)
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have noted that this is a breaking change for a major release (fix or feature that would cause existing functionality to not work as expected)

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (baf02ae) to head (d13a72a).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #820   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           18        18           
  Lines         1989      2002   +13     
=========================================
+ Hits          1989      2002   +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread xcdat/regridder/accessor.py Outdated
@tomvothecoder tomvothecoder marked this pull request as ready for review February 25, 2026 18:12
@tomvothecoder
Copy link
Copy Markdown
Collaborator Author

Notes

  • Add guard for regrid2 since it not supported

@pochedls
Copy link
Copy Markdown
Collaborator

Thanks for pushing this forward @jasonb5! This PR seems to work for the example in #816, but I think it is failing for a related case (curvilinear->rectilinear):

wget https://esgf-node.ornl.gov/thredds/fileServer/css03_data/CMIP6/CMIP/CNRM-CERFACS/CNRM-CM6-1/historical/r1i1p1f2/Amon/tas/gr/v20180917/tas_Amon_CNRM-CM6-1_historical_r1i1p1f2_gr_185001-201412.nc
wget https://esgf-node.ornl.gov/thredds/fileServer/css03_data/CMIP6/CMIP/CNRM-CERFACS/CNRM-CM6-1/historical/r1i1p1f2/SImon/siconc/gn/v20180917/siconc_SImon_CNRM-CM6-1_historical_r1i1p1f2_gn_185001-201412.nc
# %% imports
import xcdat as xc

# %% parameters
fn_source = 'siconc_SImon_CNRM-CM6-1_historical_r1i1p1f2_gn_185001-201412.nc'
fn_target = 'tas_Amon_CNRM-CM6-1_historical_r1i1p1f2_gr_185001-201412.nc'

# %% get source / target grids
ds = xc.open_dataset(fn_source)
ngrid = xc.open_dataset(fn_target)

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

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."

I can get this to work with:

import xesmf as xe

regridder = xe.Regridder(ds, ngrid, "conservative_normed", periodic=True, ignore_degenerate=True)                                            
dsr = regridder(ds)

@jasonb5 jasonb5 force-pushed the bug/816-multidim-coords branch from 27ee94b to 36aa432 Compare March 14, 2026 07:23
@jasonb5
Copy link
Copy Markdown
Collaborator

jasonb5 commented Mar 17, 2026

@pochedls Could you give this another try? Thanks!

@pochedls
Copy link
Copy Markdown
Collaborator

This is working for both of the cases I raised (code below for my reference), but I think it isn't generically picking up the coordinates.

For example, CESM2 has the following structure

Dimensions: (time: 1980, nj: 384, ni: 320, d2: 2, nvertices: 4, bnds: 2)
Coordinates:

  • time (time) object 16kB 1850-01-15 12:00:00 ... 2014-12-15 12:00:00
  • nj (nj) int32 2kB 1 2 3 4 5 6 7 8 ... 378 379 380 381 382 383 384
  • ni (ni) int32 1kB 1 2 3 4 5 6 7 8 ... 314 315 316 317 318 319 320
    lat (nj, ni) float64 983kB dask.array<chunksize=(384, 320), meta=np.ndarray>
    lon (nj, ni) float64 983kB dask.array<chunksize=(384, 320), meta=np.ndarray>
    Dimensions without coordinates: d2, nvertices, bnds
    Data variables:
    siconc (time, nj, ni) float32 973MB dask.array<chunksize=(1, 384, 320), meta=np.ndarray>

Which produces the following error when regridding:

ValueError: This DataArray has more than one dimension ['lon', 'nj'] mapped to the 'X' axis, which is an unexpected behavior. Try dropping extraneous dimensions from the DataArray first (might affect data shape).

Is there a more generic way to handle these auxiliary coordinates (x/nj/etc)?

Perlmutter code for reference.

import xsearch as xs
import xcdat as xc

p = list(xs.findPaths('historical', 'siconc', 'mon', model='CNRM-CM6-1', member = 'r1i1p1f2').keys())[0]
p2 = list(xs.findPaths('historical', 'tas', 'mon', model='CNRM-CM6-1', member = 'r1i1p1f2').keys())[0]

ds = xc.open_mfdataset(p)
ngrid = xc.open_mfdataset(p2)
dsr = ds.regridder.horizontal('siconc', ngrid, tool="xesmf", method="conservative_normed", periodic=True)

@jasonb5
Copy link
Copy Markdown
Collaborator

jasonb5 commented Mar 18, 2026

That actually caught an edge case I didn't consider. If the multidim flag is set it should return a matched coordinate no matter what. I'll push a fix and have you retest. Thanks!

@pochedls
Copy link
Copy Markdown
Collaborator

That actually caught an edge case I didn't consider. If the multidim flag is set it should return a matched coordinate no matter what. I'll push a fix and have you retest. Thanks!

Thanks @jasonb5 – I think curvilinear regridding is seemingly full of edge cases. I appreciate your effort in making this robust (and I'm happy you can quickly understand what is going on).

@jasonb5
Copy link
Copy Markdown
Collaborator

jasonb5 commented Mar 19, 2026

@pochedls There's more to this issue. My original fix may not be correct. I'm working on a new fix and will push it soon.

@jasonb5 jasonb5 force-pushed the bug/816-multidim-coords branch from b4d0557 to 048a07a Compare March 20, 2026 05:04
@jasonb5
Copy link
Copy Markdown
Collaborator

jasonb5 commented Mar 20, 2026

@pochedls Try the latest. The CESM example will fail because it has two possible X coordinates ('nj', 'lon') so xCDAT can differentiate. It looks like cf_xarray can figure out what are the correct coordinates. I may switch ds.coords to ds.cf.coordinates to fix this. Will update after some testing.

@pochedls
Copy link
Copy Markdown
Collaborator

@pochedls Try the latest. The CESM example will fail because it has two possible X coordinates ('nj', 'lon') so xCDAT can differentiate. It looks like cf_xarray can figure out what are the correct coordinates. I may switch ds.coords to ds.cf.coordinates to fix this. Will update after some testing.

FYI – this is working for CNRM-CM6-1 and the source.nc data in the original bug report. It is failing for CESM2 (as you expected). Once you update for CESM2, I can try a broader set of models

@jasonb5
Copy link
Copy Markdown
Collaborator

jasonb5 commented Apr 3, 2026

@pochedls Go ahead and try again. All three cases should work this time.

@pochedls
Copy link
Copy Markdown
Collaborator

pochedls commented Apr 6, 2026

@jasonb5 – This is working for all the examples I provided. I'm good with moving forward with this PR (fixes the issues raised), but I could also test more models if that would be helpful.

@jasonb5
Copy link
Copy Markdown
Collaborator

jasonb5 commented Apr 15, 2026

@pochedls Thanks!
@tomvothecoder Do you want to review before we merge?

@tomvothecoder
Copy link
Copy Markdown
Collaborator Author

Thanks @jasonb5 for the work and @pochedls for testing. I will review and merge early next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

[Bug]: Metadata regridding issues related to multidimensional coords

3 participants