From f6cf9b4a7445c91e731a3a15de60d0d35607f860 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 26 Feb 2021 07:55:38 -0500 Subject: [PATCH] Check invalid combination of resolution and registration in load_earth_relief (#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 --- pygmt/datasets/earth_relief.py | 25 +++++++++++++++++++------ pygmt/tests/test_datasets.py | 14 ++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/pygmt/datasets/earth_relief.py b/pygmt/datasets/earth_relief.py index c906d064f70..d57948e957e 100644 --- a/pygmt/datasets/earth_relief.py +++ b/pygmt/datasets/earth_relief.py @@ -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 ---------- @@ -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 @@ -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." @@ -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 diff --git a/pygmt/tests/test_datasets.py b/pygmt/tests/test_datasets.py index 6bdde8da40b..31728f4cbbf 100644 --- a/pygmt/tests/test_datasets.py +++ b/pygmt/tests/test_datasets.py @@ -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)