|
2 | 2 | Test the behaviour of the GMTDataArrayAccessor class.
|
3 | 3 | """
|
4 | 4 | import os
|
| 5 | +from calendar import day_abbr |
| 6 | +from pathlib import Path |
5 | 7 |
|
6 | 8 | import pytest
|
7 | 9 | import xarray as xr
|
8 | 10 | from packaging.version import Version
|
9 | 11 | from pygmt import clib, which
|
| 12 | +from pygmt.datasets import load_earth_relief |
10 | 13 | from pygmt.exceptions import GMTInvalidInput
|
11 | 14 |
|
12 | 15 | with clib.Session() as _lib:
|
@@ -98,3 +101,35 @@ def test_accessor_sliced_datacube():
|
98 | 101 | assert grid.gmt.gtype == 1 # geographic coordinate type
|
99 | 102 | finally:
|
100 | 103 | 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