Skip to content

Commit

Permalink
Check invalid combination of resolution and registration in load_eart…
Browse files Browse the repository at this point in the history
…h_relief (GenericMappingTools#965)

*Add check to see the registration type when passing a registration type for a specific resolution, and raise an error if an invalid registration is passed.
*Add a test to confirm an error is raised when an invalid registration is passed.

Co-authored-by: Will Schlitzer <[email protected]>
  • Loading branch information
2 people authored and Josh Sixsmith committed Dec 21, 2022
1 parent 745ff56 commit f6cf9b4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
25 changes: 19 additions & 6 deletions pygmt/datasets/earth_relief.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
These grids can also be accessed by passing in the file name
**@earth_relief**\_\ *res*\[_\ *reg*] to any grid plotting/processing
function. *res* is the grid resolution (see below), and *reg* is grid
registration type (**p** for pixel registration or *g* for gridline
registration). Refer to :gmt-docs:`datasets/remote-data.html` for more
details.
registration type (**p** for pixel registration or **g** for gridline
registration).
Refer to :gmt-docs:`datasets/remote-data.html#global-earth-relief-grids`
for more details.
Parameters
----------
Expand All @@ -37,8 +39,10 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
or ``'01s'``.
region : str or list
The subregion of the grid to load. Required for Earth relief grids with
resolutions higher than 5 arc-minute (i.e., ``05m``).
The subregion of the grid to load, in the forms of a list
[*xmin*, *xmax*, *ymin*, *ymax*] or a string *xmin/xmax/ymin/ymax*.
Required for Earth relief grids with resolutions higher than 5
arc-minute (i.e., ``05m``).
registration : str
Grid registration type. Either ``pixel`` for pixel registration or
Expand Down Expand Up @@ -81,7 +85,7 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
reg = f"_{registration[0]}" if registration else ""
else:
raise GMTInvalidInput(
f"Invalid grid registration: {registration}, should be either "
f"Invalid grid registration: '{registration}', should be either "
"'pixel', 'gridline' or None. Default is None, where a "
"pixel-registered grid is returned unless only the "
"gridline-registered grid is available."
Expand All @@ -90,6 +94,15 @@ def load_earth_relief(resolution="01d", region=None, registration=None):
if resolution not in non_tiled_resolutions + tiled_resolutions:
raise GMTInvalidInput(f"Invalid Earth relief resolution '{resolution}'.")

# Check combination of resolution and registeration.
if (resolution == "15s" and registration == "gridline") or (
resolution in ("03s", "01s") and registration == "pixel"
):
raise GMTInvalidInput(
f"{registration}-registered Earth relief data for "
f"resolution '{resolution}' is not supported."
)

# different ways to load tiled and non-tiled earth relief data
# Known issue: tiled grids don't support slice operation
# See https://github.com/GenericMappingTools/pygmt/issues/524
Expand Down
14 changes: 14 additions & 0 deletions pygmt/tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,17 @@ def test_earth_relief_incorrect_registration():
"""
with pytest.raises(GMTInvalidInput):
load_earth_relief(registration="improper_type")


def test_earth_relief_invalid_resolution_registration_combination():
"""
Test loading earth relief with invalid combination of resolution and
registration.
"""
for resolution, registration in [
("15s", "gridline"),
("03s", "pixel"),
("01s", "pixel"),
]:
with pytest.raises(GMTInvalidInput):
load_earth_relief(resolution=resolution, registration=registration)

0 comments on commit f6cf9b4

Please sign in to comment.