Skip to content

Commit ad0d6ad

Browse files
seismanweiji14michaelgrund
authored
GMTDataArrayAccessor: Fallback to default grid registration and gtype if the grid source file doesn't exist (#2009)
Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Michael Grund <[email protected]>
1 parent 2dfd01e commit ad0d6ad

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

pygmt/accessors.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
GMT accessor methods.
33
"""
4+
from pathlib import Path
5+
46
import xarray as xr
57
from pygmt.exceptions import GMTInvalidInput
68
from pygmt.src.grdinfo import grdinfo
@@ -50,16 +52,22 @@ class GMTDataArrayAccessor:
5052

5153
def __init__(self, xarray_obj):
5254
self._obj = xarray_obj
53-
try:
54-
self._source = self._obj.encoding["source"] # filepath to NetCDF source
55-
# Get grid registration and grid type from the last two columns of
56-
# the shortened summary information of `grdinfo`.
57-
self._registration, self._gtype = map(
58-
int, grdinfo(self._source, per_column="n").split()[-2:]
59-
)
60-
except (KeyError, ValueError):
55+
56+
self._source = self._obj.encoding.get("source")
57+
if self._source is not None and Path(self._source).exists():
58+
try:
59+
# Get grid registration and grid type from the last two columns
60+
# of the shortened summary information of `grdinfo`.
61+
self._registration, self._gtype = map(
62+
int, grdinfo(self._source, per_column="n").split()[-2:]
63+
)
64+
except ValueError:
65+
self._registration = 0 # Default to Gridline registration
66+
self._gtype = 0 # Default to Cartesian grid type
67+
else:
6168
self._registration = 0 # Default to Gridline registration
6269
self._gtype = 0 # Default to Cartesian grid type
70+
del self._source
6371

6472
@property
6573
def registration(self):

pygmt/tests/test_accessor.py

+31
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
"""
44
import os
55
import sys
6+
from pathlib import Path
67

78
import pytest
89
import xarray as xr
910
from packaging.version import Version
1011
from pygmt import __gmt_version__, which
12+
from pygmt.datasets import load_earth_relief
1113
from pygmt.exceptions import GMTInvalidInput
1214

1315

@@ -101,3 +103,32 @@ def test_accessor_sliced_datacube():
101103
assert grid.gmt.gtype == 1 # geographic coordinate type
102104
finally:
103105
os.remove(fname)
106+
107+
108+
def test_accessor_grid_source_file_not_exist():
109+
"""
110+
Check that the accessor fallbacks to the default registration and gtype
111+
when the grid source file (i.e., grid.encoding["source"]) doesn't exist.
112+
"""
113+
# Load the 05m earth relief grid, which is stored as tiles
114+
grid = load_earth_relief(
115+
resolution="05m", region=[0, 5, -5, 5], registration="pixel"
116+
)
117+
# Registration and gtype are correct
118+
assert grid.gmt.registration == 1
119+
assert grid.gmt.gtype == 1
120+
# The source grid file is defined but doesn't exist
121+
assert grid.encoding["source"].endswith(".nc")
122+
assert not Path(grid.encoding["source"]).exists()
123+
124+
# For a sliced grid, fallback to default registration and gtype,
125+
# because the source grid file doesn't exist.
126+
sliced_grid = grid[1:3, 1:3]
127+
assert sliced_grid.gmt.registration == 0
128+
assert sliced_grid.gmt.gtype == 0
129+
130+
# Still possible to manually set registration and gtype
131+
sliced_grid.gmt.registration = 1
132+
sliced_grid.gmt.gtype = 1
133+
assert sliced_grid.gmt.registration == 1
134+
assert sliced_grid.gmt.gtype == 1

0 commit comments

Comments
 (0)