Skip to content

Commit bc675ca

Browse files
seismanyvonnefroehlichweiji14
authored
GMTDataArrayAccessor: Simplify the codes for the grid registration and gtype during initialization (#3483)
Co-authored-by: Yvonne Fröhlich <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent 9447988 commit bc675ca

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

pygmt/accessors.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
GMT accessor for :class:`xarray.DataArray`.
33
"""
44

5+
import contextlib
56
from pathlib import Path
67

78
import xarray as xr
@@ -115,22 +116,17 @@ class GMTDataArrayAccessor:
115116

116117
def __init__(self, xarray_obj):
117118
self._obj = xarray_obj
118-
119-
self._source = self._obj.encoding.get("source")
120-
if self._source is not None and Path(self._source).exists():
121-
try:
122-
# Get grid registration and grid type from the last two columns
123-
# of the shortened summary information of `grdinfo`.
119+
# Default to Gridline registration and Cartesian grid type
120+
self._registration = 0
121+
self._gtype = 0
122+
123+
# If the source file exists, get grid registration and grid type from the last
124+
# two columns of the shortened summary information of grdinfo.
125+
if (_source := self._obj.encoding.get("source")) and Path(_source).exists():
126+
with contextlib.suppress(ValueError):
124127
self._registration, self._gtype = map(
125-
int, grdinfo(self._source, per_column="n").split()[-2:]
128+
int, grdinfo(_source, per_column="n").split()[-2:]
126129
)
127-
except ValueError:
128-
self._registration = 0 # Default to Gridline registration
129-
self._gtype = 0 # Default to Cartesian grid type
130-
else:
131-
self._registration = 0 # Default to Gridline registration
132-
self._gtype = 0 # Default to Cartesian grid type
133-
del self._source
134130

135131
@property
136132
def registration(self):

0 commit comments

Comments
 (0)