Avoid overlapping bounds and/or gaps when points are irregularly spaced#622
Conversation
aulemahal
left a comment
There was a problem hiding this comment.
This looks good! Thanks!
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #622 +/- ##
==========================================
+ Coverage 85.78% 87.01% +1.22%
==========================================
Files 13 15 +2
Lines 2364 3227 +863
Branches 183 307 +124
==========================================
+ Hits 2028 2808 +780
- Misses 303 374 +71
- Partials 33 45 +12
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thank you for the quick review! It looks like there is an issue with coverage reporting: #623. |
| lower = ( | ||
| da.reset_index(dim) - bound_position * diff.isel({dim: slice(0, -1)}) | ||
| ).assign_coords({dim: da[dim]}) | ||
| upper = ( | ||
| da.reset_index(dim) + bound_position * diff.isel({dim: slice(1, None)}) | ||
| ).assign_coords({dim: da[dim]}) |
There was a problem hiding this comment.
oops, merged too early.
we could optimize here by calling reset_index once and assigning coords to result directly
There was a problem hiding this comment.
No worries, I'm happy to continue working on this. I'm still pretty new to xarray, but I think I found a better way to do this and opened a pull request in #627.
The current implementation of
cf.add_boundsmay introduce overlapping bounds or bounds with gaps in between when the points are irregularly spaced. This pull request changes the algorithm for guessing the bounds so the bounds are always in the middle of two points. The lower bound of the first point and the upper bound of the last point are extrapolated as before. This is similar to how this feature has been implemented iniris: https://github.com/SciTools/iris/blob/0fa0d6bfd0446ee5cd52c0ac6372e6bef5b250ec/lib/iris/coords.py#L2470-L2477It also fixes an issue where the dimensions of the bounds variable are transposed w.r.t. the main variable.