From 8c7f8f676093546e7ad4b41c84899bd1c4891dfd Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 24 Sep 2024 10:57:01 +0800 Subject: [PATCH 01/21] Add enums GridReg and GridType for grid registration and type --- pygmt/enums.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pygmt/enums.py b/pygmt/enums.py index 7c0984eccb9..1bc42627913 100644 --- a/pygmt/enums.py +++ b/pygmt/enums.py @@ -37,3 +37,21 @@ class GridFormat(IntEnum): GD = 22 #: Import through GDAL EI = 23 #: ESRI Arc/Info ASCII Grid Interchange format (ASCII integer) EF = 24 #: ESRI Arc/Info ASCII Grid Interchange format (ASCII float) + + +class GridReg(IntEnum): + """ + Enum for the grid registration. + """ + + GRIDLINE = 0 #: Gridline registration + PIXEL = 1 #: Pixel registration + + +class GridType(IntEnum): + """ + Enum for the grid type. + """ + + CARTESIAN = 0 #: Cartesian grid + GEOGRAPHIC = 1 #: Geographic grid From 94086f70a27fa1faba04f184631b4e6ca621b154 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 24 Sep 2024 14:54:52 +0800 Subject: [PATCH 02/21] Use enums GridReg and GridType in gmt accessors --- pygmt/accessors.py | 162 ++++++++++++++++++++++++++------------------- 1 file changed, 95 insertions(+), 67 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index ec73f69238f..f23e3a2af87 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -6,6 +6,7 @@ from pathlib import Path import xarray as xr +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.src.grdinfo import grdinfo @@ -15,110 +16,131 @@ class GMTDataArrayAccessor: """ GMT accessor for :class:`xarray.DataArray`. - The accessor extends :class:`xarray.DataArray` to store GMT-specific - properties about grids, which are important for PyGMT to correctly process - and plot the grids. + The *gmt* accessor extends :class:`xarray.DataArray` to store GMT-specific + properties for grids, which are important for PyGMT to correctly process and plot + the grids. + + The *gmt* accessor contains following properties: + + - ``registration``: Grid registration type, either ``GridReg.GRIDLINE`` or + ``GridReg.PIXEL``. + - ``gtype``: Grid coordinate system type, either ``GridType.CARTESIAN`` or + ``GridType.GEOGRAPHIC``. + + and can be accessed like ``grid.gmt.registration`` and ``grid.gmt.gtype``. Notes ----- - - Due to the limitations of xarray accessors, the GMT accessors are created - once per :class:`xarray.DataArray` instance. You may lose these - GMT-specific properties when manipulating grids (e.g., arithmetic and slice - operations) or when accessing a :class:`xarray.DataArray` from a - :class:`xarray.Dataset`. In these cases, you need to manually set these - properties before passing the grid to PyGMT. + Due to the limitations of xarray accessors, the GMT accessors are created once per + :class:`xarray.DataArray` instance. You may lose these GMT-specific properties when + manipulating grids (e.g., arithmetic and slice operations) or when accessing a + :class:`xarray.DataArray` from a :class:`xarray.Dataset`. In these cases, you need + to manually set these properties before passing the grid to PyGMT. Examples -------- - - For GMT's built-in remote datasets, these GMT-specific properties are - automatically determined and you can access them as follows: + For GMT's built-in remote datasets, these GMT-specific properties are automatically + determined and you can access them as follows: >>> from pygmt.datasets import load_earth_relief >>> # Use the global Earth relief grid with 1 degree spacing >>> grid = load_earth_relief(resolution="01d", registration="pixel") - >>> # See if grid uses Gridline (0) or Pixel (1) registration + >>> # See if grid is Gridline or Pixel registration >>> grid.gmt.registration - 1 - >>> # See if grid uses Cartesian (0) or Geographic (1) coordinate system + + >>> # See if grid is in Cartesian or Geographic coordinate system >>> grid.gmt.gtype - 1 + - For :class:`xarray.DataArray` grids created by yourself, grid properties - ``registration`` and ``gtype`` default to 0 (i.e., a gridline-registered, - Cartesian grid). You need to set the correct properties before + For :class:`xarray.DataArray` grids created by yourself, ``registration`` and + ``gtype`` default to ``GridReg.GRIDLINE`` and ``GridType.CARTESIAN`` (i.e., a + gridline-registered, Cartesian grid). You need to set the correct properties before passing it to PyGMT functions: >>> import numpy as np - >>> import pygmt >>> import xarray as xr - >>> # create a DataArray in gridline coordinates of sin(lon) * cos(lat) + >>> import pygmt + >>> from pygmt.enums import GridReg, GridType + >>> # Create a DataArray in gridline coordinates of sin(lon) * cos(lat) >>> interval = 2.5 >>> lat = np.arange(90, -90 - interval, -interval) >>> lon = np.arange(0, 360 + interval, interval) >>> longrid, latgrid = np.meshgrid(lon, lat) >>> data = np.sin(np.deg2rad(longrid)) * np.cos(np.deg2rad(latgrid)) >>> grid = xr.DataArray(data, coords=[("latitude", lat), ("longitude", lon)]) - >>> # default to a gridline-registered Cartesian grid - >>> grid.gmt.registration, grid.gmt.gtype - (0, 0) - >>> # set it to a gridline-registered geographic grid - >>> grid.gmt.registration = 0 - >>> grid.gmt.gtype = 1 - >>> grid.gmt.registration, grid.gmt.gtype - (0, 1) - - Note that the accessors are created once per :class:`xarray.DataArray` - instance, so you may lose these GMT-specific properties after manipulating - your grid. + >>> # Default to a gridline-registrated Cartesian grid + >>> grid.gmt.registration + + >>> grid.gmt.gtype + + >>> # Manually set it to a gridline-registered geographic grid + >>> grid.gmt.registration = GridReg.GRIDLINE + >>> grid.gmt.gtype = GridType.GEOGRAPHIC + >>> grid.gmt.registration + + >>> grid.gmt.gtype + + + Note that the accessors are created once per :class:`xarray.DataArray` instance, so + you may lose these GMT-specific properties after manipulating your grid. Inplace assignment operators like ``*=`` don't create new instances, so the properties are still kept: >>> grid *= 2.0 - >>> grid.gmt.registration, grid.gmt.gtype - (0, 1) + >>> grid.gmt.registration + + >>> grid.gmt.gtype + - Other grid operations (e.g., arithmetic or slice operations) create new - instances, so the properties will be lost: + Other grid operations (e.g., arithmetic or slice operations) create new instances, + so the properties will be lost: >>> # grid2 is a slice of the original grid >>> grid2 = grid[0:30, 50:80] - >>> # properties are reset to the default values for new instance - >>> grid2.gmt.registration, grid2.gmt.gtype - (0, 0) - >>> # need to set these properties before passing the grid to PyGMT + >>> # Properties are reset to the default values for new instance + >>> grid2.gmt.registration + + >>> grid2.gmt.gtype + + >>> # Need to set these properties before passing the grid to PyGMT >>> grid2.gmt.registration = grid.gmt.registration >>> grid2.gmt.gtype = grid.gmt.gtype - >>> grid2.gmt.registration, grid2.gmt.gtype - (0, 1) + >>> grid2.gmt.registration + + >>> grid2.gmt.gtype + - Accessing a :class:`xarray.DataArray` from a :class:`xarray.Dataset` always - creates new instances, so these properties are always lost. The workaround - is to assign the :class:`xarray.DataArray` into a variable: + Accessing a :class:`xarray.DataArray` from a :class:`xarray.Dataset` always creates + new instances, so these properties are always lost. The workaround is to assign the + :class:`xarray.DataArray` into a variable: >>> ds = xr.Dataset({"zval": grid}) + >>> ds.zval.gmt.registration + + >>> ds.zval.gmt.gtype + + >>> # Manually set these properties won't work as expected + >>> ds.zval.gmt.registration = GridReg.GRIDLINE + >>> ds.zval.gmt.gtype = GridType.GEOGRAPHIC >>> ds.zval.gmt.registration, ds.zval.gmt.gtype - (0, 0) - >>> # manually set these properties won't work as expected - >>> ds.zval.gmt.registration, ds.zval.gmt.gtype = 0, 1 - >>> ds.zval.gmt.registration, ds.zval.gmt.gtype - (0, 0) + (, ) >>> # workaround: assign the DataArray into a variable >>> zval = ds.zval >>> zval.gmt.registration, zval.gmt.gtype - (0, 0) - >>> zval.gmt.registration, zval.gmt.gtype = 0, 1 + (, ) + >>> zval.gmt.registration = GridReg.GRIDLINE + >>> zval.gmt.gtype = GridType.GEOGRAPHIC >>> zval.gmt.registration, zval.gmt.gtype - (0, 1) + (, ) """ def __init__(self, xarray_obj): self._obj = xarray_obj + # Default to Gridline registration and Cartesian grid type - self._registration = 0 - self._gtype = 0 + self._registration = GridReg.GRIDLINE + self._gtype = GridType.CARTESIAN # If the source file exists, get grid registration and grid type from the last # two columns of the shortened summary information of grdinfo. @@ -131,33 +153,39 @@ def __init__(self, xarray_obj): @property def registration(self): """ - Registration type of the grid, either 0 (Gridline) or 1 (Pixel). + Registration type of the grid, either ``GridReg.GRIDLINE`` or + ``GridReg.PIXEL``. """ return self._registration @registration.setter def registration(self, value): - if value not in {0, 1}: + if value in {"gridline", "pixel"}: # Support for string-type values + value = GridReg[value.upper()] + if value not in GridReg: msg = ( - f"Invalid grid registration value: {value}, should be either " - "0 for Gridline registration or 1 for Pixel registration." + f"Invalid grid registration: {value}. " + "Should be either GridReg.GRIDLINE or GridReg.PIXEL." ) raise GMTInvalidInput(msg) - self._registration = value + self._registration = GridReg(value) @property def gtype(self): """ - Coordinate system type of the grid, either 0 (Cartesian) or 1 (Geographic). + Coordinate system type of the grid, either ``GridType.CARTESIAN`` or + ``GridType.GEOGRAPHIC``. """ return self._gtype @gtype.setter def gtype(self, value): - if value not in {0, 1}: + if value in {"cartesian", "geographic"}: # Support for string-type values + value = GridType[value.upper()] + if value not in GridType: msg = ( - f"Invalid coordinate system type: {value}, should be " - "either 0 for Cartesian or 1 for Geographic." + f"Invalid grid coordinate system type: '{value}'. " + "Should be either GridType.CARTESIAN or GridType.GEOGRAPHIC." ) raise GMTInvalidInput(msg) - self._gtype = value + self._gtype = GridType(value) From 8dd3d5f34eee1c32ac3d6e83c039cd25a621e040 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 18 Dec 2024 00:02:08 +0800 Subject: [PATCH 03/21] Use enums GridType and GridReg in tests --- pygmt/datatypes/grid.py | 2 +- pygmt/datatypes/image.py | 2 +- pygmt/src/tilemap.py | 3 ++- pygmt/tests/test_binstats.py | 5 +++-- pygmt/tests/test_datasets_earth_age.py | 7 +++--- pygmt/tests/test_datasets_earth_day.py | 9 ++++---- .../test_datasets_earth_free_air_anomaly.py | 7 +++--- pygmt/tests/test_datasets_earth_geoid.py | 7 +++--- .../test_datasets_earth_magnetic_anomaly.py | 13 ++++++----- pygmt/tests/test_datasets_earth_mask.py | 9 ++++---- pygmt/tests/test_datasets_earth_night.py | 9 ++++---- pygmt/tests/test_datasets_earth_relief.py | 17 +++++++------- ...atasets_earth_vertical_gravity_gradient.py | 7 +++--- .../test_datasets_load_remote_datasets.py | 3 ++- pygmt/tests/test_datasets_mars_relief.py | 7 +++--- pygmt/tests/test_datasets_mercury_relief.py | 7 +++--- pygmt/tests/test_datasets_moon_relief.py | 7 +++--- pygmt/tests/test_datasets_pluto_relief.py | 7 +++--- pygmt/tests/test_datasets_venus_relief.py | 7 +++--- pygmt/tests/test_dimfilter.py | 5 +++-- pygmt/tests/test_grdclip.py | 9 ++++---- pygmt/tests/test_grdfill.py | 9 ++++---- pygmt/tests/test_grdfilter.py | 5 +++-- pygmt/tests/test_grdgradient.py | 5 +++-- pygmt/tests/test_grdhisteq.py | 5 +++-- pygmt/tests/test_grdimage.py | 22 +++++++++++-------- pygmt/tests/test_grdlandmask.py | 5 +++-- pygmt/tests/test_grdproject.py | 7 +++--- pygmt/tests/test_grdsample.py | 11 +++++----- pygmt/tests/test_io.py | 8 +++---- pygmt/tests/test_nearneighbor.py | 5 +++-- pygmt/tests/test_sph2grd.py | 5 +++-- pygmt/tests/test_sphdistance.py | 9 ++++---- pygmt/tests/test_sphinterpolate.py | 5 +++-- pygmt/tests/test_surface.py | 5 +++-- pygmt/tests/test_triangulate.py | 9 ++++---- pygmt/tests/test_xyz2grd.py | 5 +++-- 37 files changed, 153 insertions(+), 116 deletions(-) diff --git a/pygmt/datatypes/grid.py b/pygmt/datatypes/grid.py index 18ad679aeef..e774c746570 100644 --- a/pygmt/datatypes/grid.py +++ b/pygmt/datatypes/grid.py @@ -165,7 +165,7 @@ def to_dataarray(self) -> xr.DataArray: axis: Y actual_range: [-24. -10.] >>> da.gmt.registration, da.gmt.gtype - (1, 1) + (, ) """ # The grid header header = self.header.contents diff --git a/pygmt/datatypes/image.py b/pygmt/datatypes/image.py index 8534bfaacb3..f629837c263 100644 --- a/pygmt/datatypes/image.py +++ b/pygmt/datatypes/image.py @@ -166,7 +166,7 @@ def to_dataarray(self) -> xr.DataArray: axis: Y actual_range: [-90. 90.] >>> da.gmt.registration, da.gmt.gtype - (1, 1) + (, ) """ # The image header header = self.header.contents diff --git a/pygmt/src/tilemap.py b/pygmt/src/tilemap.py index e61cd82e868..0748c83a2c9 100644 --- a/pygmt/src/tilemap.py +++ b/pygmt/src/tilemap.py @@ -6,6 +6,7 @@ from pygmt.clib import Session from pygmt.datasets.tile_map import load_tile_map +from pygmt.enums import GridType from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias try: @@ -121,7 +122,7 @@ def tilemap( zoom_adjust=zoom_adjust, ) if lonlat: - raster.gmt.gtype = 1 # Set to geographic type + raster.gmt.gtype = GridType.GEOGRAPHIC # Only set region if no_clip is None or False, so that plot is clipped to exact # bounding box region diff --git a/pygmt/tests/test_binstats.py b/pygmt/tests/test_binstats.py index b8a1a39e8ad..133ac597e3c 100644 --- a/pygmt/tests/test_binstats.py +++ b/pygmt/tests/test_binstats.py @@ -7,6 +7,7 @@ import numpy.testing as npt import pytest from pygmt import binstats +from pygmt.enums import GridReg, GridType from pygmt.helpers import GMTTempFile @@ -42,8 +43,8 @@ def test_binstats_no_outgrid(): region="g", ) assert temp_grid.dims == ("y", "x") - assert temp_grid.gmt.gtype == 0 # Cartesian grid - assert temp_grid.gmt.registration == 0 # Gridline registration + assert temp_grid.gmt.gtype == GridType.CARTESIAN + assert temp_grid.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(temp_grid.max(), 35971536) npt.assert_allclose(temp_grid.min(), 53) npt.assert_allclose(temp_grid.median(), 1232714.5) diff --git a/pygmt/tests/test_datasets_earth_age.py b/pygmt/tests/test_datasets_earth_age.py index 67ade3dd3d9..da69182ed85 100644 --- a/pygmt/tests/test_datasets_earth_age.py +++ b/pygmt/tests/test_datasets_earth_age.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_age +from pygmt.enums import GridReg def test_earth_age_01d(): @@ -18,7 +19,7 @@ def test_earth_age_01d(): assert data.attrs["units"] == "Myr" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), 0.37, atol=0.01) @@ -31,7 +32,7 @@ def test_earth_age_01d_with_region(): """ data = load_earth_age(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), 11.13, atol=0.01) @@ -45,7 +46,7 @@ def test_earth_age_01m_default_registration(): """ data = load_earth_age(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_earth_day.py b/pygmt/tests/test_datasets_earth_day.py index f04d7f7f970..474e9c07744 100644 --- a/pygmt/tests/test_datasets_earth_day.py +++ b/pygmt/tests/test_datasets_earth_day.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_blue_marble +from pygmt.enums import GridReg, GridType def test_blue_marble_01d(): @@ -18,8 +19,8 @@ def test_blue_marble_01d(): assert data.attrs["description"] == "NASA Day Images" assert data.shape == (3, 180, 360) assert data.dtype == "uint8" - assert data.gmt.registration == 1 - assert data.gmt.gtype == 1 + assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.gtype == GridType.GEOGRAPHIC npt.assert_allclose(data.y, np.arange(89.5, -90.5, -1)) npt.assert_allclose(data.x, np.arange(-179.5, 180.5, 1)) npt.assert_allclose(data.min(), 10, atol=1) @@ -33,8 +34,8 @@ def test_blue_marble_01d_with_region(): data = load_blue_marble(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (3, 10, 20) assert data.dtype == "uint8" - assert data.gmt.registration == 1 - assert data.gmt.gtype == 1 + assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.gtype == GridType.GEOGRAPHIC npt.assert_allclose(data.y, np.arange(4.5, -5.5, -1)) npt.assert_allclose(data.x, np.arange(-9.5, 10.5, 1)) npt.assert_allclose(data.min(), 10, atol=1) diff --git a/pygmt/tests/test_datasets_earth_free_air_anomaly.py b/pygmt/tests/test_datasets_earth_free_air_anomaly.py index 517a1bb9b89..250bde9e286 100644 --- a/pygmt/tests/test_datasets_earth_free_air_anomaly.py +++ b/pygmt/tests/test_datasets_earth_free_air_anomaly.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_free_air_anomaly +from pygmt.enums import GridReg def test_earth_faa_01d(): @@ -18,7 +19,7 @@ def test_earth_faa_01d(): assert data.attrs["units"] == "mGal" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -188.85, atol=0.025) @@ -31,7 +32,7 @@ def test_earth_faa_01d_with_region(): """ data = load_earth_free_air_anomaly(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -36.125, atol=0.025) @@ -45,7 +46,7 @@ def test_earth_faa_01m_default_registration(): """ data = load_earth_free_air_anomaly(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (120, 60) - assert data.gmt.registration == 1 + assert data.gmt.registration == GridReg.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 3.008333333) npt.assert_allclose(data.coords["lat"].data.max(), 4.991666666) npt.assert_allclose(data.coords["lon"].data.min(), -9.99166666) diff --git a/pygmt/tests/test_datasets_earth_geoid.py b/pygmt/tests/test_datasets_earth_geoid.py index 84bfc5d7bf4..451b615c696 100644 --- a/pygmt/tests/test_datasets_earth_geoid.py +++ b/pygmt/tests/test_datasets_earth_geoid.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_geoid +from pygmt.enums import GridReg def test_earth_geoid_01d(): @@ -18,7 +19,7 @@ def test_earth_geoid_01d(): assert data.attrs["units"] == "m" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -106.06, atol=0.01) @@ -31,7 +32,7 @@ def test_earth_geoid_01d_with_region(): """ data = load_earth_geoid(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), 5.57, atol=0.01) @@ -45,7 +46,7 @@ def test_earth_geoid_01m_default_registration(): """ data = load_earth_geoid(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_earth_magnetic_anomaly.py b/pygmt/tests/test_datasets_earth_magnetic_anomaly.py index 65e1a57601c..05a31f5ea16 100644 --- a/pygmt/tests/test_datasets_earth_magnetic_anomaly.py +++ b/pygmt/tests/test_datasets_earth_magnetic_anomaly.py @@ -6,6 +6,7 @@ import numpy.testing as npt import pytest from pygmt.datasets import load_earth_magnetic_anomaly +from pygmt.enums import GridReg from pygmt.exceptions import GMTInvalidInput @@ -20,7 +21,7 @@ def test_earth_mag_01d(): assert data.attrs["units"] == "nT" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -336.2, atol=0.2) @@ -33,7 +34,7 @@ def test_earth_mag_01d_with_region(): """ data = load_earth_magnetic_anomaly(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -54.4, atol=0.2) @@ -47,7 +48,7 @@ def test_earth_mag_02m_default_registration(): """ data = load_earth_magnetic_anomaly(resolution="02m", region=[-10, -9, 3, 5]) assert data.shape == (60, 30) - assert data.gmt.registration == 1 + assert data.gmt.registration == GridReg.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 3.016666667) npt.assert_allclose(data.coords["lat"].data.max(), 4.983333333) npt.assert_allclose(data.coords["lon"].data.min(), -9.98333333) @@ -67,7 +68,7 @@ def test_earth_mag4km_01d(): assert data.attrs["units"] == "nT" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -436.8, atol=0.2) @@ -102,7 +103,7 @@ def test_earth_mag4km_02m_default_registration(): data_source="emag2_4km", ) assert data.shape == (60, 90) - assert data.gmt.registration == 1 + assert data.gmt.registration == GridReg.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 4.01666667) npt.assert_allclose(data.coords["lat"].data.max(), 5.98333333) npt.assert_allclose(data.coords["lon"].data.min(), -114.98333333) @@ -154,7 +155,7 @@ def test_earth_mag_03m_wdmam_with_region(): data = load_earth_magnetic_anomaly( resolution="03m", region=[10, 13, -60, -58], data_source="wdmam" ) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE assert data.shape == (41, 61) assert data.lat.min() == -60 assert data.lat.max() == -58 diff --git a/pygmt/tests/test_datasets_earth_mask.py b/pygmt/tests/test_datasets_earth_mask.py index c449e1a79a7..aebcb8b917e 100644 --- a/pygmt/tests/test_datasets_earth_mask.py +++ b/pygmt/tests/test_datasets_earth_mask.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_mask +from pygmt.enums import GridReg, GridType def test_earth_mask_01d(): @@ -16,8 +17,8 @@ def test_earth_mask_01d(): assert data.attrs["description"] == "GSHHG Earth mask" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 - assert data.gmt.gtype == 1 + assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.gtype == GridType.GEOGRAPHIC assert data.dtype == "int8" npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) @@ -32,8 +33,8 @@ def test_earth_mask_01d_with_region(): """ data = load_earth_mask(resolution="01d", region=[-7, 4, 13, 19]) assert data.shape == (7, 12) - assert data.gmt.registration == 0 - assert data.gmt.gtype == 1 + assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.gtype == GridType.GEOGRAPHIC assert data.dtype == "int8" npt.assert_allclose(data.lat, np.arange(13, 20, 1)) npt.assert_allclose(data.lon, np.arange(-7, 5, 1)) diff --git a/pygmt/tests/test_datasets_earth_night.py b/pygmt/tests/test_datasets_earth_night.py index dd6b3374033..4b42581e869 100644 --- a/pygmt/tests/test_datasets_earth_night.py +++ b/pygmt/tests/test_datasets_earth_night.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_black_marble +from pygmt.enums import GridReg, GridType def test_black_marble_01d(): @@ -18,8 +19,8 @@ def test_black_marble_01d(): assert data.attrs["description"] == "NASA Night Images" assert data.shape == (3, 180, 360) assert data.dtype == "uint8" - assert data.gmt.registration == 1 - assert data.gmt.gtype == 1 + assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.gtype == GridType.GEOGRAPHIC npt.assert_allclose(data.y, np.arange(89.5, -90.5, -1)) npt.assert_allclose(data.x, np.arange(-179.5, 180.5, 1)) npt.assert_allclose(data.min(), 3, atol=1) @@ -33,8 +34,8 @@ def test_black_marble_01d_with_region(): data = load_black_marble(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (3, 10, 20) assert data.dtype == "uint8" - assert data.gmt.registration == 1 - assert data.gmt.gtype == 1 + assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.gtype == GridType.GEOGRAPHIC npt.assert_allclose(data.y, np.arange(4.5, -5.5, -1)) npt.assert_allclose(data.x, np.arange(-9.5, 10.5, 1)) npt.assert_allclose(data.min(), 3, atol=1) diff --git a/pygmt/tests/test_datasets_earth_relief.py b/pygmt/tests/test_datasets_earth_relief.py index 44af0e4ff98..839e13f1a63 100644 --- a/pygmt/tests/test_datasets_earth_relief.py +++ b/pygmt/tests/test_datasets_earth_relief.py @@ -8,6 +8,7 @@ from packaging.version import Version from pygmt.clib import __gmt_version__ from pygmt.datasets import load_earth_relief +from pygmt.enums import GridReg from pygmt.exceptions import GMTInvalidInput @@ -25,7 +26,7 @@ def test_earth_relief_01d_igpp_synbath(data_source): assert data.attrs["vertical_datum"] == "EGM96" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -7174.0, atol=0.5) @@ -45,7 +46,7 @@ def test_earth_relief_01d_gebco(data_source): assert data.attrs["vertical_datum"] == "EGM96" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -7169.0, atol=1.0) @@ -62,7 +63,7 @@ def test_earth_relief_01d_with_region_srtm(): data_source="igpp", ) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -5118.0, atol=0.5) @@ -79,7 +80,7 @@ def test_earth_relief_01d_with_region_gebco(): data_source="gebco", ) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -5118.0, atol=1.0) @@ -92,7 +93,7 @@ def test_earth_relief_30m(): """ data = load_earth_relief(resolution="30m") assert data.shape == (361, 721) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 90.5, 0.5)) npt.assert_allclose(data.lon, np.arange(-180, 180.5, 0.5)) npt.assert_allclose(data.min(), -8279.5, atol=0.5) @@ -110,7 +111,7 @@ def test_earth_gebcosi_15m_with_region(): data_source="gebcosi", ) assert data.shape == (16, 8) - assert data.gmt.registration == 1 + assert data.gmt.registration == GridReg.PIXEL npt.assert_allclose(data.lat, np.arange(-87.875, -84, 0.25)) npt.assert_allclose(data.lon, np.arange(85.125, 87, 0.25)) npt.assert_allclose(data.min(), -492, atol=1.0) @@ -183,7 +184,7 @@ def test_earth_relief_15s_default_registration(): """ data = load_earth_relief(resolution="15s", region=[-10, -9.5, 4, 5]) assert data.shape == (240, 120) - assert data.gmt.registration == 1 + assert data.gmt.registration == GridReg.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 4.002083) npt.assert_allclose(data.coords["lat"].data.max(), 4.997917) npt.assert_allclose(data.coords["lon"].data.min(), -9.997917) @@ -203,7 +204,7 @@ def test_earth_relief_03s_default_registration(): """ data = load_earth_relief(resolution="03s", region=[-10, -9.8, 4.9, 5]) assert data.shape == (121, 241) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.coords["lat"].data.min(), 4.9) npt.assert_allclose(data.coords["lat"].data.max(), 5) npt.assert_allclose(data.coords["lon"].data.min(), -10) diff --git a/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py b/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py index 67070e46541..80ea3a3f981 100644 --- a/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py +++ b/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_vertical_gravity_gradient +from pygmt.enums import GridReg def test_earth_vertical_gravity_gradient_01d(): @@ -18,7 +19,7 @@ def test_earth_vertical_gravity_gradient_01d(): assert data.attrs["units"] == "Eotvos" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -40.1875, atol=1 / 32) @@ -34,7 +35,7 @@ def test_earth_vertical_gravity_gradient_01d_with_region(): resolution="01d", region=[-10, 10, -5, 5] ) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -5.34375, atol=1 / 32) @@ -50,7 +51,7 @@ def test_earth_vertical_gravity_gradient_01m_default_registration(): resolution="01m", region=[-10, -9, 3, 5] ) assert data.shape == (120, 60) - assert data.gmt.registration == 1 + assert data.gmt.registration == GridReg.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 3.008333333) npt.assert_allclose(data.coords["lat"].data.max(), 4.991666666) npt.assert_allclose(data.coords["lon"].data.min(), -9.99166666) diff --git a/pygmt/tests/test_datasets_load_remote_datasets.py b/pygmt/tests/test_datasets_load_remote_datasets.py index b2de1ebddfa..645af9b9cd0 100644 --- a/pygmt/tests/test_datasets_load_remote_datasets.py +++ b/pygmt/tests/test_datasets_load_remote_datasets.py @@ -4,6 +4,7 @@ import pytest from pygmt.datasets.load_remote_dataset import _load_remote_dataset +from pygmt.enums import GridReg from pygmt.exceptions import GMTInvalidInput @@ -30,7 +31,7 @@ def test_load_remote_dataset_benchmark_with_region(): assert data.attrs["long_name"] == "ages (Myr)" assert data.attrs["units"] == "Myr" assert data.attrs["horizontal_datum"] == "WGS84" - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE assert data.shape == (11, 21) # Can't access the cpt attribute using virtual files # assert data.attrs["cpt"] == "@earth_age.cpt" diff --git a/pygmt/tests/test_datasets_mars_relief.py b/pygmt/tests/test_datasets_mars_relief.py index 88c437848f6..f86dfa5378b 100644 --- a/pygmt/tests/test_datasets_mars_relief.py +++ b/pygmt/tests/test_datasets_mars_relief.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_mars_relief +from pygmt.enums import GridReg def test_mars_relief_01d(): @@ -17,7 +18,7 @@ def test_mars_relief_01d(): assert data.attrs["description"] == "NASA Mars (MOLA) relief" assert data.attrs["units"] == "meters" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -7421.0, atol=0.5) @@ -30,7 +31,7 @@ def test_mars_relief_01d_with_region(): """ data = load_mars_relief(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -2502.0, atol=0.5) @@ -44,7 +45,7 @@ def test_mars_relief_01m_default_registration(): """ data = load_mars_relief(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_mercury_relief.py b/pygmt/tests/test_datasets_mercury_relief.py index 65dbe62e0d1..a2504a319fe 100644 --- a/pygmt/tests/test_datasets_mercury_relief.py +++ b/pygmt/tests/test_datasets_mercury_relief.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_mercury_relief +from pygmt.enums import GridReg def test_mercury_relief_01d(): @@ -17,7 +18,7 @@ def test_mercury_relief_01d(): assert data.attrs["description"] == "USGS Mercury relief" assert data.attrs["units"] == "meters" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -5103.5, atol=0.5) @@ -30,7 +31,7 @@ def test_mercury_relief_01d_with_region(): """ data = load_mercury_relief(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -327.0, atol=0.5) @@ -44,7 +45,7 @@ def test_mercury_relief_01m_default_registration(): """ data = load_mercury_relief(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_moon_relief.py b/pygmt/tests/test_datasets_moon_relief.py index 87f10ded098..b0b6734dcc2 100644 --- a/pygmt/tests/test_datasets_moon_relief.py +++ b/pygmt/tests/test_datasets_moon_relief.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_moon_relief +from pygmt.enums import GridReg def test_moon_relief_01d(): @@ -17,7 +18,7 @@ def test_moon_relief_01d(): assert data.attrs["description"] == "USGS Moon (LOLA) relief" assert data.attrs["units"] == "meters" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -7669.0, atol=0.5) @@ -30,7 +31,7 @@ def test_moon_relief_01d_with_region(): """ data = load_moon_relief(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -1122.0, atol=0.5) @@ -44,7 +45,7 @@ def test_moon_relief_01m_default_registration(): """ data = load_moon_relief(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_pluto_relief.py b/pygmt/tests/test_datasets_pluto_relief.py index 9979f5915a5..f94f450832d 100644 --- a/pygmt/tests/test_datasets_pluto_relief.py +++ b/pygmt/tests/test_datasets_pluto_relief.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_pluto_relief +from pygmt.enums import GridReg def test_pluto_relief_01d(): @@ -17,7 +18,7 @@ def test_pluto_relief_01d(): assert data.attrs["description"] == "USGS Pluto relief" assert data.attrs["units"] == "meters" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -3021.0, atol=0.25) @@ -30,7 +31,7 @@ def test_pluto_relief_01d_with_region(): """ data = load_pluto_relief(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -1319.25, atol=0.25) @@ -44,7 +45,7 @@ def test_pluto_relief_01m_default_registration(): """ data = load_pluto_relief(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_venus_relief.py b/pygmt/tests/test_datasets_venus_relief.py index f2dc9a9489f..33dcf01fb92 100644 --- a/pygmt/tests/test_datasets_venus_relief.py +++ b/pygmt/tests/test_datasets_venus_relief.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_venus_relief +from pygmt.enums import GridReg def test_venus_relief_01d(): @@ -17,7 +18,7 @@ def test_venus_relief_01d(): assert data.attrs["description"] == "NASA Magellan Venus relief" assert data.attrs["units"] == "meters" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -2069.0, atol=0.5) @@ -30,7 +31,7 @@ def test_venus_relief_01d_with_region(): """ data = load_venus_relief(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -1244.0, atol=0.5) @@ -44,7 +45,7 @@ def test_venus_relief_01m_default_registration(): """ data = load_venus_relief(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridReg.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_dimfilter.py b/pygmt/tests/test_dimfilter.py index 9e998ac9980..9a3de9d582d 100644 --- a/pygmt/tests/test_dimfilter.py +++ b/pygmt/tests/test_dimfilter.py @@ -7,6 +7,7 @@ import pytest import xarray as xr from pygmt import dimfilter, load_dataarray +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -69,8 +70,8 @@ def test_dimfilter_no_outgrid(grid, expected_grid): grid=grid, filter="m600", distance=4, sectors="l6", region=[-55, -51, -24, -19] ) assert result.dims == ("lat", "lon") - assert result.gmt.gtype == 1 # Geographic grid - assert result.gmt.registration == 1 # Pixel registration + assert result.gmt.gtype == GridType.GEOGRAPHIC + assert result.gmt.registration == GridReg.PIXEL xr.testing.assert_allclose(a=result, b=expected_grid) diff --git a/pygmt/tests/test_grdclip.py b/pygmt/tests/test_grdclip.py index a0f2e4a8d7c..2c28e3c04a3 100644 --- a/pygmt/tests/test_grdclip.py +++ b/pygmt/tests/test_grdclip.py @@ -7,6 +7,7 @@ import pytest import xarray as xr from pygmt import grdclip, load_dataarray +from pygmt.enums import GridReg, GridType from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -51,8 +52,8 @@ def test_grdclip_outgrid(grid, expected_grid): assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists temp_grid = load_dataarray(tmpfile.name) assert temp_grid.dims == ("lat", "lon") - assert temp_grid.gmt.gtype == 1 # Geographic grid - assert temp_grid.gmt.registration == 1 # Pixel registration + assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC + assert temp_grid.gmt.registration == GridReg.PIXEL xr.testing.assert_allclose(a=temp_grid, b=expected_grid) @@ -65,6 +66,6 @@ def test_grdclip_no_outgrid(grid, expected_grid): grid=grid, below=[550, -1000], above=[700, 1000], region=[-53, -49, -19, -16] ) assert temp_grid.dims == ("lat", "lon") - assert temp_grid.gmt.gtype == 1 # Geographic grid - assert temp_grid.gmt.registration == 1 # Pixel registration + assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC + assert temp_grid.gmt.registration == GridReg.PIXEL xr.testing.assert_allclose(a=temp_grid, b=expected_grid) diff --git a/pygmt/tests/test_grdfill.py b/pygmt/tests/test_grdfill.py index f7c4730b744..d3bb4369f0e 100644 --- a/pygmt/tests/test_grdfill.py +++ b/pygmt/tests/test_grdfill.py @@ -8,6 +8,7 @@ import pytest import xarray as xr from pygmt import grdfill, load_dataarray +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -78,8 +79,8 @@ def test_grdfill_dataarray_out(grid, expected_grid): result = grdfill(grid=grid, mode="c20") # check information of the output grid assert isinstance(result, xr.DataArray) - assert result.gmt.gtype == 1 # Geographic grid - assert result.gmt.registration == 1 # Pixel registration + assert result.gmt.gtype == GridType.GEOGRAPHIC + assert result.gmt.registration == GridReg.PIXEL # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) @@ -93,8 +94,8 @@ def test_grdfill_asymmetric_pad(grid, expected_grid): result = grdfill(grid=grid, mode="c20", region=[-55, -50, -24, -16]) # check information of the output grid assert isinstance(result, xr.DataArray) - assert result.gmt.gtype == 1 # Geographic grid - assert result.gmt.registration == 1 # Pixel registration + assert result.gmt.gtype == GridType.GEOGRAPHIC + assert result.gmt.registration == GridReg.PIXEL # check information of the output grid xr.testing.assert_allclose( a=result, b=expected_grid.sel(lon=slice(-55, -50), lat=slice(-24, -16)) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 5cbe3574767..f5e5474fae5 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -8,6 +8,7 @@ import pytest import xarray as xr from pygmt import grdfilter, load_dataarray +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -50,8 +51,8 @@ def test_grdfilter_dataarray_in_dataarray_out(grid, expected_grid): ) # check information of the output grid assert isinstance(result, xr.DataArray) - assert result.gmt.gtype == 1 # Geographic grid - assert result.gmt.registration == 1 # Pixel registration + assert result.gmt.gtype == GridType.GEOGRAPHIC + assert result.gmt.registration == GridReg.PIXEL # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py index dc082d50d90..05258257dbe 100644 --- a/pygmt/tests/test_grdgradient.py +++ b/pygmt/tests/test_grdgradient.py @@ -7,6 +7,7 @@ import pytest import xarray as xr from pygmt import grdgradient, load_dataarray +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -66,8 +67,8 @@ def test_grdgradient_no_outgrid(grid, expected_grid): ) # check information of the output grid assert isinstance(result, xr.DataArray) - assert result.gmt.gtype == 1 # Geographic grid - assert result.gmt.registration == 1 # Pixel registration + assert result.gmt.gtype == GridType.GEOGRAPHIC + assert result.gmt.registration == GridReg.PIXEL # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) diff --git a/pygmt/tests/test_grdhisteq.py b/pygmt/tests/test_grdhisteq.py index 3c5a3df2d8d..7e212965615 100644 --- a/pygmt/tests/test_grdhisteq.py +++ b/pygmt/tests/test_grdhisteq.py @@ -9,6 +9,7 @@ import pytest import xarray as xr from pygmt import grdhisteq, load_dataarray +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -78,8 +79,8 @@ def test_equalize_grid_no_outgrid(grid, expected_grid, region): temp_grid = grdhisteq.equalize_grid( grid=grid, divisions=2, region=region, outgrid=None ) - assert temp_grid.gmt.gtype == 1 # Geographic grid - assert temp_grid.gmt.registration == 1 # Pixel registration + assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC + assert temp_grid.gmt.registration == GridReg.PIXEL xr.testing.assert_allclose(a=temp_grid, b=expected_grid) diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index 943b3f12ddc..01b875303e7 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -9,6 +9,7 @@ from pygmt import Figure from pygmt.clib import __gmt_version__ from pygmt.datasets import load_earth_relief +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers.testing import check_figures_equal @@ -164,14 +165,15 @@ def test_grdimage_over_dateline(xrgrid): """ Ensure no gaps are plotted over the 180 degree international dateline. - Specifically checking that `xrgrid.gmt.gtype = 1` sets `GMT_GRID_IS_GEO`, - and that `xrgrid.gmt.registration = 0` sets `GMT_GRID_NODE_REG`. Note that - there would be a gap over the dateline if a pixel registered grid is used. + Specifically checking that ``xrgrid.gmt.gtype = GridType.GEOGRAPHIC`` sets + ``GMT_GRID_IS_GEO``, and that ``xrgrid.gmt.registration = GridReg.GRIDLINE` sets + ``GMT_GRID_NODE_REG``. Note that there would be a gap over the dateline if a pixel + registered grid is used. See also https://github.com/GenericMappingTools/pygmt/issues/375. """ fig = Figure() - assert xrgrid.gmt.registration == 0 # gridline registration - xrgrid.gmt.gtype = 1 # geographic coordinate system + assert xrgrid.gmt.registration == GridReg.GRIDLINE + xrgrid.gmt.gtype = GridType.GEOGRAPHIC fig.grdimage(grid=xrgrid, region="g", projection="A0/0/1c") return fig @@ -188,8 +190,8 @@ def test_grdimage_global_subset(grid_360): """ # Get a slice of South America and Africa only (lat=-90:31, lon=-180:41) sliced_grid = grid_360[0:121, 0:221] - assert sliced_grid.gmt.registration == 0 # gridline registration - assert sliced_grid.gmt.gtype == 0 # Cartesian coordinate system + assert sliced_grid.gmt.registration == GridReg.GRIDLINE + assert sliced_grid.gmt.gtype == GridType.CARTESIAN fig = Figure() fig.grdimage( @@ -274,14 +276,16 @@ def test_grdimage_grid_no_redundant_360(): # Global grid [-180, 179, -90, 90] without redundant longitude at 180/-180 da3 = da1[:, 0:360] - da3.gmt.registration, da3.gmt.gtype = 0, 1 + da3.gmt.registration = GridReg.GRIDLINE + da3.gmt.gtype = GridType.GEOGRAPHIC assert da3.shape == (181, 360) assert da3.lon.to_numpy().min() == -180.0 assert da3.lon.to_numpy().max() == 179.0 # Global grid [0, 359, -90, 90] without redundant longitude at 360/0 da4 = da2[:, 0:360] - da4.gmt.registration, da4.gmt.gtype = 0, 1 + da4.gmt.registration = GridReg.GRIDLINE + da4.gmt.gtype = GridType.GEOGRAPHIC assert da4.shape == (181, 360) assert da4.lon.to_numpy().min() == 0.0 assert da4.lon.to_numpy().max() == 359.0 diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py index ae51ba2eda4..5c5e751e0a4 100644 --- a/pygmt/tests/test_grdlandmask.py +++ b/pygmt/tests/test_grdlandmask.py @@ -7,6 +7,7 @@ import pytest import xarray as xr from pygmt import grdlandmask, load_dataarray +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -53,8 +54,8 @@ def test_grdlandmask_no_outgrid(expected_grid): result = grdlandmask(spacing=1, region=[125, 130, 30, 35], cores=2) # check information of the output grid assert isinstance(result, xr.DataArray) - assert result.gmt.gtype == 1 # Geographic grid - assert result.gmt.registration == 0 # Gridline registration + assert result.gmt.gtype == GridType.GEOGRAPHIC + assert result.gmt.registration == GridReg.GRIDLINE # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) diff --git a/pygmt/tests/test_grdproject.py b/pygmt/tests/test_grdproject.py index 644ac311c54..8d2b326044d 100644 --- a/pygmt/tests/test_grdproject.py +++ b/pygmt/tests/test_grdproject.py @@ -7,6 +7,7 @@ import pytest import xarray as xr from pygmt import grdproject, load_dataarray +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -70,12 +71,12 @@ def test_grdproject_no_outgrid(grid, projection, expected_grid): Also check that providing the projection as an EPSG code or PROJ4 string works. """ - assert grid.gmt.gtype == 1 # Geographic grid + assert grid.gmt.gtype == GridType.GEOGRAPHIC result = grdproject( grid=grid, projection=projection, spacing=3, region=[-53, -51, -20, -17] ) - assert result.gmt.gtype == 0 # Rectangular grid - assert result.gmt.registration == 1 # Pixel registration + assert result.gmt.gtype == GridType.CARTESIAN + assert result.gmt.registration == GridReg.PIXEL # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) diff --git a/pygmt/tests/test_grdsample.py b/pygmt/tests/test_grdsample.py index 4c9e64139c3..276c710bee7 100644 --- a/pygmt/tests/test_grdsample.py +++ b/pygmt/tests/test_grdsample.py @@ -7,6 +7,7 @@ import pytest import xarray as xr from pygmt import grdsample, load_dataarray +from pygmt.enums import GridReg, GridType from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -78,8 +79,8 @@ def test_grdsample_dataarray_out(grid, expected_grid, region, spacing): result = grdsample(grid=grid, spacing=spacing, region=region, cores=2) # check information of the output grid assert isinstance(result, xr.DataArray) - assert result.gmt.gtype == 1 # Geographic grid - assert result.gmt.registration == 1 # Pixel registration + assert result.gmt.gtype == GridType.GEOGRAPHIC + assert result.gmt.registration == GridReg.PIXEL # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) @@ -88,8 +89,8 @@ def test_grdsample_registration_changes(grid): """ Test grdsample with no set outgrid and applying registration changes. """ - assert grid.gmt.registration == 1 # Pixel registration + assert grid.gmt.registration == GridReg.PIXEL translated_grid = grdsample(grid=grid, translate=True) - assert translated_grid.gmt.registration == 0 # Gridline registration + assert translated_grid.gmt.registration == GridReg.GRIDLINE registration_grid = grdsample(grid=translated_grid, registration="p") - assert registration_grid.gmt.registration == 1 # Pixel registration + assert registration_grid.gmt.registration == GridReg.PIXEL diff --git a/pygmt/tests/test_io.py b/pygmt/tests/test_io.py index 90ae4f2df83..9e8232374ef 100644 --- a/pygmt/tests/test_io.py +++ b/pygmt/tests/test_io.py @@ -5,6 +5,7 @@ import numpy as np import pytest import xarray as xr +from pygmt.enums import GridReg, GridType from pygmt.helpers import GMTTempFile from pygmt.io import load_dataarray @@ -22,10 +23,9 @@ def test_io_load_dataarray(): ) grid.to_netcdf(tmpfile.name) dataarray = load_dataarray(tmpfile.name) - assert dataarray.gmt.gtype == 0 # Cartesian grid - assert dataarray.gmt.registration == 1 # Pixel registration - # this would fail if we used xr.open_dataarray instead of - # load_dataarray + assert dataarray.gmt.gtype == GridType.CARTESIAN + assert dataarray.gmt.registration == GridReg.PIXEL + # this would fail if we used xr.open_dataarray instead of load_dataarray dataarray.to_netcdf(tmpfile.name) diff --git a/pygmt/tests/test_nearneighbor.py b/pygmt/tests/test_nearneighbor.py index 9d08aa0e8d1..010543515fd 100644 --- a/pygmt/tests/test_nearneighbor.py +++ b/pygmt/tests/test_nearneighbor.py @@ -10,6 +10,7 @@ import xarray as xr from pygmt import nearneighbor from pygmt.datasets import load_sample_data +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -32,8 +33,8 @@ def test_nearneighbor_input_data(array_func, ship_data): data=data, spacing="5m", region=[245, 255, 20, 30], search_radius="10m" ) assert isinstance(output, xr.DataArray) - assert output.gmt.registration == 0 # Gridline registration - assert output.gmt.gtype == 1 # Geographic type + assert output.gmt.registration == GridReg.GRIDLINE + assert output.gmt.gtype == GridType.GEOGRAPHIC assert output.shape == (121, 121) npt.assert_allclose(output.mean(), -2378.2385) diff --git a/pygmt/tests/test_sph2grd.py b/pygmt/tests/test_sph2grd.py index 89b4abeff25..cc8de1b943a 100644 --- a/pygmt/tests/test_sph2grd.py +++ b/pygmt/tests/test_sph2grd.py @@ -7,6 +7,7 @@ import numpy.testing as npt import pytest from pygmt import sph2grd +from pygmt.enums import GridReg, GridType from pygmt.helpers import GMTTempFile @@ -29,8 +30,8 @@ def test_sph2grd_no_outgrid(): """ temp_grid = sph2grd(data="@EGM96_to_36.txt", spacing=1, region="g", cores=2) assert temp_grid.dims == ("y", "x") - assert temp_grid.gmt.gtype == 0 # Cartesian grid - assert temp_grid.gmt.registration == 0 # Gridline registration + assert temp_grid.gmt.gtype == GridType.CARTESIAN + assert temp_grid.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(temp_grid.max(), 0.00021961, rtol=1e-4) npt.assert_allclose(temp_grid.min(), -0.0004326, rtol=1e-4) npt.assert_allclose(temp_grid.median(), -0.00010894, rtol=1e-4) diff --git a/pygmt/tests/test_sphdistance.py b/pygmt/tests/test_sphdistance.py index be8cbd1a066..eb1e334beda 100644 --- a/pygmt/tests/test_sphdistance.py +++ b/pygmt/tests/test_sphdistance.py @@ -8,6 +8,7 @@ import numpy.testing as npt import pytest from pygmt import sphdistance +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -29,8 +30,8 @@ def test_sphdistance_xy_inputs(): x = [85.5, 82.3, 85.8, 86.5] temp_grid = sphdistance(x=x, y=y, spacing=[1, 2], region=[82, 87, 22, 24]) assert temp_grid.dims == ("lat", "lon") - assert temp_grid.gmt.gtype == 1 # Geographic grid - assert temp_grid.gmt.registration == 0 # Gridline registration + assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC + assert temp_grid.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(temp_grid.max(), 232977.546875) npt.assert_allclose(temp_grid.min(), 0) npt.assert_allclose(temp_grid.median(), 0) @@ -56,8 +57,8 @@ def test_sphdistance_no_outgrid(array): """ temp_grid = sphdistance(data=array, spacing=[1, 2], region=[82, 87, 22, 24]) assert temp_grid.dims == ("lat", "lon") - assert temp_grid.gmt.gtype == 1 # Geographic grid - assert temp_grid.gmt.registration == 0 # Gridline registration + assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC + assert temp_grid.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(temp_grid.max(), 232977.546875) npt.assert_allclose(temp_grid.min(), 0) npt.assert_allclose(temp_grid.median(), 0) diff --git a/pygmt/tests/test_sphinterpolate.py b/pygmt/tests/test_sphinterpolate.py index d7e24eba780..0991ab992be 100644 --- a/pygmt/tests/test_sphinterpolate.py +++ b/pygmt/tests/test_sphinterpolate.py @@ -8,6 +8,7 @@ import pytest from pygmt import sphinterpolate from pygmt.datasets import load_sample_data +from pygmt.enums import GridReg, GridType from pygmt.helpers import GMTTempFile @@ -36,8 +37,8 @@ def test_sphinterpolate_no_outgrid(mars): """ temp_grid = sphinterpolate(data=mars, spacing=1, region="g") assert temp_grid.dims == ("lat", "lon") - assert temp_grid.gmt.gtype == 1 # Geographic grid - assert temp_grid.gmt.registration == 0 # Gridline registration + assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC + assert temp_grid.gmt.registration == GridReg.GRIDLINE npt.assert_allclose(temp_grid.max(), 14628.144) npt.assert_allclose(temp_grid.min(), -6908.1987) npt.assert_allclose(temp_grid.median(), 118.96849) diff --git a/pygmt/tests/test_surface.py b/pygmt/tests/test_surface.py index e8ec0cf3445..75e77d8d89a 100644 --- a/pygmt/tests/test_surface.py +++ b/pygmt/tests/test_surface.py @@ -8,6 +8,7 @@ import pytest import xarray as xr from pygmt import surface, which +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -72,8 +73,8 @@ def check_values(grid, expected_grid): Check the attributes and values of the DataArray returned by surface. """ assert isinstance(grid, xr.DataArray) - assert grid.gmt.registration == 0 # Gridline registration - assert grid.gmt.gtype == 0 # Cartesian type + assert grid.gmt.registration == GridReg.GRIDLINE + assert grid.gmt.gtype == GridType.CARTESIAN xr.testing.assert_allclose(a=grid, b=expected_grid) diff --git a/pygmt/tests/test_triangulate.py b/pygmt/tests/test_triangulate.py index f0a47c6e4ae..5d4367c3934 100644 --- a/pygmt/tests/test_triangulate.py +++ b/pygmt/tests/test_triangulate.py @@ -9,6 +9,7 @@ import pytest import xarray as xr from pygmt import triangulate, which +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -139,8 +140,8 @@ def test_regular_grid_no_outgrid(dataframe, expected_grid): data = dataframe.to_numpy() output = triangulate.regular_grid(data=data, spacing=1, region=[2, 4, 5, 6]) assert isinstance(output, xr.DataArray) - assert output.gmt.registration == 0 # Gridline registration - assert output.gmt.gtype == 0 # Cartesian type + assert output.gmt.registration == GridReg.GRIDLINE + assert output.gmt.gtype == GridType.CARTESIAN xr.testing.assert_allclose(a=output, b=expected_grid) @@ -157,6 +158,6 @@ def test_regular_grid_with_outgrid_param(dataframe, expected_grid): assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists with xr.open_dataarray(tmpfile.name) as grid: assert isinstance(grid, xr.DataArray) - assert grid.gmt.registration == 0 # Gridline registration - assert grid.gmt.gtype == 0 # Cartesian type + assert grid.gmt.registration == GridReg.GRIDLINE + assert grid.gmt.gtype == GridType.CARTESIAN xr.testing.assert_allclose(a=grid, b=expected_grid) diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index 56b2d1167e2..ddc9a300231 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -9,6 +9,7 @@ import xarray as xr from pygmt import load_dataarray, xyz2grd from pygmt.datasets import load_sample_data +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -48,8 +49,8 @@ def test_xyz2grd_input_array(array_func, ship_data, expected_grid): """ output = xyz2grd(data=array_func(ship_data), spacing=5, region=[245, 255, 20, 30]) assert isinstance(output, xr.DataArray) - assert output.gmt.registration == 0 # Gridline registration - assert output.gmt.gtype == 0 # Cartesian type + assert output.gmt.registration == GridReg.GRIDLINE + assert output.gmt.gtype == GridType.CARTESIAN xr.testing.assert_allclose(a=output, b=expected_grid) From fd099b084ba10178b2eb815ee5b7543c0f70065c Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 17 Dec 2024 23:31:52 +0800 Subject: [PATCH 04/21] Use enums GridReg and GridType in test_accessor.py --- pygmt/tests/test_accessor.py | 45 ++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/pygmt/tests/test_accessor.py b/pygmt/tests/test_accessor.py index 2701d2a0b3a..f7c3bac26ec 100644 --- a/pygmt/tests/test_accessor.py +++ b/pygmt/tests/test_accessor.py @@ -11,6 +11,7 @@ from pygmt import which from pygmt.clib import __gmt_version__ from pygmt.datasets import load_earth_relief +from pygmt.enums import GridReg, GridType from pygmt.exceptions import GMTInvalidInput @@ -21,8 +22,8 @@ def test_accessor_gridline_cartesian(): """ fname = which(fname="@test.dat.nc", download="a") grid = xr.open_dataarray(fname) - assert grid.gmt.registration == 0 # gridline registration - assert grid.gmt.gtype == 0 # cartesian coordinate type + assert grid.gmt.registration == GridReg.GRIDLINE + assert grid.gmt.gtype == GridType.CARTESIAN def test_accessor_pixel_geographic(): @@ -32,8 +33,8 @@ def test_accessor_pixel_geographic(): """ fname = which(fname="@earth_relief_01d_p", download="a") grid = xr.open_dataarray(fname, engine="netcdf4") - assert grid.gmt.registration == 1 # pixel registration - assert grid.gmt.gtype == 1 # geographic coordinate type + assert grid.gmt.registration == GridReg.PIXEL + assert grid.gmt.gtype == GridType.GEOGRAPHIC def test_accessor_set_pixel_registration(): @@ -41,9 +42,9 @@ def test_accessor_set_pixel_registration(): Check that we can set a grid to be Pixel registered with a registration value of 1. """ grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]]) - assert grid.gmt.registration == 0 # default to gridline registration - grid.gmt.registration = 1 # set to pixel registration - assert grid.gmt.registration == 1 # ensure changed to pixel registration + assert grid.gmt.registration == GridReg.GRIDLINE + grid.gmt.registration = GridReg.PIXEL + assert grid.gmt.registration == GridReg.PIXEL @pytest.mark.benchmark @@ -53,11 +54,11 @@ def test_accessor_set_geographic_cartesian_roundtrip(): using a gtype of 1, set it to Geographic 0, and then back to Cartesian again 1. """ grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]]) - assert grid.gmt.gtype == 0 # default to cartesian coordinate type - grid.gmt.gtype = 1 # set to geographic type - assert grid.gmt.gtype == 1 # ensure changed to geographic coordinate type - grid.gmt.gtype = 0 # set back to cartesian type - assert grid.gmt.gtype == 0 # ensure changed to cartesian coordinate type + assert grid.gmt.gtype == GridType.CARTESIAN + grid.gmt.gtype = GridType.GEOGRAPHIC + assert grid.gmt.gtype == GridType.GEOGRAPHIC + grid.gmt.gtype = GridType.CARTESIAN + assert grid.gmt.gtype == GridType.CARTESIAN def test_accessor_set_non_boolean(): @@ -93,8 +94,8 @@ def test_accessor_sliced_datacube(): with xr.open_dataset(fname) as dataset: grid = dataset.sel(level=500, month=1, drop=True).z - assert grid.gmt.registration == 0 # gridline registration - assert grid.gmt.gtype == 1 # geographic coordinate type + assert grid.gmt.registration == GridReg.GRIDLINE + assert grid.gmt.gtype == GridType.GEOGRAPHIC finally: Path(fname).unlink() @@ -109,19 +110,19 @@ def test_accessor_grid_source_file_not_exist(): resolution="05m", region=[0, 5, -5, 5], registration="pixel" ) # Registration and gtype are correct - assert grid.gmt.registration == 1 - assert grid.gmt.gtype == 1 + assert grid.gmt.registration == GridReg.PIXEL + assert grid.gmt.gtype == GridType.GEOGRAPHIC # The source grid file is undefined. assert grid.encoding.get("source") is None # For a sliced grid, fallback to default registration and gtype, # because the source grid file doesn't exist. sliced_grid = grid[1:3, 1:3] - assert sliced_grid.gmt.registration == 0 - assert sliced_grid.gmt.gtype == 0 + assert sliced_grid.gmt.registration == GridReg.GRIDLINE + assert sliced_grid.gmt.gtype == GridType.CARTESIAN # Still possible to manually set registration and gtype - sliced_grid.gmt.registration = 1 - sliced_grid.gmt.gtype = 1 - assert sliced_grid.gmt.registration == 1 - assert sliced_grid.gmt.gtype == 1 + sliced_grid.gmt.registration = GridReg.PIXEL + sliced_grid.gmt.gtype = GridType.GEOGRAPHIC + assert sliced_grid.gmt.registration == GridReg.PIXEL + assert sliced_grid.gmt.gtype == GridType.GEOGRAPHIC From ed7f45116629600ce0948e7dfa672416d5cd16b5 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 18 Dec 2024 23:04:53 +0800 Subject: [PATCH 05/21] Fix the checking of valid enum values for Python<=3.11 --- pygmt/accessors.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index f23e3a2af87..4e9d2eee931 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -162,7 +162,8 @@ def registration(self): def registration(self, value): if value in {"gridline", "pixel"}: # Support for string-type values value = GridReg[value.upper()] - if value not in GridReg: + # Can be simplified to `if value not in GridReg` after requiring Python 3.12+. + if value not in GridReg.__members__.values(): msg = ( f"Invalid grid registration: {value}. " "Should be either GridReg.GRIDLINE or GridReg.PIXEL." @@ -182,7 +183,8 @@ def gtype(self): def gtype(self, value): if value in {"cartesian", "geographic"}: # Support for string-type values value = GridType[value.upper()] - if value not in GridType: + # Can be simplified to `if value not in GridType` after requiring Python 3.12+. + if value not in GridType.__members__.values(): msg = ( f"Invalid grid coordinate system type: '{value}'. " "Should be either GridType.CARTESIAN or GridType.GEOGRAPHIC." From 259771abe92166262532d290a2bf26c14fb7b7b4 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 18 Dec 2024 23:15:24 +0800 Subject: [PATCH 06/21] Remove the support of string-type values --- pygmt/accessors.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index 4e9d2eee931..cdc8e5b9a44 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -160,8 +160,6 @@ def registration(self): @registration.setter def registration(self, value): - if value in {"gridline", "pixel"}: # Support for string-type values - value = GridReg[value.upper()] # Can be simplified to `if value not in GridReg` after requiring Python 3.12+. if value not in GridReg.__members__.values(): msg = ( @@ -181,8 +179,6 @@ def gtype(self): @gtype.setter def gtype(self, value): - if value in {"cartesian", "geographic"}: # Support for string-type values - value = GridType[value.upper()] # Can be simplified to `if value not in GridType` after requiring Python 3.12+. if value not in GridType.__members__.values(): msg = ( From f0b30ab63c7bbf92e91fa8ffa2ebc1c581e5933d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 18 Dec 2024 23:33:29 +0800 Subject: [PATCH 07/21] Improve the accessor tests to test both numerical and enum values --- pygmt/tests/test_accessor.py | 74 +++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/pygmt/tests/test_accessor.py b/pygmt/tests/test_accessor.py index f7c3bac26ec..9eb8968bd53 100644 --- a/pygmt/tests/test_accessor.py +++ b/pygmt/tests/test_accessor.py @@ -17,19 +17,19 @@ def test_accessor_gridline_cartesian(): """ - Check that a grid returns a registration value of 0 when Gridline registered, and a - gtype value of 1 when using Geographic coordinates. + Check that the accessor returns the correct registration and gtype values for a + Cartesian, gridline-registered grid. """ fname = which(fname="@test.dat.nc", download="a") - grid = xr.open_dataarray(fname) + grid = xr.open_dataarray(fname, engine="netcdf4") assert grid.gmt.registration == GridReg.GRIDLINE assert grid.gmt.gtype == GridType.CARTESIAN def test_accessor_pixel_geographic(): """ - Check that a grid returns a registration value of 1 when Pixel registered, and a - gtype value of 0 when using Cartesian coordinates. + Check that the accessor returns the correct registration and gtype values for a + geographic, pixel-registered grid. """ fname = which(fname="@earth_relief_01d_p", download="a") grid = xr.open_dataarray(fname, engine="netcdf4") @@ -37,41 +37,69 @@ def test_accessor_pixel_geographic(): assert grid.gmt.gtype == GridType.GEOGRAPHIC -def test_accessor_set_pixel_registration(): +def test_accessor_set_registration(): """ - Check that we can set a grid to be Pixel registered with a registration value of 1. + Check that we can set the registration of a grid. """ grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]]) - assert grid.gmt.registration == GridReg.GRIDLINE + assert grid.gmt.registration == GridReg.GRIDLINE == 0 # Default registration + + # Set the registration to pixel grid.gmt.registration = GridReg.PIXEL - assert grid.gmt.registration == GridReg.PIXEL + assert grid.gmt.registration == GridReg.PIXEL == 1 + + # Set the registration to gridline + grid.gmt.registration = GridReg.GRIDLINE + assert grid.gmt.registration == GridReg.GRIDLINE == 0 + + # Set the registration to pixel but using a numerical value + grid.gmt.registration = 1 + assert grid.gmt.registration == GridReg.PIXEL == 1 + + # Set the registration to gridline but using a numerical value + grid.gmt.registration = 0 + assert grid.gmt.registration == GridReg.GRIDLINE == 0 @pytest.mark.benchmark -def test_accessor_set_geographic_cartesian_roundtrip(): +def test_accessor_set_gtype(): """ - Check that we can set a grid to switch between the default Cartesian coordinate type - using a gtype of 1, set it to Geographic 0, and then back to Cartesian again 1. + Check that we can set the gtype of a grid. """ grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]]) - assert grid.gmt.gtype == GridType.CARTESIAN + assert grid.gmt.gtype == GridType.CARTESIAN == 0 # Default gtype + + # Set the gtype to geographic grid.gmt.gtype = GridType.GEOGRAPHIC - assert grid.gmt.gtype == GridType.GEOGRAPHIC + assert grid.gmt.gtype == GridType.GEOGRAPHIC == 1 + + # Set the gtype to Cartesian grid.gmt.gtype = GridType.CARTESIAN - assert grid.gmt.gtype == GridType.CARTESIAN + assert grid.gmt.gtype == GridType.CARTESIAN == 0 + + # Set the gtype to geographic but using a numerical value + grid.gmt.gtype = 1 + assert grid.gmt.gtype == GridType.GEOGRAPHIC == 1 + # Set the gtype to Cartesian but using a numerical value + grid.gmt.gtype = 0 + assert grid.gmt.gtype == GridType.CARTESIAN == 0 -def test_accessor_set_non_boolean(): + +def test_accessor_set_invalid_registration_and_gtype(): """ - Check that setting non boolean values on registration and gtype do not work. + Check that setting invalid values on registration and gtype do not work. """ grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]]) with pytest.raises(GMTInvalidInput): grid.gmt.registration = "2" - + with pytest.raises(GMTInvalidInput): + grid.gmt.registration = "pixel" with pytest.raises(GMTInvalidInput): grid.gmt.gtype = 2 + with pytest.raises(GMTInvalidInput): + grid.gmt.gtype = "geographic" @pytest.mark.xfail( @@ -105,23 +133,23 @@ def test_accessor_grid_source_file_not_exist(): Check that the accessor fallbacks to the default registration and gtype when the grid source file (i.e., grid.encoding["source"]) doesn't exist. """ - # Load the 05m earth relief grid, which is stored as tiles + # Load the 05m earth relief grid, which is stored as tiles. grid = load_earth_relief( resolution="05m", region=[0, 5, -5, 5], registration="pixel" ) - # Registration and gtype are correct + # Registration and gtype are correct. assert grid.gmt.registration == GridReg.PIXEL assert grid.gmt.gtype == GridType.GEOGRAPHIC # The source grid file is undefined. assert grid.encoding.get("source") is None - # For a sliced grid, fallback to default registration and gtype, - # because the source grid file doesn't exist. + # For a sliced grid, fallback to default registration and gtype, because the source + # grid file doesn't exist. sliced_grid = grid[1:3, 1:3] assert sliced_grid.gmt.registration == GridReg.GRIDLINE assert sliced_grid.gmt.gtype == GridType.CARTESIAN - # Still possible to manually set registration and gtype + # Still possible to manually set registration and gtype. sliced_grid.gmt.registration = GridReg.PIXEL sliced_grid.gmt.gtype = GridType.GEOGRAPHIC assert sliced_grid.gmt.registration == GridReg.PIXEL From 946a4cde5ad8bcadeebe62b473e10c52798e4109 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 19 Dec 2024 10:45:31 +0800 Subject: [PATCH 08/21] Rename GridReg to GridRegistration --- pygmt/enums.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/enums.py b/pygmt/enums.py index 1bc42627913..34419a57e06 100644 --- a/pygmt/enums.py +++ b/pygmt/enums.py @@ -39,7 +39,7 @@ class GridFormat(IntEnum): EF = 24 #: ESRI Arc/Info ASCII Grid Interchange format (ASCII float) -class GridReg(IntEnum): +class GridRegistration(IntEnum): """ Enum for the grid registration. """ From a353bbf4e1f6cbaf21a6ec74f4b3904d49bd913c Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 19 Dec 2024 10:56:48 +0800 Subject: [PATCH 09/21] Rename GridReg to GridRegistration --- pygmt/accessors.py | 55 ++++++++++--------- pygmt/datatypes/grid.py | 2 +- pygmt/datatypes/image.py | 2 +- pygmt/tests/test_accessor.py | 32 ++++++----- pygmt/tests/test_binstats.py | 4 +- pygmt/tests/test_datasets_earth_age.py | 8 +-- pygmt/tests/test_datasets_earth_day.py | 6 +- .../test_datasets_earth_free_air_anomaly.py | 8 +-- pygmt/tests/test_datasets_earth_geoid.py | 8 +-- .../test_datasets_earth_magnetic_anomaly.py | 14 ++--- pygmt/tests/test_datasets_earth_mask.py | 6 +- pygmt/tests/test_datasets_earth_night.py | 6 +- pygmt/tests/test_datasets_earth_relief.py | 18 +++--- ...atasets_earth_vertical_gravity_gradient.py | 8 +-- .../test_datasets_load_remote_datasets.py | 4 +- pygmt/tests/test_datasets_mars_relief.py | 8 +-- pygmt/tests/test_datasets_mercury_relief.py | 8 +-- pygmt/tests/test_datasets_moon_relief.py | 8 +-- pygmt/tests/test_datasets_pluto_relief.py | 8 +-- pygmt/tests/test_datasets_venus_relief.py | 8 +-- pygmt/tests/test_dimfilter.py | 4 +- pygmt/tests/test_grdclip.py | 6 +- pygmt/tests/test_grdfill.py | 6 +- pygmt/tests/test_grdfilter.py | 4 +- pygmt/tests/test_grdgradient.py | 4 +- pygmt/tests/test_grdhisteq.py | 4 +- pygmt/tests/test_grdimage.py | 16 +++--- pygmt/tests/test_grdlandmask.py | 4 +- pygmt/tests/test_grdproject.py | 4 +- pygmt/tests/test_grdsample.py | 10 ++-- pygmt/tests/test_io.py | 4 +- pygmt/tests/test_nearneighbor.py | 4 +- pygmt/tests/test_sph2grd.py | 4 +- pygmt/tests/test_sphdistance.py | 6 +- pygmt/tests/test_sphinterpolate.py | 4 +- pygmt/tests/test_surface.py | 4 +- pygmt/tests/test_triangulate.py | 6 +- pygmt/tests/test_xyz2grd.py | 4 +- 38 files changed, 161 insertions(+), 158 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index cdc8e5b9a44..52b10ea3d15 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -6,7 +6,7 @@ from pathlib import Path import xarray as xr -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.src.grdinfo import grdinfo @@ -22,8 +22,8 @@ class GMTDataArrayAccessor: The *gmt* accessor contains following properties: - - ``registration``: Grid registration type, either ``GridReg.GRIDLINE`` or - ``GridReg.PIXEL``. + - ``registration``: Grid registration type, either ``GridRegistration.GRIDLINE`` or + ``GridRegistration.PIXEL``. - ``gtype``: Grid coordinate system type, either ``GridType.CARTESIAN`` or ``GridType.GEOGRAPHIC``. @@ -47,20 +47,20 @@ class GMTDataArrayAccessor: >>> grid = load_earth_relief(resolution="01d", registration="pixel") >>> # See if grid is Gridline or Pixel registration >>> grid.gmt.registration - + >>> # See if grid is in Cartesian or Geographic coordinate system >>> grid.gmt.gtype For :class:`xarray.DataArray` grids created by yourself, ``registration`` and - ``gtype`` default to ``GridReg.GRIDLINE`` and ``GridType.CARTESIAN`` (i.e., a - gridline-registered, Cartesian grid). You need to set the correct properties before - passing it to PyGMT functions: + ``gtype`` default to ``GridRegistration.GRIDLINE`` and ``GridType.CARTESIAN`` (i.e., + a gridline-registered, Cartesian grid). You need to set the correct properties + before passing it to PyGMT functions: >>> import numpy as np >>> import xarray as xr >>> import pygmt - >>> from pygmt.enums import GridReg, GridType + >>> from pygmt.enums import GridRegistration, GridType >>> # Create a DataArray in gridline coordinates of sin(lon) * cos(lat) >>> interval = 2.5 >>> lat = np.arange(90, -90 - interval, -interval) @@ -70,14 +70,14 @@ class GMTDataArrayAccessor: >>> grid = xr.DataArray(data, coords=[("latitude", lat), ("longitude", lon)]) >>> # Default to a gridline-registrated Cartesian grid >>> grid.gmt.registration - + >>> grid.gmt.gtype >>> # Manually set it to a gridline-registered geographic grid - >>> grid.gmt.registration = GridReg.GRIDLINE + >>> grid.gmt.registration = GridRegistration.GRIDLINE >>> grid.gmt.gtype = GridType.GEOGRAPHIC >>> grid.gmt.registration - + >>> grid.gmt.gtype @@ -89,7 +89,7 @@ class GMTDataArrayAccessor: >>> grid *= 2.0 >>> grid.gmt.registration - + >>> grid.gmt.gtype @@ -100,14 +100,14 @@ class GMTDataArrayAccessor: >>> grid2 = grid[0:30, 50:80] >>> # Properties are reset to the default values for new instance >>> grid2.gmt.registration - + >>> grid2.gmt.gtype >>> # Need to set these properties before passing the grid to PyGMT >>> grid2.gmt.registration = grid.gmt.registration >>> grid2.gmt.gtype = grid.gmt.gtype >>> grid2.gmt.registration - + >>> grid2.gmt.gtype @@ -117,29 +117,29 @@ class GMTDataArrayAccessor: >>> ds = xr.Dataset({"zval": grid}) >>> ds.zval.gmt.registration - + >>> ds.zval.gmt.gtype >>> # Manually set these properties won't work as expected - >>> ds.zval.gmt.registration = GridReg.GRIDLINE + >>> ds.zval.gmt.registration = GridRegistration.GRIDLINE >>> ds.zval.gmt.gtype = GridType.GEOGRAPHIC >>> ds.zval.gmt.registration, ds.zval.gmt.gtype - (, ) + (, ) >>> # workaround: assign the DataArray into a variable >>> zval = ds.zval >>> zval.gmt.registration, zval.gmt.gtype - (, ) - >>> zval.gmt.registration = GridReg.GRIDLINE + (, ) + >>> zval.gmt.registration = GridRegistration.GRIDLINE >>> zval.gmt.gtype = GridType.GEOGRAPHIC >>> zval.gmt.registration, zval.gmt.gtype - (, ) + (, ) """ def __init__(self, xarray_obj): self._obj = xarray_obj # Default to Gridline registration and Cartesian grid type - self._registration = GridReg.GRIDLINE + self._registration = GridRegistration.GRIDLINE self._gtype = GridType.CARTESIAN # If the source file exists, get grid registration and grid type from the last @@ -153,21 +153,22 @@ def __init__(self, xarray_obj): @property def registration(self): """ - Registration type of the grid, either ``GridReg.GRIDLINE`` or - ``GridReg.PIXEL``. + Registration type of the grid, either ``GridRegistration.GRIDLINE`` or + ``GridRegistration.PIXEL``. """ return self._registration @registration.setter def registration(self, value): - # Can be simplified to `if value not in GridReg` after requiring Python 3.12+. - if value not in GridReg.__members__.values(): + # Can be simplified to `if value not in GridRegistration` after requiring + # Python 3.12+. + if value not in GridRegistration.__members__.values(): msg = ( f"Invalid grid registration: {value}. " - "Should be either GridReg.GRIDLINE or GridReg.PIXEL." + "Should be either GridRegistration.GRIDLINE or GridRegistration.PIXEL." ) raise GMTInvalidInput(msg) - self._registration = GridReg(value) + self._registration = GridRegistration(value) @property def gtype(self): diff --git a/pygmt/datatypes/grid.py b/pygmt/datatypes/grid.py index e774c746570..8b4426b01c5 100644 --- a/pygmt/datatypes/grid.py +++ b/pygmt/datatypes/grid.py @@ -165,7 +165,7 @@ def to_dataarray(self) -> xr.DataArray: axis: Y actual_range: [-24. -10.] >>> da.gmt.registration, da.gmt.gtype - (, ) + (, ) """ # The grid header header = self.header.contents diff --git a/pygmt/datatypes/image.py b/pygmt/datatypes/image.py index f629837c263..3cd3a106bc1 100644 --- a/pygmt/datatypes/image.py +++ b/pygmt/datatypes/image.py @@ -166,7 +166,7 @@ def to_dataarray(self) -> xr.DataArray: axis: Y actual_range: [-90. 90.] >>> da.gmt.registration, da.gmt.gtype - (, ) + (, ) """ # The image header header = self.header.contents diff --git a/pygmt/tests/test_accessor.py b/pygmt/tests/test_accessor.py index 9eb8968bd53..7492e55fd40 100644 --- a/pygmt/tests/test_accessor.py +++ b/pygmt/tests/test_accessor.py @@ -11,7 +11,7 @@ from pygmt import which from pygmt.clib import __gmt_version__ from pygmt.datasets import load_earth_relief -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput @@ -22,7 +22,7 @@ def test_accessor_gridline_cartesian(): """ fname = which(fname="@test.dat.nc", download="a") grid = xr.open_dataarray(fname, engine="netcdf4") - assert grid.gmt.registration == GridReg.GRIDLINE + assert grid.gmt.registration == GridRegistration.GRIDLINE assert grid.gmt.gtype == GridType.CARTESIAN @@ -33,7 +33,7 @@ def test_accessor_pixel_geographic(): """ fname = which(fname="@earth_relief_01d_p", download="a") grid = xr.open_dataarray(fname, engine="netcdf4") - assert grid.gmt.registration == GridReg.PIXEL + assert grid.gmt.registration == GridRegistration.PIXEL assert grid.gmt.gtype == GridType.GEOGRAPHIC @@ -42,23 +42,25 @@ def test_accessor_set_registration(): Check that we can set the registration of a grid. """ grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]]) - assert grid.gmt.registration == GridReg.GRIDLINE == 0 # Default registration + assert ( + grid.gmt.registration == GridRegistration.GRIDLINE == 0 + ) # Default registration # Set the registration to pixel - grid.gmt.registration = GridReg.PIXEL - assert grid.gmt.registration == GridReg.PIXEL == 1 + grid.gmt.registration = GridRegistration.PIXEL + assert grid.gmt.registration == GridRegistration.PIXEL == 1 # Set the registration to gridline - grid.gmt.registration = GridReg.GRIDLINE - assert grid.gmt.registration == GridReg.GRIDLINE == 0 + grid.gmt.registration = GridRegistration.GRIDLINE + assert grid.gmt.registration == GridRegistration.GRIDLINE == 0 # Set the registration to pixel but using a numerical value grid.gmt.registration = 1 - assert grid.gmt.registration == GridReg.PIXEL == 1 + assert grid.gmt.registration == GridRegistration.PIXEL == 1 # Set the registration to gridline but using a numerical value grid.gmt.registration = 0 - assert grid.gmt.registration == GridReg.GRIDLINE == 0 + assert grid.gmt.registration == GridRegistration.GRIDLINE == 0 @pytest.mark.benchmark @@ -122,7 +124,7 @@ def test_accessor_sliced_datacube(): with xr.open_dataset(fname) as dataset: grid = dataset.sel(level=500, month=1, drop=True).z - assert grid.gmt.registration == GridReg.GRIDLINE + assert grid.gmt.registration == GridRegistration.GRIDLINE assert grid.gmt.gtype == GridType.GEOGRAPHIC finally: Path(fname).unlink() @@ -138,7 +140,7 @@ def test_accessor_grid_source_file_not_exist(): resolution="05m", region=[0, 5, -5, 5], registration="pixel" ) # Registration and gtype are correct. - assert grid.gmt.registration == GridReg.PIXEL + assert grid.gmt.registration == GridRegistration.PIXEL assert grid.gmt.gtype == GridType.GEOGRAPHIC # The source grid file is undefined. assert grid.encoding.get("source") is None @@ -146,11 +148,11 @@ def test_accessor_grid_source_file_not_exist(): # For a sliced grid, fallback to default registration and gtype, because the source # grid file doesn't exist. sliced_grid = grid[1:3, 1:3] - assert sliced_grid.gmt.registration == GridReg.GRIDLINE + assert sliced_grid.gmt.registration == GridRegistration.GRIDLINE assert sliced_grid.gmt.gtype == GridType.CARTESIAN # Still possible to manually set registration and gtype. - sliced_grid.gmt.registration = GridReg.PIXEL + sliced_grid.gmt.registration = GridRegistration.PIXEL sliced_grid.gmt.gtype = GridType.GEOGRAPHIC - assert sliced_grid.gmt.registration == GridReg.PIXEL + assert sliced_grid.gmt.registration == GridRegistration.PIXEL assert sliced_grid.gmt.gtype == GridType.GEOGRAPHIC diff --git a/pygmt/tests/test_binstats.py b/pygmt/tests/test_binstats.py index 133ac597e3c..eeaa22020d5 100644 --- a/pygmt/tests/test_binstats.py +++ b/pygmt/tests/test_binstats.py @@ -7,7 +7,7 @@ import numpy.testing as npt import pytest from pygmt import binstats -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.helpers import GMTTempFile @@ -44,7 +44,7 @@ def test_binstats_no_outgrid(): ) assert temp_grid.dims == ("y", "x") assert temp_grid.gmt.gtype == GridType.CARTESIAN - assert temp_grid.gmt.registration == GridReg.GRIDLINE + assert temp_grid.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(temp_grid.max(), 35971536) npt.assert_allclose(temp_grid.min(), 53) npt.assert_allclose(temp_grid.median(), 1232714.5) diff --git a/pygmt/tests/test_datasets_earth_age.py b/pygmt/tests/test_datasets_earth_age.py index da69182ed85..3407ac0776a 100644 --- a/pygmt/tests/test_datasets_earth_age.py +++ b/pygmt/tests/test_datasets_earth_age.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_age -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration def test_earth_age_01d(): @@ -19,7 +19,7 @@ def test_earth_age_01d(): assert data.attrs["units"] == "Myr" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), 0.37, atol=0.01) @@ -32,7 +32,7 @@ def test_earth_age_01d_with_region(): """ data = load_earth_age(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), 11.13, atol=0.01) @@ -46,7 +46,7 @@ def test_earth_age_01m_default_registration(): """ data = load_earth_age(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_earth_day.py b/pygmt/tests/test_datasets_earth_day.py index 474e9c07744..f04269135f8 100644 --- a/pygmt/tests/test_datasets_earth_day.py +++ b/pygmt/tests/test_datasets_earth_day.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_blue_marble -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType def test_blue_marble_01d(): @@ -19,7 +19,7 @@ def test_blue_marble_01d(): assert data.attrs["description"] == "NASA Day Images" assert data.shape == (3, 180, 360) assert data.dtype == "uint8" - assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.registration == GridRegistration.PIXEL assert data.gmt.gtype == GridType.GEOGRAPHIC npt.assert_allclose(data.y, np.arange(89.5, -90.5, -1)) npt.assert_allclose(data.x, np.arange(-179.5, 180.5, 1)) @@ -34,7 +34,7 @@ def test_blue_marble_01d_with_region(): data = load_blue_marble(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (3, 10, 20) assert data.dtype == "uint8" - assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.registration == GridRegistration.PIXEL assert data.gmt.gtype == GridType.GEOGRAPHIC npt.assert_allclose(data.y, np.arange(4.5, -5.5, -1)) npt.assert_allclose(data.x, np.arange(-9.5, 10.5, 1)) diff --git a/pygmt/tests/test_datasets_earth_free_air_anomaly.py b/pygmt/tests/test_datasets_earth_free_air_anomaly.py index 250bde9e286..e241edc803b 100644 --- a/pygmt/tests/test_datasets_earth_free_air_anomaly.py +++ b/pygmt/tests/test_datasets_earth_free_air_anomaly.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_free_air_anomaly -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration def test_earth_faa_01d(): @@ -19,7 +19,7 @@ def test_earth_faa_01d(): assert data.attrs["units"] == "mGal" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -188.85, atol=0.025) @@ -32,7 +32,7 @@ def test_earth_faa_01d_with_region(): """ data = load_earth_free_air_anomaly(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -36.125, atol=0.025) @@ -46,7 +46,7 @@ def test_earth_faa_01m_default_registration(): """ data = load_earth_free_air_anomaly(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (120, 60) - assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.registration == GridRegistration.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 3.008333333) npt.assert_allclose(data.coords["lat"].data.max(), 4.991666666) npt.assert_allclose(data.coords["lon"].data.min(), -9.99166666) diff --git a/pygmt/tests/test_datasets_earth_geoid.py b/pygmt/tests/test_datasets_earth_geoid.py index 451b615c696..3e2d83a77f5 100644 --- a/pygmt/tests/test_datasets_earth_geoid.py +++ b/pygmt/tests/test_datasets_earth_geoid.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_geoid -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration def test_earth_geoid_01d(): @@ -19,7 +19,7 @@ def test_earth_geoid_01d(): assert data.attrs["units"] == "m" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -106.06, atol=0.01) @@ -32,7 +32,7 @@ def test_earth_geoid_01d_with_region(): """ data = load_earth_geoid(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), 5.57, atol=0.01) @@ -46,7 +46,7 @@ def test_earth_geoid_01m_default_registration(): """ data = load_earth_geoid(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_earth_magnetic_anomaly.py b/pygmt/tests/test_datasets_earth_magnetic_anomaly.py index 05a31f5ea16..e580e565094 100644 --- a/pygmt/tests/test_datasets_earth_magnetic_anomaly.py +++ b/pygmt/tests/test_datasets_earth_magnetic_anomaly.py @@ -6,7 +6,7 @@ import numpy.testing as npt import pytest from pygmt.datasets import load_earth_magnetic_anomaly -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration from pygmt.exceptions import GMTInvalidInput @@ -21,7 +21,7 @@ def test_earth_mag_01d(): assert data.attrs["units"] == "nT" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -336.2, atol=0.2) @@ -34,7 +34,7 @@ def test_earth_mag_01d_with_region(): """ data = load_earth_magnetic_anomaly(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -54.4, atol=0.2) @@ -48,7 +48,7 @@ def test_earth_mag_02m_default_registration(): """ data = load_earth_magnetic_anomaly(resolution="02m", region=[-10, -9, 3, 5]) assert data.shape == (60, 30) - assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.registration == GridRegistration.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 3.016666667) npt.assert_allclose(data.coords["lat"].data.max(), 4.983333333) npt.assert_allclose(data.coords["lon"].data.min(), -9.98333333) @@ -68,7 +68,7 @@ def test_earth_mag4km_01d(): assert data.attrs["units"] == "nT" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -436.8, atol=0.2) @@ -103,7 +103,7 @@ def test_earth_mag4km_02m_default_registration(): data_source="emag2_4km", ) assert data.shape == (60, 90) - assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.registration == GridRegistration.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 4.01666667) npt.assert_allclose(data.coords["lat"].data.max(), 5.98333333) npt.assert_allclose(data.coords["lon"].data.min(), -114.98333333) @@ -155,7 +155,7 @@ def test_earth_mag_03m_wdmam_with_region(): data = load_earth_magnetic_anomaly( resolution="03m", region=[10, 13, -60, -58], data_source="wdmam" ) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.shape == (41, 61) assert data.lat.min() == -60 assert data.lat.max() == -58 diff --git a/pygmt/tests/test_datasets_earth_mask.py b/pygmt/tests/test_datasets_earth_mask.py index aebcb8b917e..3e33ab1172b 100644 --- a/pygmt/tests/test_datasets_earth_mask.py +++ b/pygmt/tests/test_datasets_earth_mask.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_mask -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType def test_earth_mask_01d(): @@ -17,7 +17,7 @@ def test_earth_mask_01d(): assert data.attrs["description"] == "GSHHG Earth mask" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.gmt.gtype == GridType.GEOGRAPHIC assert data.dtype == "int8" npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) @@ -33,7 +33,7 @@ def test_earth_mask_01d_with_region(): """ data = load_earth_mask(resolution="01d", region=[-7, 4, 13, 19]) assert data.shape == (7, 12) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.gmt.gtype == GridType.GEOGRAPHIC assert data.dtype == "int8" npt.assert_allclose(data.lat, np.arange(13, 20, 1)) diff --git a/pygmt/tests/test_datasets_earth_night.py b/pygmt/tests/test_datasets_earth_night.py index 4b42581e869..0865feaa1e8 100644 --- a/pygmt/tests/test_datasets_earth_night.py +++ b/pygmt/tests/test_datasets_earth_night.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_black_marble -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType def test_black_marble_01d(): @@ -19,7 +19,7 @@ def test_black_marble_01d(): assert data.attrs["description"] == "NASA Night Images" assert data.shape == (3, 180, 360) assert data.dtype == "uint8" - assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.registration == GridRegistration.PIXEL assert data.gmt.gtype == GridType.GEOGRAPHIC npt.assert_allclose(data.y, np.arange(89.5, -90.5, -1)) npt.assert_allclose(data.x, np.arange(-179.5, 180.5, 1)) @@ -34,7 +34,7 @@ def test_black_marble_01d_with_region(): data = load_black_marble(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (3, 10, 20) assert data.dtype == "uint8" - assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.registration == GridRegistration.PIXEL assert data.gmt.gtype == GridType.GEOGRAPHIC npt.assert_allclose(data.y, np.arange(4.5, -5.5, -1)) npt.assert_allclose(data.x, np.arange(-9.5, 10.5, 1)) diff --git a/pygmt/tests/test_datasets_earth_relief.py b/pygmt/tests/test_datasets_earth_relief.py index 839e13f1a63..0ebff8b4587 100644 --- a/pygmt/tests/test_datasets_earth_relief.py +++ b/pygmt/tests/test_datasets_earth_relief.py @@ -8,7 +8,7 @@ from packaging.version import Version from pygmt.clib import __gmt_version__ from pygmt.datasets import load_earth_relief -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration from pygmt.exceptions import GMTInvalidInput @@ -26,7 +26,7 @@ def test_earth_relief_01d_igpp_synbath(data_source): assert data.attrs["vertical_datum"] == "EGM96" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -7174.0, atol=0.5) @@ -46,7 +46,7 @@ def test_earth_relief_01d_gebco(data_source): assert data.attrs["vertical_datum"] == "EGM96" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -7169.0, atol=1.0) @@ -63,7 +63,7 @@ def test_earth_relief_01d_with_region_srtm(): data_source="igpp", ) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -5118.0, atol=0.5) @@ -80,7 +80,7 @@ def test_earth_relief_01d_with_region_gebco(): data_source="gebco", ) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -5118.0, atol=1.0) @@ -93,7 +93,7 @@ def test_earth_relief_30m(): """ data = load_earth_relief(resolution="30m") assert data.shape == (361, 721) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 90.5, 0.5)) npt.assert_allclose(data.lon, np.arange(-180, 180.5, 0.5)) npt.assert_allclose(data.min(), -8279.5, atol=0.5) @@ -111,7 +111,7 @@ def test_earth_gebcosi_15m_with_region(): data_source="gebcosi", ) assert data.shape == (16, 8) - assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.registration == GridRegistration.PIXEL npt.assert_allclose(data.lat, np.arange(-87.875, -84, 0.25)) npt.assert_allclose(data.lon, np.arange(85.125, 87, 0.25)) npt.assert_allclose(data.min(), -492, atol=1.0) @@ -184,7 +184,7 @@ def test_earth_relief_15s_default_registration(): """ data = load_earth_relief(resolution="15s", region=[-10, -9.5, 4, 5]) assert data.shape == (240, 120) - assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.registration == GridRegistration.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 4.002083) npt.assert_allclose(data.coords["lat"].data.max(), 4.997917) npt.assert_allclose(data.coords["lon"].data.min(), -9.997917) @@ -204,7 +204,7 @@ def test_earth_relief_03s_default_registration(): """ data = load_earth_relief(resolution="03s", region=[-10, -9.8, 4.9, 5]) assert data.shape == (121, 241) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.coords["lat"].data.min(), 4.9) npt.assert_allclose(data.coords["lat"].data.max(), 5) npt.assert_allclose(data.coords["lon"].data.min(), -10) diff --git a/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py b/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py index 80ea3a3f981..9851f8ceea9 100644 --- a/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py +++ b/pygmt/tests/test_datasets_earth_vertical_gravity_gradient.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_vertical_gravity_gradient -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration def test_earth_vertical_gravity_gradient_01d(): @@ -19,7 +19,7 @@ def test_earth_vertical_gravity_gradient_01d(): assert data.attrs["units"] == "Eotvos" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -40.1875, atol=1 / 32) @@ -35,7 +35,7 @@ def test_earth_vertical_gravity_gradient_01d_with_region(): resolution="01d", region=[-10, 10, -5, 5] ) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -5.34375, atol=1 / 32) @@ -51,7 +51,7 @@ def test_earth_vertical_gravity_gradient_01m_default_registration(): resolution="01m", region=[-10, -9, 3, 5] ) assert data.shape == (120, 60) - assert data.gmt.registration == GridReg.PIXEL + assert data.gmt.registration == GridRegistration.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 3.008333333) npt.assert_allclose(data.coords["lat"].data.max(), 4.991666666) npt.assert_allclose(data.coords["lon"].data.min(), -9.99166666) diff --git a/pygmt/tests/test_datasets_load_remote_datasets.py b/pygmt/tests/test_datasets_load_remote_datasets.py index 645af9b9cd0..1f0c49bc4ed 100644 --- a/pygmt/tests/test_datasets_load_remote_datasets.py +++ b/pygmt/tests/test_datasets_load_remote_datasets.py @@ -4,7 +4,7 @@ import pytest from pygmt.datasets.load_remote_dataset import _load_remote_dataset -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration from pygmt.exceptions import GMTInvalidInput @@ -31,7 +31,7 @@ def test_load_remote_dataset_benchmark_with_region(): assert data.attrs["long_name"] == "ages (Myr)" assert data.attrs["units"] == "Myr" assert data.attrs["horizontal_datum"] == "WGS84" - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.shape == (11, 21) # Can't access the cpt attribute using virtual files # assert data.attrs["cpt"] == "@earth_age.cpt" diff --git a/pygmt/tests/test_datasets_mars_relief.py b/pygmt/tests/test_datasets_mars_relief.py index f86dfa5378b..e60265b6044 100644 --- a/pygmt/tests/test_datasets_mars_relief.py +++ b/pygmt/tests/test_datasets_mars_relief.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_mars_relief -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration def test_mars_relief_01d(): @@ -18,7 +18,7 @@ def test_mars_relief_01d(): assert data.attrs["description"] == "NASA Mars (MOLA) relief" assert data.attrs["units"] == "meters" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -7421.0, atol=0.5) @@ -31,7 +31,7 @@ def test_mars_relief_01d_with_region(): """ data = load_mars_relief(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -2502.0, atol=0.5) @@ -45,7 +45,7 @@ def test_mars_relief_01m_default_registration(): """ data = load_mars_relief(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_mercury_relief.py b/pygmt/tests/test_datasets_mercury_relief.py index a2504a319fe..a81af88066e 100644 --- a/pygmt/tests/test_datasets_mercury_relief.py +++ b/pygmt/tests/test_datasets_mercury_relief.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_mercury_relief -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration def test_mercury_relief_01d(): @@ -18,7 +18,7 @@ def test_mercury_relief_01d(): assert data.attrs["description"] == "USGS Mercury relief" assert data.attrs["units"] == "meters" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -5103.5, atol=0.5) @@ -31,7 +31,7 @@ def test_mercury_relief_01d_with_region(): """ data = load_mercury_relief(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -327.0, atol=0.5) @@ -45,7 +45,7 @@ def test_mercury_relief_01m_default_registration(): """ data = load_mercury_relief(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_moon_relief.py b/pygmt/tests/test_datasets_moon_relief.py index b0b6734dcc2..50d20e910ee 100644 --- a/pygmt/tests/test_datasets_moon_relief.py +++ b/pygmt/tests/test_datasets_moon_relief.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_moon_relief -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration def test_moon_relief_01d(): @@ -18,7 +18,7 @@ def test_moon_relief_01d(): assert data.attrs["description"] == "USGS Moon (LOLA) relief" assert data.attrs["units"] == "meters" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -7669.0, atol=0.5) @@ -31,7 +31,7 @@ def test_moon_relief_01d_with_region(): """ data = load_moon_relief(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -1122.0, atol=0.5) @@ -45,7 +45,7 @@ def test_moon_relief_01m_default_registration(): """ data = load_moon_relief(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_pluto_relief.py b/pygmt/tests/test_datasets_pluto_relief.py index f94f450832d..dd6ac599ab4 100644 --- a/pygmt/tests/test_datasets_pluto_relief.py +++ b/pygmt/tests/test_datasets_pluto_relief.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_pluto_relief -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration def test_pluto_relief_01d(): @@ -18,7 +18,7 @@ def test_pluto_relief_01d(): assert data.attrs["description"] == "USGS Pluto relief" assert data.attrs["units"] == "meters" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -3021.0, atol=0.25) @@ -31,7 +31,7 @@ def test_pluto_relief_01d_with_region(): """ data = load_pluto_relief(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -1319.25, atol=0.25) @@ -45,7 +45,7 @@ def test_pluto_relief_01m_default_registration(): """ data = load_pluto_relief(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_venus_relief.py b/pygmt/tests/test_datasets_venus_relief.py index 33dcf01fb92..2790ad247e7 100644 --- a/pygmt/tests/test_datasets_venus_relief.py +++ b/pygmt/tests/test_datasets_venus_relief.py @@ -5,7 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_venus_relief -from pygmt.enums import GridReg +from pygmt.enums import GridRegistration def test_venus_relief_01d(): @@ -18,7 +18,7 @@ def test_venus_relief_01d(): assert data.attrs["description"] == "NASA Magellan Venus relief" assert data.attrs["units"] == "meters" assert data.shape == (181, 361) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -2069.0, atol=0.5) @@ -31,7 +31,7 @@ def test_venus_relief_01d_with_region(): """ data = load_venus_relief(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -1244.0, atol=0.5) @@ -45,7 +45,7 @@ def test_venus_relief_01m_default_registration(): """ data = load_venus_relief(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == GridReg.GRIDLINE + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_dimfilter.py b/pygmt/tests/test_dimfilter.py index 9a3de9d582d..3de660e9b4b 100644 --- a/pygmt/tests/test_dimfilter.py +++ b/pygmt/tests/test_dimfilter.py @@ -7,7 +7,7 @@ import pytest import xarray as xr from pygmt import dimfilter, load_dataarray -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -71,7 +71,7 @@ def test_dimfilter_no_outgrid(grid, expected_grid): ) assert result.dims == ("lat", "lon") assert result.gmt.gtype == GridType.GEOGRAPHIC - assert result.gmt.registration == GridReg.PIXEL + assert result.gmt.registration == GridRegistration.PIXEL xr.testing.assert_allclose(a=result, b=expected_grid) diff --git a/pygmt/tests/test_grdclip.py b/pygmt/tests/test_grdclip.py index 2c28e3c04a3..d759e28eb91 100644 --- a/pygmt/tests/test_grdclip.py +++ b/pygmt/tests/test_grdclip.py @@ -7,7 +7,7 @@ import pytest import xarray as xr from pygmt import grdclip, load_dataarray -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -53,7 +53,7 @@ def test_grdclip_outgrid(grid, expected_grid): temp_grid = load_dataarray(tmpfile.name) assert temp_grid.dims == ("lat", "lon") assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC - assert temp_grid.gmt.registration == GridReg.PIXEL + assert temp_grid.gmt.registration == GridRegistration.PIXEL xr.testing.assert_allclose(a=temp_grid, b=expected_grid) @@ -67,5 +67,5 @@ def test_grdclip_no_outgrid(grid, expected_grid): ) assert temp_grid.dims == ("lat", "lon") assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC - assert temp_grid.gmt.registration == GridReg.PIXEL + assert temp_grid.gmt.registration == GridRegistration.PIXEL xr.testing.assert_allclose(a=temp_grid, b=expected_grid) diff --git a/pygmt/tests/test_grdfill.py b/pygmt/tests/test_grdfill.py index d3bb4369f0e..8d36125c279 100644 --- a/pygmt/tests/test_grdfill.py +++ b/pygmt/tests/test_grdfill.py @@ -8,7 +8,7 @@ import pytest import xarray as xr from pygmt import grdfill, load_dataarray -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -80,7 +80,7 @@ def test_grdfill_dataarray_out(grid, expected_grid): # check information of the output grid assert isinstance(result, xr.DataArray) assert result.gmt.gtype == GridType.GEOGRAPHIC - assert result.gmt.registration == GridReg.PIXEL + assert result.gmt.registration == GridRegistration.PIXEL # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) @@ -95,7 +95,7 @@ def test_grdfill_asymmetric_pad(grid, expected_grid): # check information of the output grid assert isinstance(result, xr.DataArray) assert result.gmt.gtype == GridType.GEOGRAPHIC - assert result.gmt.registration == GridReg.PIXEL + assert result.gmt.registration == GridRegistration.PIXEL # check information of the output grid xr.testing.assert_allclose( a=result, b=expected_grid.sel(lon=slice(-55, -50), lat=slice(-24, -16)) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index f5e5474fae5..67c94f6a52c 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -8,7 +8,7 @@ import pytest import xarray as xr from pygmt import grdfilter, load_dataarray -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -52,7 +52,7 @@ def test_grdfilter_dataarray_in_dataarray_out(grid, expected_grid): # check information of the output grid assert isinstance(result, xr.DataArray) assert result.gmt.gtype == GridType.GEOGRAPHIC - assert result.gmt.registration == GridReg.PIXEL + assert result.gmt.registration == GridRegistration.PIXEL # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py index 05258257dbe..c8b6695fa7d 100644 --- a/pygmt/tests/test_grdgradient.py +++ b/pygmt/tests/test_grdgradient.py @@ -7,7 +7,7 @@ import pytest import xarray as xr from pygmt import grdgradient, load_dataarray -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -68,7 +68,7 @@ def test_grdgradient_no_outgrid(grid, expected_grid): # check information of the output grid assert isinstance(result, xr.DataArray) assert result.gmt.gtype == GridType.GEOGRAPHIC - assert result.gmt.registration == GridReg.PIXEL + assert result.gmt.registration == GridRegistration.PIXEL # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) diff --git a/pygmt/tests/test_grdhisteq.py b/pygmt/tests/test_grdhisteq.py index 7e212965615..3a9f0740adf 100644 --- a/pygmt/tests/test_grdhisteq.py +++ b/pygmt/tests/test_grdhisteq.py @@ -9,7 +9,7 @@ import pytest import xarray as xr from pygmt import grdhisteq, load_dataarray -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -80,7 +80,7 @@ def test_equalize_grid_no_outgrid(grid, expected_grid, region): grid=grid, divisions=2, region=region, outgrid=None ) assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC - assert temp_grid.gmt.registration == GridReg.PIXEL + assert temp_grid.gmt.registration == GridRegistration.PIXEL xr.testing.assert_allclose(a=temp_grid, b=expected_grid) diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index 01b875303e7..fdcbc448da3 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -9,7 +9,7 @@ from pygmt import Figure from pygmt.clib import __gmt_version__ from pygmt.datasets import load_earth_relief -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers.testing import check_figures_equal @@ -166,13 +166,13 @@ def test_grdimage_over_dateline(xrgrid): Ensure no gaps are plotted over the 180 degree international dateline. Specifically checking that ``xrgrid.gmt.gtype = GridType.GEOGRAPHIC`` sets - ``GMT_GRID_IS_GEO``, and that ``xrgrid.gmt.registration = GridReg.GRIDLINE` sets - ``GMT_GRID_NODE_REG``. Note that there would be a gap over the dateline if a pixel - registered grid is used. + ``GMT_GRID_IS_GEO``, and that ``xrgrid.gmt.registration = GridRegistration.GRIDLINE` + sets ``GMT_GRID_NODE_REG``. Note that there would be a gap over the dateline if a + pixel-registered grid is used. See also https://github.com/GenericMappingTools/pygmt/issues/375. """ fig = Figure() - assert xrgrid.gmt.registration == GridReg.GRIDLINE + assert xrgrid.gmt.registration == GridRegistration.GRIDLINE xrgrid.gmt.gtype = GridType.GEOGRAPHIC fig.grdimage(grid=xrgrid, region="g", projection="A0/0/1c") return fig @@ -190,7 +190,7 @@ def test_grdimage_global_subset(grid_360): """ # Get a slice of South America and Africa only (lat=-90:31, lon=-180:41) sliced_grid = grid_360[0:121, 0:221] - assert sliced_grid.gmt.registration == GridReg.GRIDLINE + assert sliced_grid.gmt.registration == GridRegistration.GRIDLINE assert sliced_grid.gmt.gtype == GridType.CARTESIAN fig = Figure() @@ -276,7 +276,7 @@ def test_grdimage_grid_no_redundant_360(): # Global grid [-180, 179, -90, 90] without redundant longitude at 180/-180 da3 = da1[:, 0:360] - da3.gmt.registration = GridReg.GRIDLINE + da3.gmt.registration = GridRegistration.GRIDLINE da3.gmt.gtype = GridType.GEOGRAPHIC assert da3.shape == (181, 360) assert da3.lon.to_numpy().min() == -180.0 @@ -284,7 +284,7 @@ def test_grdimage_grid_no_redundant_360(): # Global grid [0, 359, -90, 90] without redundant longitude at 360/0 da4 = da2[:, 0:360] - da4.gmt.registration = GridReg.GRIDLINE + da4.gmt.registration = GridRegistration.GRIDLINE da4.gmt.gtype = GridType.GEOGRAPHIC assert da4.shape == (181, 360) assert da4.lon.to_numpy().min() == 0.0 diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py index 5c5e751e0a4..bfae4cc965c 100644 --- a/pygmt/tests/test_grdlandmask.py +++ b/pygmt/tests/test_grdlandmask.py @@ -7,7 +7,7 @@ import pytest import xarray as xr from pygmt import grdlandmask, load_dataarray -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -55,7 +55,7 @@ def test_grdlandmask_no_outgrid(expected_grid): # check information of the output grid assert isinstance(result, xr.DataArray) assert result.gmt.gtype == GridType.GEOGRAPHIC - assert result.gmt.registration == GridReg.GRIDLINE + assert result.gmt.registration == GridRegistration.GRIDLINE # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) diff --git a/pygmt/tests/test_grdproject.py b/pygmt/tests/test_grdproject.py index 8d2b326044d..efd8ae7503d 100644 --- a/pygmt/tests/test_grdproject.py +++ b/pygmt/tests/test_grdproject.py @@ -7,7 +7,7 @@ import pytest import xarray as xr from pygmt import grdproject, load_dataarray -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -76,7 +76,7 @@ def test_grdproject_no_outgrid(grid, projection, expected_grid): grid=grid, projection=projection, spacing=3, region=[-53, -51, -20, -17] ) assert result.gmt.gtype == GridType.CARTESIAN - assert result.gmt.registration == GridReg.PIXEL + assert result.gmt.registration == GridRegistration.PIXEL # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) diff --git a/pygmt/tests/test_grdsample.py b/pygmt/tests/test_grdsample.py index 276c710bee7..7559a791623 100644 --- a/pygmt/tests/test_grdsample.py +++ b/pygmt/tests/test_grdsample.py @@ -7,7 +7,7 @@ import pytest import xarray as xr from pygmt import grdsample, load_dataarray -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -80,7 +80,7 @@ def test_grdsample_dataarray_out(grid, expected_grid, region, spacing): # check information of the output grid assert isinstance(result, xr.DataArray) assert result.gmt.gtype == GridType.GEOGRAPHIC - assert result.gmt.registration == GridReg.PIXEL + assert result.gmt.registration == GridRegistration.PIXEL # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) @@ -89,8 +89,8 @@ def test_grdsample_registration_changes(grid): """ Test grdsample with no set outgrid and applying registration changes. """ - assert grid.gmt.registration == GridReg.PIXEL + assert grid.gmt.registration == GridRegistration.PIXEL translated_grid = grdsample(grid=grid, translate=True) - assert translated_grid.gmt.registration == GridReg.GRIDLINE + assert translated_grid.gmt.registration == GridRegistration.GRIDLINE registration_grid = grdsample(grid=translated_grid, registration="p") - assert registration_grid.gmt.registration == GridReg.PIXEL + assert registration_grid.gmt.registration == GridRegistration.PIXEL diff --git a/pygmt/tests/test_io.py b/pygmt/tests/test_io.py index 9e8232374ef..ade706486e3 100644 --- a/pygmt/tests/test_io.py +++ b/pygmt/tests/test_io.py @@ -5,7 +5,7 @@ import numpy as np import pytest import xarray as xr -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.helpers import GMTTempFile from pygmt.io import load_dataarray @@ -24,7 +24,7 @@ def test_io_load_dataarray(): grid.to_netcdf(tmpfile.name) dataarray = load_dataarray(tmpfile.name) assert dataarray.gmt.gtype == GridType.CARTESIAN - assert dataarray.gmt.registration == GridReg.PIXEL + assert dataarray.gmt.registration == GridRegistration.PIXEL # this would fail if we used xr.open_dataarray instead of load_dataarray dataarray.to_netcdf(tmpfile.name) diff --git a/pygmt/tests/test_nearneighbor.py b/pygmt/tests/test_nearneighbor.py index 010543515fd..aa25c6a0fa3 100644 --- a/pygmt/tests/test_nearneighbor.py +++ b/pygmt/tests/test_nearneighbor.py @@ -10,7 +10,7 @@ import xarray as xr from pygmt import nearneighbor from pygmt.datasets import load_sample_data -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -33,7 +33,7 @@ def test_nearneighbor_input_data(array_func, ship_data): data=data, spacing="5m", region=[245, 255, 20, 30], search_radius="10m" ) assert isinstance(output, xr.DataArray) - assert output.gmt.registration == GridReg.GRIDLINE + assert output.gmt.registration == GridRegistration.GRIDLINE assert output.gmt.gtype == GridType.GEOGRAPHIC assert output.shape == (121, 121) npt.assert_allclose(output.mean(), -2378.2385) diff --git a/pygmt/tests/test_sph2grd.py b/pygmt/tests/test_sph2grd.py index cc8de1b943a..e3c71293621 100644 --- a/pygmt/tests/test_sph2grd.py +++ b/pygmt/tests/test_sph2grd.py @@ -7,7 +7,7 @@ import numpy.testing as npt import pytest from pygmt import sph2grd -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.helpers import GMTTempFile @@ -31,7 +31,7 @@ def test_sph2grd_no_outgrid(): temp_grid = sph2grd(data="@EGM96_to_36.txt", spacing=1, region="g", cores=2) assert temp_grid.dims == ("y", "x") assert temp_grid.gmt.gtype == GridType.CARTESIAN - assert temp_grid.gmt.registration == GridReg.GRIDLINE + assert temp_grid.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(temp_grid.max(), 0.00021961, rtol=1e-4) npt.assert_allclose(temp_grid.min(), -0.0004326, rtol=1e-4) npt.assert_allclose(temp_grid.median(), -0.00010894, rtol=1e-4) diff --git a/pygmt/tests/test_sphdistance.py b/pygmt/tests/test_sphdistance.py index eb1e334beda..ac3701fb0c3 100644 --- a/pygmt/tests/test_sphdistance.py +++ b/pygmt/tests/test_sphdistance.py @@ -8,7 +8,7 @@ import numpy.testing as npt import pytest from pygmt import sphdistance -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -31,7 +31,7 @@ def test_sphdistance_xy_inputs(): temp_grid = sphdistance(x=x, y=y, spacing=[1, 2], region=[82, 87, 22, 24]) assert temp_grid.dims == ("lat", "lon") assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC - assert temp_grid.gmt.registration == GridReg.GRIDLINE + assert temp_grid.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(temp_grid.max(), 232977.546875) npt.assert_allclose(temp_grid.min(), 0) npt.assert_allclose(temp_grid.median(), 0) @@ -58,7 +58,7 @@ def test_sphdistance_no_outgrid(array): temp_grid = sphdistance(data=array, spacing=[1, 2], region=[82, 87, 22, 24]) assert temp_grid.dims == ("lat", "lon") assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC - assert temp_grid.gmt.registration == GridReg.GRIDLINE + assert temp_grid.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(temp_grid.max(), 232977.546875) npt.assert_allclose(temp_grid.min(), 0) npt.assert_allclose(temp_grid.median(), 0) diff --git a/pygmt/tests/test_sphinterpolate.py b/pygmt/tests/test_sphinterpolate.py index 0991ab992be..7b91f00b24e 100644 --- a/pygmt/tests/test_sphinterpolate.py +++ b/pygmt/tests/test_sphinterpolate.py @@ -8,7 +8,7 @@ import pytest from pygmt import sphinterpolate from pygmt.datasets import load_sample_data -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.helpers import GMTTempFile @@ -38,7 +38,7 @@ def test_sphinterpolate_no_outgrid(mars): temp_grid = sphinterpolate(data=mars, spacing=1, region="g") assert temp_grid.dims == ("lat", "lon") assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC - assert temp_grid.gmt.registration == GridReg.GRIDLINE + assert temp_grid.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(temp_grid.max(), 14628.144) npt.assert_allclose(temp_grid.min(), -6908.1987) npt.assert_allclose(temp_grid.median(), 118.96849) diff --git a/pygmt/tests/test_surface.py b/pygmt/tests/test_surface.py index 75e77d8d89a..b49e6875304 100644 --- a/pygmt/tests/test_surface.py +++ b/pygmt/tests/test_surface.py @@ -8,7 +8,7 @@ import pytest import xarray as xr from pygmt import surface, which -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -73,7 +73,7 @@ def check_values(grid, expected_grid): Check the attributes and values of the DataArray returned by surface. """ assert isinstance(grid, xr.DataArray) - assert grid.gmt.registration == GridReg.GRIDLINE + assert grid.gmt.registration == GridRegistration.GRIDLINE assert grid.gmt.gtype == GridType.CARTESIAN xr.testing.assert_allclose(a=grid, b=expected_grid) diff --git a/pygmt/tests/test_triangulate.py b/pygmt/tests/test_triangulate.py index 5d4367c3934..5f9c7a4c319 100644 --- a/pygmt/tests/test_triangulate.py +++ b/pygmt/tests/test_triangulate.py @@ -9,7 +9,7 @@ import pytest import xarray as xr from pygmt import triangulate, which -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -140,7 +140,7 @@ def test_regular_grid_no_outgrid(dataframe, expected_grid): data = dataframe.to_numpy() output = triangulate.regular_grid(data=data, spacing=1, region=[2, 4, 5, 6]) assert isinstance(output, xr.DataArray) - assert output.gmt.registration == GridReg.GRIDLINE + assert output.gmt.registration == GridRegistration.GRIDLINE assert output.gmt.gtype == GridType.CARTESIAN xr.testing.assert_allclose(a=output, b=expected_grid) @@ -158,6 +158,6 @@ def test_regular_grid_with_outgrid_param(dataframe, expected_grid): assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists with xr.open_dataarray(tmpfile.name) as grid: assert isinstance(grid, xr.DataArray) - assert grid.gmt.registration == GridReg.GRIDLINE + assert grid.gmt.registration == GridRegistration.GRIDLINE assert grid.gmt.gtype == GridType.CARTESIAN xr.testing.assert_allclose(a=grid, b=expected_grid) diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index ddc9a300231..9e22d376695 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -9,7 +9,7 @@ import xarray as xr from pygmt import load_dataarray, xyz2grd from pygmt.datasets import load_sample_data -from pygmt.enums import GridReg, GridType +from pygmt.enums import GridRegistration, GridType from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -49,7 +49,7 @@ def test_xyz2grd_input_array(array_func, ship_data, expected_grid): """ output = xyz2grd(data=array_func(ship_data), spacing=5, region=[245, 255, 20, 30]) assert isinstance(output, xr.DataArray) - assert output.gmt.registration == GridReg.GRIDLINE + assert output.gmt.registration == GridRegistration.GRIDLINE assert output.gmt.gtype == GridType.CARTESIAN xr.testing.assert_allclose(a=output, b=expected_grid) From d2a3e6830651acd52e190a70cc4696cd0f9759ce Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 19 Dec 2024 12:09:06 +0800 Subject: [PATCH 10/21] Minor fixes --- pygmt/accessors.py | 2 +- pygmt/tests/test_accessor.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index 52b10ea3d15..0989a1565c1 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -164,7 +164,7 @@ def registration(self, value): # Python 3.12+. if value not in GridRegistration.__members__.values(): msg = ( - f"Invalid grid registration: {value}. " + f"Invalid grid registration: '{value}'. " "Should be either GridRegistration.GRIDLINE or GridRegistration.PIXEL." ) raise GMTInvalidInput(msg) diff --git a/pygmt/tests/test_accessor.py b/pygmt/tests/test_accessor.py index 7492e55fd40..e3a4b35de7f 100644 --- a/pygmt/tests/test_accessor.py +++ b/pygmt/tests/test_accessor.py @@ -42,9 +42,8 @@ def test_accessor_set_registration(): Check that we can set the registration of a grid. """ grid = xr.DataArray(data=[[0.1, 0.2], [0.3, 0.4]]) - assert ( - grid.gmt.registration == GridRegistration.GRIDLINE == 0 - ) # Default registration + # Default to gridline registration + assert grid.gmt.registration == GridRegistration.GRIDLINE == 0 # Set the registration to pixel grid.gmt.registration = GridRegistration.PIXEL From c17983bbc44693edcd60d3822921bffef9606020 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 19 Dec 2024 14:17:11 +0800 Subject: [PATCH 11/21] Improve accessor docstrings --- pygmt/accessors.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index 0989a1565c1..29fa086e442 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -18,25 +18,13 @@ class GMTDataArrayAccessor: The *gmt* accessor extends :class:`xarray.DataArray` to store GMT-specific properties for grids, which are important for PyGMT to correctly process and plot - the grids. - - The *gmt* accessor contains following properties: + the grids. The *gmt* accessor contains following properties: - ``registration``: Grid registration type, either ``GridRegistration.GRIDLINE`` or ``GridRegistration.PIXEL``. - ``gtype``: Grid coordinate system type, either ``GridType.CARTESIAN`` or ``GridType.GEOGRAPHIC``. - and can be accessed like ``grid.gmt.registration`` and ``grid.gmt.gtype``. - - Notes - ----- - Due to the limitations of xarray accessors, the GMT accessors are created once per - :class:`xarray.DataArray` instance. You may lose these GMT-specific properties when - manipulating grids (e.g., arithmetic and slice operations) or when accessing a - :class:`xarray.DataArray` from a :class:`xarray.Dataset`. In these cases, you need - to manually set these properties before passing the grid to PyGMT. - Examples -------- For GMT's built-in remote datasets, these GMT-specific properties are automatically @@ -81,8 +69,13 @@ class GMTDataArrayAccessor: >>> grid.gmt.gtype - Note that the accessors are created once per :class:`xarray.DataArray` instance, so - you may lose these GMT-specific properties after manipulating your grid. + Notes + ----- + Due to the limitations of xarray accessors, the GMT accessors are created once per + :class:`xarray.DataArray` instance. You may lose these GMT-specific properties when + manipulating grids (e.g., arithmetic and slice operations) or when accessing a + :class:`xarray.DataArray` from a :class:`xarray.Dataset`. In these cases, you need + to manually set these properties before passing the grid to PyGMT. Inplace assignment operators like ``*=`` don't create new instances, so the properties are still kept: From 724f27485f3ae0a10751f9b8278048d75a4d79c7 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 19 Dec 2024 15:56:55 +0800 Subject: [PATCH 12/21] Update pygmt/accessors.py [skip ci] Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com> --- pygmt/accessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index 29fa086e442..0a0932e7978 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -18,7 +18,7 @@ class GMTDataArrayAccessor: The *gmt* accessor extends :class:`xarray.DataArray` to store GMT-specific properties for grids, which are important for PyGMT to correctly process and plot - the grids. The *gmt* accessor contains following properties: + the grids. The *gmt* accessor contains the following properties: - ``registration``: Grid registration type, either ``GridRegistration.GRIDLINE`` or ``GridRegistration.PIXEL``. From 8c1513ae0156214b19fed7c3708af6d102133830 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 19 Dec 2024 15:59:45 +0800 Subject: [PATCH 13/21] Fix references to enums GridRegistration and GridType --- pygmt/accessors.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index 0a0932e7978..8f80833a184 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -20,10 +20,8 @@ class GMTDataArrayAccessor: properties for grids, which are important for PyGMT to correctly process and plot the grids. The *gmt* accessor contains the following properties: - - ``registration``: Grid registration type, either ``GridRegistration.GRIDLINE`` or - ``GridRegistration.PIXEL``. - - ``gtype``: Grid coordinate system type, either ``GridType.CARTESIAN`` or - ``GridType.GEOGRAPHIC``. + - ``registration``: Grid registration type :class:`pygmt.enums.GridRegistration`. + - ``gtype``: Grid coordinate system type :class:`pygmt.enums.GridType`. Examples -------- From d525edef642773fd3f412c14eb73c948e0b870b2 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 21 Dec 2024 13:39:56 +0800 Subject: [PATCH 14/21] Use TODO in comments so we can easily find it in the future --- pygmt/accessors.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index 8f80833a184..e7f5636ce1c 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -144,14 +144,13 @@ def __init__(self, xarray_obj): @property def registration(self): """ - Registration type of the grid, either ``GridRegistration.GRIDLINE`` or - ``GridRegistration.PIXEL``. + Grid registration type :class:`pygmt.enums.GridRegistration`. """ return self._registration @registration.setter def registration(self, value): - # Can be simplified to `if value not in GridRegistration` after requiring + # TODO: Simplify to `if value not in GridRegistration` after requiring # Python 3.12+. if value not in GridRegistration.__members__.values(): msg = ( @@ -164,14 +163,13 @@ def registration(self, value): @property def gtype(self): """ - Coordinate system type of the grid, either ``GridType.CARTESIAN`` or - ``GridType.GEOGRAPHIC``. + Grid coordinate system type :class:`pygmt.enums.GridType`. """ return self._gtype @gtype.setter def gtype(self, value): - # Can be simplified to `if value not in GridType` after requiring Python 3.12+. + # TODO: Simplify to `if value not in GridType` after requiring Python 3.12+.. if value not in GridType.__members__.values(): msg = ( f"Invalid grid coordinate system type: '{value}'. " From 5139666dc002b46f30ee7ae9ce6f6d16edf6abf9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 26 Dec 2024 11:25:00 +0800 Subject: [PATCH 15/21] Update tests for earth_dist --- pygmt/tests/test_datasets_earth_dist.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_datasets_earth_dist.py b/pygmt/tests/test_datasets_earth_dist.py index e0d91d77a84..e6d80422883 100644 --- a/pygmt/tests/test_datasets_earth_dist.py +++ b/pygmt/tests/test_datasets_earth_dist.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_dist +from pygmt.enums import GridRegistration def test_earth_dist_01d(): @@ -17,7 +18,7 @@ def test_earth_dist_01d(): assert data.attrs["units"] == "km" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -2655.7, atol=0.01) @@ -30,7 +31,7 @@ def test_earth_dist_01d_with_region(): """ data = load_earth_dist(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -1081.94, atol=0.01) @@ -44,7 +45,7 @@ def test_earth_dist_01m_default_registration(): """ data = load_earth_dist(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 From 121b8457f2dab94329a24ef2aed21170c07f1879 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 26 Dec 2024 20:52:04 +0800 Subject: [PATCH 16/21] Update two more dataset tests --- pygmt/tests/test_datasets_earth_mean_sea_surface.py | 7 ++++--- pygmt/tests/test_datasets_mean_dynamic_topography.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pygmt/tests/test_datasets_earth_mean_sea_surface.py b/pygmt/tests/test_datasets_earth_mean_sea_surface.py index 84b2b7123cc..c93b180936c 100644 --- a/pygmt/tests/test_datasets_earth_mean_sea_surface.py +++ b/pygmt/tests/test_datasets_earth_mean_sea_surface.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_mean_sea_surface +from pygmt.enums import GridRegistration def test_earth_mss_01d(): @@ -17,7 +18,7 @@ def test_earth_mss_01d(): assert data.attrs["units"] == "meters" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.Gridline npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -104.71, atol=0.01) @@ -30,7 +31,7 @@ def test_earth_mss_01d_with_region(): """ data = load_earth_mean_sea_surface(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.Gridline npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), 6.53, atol=0.01) @@ -44,7 +45,7 @@ def test_earth_mss_01m_default_registration(): """ data = load_earth_mean_sea_surface(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.Gridline assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_mean_dynamic_topography.py b/pygmt/tests/test_datasets_mean_dynamic_topography.py index deae6e90a60..2a8480a4ee7 100644 --- a/pygmt/tests/test_datasets_mean_dynamic_topography.py +++ b/pygmt/tests/test_datasets_mean_dynamic_topography.py @@ -5,6 +5,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_mean_dynamic_topography +from pygmt.enums import GridRegistration def test_earth_mdt_01d(): @@ -17,7 +18,7 @@ def test_earth_mdt_01d(): assert data.attrs["units"] == "meters" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.Gridline npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -1.4668, atol=0.0001) @@ -30,7 +31,7 @@ def test_earth_mdt_01d_with_region(): """ data = load_earth_mean_dynamic_topography(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.Gridline npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), 0.346, atol=0.0001) @@ -44,7 +45,7 @@ def test_earth_mdt_07m_default_registration(): """ data = load_earth_mean_dynamic_topography(resolution="07m", region=[-10, -9, 3, 5]) assert data.shape == (17, 9) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.Gridline assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 From e536a91a2f3aeba9803ad851600af3c5c9c9243d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 5 Jan 2025 11:07:46 +0800 Subject: [PATCH 17/21] Apply to newly added datasets --- pygmt/tests/test_datasets_earth_deflection.py | 13 +++++++------ pygmt/tests/test_datasets_earth_free_air_anomaly.py | 6 +++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pygmt/tests/test_datasets_earth_deflection.py b/pygmt/tests/test_datasets_earth_deflection.py index 9118779a379..a30eab07902 100644 --- a/pygmt/tests/test_datasets_earth_deflection.py +++ b/pygmt/tests/test_datasets_earth_deflection.py @@ -6,6 +6,7 @@ import numpy as np import numpy.testing as npt from pygmt.datasets import load_earth_deflection +from pygmt.enums import GridRegistration def test_earth_edefl_01d(): @@ -19,7 +20,7 @@ def test_earth_edefl_01d(): assert data.attrs["units"] == "micro-radians" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -142.64, atol=0.04) @@ -32,7 +33,7 @@ def test_earth_edefl_01d_with_region(): """ data = load_earth_deflection(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -28.92, atol=0.04) @@ -46,7 +47,7 @@ def test_earth_edefl_01m_default_registration(): """ data = load_earth_deflection(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (120, 60) - assert data.gmt.registration == 1 + assert data.gmt.registration == GridRegistration.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 3.008333333) npt.assert_allclose(data.coords["lat"].data.max(), 4.991666666) npt.assert_allclose(data.coords["lon"].data.min(), -9.99166666) @@ -66,7 +67,7 @@ def test_earth_ndefl_01d(): assert data.attrs["units"] == "micro-radians" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -214.8, atol=0.04) @@ -81,7 +82,7 @@ def test_earth_ndefl_01d_with_region(): resolution="01d", region=[-10, 10, -5, 5], component="north" ) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), -48.08, atol=0.04) @@ -97,7 +98,7 @@ def test_earth_ndefl_01m_default_registration(): resolution="01m", region=[-10, -9, 3, 5], component="north" ) assert data.shape == (120, 60) - assert data.gmt.registration == 1 + assert data.gmt.registration == GridRegistration.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 3.008333333) npt.assert_allclose(data.coords["lat"].data.max(), 4.991666666) npt.assert_allclose(data.coords["lon"].data.min(), -9.99166666) diff --git a/pygmt/tests/test_datasets_earth_free_air_anomaly.py b/pygmt/tests/test_datasets_earth_free_air_anomaly.py index f2faeaffcaf..a60c1fec2e6 100644 --- a/pygmt/tests/test_datasets_earth_free_air_anomaly.py +++ b/pygmt/tests/test_datasets_earth_free_air_anomaly.py @@ -66,7 +66,7 @@ def test_earth_faaerror_01d(): assert data.attrs["units"] == "mGal" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), 0.0, atol=0.04) @@ -81,7 +81,7 @@ def test_earth_faaerror_01d_with_region(): resolution="01d", region=[-10, 10, -5, 5], uncertainty=True ) assert data.shape == (11, 21) - assert data.gmt.registration == 0 + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), 0.72, atol=0.04) @@ -97,7 +97,7 @@ def test_earth_faaerror_01m_default_registration(): resolution="01m", region=[-10, -9, 3, 5], uncertainty=True ) assert data.shape == (120, 60) - assert data.gmt.registration == 1 + assert data.gmt.registration == GridRegistration.PIXEL npt.assert_allclose(data.coords["lat"].data.min(), 3.008333333) npt.assert_allclose(data.coords["lat"].data.max(), 4.991666666) npt.assert_allclose(data.coords["lon"].data.min(), -9.99166666) From 3ddb42d166bd8b464342ff3648bd40d068f4425d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 5 Jan 2025 11:08:35 +0800 Subject: [PATCH 18/21] Fix TODO comments --- pygmt/accessors.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index e7f5636ce1c..7147022ae78 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -150,8 +150,7 @@ def registration(self): @registration.setter def registration(self, value): - # TODO: Simplify to `if value not in GridRegistration` after requiring - # Python 3.12+. + # TODO(Python>=3.12): Simplify to `if value not in GridRegistration`. if value not in GridRegistration.__members__.values(): msg = ( f"Invalid grid registration: '{value}'. " @@ -169,7 +168,7 @@ def gtype(self): @gtype.setter def gtype(self, value): - # TODO: Simplify to `if value not in GridType` after requiring Python 3.12+.. + # TODO(Python>=3.12): Simplify to `if value not in GridType`. if value not in GridType.__members__.values(): msg = ( f"Invalid grid coordinate system type: '{value}'. " From 4bd2a4643178a2b76ae3aaedb14630ec18963c22 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 5 Jan 2025 11:15:32 +0800 Subject: [PATCH 19/21] Fix to upper case --- pygmt/tests/test_datasets_earth_mean_sea_surface.py | 6 +++--- pygmt/tests/test_datasets_mean_dynamic_topography.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pygmt/tests/test_datasets_earth_mean_sea_surface.py b/pygmt/tests/test_datasets_earth_mean_sea_surface.py index c93b180936c..7206cbf911f 100644 --- a/pygmt/tests/test_datasets_earth_mean_sea_surface.py +++ b/pygmt/tests/test_datasets_earth_mean_sea_surface.py @@ -18,7 +18,7 @@ def test_earth_mss_01d(): assert data.attrs["units"] == "meters" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridRegistration.Gridline + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -104.71, atol=0.01) @@ -31,7 +31,7 @@ def test_earth_mss_01d_with_region(): """ data = load_earth_mean_sea_surface(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridRegistration.Gridline + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), 6.53, atol=0.01) @@ -45,7 +45,7 @@ def test_earth_mss_01m_default_registration(): """ data = load_earth_mean_sea_surface(resolution="01m", region=[-10, -9, 3, 5]) assert data.shape == (121, 61) - assert data.gmt.registration == GridRegistration.Gridline + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 diff --git a/pygmt/tests/test_datasets_mean_dynamic_topography.py b/pygmt/tests/test_datasets_mean_dynamic_topography.py index 2a8480a4ee7..c373b727e79 100644 --- a/pygmt/tests/test_datasets_mean_dynamic_topography.py +++ b/pygmt/tests/test_datasets_mean_dynamic_topography.py @@ -18,7 +18,7 @@ def test_earth_mdt_01d(): assert data.attrs["units"] == "meters" assert data.attrs["horizontal_datum"] == "WGS84" assert data.shape == (181, 361) - assert data.gmt.registration == GridRegistration.Gridline + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-90, 91, 1)) npt.assert_allclose(data.lon, np.arange(-180, 181, 1)) npt.assert_allclose(data.min(), -1.4668, atol=0.0001) @@ -31,7 +31,7 @@ def test_earth_mdt_01d_with_region(): """ data = load_earth_mean_dynamic_topography(resolution="01d", region=[-10, 10, -5, 5]) assert data.shape == (11, 21) - assert data.gmt.registration == GridRegistration.Gridline + assert data.gmt.registration == GridRegistration.GRIDLINE npt.assert_allclose(data.lat, np.arange(-5, 6, 1)) npt.assert_allclose(data.lon, np.arange(-10, 11, 1)) npt.assert_allclose(data.min(), 0.346, atol=0.0001) @@ -45,7 +45,7 @@ def test_earth_mdt_07m_default_registration(): """ data = load_earth_mean_dynamic_topography(resolution="07m", region=[-10, -9, 3, 5]) assert data.shape == (17, 9) - assert data.gmt.registration == GridRegistration.Gridline + assert data.gmt.registration == GridRegistration.GRIDLINE assert data.coords["lat"].data.min() == 3.0 assert data.coords["lat"].data.max() == 5.0 assert data.coords["lon"].data.min() == -10.0 From d9bfa3350d8c9ab0dcaea9f56a0077f26264afeb Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 10 Feb 2025 09:33:04 +0800 Subject: [PATCH 20/21] Apply suggestions from code review Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/accessors.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index 7147022ae78..a19942362c9 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -31,7 +31,7 @@ class GMTDataArrayAccessor: >>> from pygmt.datasets import load_earth_relief >>> # Use the global Earth relief grid with 1 degree spacing >>> grid = load_earth_relief(resolution="01d", registration="pixel") - >>> # See if grid is Gridline or Pixel registration + >>> # See if grid uses Gridline or Pixel registration >>> grid.gmt.registration >>> # See if grid is in Cartesian or Geographic coordinate system @@ -54,7 +54,7 @@ class GMTDataArrayAccessor: >>> longrid, latgrid = np.meshgrid(lon, lat) >>> data = np.sin(np.deg2rad(longrid)) * np.cos(np.deg2rad(latgrid)) >>> grid = xr.DataArray(data, coords=[("latitude", lat), ("longitude", lon)]) - >>> # Default to a gridline-registrated Cartesian grid + >>> # Default to a gridline-registered Cartesian grid >>> grid.gmt.registration >>> grid.gmt.gtype @@ -154,7 +154,7 @@ def registration(self, value): if value not in GridRegistration.__members__.values(): msg = ( f"Invalid grid registration: '{value}'. " - "Should be either GridRegistration.GRIDLINE or GridRegistration.PIXEL." + "Should be either GridRegistration.GRIDLINE (0) or GridRegistration.PIXEL (1)." ) raise GMTInvalidInput(msg) self._registration = GridRegistration(value) From 3c0d77b46e57e875db2cc06d7c18e83cf2abf857 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 10 Feb 2025 09:35:47 +0800 Subject: [PATCH 21/21] Mention integer enums in the error message --- pygmt/accessors.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index a19942362c9..711778b7d11 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -153,8 +153,8 @@ def registration(self, value): # TODO(Python>=3.12): Simplify to `if value not in GridRegistration`. if value not in GridRegistration.__members__.values(): msg = ( - f"Invalid grid registration: '{value}'. " - "Should be either GridRegistration.GRIDLINE (0) or GridRegistration.PIXEL (1)." + f"Invalid grid registration: '{value}'. Should be either " + "GridRegistration.GRIDLINE (0) or GridRegistration.PIXEL (1)." ) raise GMTInvalidInput(msg) self._registration = GridRegistration(value) @@ -172,7 +172,7 @@ def gtype(self, value): if value not in GridType.__members__.values(): msg = ( f"Invalid grid coordinate system type: '{value}'. " - "Should be either GridType.CARTESIAN or GridType.GEOGRAPHIC." + "Should be either GridType.CARTESIAN (0) or GridType.GEOGRAPHIC (1)." ) raise GMTInvalidInput(msg) self._gtype = GridType(value)