-
Notifications
You must be signed in to change notification settings - Fork 228
Initialize a GMTDataArrayAccessor #500
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
Changes from all commits
8a539b5
0ab88cd
49fabb3
8db355a
e33c5eb
896ed59
99dbd1f
f0ac444
b567eac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
""" | ||
Non-plot GMT modules. | ||
""" | ||
import xarray as xr | ||
|
||
from .clib import Session | ||
from .helpers import ( | ||
build_arg_string, | ||
|
@@ -214,3 +216,71 @@ def __exit__(self, exc_type, exc_value, traceback): | |
) | ||
with Session() as lib: | ||
lib.call_module("set", arg_str) | ||
|
||
|
||
@xr.register_dataarray_accessor("gmt") | ||
class GMTDataArrayAccessor: | ||
""" | ||
This is the GMT extension for :class:`xarray.DataArray`. | ||
|
||
You can access various GMT specific metadata about your grid as follows: | ||
|
||
>>> from pygmt.datasets import load_earth_relief | ||
>>> # Use the global Earth relief grid with 1 degree spacing | ||
>>> grid = load_earth_relief(resolution="01d") | ||
|
||
>>> # See if grid uses Gridline (0) or Pixel (1) registration | ||
>>> grid.gmt.registration | ||
1 | ||
>>> # See if grid uses Cartesian (0) or Geographic (1) coordinate system | ||
>>> grid.gmt.gtype | ||
1 | ||
""" | ||
|
||
def __init__(self, xarray_obj): | ||
self._obj = xarray_obj | ||
try: | ||
self._source = self._obj.encoding["source"] # filepath to NetCDF source | ||
# From the shortened summary information of `grdinfo`, | ||
# get grid registration in column 10, and grid type in column 11 | ||
self._registration, self._gtype = map( | ||
int, grdinfo(self._source, C="n", o="10,11").split() | ||
) | ||
except KeyError: | ||
self._registration = 0 # Default to Gridline registration | ||
self._gtype = 0 # Default to Cartesian grid type | ||
|
||
@property | ||
def registration(self): | ||
""" | ||
Registration type of the grid, either Gridline (0) or Pixel (1). | ||
""" | ||
return self._registration | ||
|
||
@registration.setter | ||
def registration(self, value): | ||
if value in (0, 1): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be more user friendly if we can also set the properties by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. Let's leave it for now, but introduce this once we work out how to do the |
||
self._registration = value | ||
else: | ||
raise GMTInvalidInput( | ||
f"Invalid grid registration value: {value}, should be a boolean of " | ||
"either 0 for Gridline registration or 1 for Pixel registration" | ||
) | ||
|
||
@property | ||
def gtype(self): | ||
""" | ||
Coordinate system type of the grid, either Cartesian (0) or Geographic | ||
(1). | ||
""" | ||
return self._gtype | ||
|
||
@gtype.setter | ||
def gtype(self, value): | ||
if value in (0, 1): | ||
self._gtype = value | ||
else: | ||
raise GMTInvalidInput( | ||
f"Invalid coordinate system type: {value}, should be a boolean of " | ||
"either 0 for Cartesian or 1 for Geographic" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Test the behaviour of the GMTDataArrayAccessor class | ||
""" | ||
import pytest | ||
import xarray as xr | ||
|
||
from .. import which | ||
from ..exceptions import GMTInvalidInput | ||
|
||
|
||
def test_accessor_gridline_cartesian(): | ||
""" | ||
Check that a grid returns a registration value of 0 when Gridline | ||
registered, and a gtype value of 1 when using Geographic coordinates. | ||
""" | ||
fname = which(fname="@test.dat.nc", download="a") | ||
grid = xr.open_dataarray(fname) | ||
assert grid.gmt.registration == 0 # gridline registration | ||
assert grid.gmt.gtype == 0 # cartesian coordinate type | ||
|
||
|
||
def test_accessor_pixel_geographic(): | ||
""" | ||
Check that a grid returns a registration value of 1 when Pixel registered, | ||
and a gtype value of 0 when using Cartesian coordinates. | ||
""" | ||
fname = which(fname="@earth_relief_01d_p", download="a") | ||
grid = xr.open_dataarray(fname) | ||
assert grid.gmt.registration == 1 # pixel registration | ||
assert grid.gmt.gtype == 1 # geographic coordinate type | ||
|
||
|
||
def test_accessor_set_pixel_registration(): | ||
""" | ||
Check that we can set a grid to be Pixel registered with a registration | ||
value of 1. | ||
""" | ||
grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]]) | ||
assert grid.gmt.registration == 0 # default to gridline registration | ||
grid.gmt.registration = 1 # set to pixel registration | ||
assert grid.gmt.registration == 1 # ensure changed to pixel registration | ||
|
||
|
||
def test_accessor_set_geographic_cartesian_roundtrip(): | ||
""" | ||
Check that we can set a grid to switch between the default Cartesian | ||
coordinate type using a gtype of 1, set it to Geographic 0, and then back | ||
to Cartesian again 1. | ||
""" | ||
grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]]) | ||
assert grid.gmt.gtype == 0 # default to cartesian coordinate type | ||
grid.gmt.gtype = 1 # set to geographic type | ||
assert grid.gmt.gtype == 1 # ensure changed to geographic coordinate type | ||
grid.gmt.gtype = 0 # set back to cartesian type | ||
assert grid.gmt.gtype == 0 # ensure changed to cartesian coordinate type | ||
|
||
|
||
def test_accessor_set_non_boolean(): | ||
""" | ||
Check that setting non boolean values on registration and gtype do not work | ||
""" | ||
grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]]) | ||
|
||
with pytest.raises(GMTInvalidInput): | ||
grid.gmt.registration = "2" | ||
|
||
with pytest.raises(GMTInvalidInput): | ||
grid.gmt.gtype = 2 |
Uh oh!
There was an error while loading. Please reload this page.