Stream: general

Topic: xarray open_mfdataset add_bounds problem


view this post on Zulip Jim Edwards (Mar 17 2025 at 18:58):

I am looking at an xarray routine that internally computes cell boundaries. Then the next routine complains that the
cell boundaries are not monotonic. It looks to me like a roundoff error and I wonder if anyone here knows how to avoid it?
The cell bounds in question are below.
[ -1.8848168 , -0.9424084 ],
[ -0.94240844, 0. ],
[ 0. , 0.94240844],
[ 0.9424084 , 1.8848168 ],

The routine that calculates these boundaries is open_mfdataset with argument add_bounds=["X", "Y"]

view this post on Zulip Thomas Martin (Mar 18 2025 at 18:05):

Do you have test/sample data I can work though?

view this post on Zulip Drew Camron (Mar 18 2025 at 19:59):

Are you using xcdat for your first routine? add_bounds is not functionality default in xarray.

xcdat appears to calculate a naive linear interpolation between your grid coords, ie 0.5 * (x_2 - x_1). This approach is simple but potentially a.) not a realistic representation for curvilinear grids and b.) susceptible to imprecise math. If this is fine for your approach, you could try manually rounding a few digits off your calculated boundaries. This may minorly exaggerate the existing error of this linear grid spacing approximation.

You could also open up an issue on the xcdat GitHub to see what other folks are doing or recommend, but this doesn't seem to necessarily be a bug on their end.

I might consider other methods for calculating cell boundaries, or getting that information from the underlying model or grid specifications.

Echoing Thomas: providing sample code, data, and your traceback could also illuminate potential solutions in your dependent routine as well.


Last updated: May 16 2025 at 17:14 UTC