cf-xarray icon indicating copy to clipboard operation
cf-xarray copied to clipboard

2D bounds - simple version

Open aulemahal opened this issue 1 year ago • 0 comments

Fixes #71!

I went through xgcm's code (using 0.6.1) and realized that the proposed solution for estimating 2D bounds was indeed quite simple! And that we already had the functions to implement it.

So here I enable ds.cf.add_bounds to estimate 2D bounds by estimating the 1D bounds on the two axes successively, and then reordering the values to fit the conventions.

  • The dim argument can now be a list (of length 1 or 2)
  • There is now a possibility that we add 2D bounds and 1D bounds on the same dataset. For now, if the bounds dim already exists and has a different length than the new one, the latter is renamed bounds2, which is a bit inelegant... Any ideas?
  • I am always confused about the order to use... It doesn't really matter for most applications I guess (i.e. xESMF), but I'm not 100% sure I coded the right one.
  • I will add more tests.

I first used @zxdawn's gist (https://gist.github.com/zxdawn/49c73e4d1ef9043580644e57eaa16d83) with xgcm 0.6.1. This implementation in cf-xarray does almost the exact same thing. I then compared with an old gist of mine (https://gist.github.com/aulemahal/58c0df9757635cb9b7f862a6d2a223ab) where I generated grid bounds for a RotatedPole grid. The idea there was to generate the 1D bounds on the regular rlat/rlon grid and then use cartopy/pyproj to project them to lat/lon.

I compared this PR's implementation with the projection one and the model-provided bounds for a regional model grid at a resolution of 0.11° and I got this : image

The maps show the distance between the estimated bounds with the reference ones. Units are lat/lon degrees, so this means an error of maximum 2% of the grid resolution for this implementation. Seems reasonable to me, what do you think?

In the parent issue, there was a detailed proposition of moving the interesting grid and metric generation form xgcm to cf-xarray. I think it is still an interesting idea, but I felt the specific issue of 2D bounds was long overdue!

~CI issues seems to be unrelated to the PR, but rather to #365 and may be an update in xarray?~ Ok,there are indeed issues with this PR :sweat: .

aulemahal avatar Oct 21 '22 15:10 aulemahal