SHTOOLS / SHTOOLS

SHTOOLS - Spherical Harmonic Tools

Home Page:https://shtools.github.io/SHTOOLS/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convert SHGrid class to a subclass of xarray.DataArray

MarkWieczorek opened this issue · comments

Desired feature

The SHGrid class is a custom class that was initially designed to support grids of data for use with spherical harmonic transforms, metadata, standard arithemetic operations, spherical harmonic transforms, and simple plotting. Most of these operations are currently supported by the xarray package using the DataArray class. Though SHGrid class instances can be converted to an xarray DataArray, for simplicity, and for additional features that are included in the DataArray class, it might instead be best to do away with the custom SHGrid class, and simply make SHGrid a subclass of DataArray.

Problems to resolve

There are two ways of doing this.

First, you could subclass DataArray, add the custom method .expand() for spherical harmonic transforms, add the metadata in the DataArray attributes, and override the xarray .plot() routine with the custom pyshtools .plot() routine.

Second, as opposed to overriding the xarray .plot() routine, one could instead create a custom shtools dataset accessor that would allow the new routines to be called as

grid.shtools.plot()
grid.shtools.plotgmt()
coeffs = grid.shtools.expand()

The benefit of using an accessor is that the original xarray function remains untouched. The drawback is that the notation is rather heavy, and the user is certain to call grid.plot() many times by accident. At some point in the future DataArray.plot() might do everything that SHGrid.plot() does, but we are not yet to that point.

Lastly, thought should be given to remaining backward compatible with the original SHGrid class. For example, with an xarray DataArray, it will no longer be necessary to have the .lats() and .lons() methods, as the latitudes and longitudes are stored directly in the DataArray class. The choice is thus either to deprecate these old functions or to keep them for backwards compatibility (perhaps without providing documentation to keep this hidden).

If this feature is implemented, this would be released as pyshtools 5.0 if backward compatibility could not be assured.