Allow DataArray to hold cell boundaries as coordinate variables
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
Cell boundaries can be either N+1 sized arrays as suggested by xgcm/xmitgcm#15, or (N,2) sized arrays as suggested by the CF convention. However, a DataArray cannot hold both kinds of coordinate variables because they contain a new dimension.
If you try to assign a new coordinate to a DataArray by dr.assign_coords(), you will get
ValueError: cannot add coordinates with new dimensions to a DataArray
On the other hand, if your DataSet contains cell boundary variables (for example, #667), the bounds will be dropped when you extract a single variable into a DataArray.
Having cell bounds available in a DataArray is important for a couple of applications:
-
Pass cell bounds to DataArray's plotting methods (xgcm/xmitgcm#15). I am aware of the discussion about inferring boundaries (#781). However, for the Cube-Sphere grid or the Lat-Lon-Cap grid (reference) which have tiles covering the poles, I have to explicitly pass cell bounds to the original plt.pcolormesh() to get a good-looking plot. (see this comment for details)
-
For conservative (i.e. area-weighted) regridding (mentioned in #486). Cell centers are enough for bilinear interpolation or other simple resamping, but for any Finite-Volume meshes, knowing the boundaries is crucial if you want to conserve the total amount of mass or flux.
Plotting or regridding will work fine if you pass cell bounds as an additional argument to a wrapper function. However, having a single DataArray object containing boundary information seems like a more elegant solution. Is it possible to let DataArray accept N+1 sized coordinate variables, and be able to inherit them from the parent DataSet? If that's too drastic, is it possible to write an accessor to extend DataArray's capability? Say, a "bound" accessor for a new attribute ds.bnd['lat_b'], which can be kept when a DataArray gets extracted (ds['data_var'].bnd['lat_b'] )? Does this make sense?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by examining DataArray.assign_coords() and the behavior when extracting a variable from a DataSet, focusing on why coordinates with new dimensions are rejected or dropped. Compare the N+1 and (N,2) boundary cases described in the issue. Done means a DataArray can retain cell-boundary coordinates from its parent DataSet without breaking existing coordinate behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100