Skip to content

Commit 37869d4

Browse files
committed
Add a unit test
1 parent bae08d1 commit 37869d4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

pygmt/tests/test_accessor.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
Test the behaviour of the GMTDataArrayAccessor class.
33
"""
44
import os
5+
from calendar import day_abbr
6+
from pathlib import Path
57

68
import pytest
79
import xarray as xr
810
from packaging.version import Version
911
from pygmt import clib, which
12+
from pygmt.datasets import load_earth_relief
1013
from pygmt.exceptions import GMTInvalidInput
1114

1215
with clib.Session() as _lib:
@@ -98,3 +101,35 @@ def test_accessor_sliced_datacube():
98101
assert grid.gmt.gtype == 1 # geographic coordinate type
99102
finally:
100103
os.remove(fname)
104+
105+
106+
def test_accessor_grid_source_file_not_found():
107+
"""
108+
Check that the accessor fallbacks to the default registration and gtype
109+
when grid.encoding["source"] is given but the file is not found.
110+
111+
Address issue https://github.com/GenericMappingTools/pygmt/issues/1984.
112+
"""
113+
grid = load_earth_relief(
114+
resolution="01d", region=[0, 5, -5, 5], registration="pixel"
115+
)
116+
# check the original grid
117+
assert len(grid.encoding["source"]) > 0
118+
assert grid.gmt.registration == 1
119+
assert grid.gmt.gtype == 1
120+
121+
# generate a new dataset
122+
dataset = grid.to_dataset(name="height")
123+
# source file is given but not found
124+
assert len(dataset.height.encoding["source"]) > 0
125+
assert not Path(dataset.height.encoding["source"]).exists()
126+
# fallback to default registration and gtype
127+
assert dataset.height.gmt.registration == 0
128+
assert dataset.height.gmt.gtype == 0
129+
130+
# manually set the registration and gtype
131+
dataset.height.gmt.registration = 1
132+
dataset.height.gmt.gtype = 1
133+
# the registration and gtype should be correct now.
134+
assert dataset.height.gmt.registration == 1
135+
assert dataset.height.gmt.gtype == 1

0 commit comments

Comments
 (0)