File tree Expand file tree Collapse file tree 1 file changed +10
-14
lines changed Expand file tree Collapse file tree 1 file changed +10
-14
lines changed Original file line number Diff line number Diff line change 22GMT accessor for :class:`xarray.DataArray`.
33"""
44
5+ import contextlib
56from pathlib import Path
67
78import 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 ):
You can’t perform that action at this time.
0 commit comments