File tree 1 file changed +10
-14
lines changed
1 file changed +10
-14
lines changed Original file line number Diff line number Diff line change 2
2
GMT accessor for :class:`xarray.DataArray`.
3
3
"""
4
4
5
+ import contextlib
5
6
from pathlib import Path
6
7
7
8
import xarray as xr
@@ -115,22 +116,17 @@ class GMTDataArrayAccessor:
115
116
116
117
def __init__ (self , xarray_obj ):
117
118
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 ):
124
127
self ._registration , self ._gtype = map (
125
- int , grdinfo (self . _source , per_column = "n" ).split ()[- 2 :]
128
+ int , grdinfo (_source , per_column = "n" ).split ()[- 2 :]
126
129
)
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
134
130
135
131
@property
136
132
def registration (self ):
You can’t perform that action at this time.
0 commit comments