|
3 | 3 | """
|
4 | 4 | import os
|
5 | 5 | import sys
|
| 6 | +from pathlib import Path |
6 | 7 |
|
7 | 8 | import pytest
|
8 | 9 | import xarray as xr
|
9 | 10 | from packaging.version import Version
|
10 | 11 | from pygmt import __gmt_version__, which
|
| 12 | +from pygmt.datasets import load_earth_relief |
11 | 13 | from pygmt.exceptions import GMTInvalidInput
|
12 | 14 |
|
13 | 15 |
|
@@ -101,3 +103,32 @@ def test_accessor_sliced_datacube():
|
101 | 103 | assert grid.gmt.gtype == 1 # geographic coordinate type
|
102 | 104 | finally:
|
103 | 105 | 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