-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make a gmt xarray accessor to store metadata from grdinfo #499
Comments
More in-depth discussions are available at pydata/xarray#3205 and pydata/xarray#3268. To understand these limitations, here is a simplified version of the import xarray as xr
@xr.register_dataarray_accessor("gmt")
class GMTDataArrayAccessor:
def __init__(self, obj):
print("Inialize the gmt accessor")
self._obj = obj
self._registration = 0
self._gtype = 0
@property
def registration(self):
print("Returning registration")
return self._registration
@registration.setter
def registration(self, value):
print("Setting registration")
self._registration = value
@property
def gtype(self):
print("Returning gtype")
return self._gtype
@gtype.setter
def gtype(self, value):
print("Setting gtype")
self._gtype = value |
The following test shows that accessors are created/initialized after any grid operations (like slice and arithmetic operations): import numpy as np
import xarray as xp
interval = 2.5
lat = np.arange(90, -90 - interval, -interval)
lon = np.arange(0, 360 + interval, interval)
longrid, latgrid = np.meshgrid(lon, lat)
data = np.sin(np.deg2rad(longrid)) * np.cos(np.deg2rad(latgrid))
grid = xr.DataArray(data, coords=[("latitude", lat), ("longitude", lon)])
print(grid.gmt.registration, grid.gmt.gtype)
grid.gmt.registration = 1
grid.gmt.gtype = 1
print(grid.gmt.registration, grid.gmt.gtype)
sliced_grid = grid[0:30, 0:30]
print(sliced_grid.gmt.registration, sliced_grid.gmt.gtype)
grid *= 2.0
print(grid.gmt.registration, grid.gmt.gtype)
grid2 = grid * 2.0
print(grid2.gmt.registration, grid2.gmt.gtype) The output of the script is:
|
The following test shows that accesors are created/initialized when accessing a DataArray from a Dataset. (The test is modified from pydata/xarray#3205 (comment)) import xarray as xr
import numpy as np
ds = xr.Dataset({'a': xr.DataArray(np.array([1, 2, 3])), 'b': xr.DataArray(np.array([4, 5, 6]))})
print(ds["a"].gmt.registration, ds["a"].gmt.gtype)
print(ds["a"].gmt.registration, ds["a"].gmt.gtype)
var = ds["a"]
print(var.gmt.registration, var.gmt.gtype)
print(var.gmt.registration, var.gmt.gtype) The output is:
|
Originally posted by @seisman in #2398 (comment). Keeping track of the
|
Here is an idea to improve user experiences with GMT accessor property Here is an example class to demonstrate the implementation: class GMTAccessor:
_registration_mapping = {
"gridline": 0,
"pixel": 1,
}
@property
def registration(self):
return {v: k for k, v in self._registration_mapping.items()}[self._registration]
@registration.setter
def registration(self, value):
valid_values = [*self._registration_mapping.keys(), *self._registration_mapping.values()]
if value not in valid_values:
msg = f"Invalid registration value. Must be in {valid_values}."
raise ValueError(msg)
self._registration = self._registration_mapping.get(value, value) Here is an example showing how it works. Users can assign the more readable
The output is:
The new implementation is still a breaking change. In the old implementation, users can check if Thoughts? |
How about using an enum so that backwards compatibility is preserved? Something similar to the |
It means a steeper learning curve for users. Users have to write codes like:
|
But you can tab-complete the registration and gtype if they were enums 😆 I actually think the enums look much more readable. Can also consider making a |
I will see how it works later. Anyway, it won't be in the v0.13.0 release. |
Description of the desired feature
Metadata (data about data) is important, and there are specific kinds of metadata used by GMT when it decides how to plot a grid, such as:
Instead of explicitly wrapping
gmt info
orgmt grdinfo
as in #147, why not allow the user to just access it viagrid.gmt.someattribute
? This would make use of xarray accessors and callgrdinfo
under the hood to retrieve those metadata properties.Pros:
fig.basemap()
et al. use it automaticallycrs
and whatnot.Cons:
grid.attrs["someattribute"]
orpygmt.grdinfo(grid)
, but that's just a documentation thing.In reality, this
gmt
accessor can do more than just hold metadata. We could extend it to dogrid.gmt.plot
and more. But let's start with having it hold the metadata we want, and then build on top of it from there.References:
Other xarray accessor examples:
Are you willing to help implement and maintain this feature? Yes
The text was updated successfully, but these errors were encountered: