Skip to content

Commit

Permalink
GMTDataArrayAccessor: Simplify the codes for the grid registration an…
Browse files Browse the repository at this point in the history
…d gtype during initialization (#3483)

Co-authored-by: Yvonne Fröhlich <[email protected]>
Co-authored-by: Wei Ji <[email protected]>
  • Loading branch information
3 people authored Oct 7, 2024
1 parent 9447988 commit bc675ca
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions pygmt/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
GMT accessor for :class:`xarray.DataArray`.
"""

import contextlib
from pathlib import Path

import xarray as xr
Expand Down Expand Up @@ -115,22 +116,17 @@ class GMTDataArrayAccessor:

def __init__(self, xarray_obj):
self._obj = xarray_obj

self._source = self._obj.encoding.get("source")
if self._source is not None and Path(self._source).exists():
try:
# Get grid registration and grid type from the last two columns
# of the shortened summary information of `grdinfo`.
# Default to Gridline registration and Cartesian grid type
self._registration = 0
self._gtype = 0

# If the source file exists, get grid registration and grid type from the last
# two columns of the shortened summary information of grdinfo.
if (_source := self._obj.encoding.get("source")) and Path(_source).exists():
with contextlib.suppress(ValueError):
self._registration, self._gtype = map(
int, grdinfo(self._source, per_column="n").split()[-2:]
int, grdinfo(_source, per_column="n").split()[-2:]
)
except ValueError:
self._registration = 0 # Default to Gridline registration
self._gtype = 0 # Default to Cartesian grid type
else:
self._registration = 0 # Default to Gridline registration
self._gtype = 0 # Default to Cartesian grid type
del self._source

@property
def registration(self):
Expand Down

0 comments on commit bc675ca

Please sign in to comment.