Skip to content

Set registration and gtype properly as enums on gmt accessor init - #3942

Merged
seisman merged 4 commits into
mainfrom
enum_is_compare
May 9, 2025
Merged

Set registration and gtype properly as enums on gmt accessor init#3942
seisman merged 4 commits into
mainfrom
enum_is_compare

Conversation

@weiji14

@weiji14weiji14 commented May 9, 2025

Copy link
Copy Markdown
Member

Description of proposed changes

Set .gmt.registration and .gmt.gtype accessor values as enums instead of int, for the case when reading from a source file, thereby removing the # type: ignore[assignment] statement.

Also updates tests so that enum member comparison is done using is instead of ==, e.g.:

- assert data.gmt.gtype == GridType.GEOGRAPHIC+ assert data.gmt.gtype is GridType.GEOGRAPHIC- assert data.gmt.registration == GridRegistration.PIXEL+ assert data.gmt.registration is GridRegistration.PIXEL

References:

Fixes#3932 (comment), patches #3696.

Preview:

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash command is:

  • /format: automatically format and lint the code

@weiji14weiji14 self-assigned this May 9, 2025
@weiji14weiji14 added the maintenance Boring but important stuff for the core devs label May 9, 2025
@weiji14

weiji14 commented May 9, 2025

Copy link
Copy Markdown
MemberAuthor

19 test failures 😂 at https://github.com/GenericMappingTools/pygmt/actions/runs/14918473621/job/41909159614?pr=3942#step:10:1078:

Details
=================================== FAILURES ===================================
_____________________________ test_blue_marble_01d _____________________________
deftest_blue_marble_01d():
"""
Test some properties of the Blue Marble 01d data.
"""
data = load_blue_marble(resolution="01d")
assert data.name =="z"assert data.long_name =="blue_marble"assert data.attrs["horizontal_datum"] =="WGS84"assert data.attrs["description"] =="NASA Day Images"assert data.shape == (3, 180, 360)
assert data.dtype =="uint8"
> assert data.gmt.registration is GridRegistration.PIXEL
E AssertionError: assert 1 is <GridRegistration.PIXEL: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0edc2bd0>.registration
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0edc2bd0> = <xarray.DataArray 'z' (band: 3, y: 180, x: 360)> Size: 194kB\narray([[[ 10, 10, 10, ..., 10, 10, 10],\n [ 10, 10, 10, ..., 10, 10, 10],\n [ 10, 10, 10, ..., 10, 10, 10],\n ...,\n [192, 193, 193, ..., 193, 192, 191],\n [204, 206, 206, ..., 205, 206, 204],\n [208, 210, 210, ..., 210, 210, 208]],\n\n [[ 10, 10, 10, ..., 10, 10, 10],\n [ 10, 10, 10, ..., 10, 10, 10],\n [ 10, 10, 10, ..., 10, 10, 10],\n ...,\n [186, 187, 188, ..., 187, 186, 185],\n [196, 198, 198, ..., 197, 197, 196],\n [199, 201, 201, ..., 201, 202, 199]],\n\n [[ 51, 51, 51, ..., 51, 51, 51],\n [ 51, 51, 51, ..., 51, 51, 51],\n [ 51, 51, 51, ..., 51, 51, 51],\n ...,\n [177, 179, 179, ..., 178, 177, 177],\n [185, 187, 187, ..., 187, 186, 185],\n [189, 191, 191, ..., 19
E + and <GridRegistration.PIXEL: 1> = GridRegistration.PIXEL
../pygmt/tests/test_datasets_earth_day.py:22: AssertionError
_______________________ test_blue_marble_01d_with_region _______________________
deftest_blue_marble_01d_with_region():
"""
Test loading low-resolution Blue Marble 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 is GridRegistration.PIXEL
E AssertionError: assert 1 is <GridRegistration.PIXEL: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0edf8d70>.registration
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0edf8d70> = <xarray.DataArray 'z' (band: 3, y: 10, x: 20)> Size: 600B\narray([[[14, 29, 41, 29, 18, 15, 16, 21, 20, 13, 10, 10, 10, 10, 11, 33,\n 62, 55, 45, 58],\n [10, 10, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11,\n 13, 12, 19, 36],\n [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n 10, 10, 11, 24],\n [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n 10, 10, 11, 31],\n [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n 12, 10, 11, 37],\n [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n 11, 10, 17, 47],\n [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n 10, 10, 16, 43],\n [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n 10, 10, 11, 23],\n [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
E + and <GridRegistration.PIXEL: 1> = GridRegistration.PIXEL
../pygmt/tests/test_datasets_earth_day.py:37: AssertionError
____________________________ test_black_marble_01d _____________________________
deftest_black_marble_01d():
"""
Test some properties of the Black Marble 01d data.
"""
data = load_black_marble(resolution="01d")
assert data.name =="z"assert data.long_name =="black_marble"assert data.attrs["horizontal_datum"] =="WGS84"assert data.attrs["description"] =="NASA Night Images"assert data.shape == (3, 180, 360)
assert data.dtype =="uint8"
> assert data.gmt.registration is GridRegistration.PIXEL
E AssertionError: assert 1 is <GridRegistration.PIXEL: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0edc1f10>.registration
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0edc1f10> = <xarray.DataArray 'z' (band: 3, y: 180, x: 360)> Size: 194kB\narray([[[ 4, 4, 4, ..., 4, 4, 4],\n [ 4, 4, 4, ..., 4, 4, 4],\n [ 4, 4, 4, ..., 4, 4, 4],\n ...,\n [43, 43, 43, ..., 43, 43, 43],\n [43, 43, 43, ..., 43, 43, 43],\n [43, 43, 43, ..., 43, 43, 43]],\n\n [[ 5, 5, 5, ..., 5, 5, 5],\n [ 5, 5, 5, ..., 5, 5, 5],\n [ 5, 5, 5, ..., 5, 5, 5],\n ...,\n [51, 51, 51, ..., 51, 51, 51],\n [51, 51, 51, ..., 51, 51, 51],\n [51, 51, 51, ..., 51, 51, 51]],\n\n [[14, 14, 14, ..., 14, 14, 14],\n [14, 14, 14, ..., 14, 14, 14],\n [14, 14, 14, ..., 14, 14, 14],\n ...,\n [84, 84, 84, ..., 84, 84, 84],\n [84, 84, 84, ..., 84, 84, 84],\n [84, 84, 84, ..., 84, 84, 84]]], dtype=uint8)\nCoordinates:\n * y (y) float64 1kB 89.5 88.5 87.5 86.5 ... -86.5 -
E + and <GridRegistration.PIXEL: 1> = GridRegistration.PIXEL
../pygmt/tests/test_datasets_earth_night.py:22: AssertionError
______________________ test_black_marble_01d_with_region _______________________
deftest_black_marble_01d_with_region():
"""
Test loading low-resolution Black Marble 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 is GridRegistration.PIXEL
E AssertionError: assert 1 is <GridRegistration.PIXEL: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0edf9070>.registration
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0edf9070> = <xarray.DataArray 'z' (band: 3, y: 10, x: 20)> Size: 600B\narray([[[ 4, 6, 8, 7, 5, 6, 6, 7, 9, 6, 4, 4, 4, 4, 4, 13,\n 38, 33, 30, 15],\n [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,\n 7, 9, 12, 11],\n [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,\n 4, 4, 4, 8],\n [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,\n 4, 4, 6, 17],\n [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,\n 5, 4, 5, 13],\n [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,\n 4, 4, 6, 10],\n [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,\n 4, 4, 7, 11],\n [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,\n 4, 4, 4, 7],\n [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
E + and <GridRegistration.PIXEL: 1> = GridRegistration.PIXEL
../pygmt/tests/test_datasets_earth_night.py:37: AssertionError
_____________________________ test_grdclip_outgrid _____________________________
grid = <xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B
array([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],
...d by Gaussian Cartesian filtering (111.2 km fullwidt...
actual_range: [190. 981.]
long_name: elevation (m)
expected_grid = <xarray.DataArray (lat: 3, lon: 4)> Size: 96B
array([[ 1000. , 570.5, -1000. , -1000. ],
[ 1000. , 1000. , ...])
Coordinates:
* lon (lon) float64 32B -52.5 -51.5 -50.5 -49.5
* lat (lat) float64 24B -18.5 -17.5 -16.5
deftest_grdclip_outgrid(grid, expected_grid):
"""
Test the below and above parameters for grdclip and creates a test outgrid.
"""with GMTTempFile(suffix=".nc") as tmpfile:
result = grdclip(
grid=grid,
outgrid=tmpfile.name,
below=[550, -1000],
above=[700, 1000],
region=[-53, -49, -19, -16],
)
assert result isNone# return value is Noneassert Path(tmpfile.name).stat().st_size >0# check that outgrid exists
temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
assert temp_grid.dims == ("lat", "lon")
> assert temp_grid.gmt.gtype is GridType.GEOGRAPHIC
E AssertionError: assert 1 is <GridType.GEOGRAPHIC: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0ee455b0>.gtype
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0ee455b0> = <xarray.DataArray 'z' (lat: 3, lon: 4)> Size: 48B\narray([[ 1000. , 570.5, -1000. , -1000. ],\n [ 1000. , 1000. , 571.5, 638.5],\n [ 555.5, 556. , 580. , 1000. ]], dtype=float32)\nCoordinates:\n * lat (lat) float64 24B -18.5 -17.5 -16.5\n * lon (lon) float64 32B -52.5 -51.5 -50.5 -49.5\nAttributes:\n Conventions: CF-1.7\n title: \n history: gmt grdclip @GMTAPI@-S-I-G-M-G-N-000000 -G/tmp/pygmt-fy_1g...\n description: \n actual_range: [-1000. 1000.]\n long_name: z.gmt
E + and <GridType.GEOGRAPHIC: 1> = GridType.GEOGRAPHIC
../pygmt/tests/test_grdclip.py:59: AssertionError
_______________________ test_grdproject_no_outgrid[M10c] _______________________
grid = <xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B
array([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],
...d by Gaussian Cartesian filtering (111.2 km fullwidt...
actual_range: [190. 981.]
long_name: elevation (m)
projection = 'M10c'
expected_grid = <xarray.DataArray (y: 5, x: 3)> Size: 120B
array([[427.85062, 431.05698, 452.34268],
[563.92957, 540.5212 , 501...])
Coordinates:
* x (x) float64 24B 1.667 5.0 8.333
* y (y) float64 40B 1.572 4.717 7.862 11.01 14.15
@pytest.mark.benchmark
@pytest.mark.parametrize(
"projection",
["M10c", "EPSG:3395 +width=10", "+proj=merc +ellps=WGS84 +units=m +width=10"],
)
deftest_grdproject_no_outgrid(grid, projection, expected_grid):
"""
Test grdproject with no set outgrid.
Also check that providing the projection as an EPSG code orPROJ4 string works.
"""
> assert grid.gmt.gtype is GridType.GEOGRAPHIC
E AssertionError: assert 1 is <GridType.GEOGRAPHIC: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0cc83710>.gtype
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0cc83710> = <xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B\narray([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],\n [383. , 284.5, 344.5, 394. , 491. , 556.5, 578.5, 618.5],\n [373. , 367.5, 349. , 352.5, 419.5, 428. , 570. , 667.5],\n [557. , 435. , 385.5, 345.5, 413.5, 496. , 519.5, 833.5],\n [561.5, 539. , 446.5, 481.5, 439.5, 553. , 726.5, 981. ],\n [310. , 521.5, 757. , 570.5, 538.5, 524. , 686.5, 794. ],\n [521.5, 682.5, 796. , 886. , 571.5, 638.5, 739.5, 881.5],\n [308. , 595.5, 555.5, 556. , 580. , 770. , 927. , 920. ],\n [601. , 526.5, 535. , 299. , 398.5, 645. , 797.5, 964. ],\n [494.5, 488.5, 357. , 254.5, 286. , 484.5, 653.5, 930. ],\n [450.5, 395.5, 366. , 248. , 250. , 354.5, 550. , 797.5],\n [345.5, 320. , 335. , 292. , 207.5, 247. , 325. , 346.5],\n [349. , 313. , 325.5, 247. , 191. , 225. , 260. , 452.5],\n [347.
E + and <GridType.GEOGRAPHIC: 1> = GridType.GEOGRAPHIC
../pygmt/tests/test_grdproject.py:74: AssertionError
_______________ test_grdproject_no_outgrid[EPSG:3395 +width=10] ________________
grid = <xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B
array([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],
...d by Gaussian Cartesian filtering (111.2 km fullwidt...
actual_range: [190. 981.]
long_name: elevation (m)
projection = 'EPSG:3395 +width=10'
expected_grid = <xarray.DataArray (y: 5, x: 3)> Size: 120B
array([[427.85062, 431.05698, 452.34268],
[563.92957, 540.5212 , 501...])
Coordinates:
* x (x) float64 24B 1.667 5.0 8.333
* y (y) float64 40B 1.572 4.717 7.862 11.01 14.15
@pytest.mark.benchmark
@pytest.mark.parametrize(
"projection",
["M10c", "EPSG:3395 +width=10", "+proj=merc +ellps=WGS84 +units=m +width=10"],
)
deftest_grdproject_no_outgrid(grid, projection, expected_grid):
"""
Test grdproject with no set outgrid.
Also check that providing the projection as an EPSG code orPROJ4 string works.
"""
> assert grid.gmt.gtype is GridType.GEOGRAPHIC
E AssertionError: assert 1 is <GridType.GEOGRAPHIC: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0cc83710>.gtype
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0cc83710> = <xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B\narray([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],\n [383. , 284.5, 344.5, 394. , 491. , 556.5, 578.5, 618.5],\n [373. , 367.5, 349. , 352.5, 419.5, 428. , 570. , 667.5],\n [557. , 435. , 385.5, 345.5, 413.5, 496. , 519.5, 833.5],\n [561.5, 539. , 446.5, 481.5, 439.5, 553. , 726.5, 981. ],\n [310. , 521.5, 757. , 570.5, 538.5, 524. , 686.5, 794. ],\n [521.5, 682.5, 796. , 886. , 571.5, 638.5, 739.5, 881.5],\n [308. , 595.5, 555.5, 556. , 580. , 770. , 927. , 920. ],\n [601. , 526.5, 535. , 299. , 398.5, 645. , 797.5, 964. ],\n [494.5, 488.5, 357. , 254.5, 286. , 484.5, 653.5, 930. ],\n [450.5, 395.5, 366. , 248. , 250. , 354.5, 550. , 797.5],\n [345.5, 320. , 335. , 292. , 207.5, 247. , 325. , 346.5],\n [349. , 313. , 325.5, 247. , 191. , 225. , 260. , 452.5],\n [347.
E + and <GridType.GEOGRAPHIC: 1> = GridType.GEOGRAPHIC
../pygmt/tests/test_grdproject.py:74: AssertionError
____ test_grdproject_no_outgrid[+proj=merc +ellps=WGS84 +units=m +width=10] ____
grid = <xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B
array([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],
...d by Gaussian Cartesian filtering (111.2 km fullwidt...
actual_range: [190. 981.]
long_name: elevation (m)
projection = '+proj=merc +ellps=WGS84 +units=m +width=10'
expected_grid = <xarray.DataArray (y: 5, x: 3)> Size: 120B
array([[427.85062, 431.05698, 452.34268],
[563.92957, 540.5212 , 501...])
Coordinates:
* x (x) float64 24B 1.667 5.0 8.333
* y (y) float64 40B 1.572 4.717 7.862 11.01 14.15
@pytest.mark.benchmark
@pytest.mark.parametrize(
"projection",
["M10c", "EPSG:3395 +width=10", "+proj=merc +ellps=WGS84 +units=m +width=10"],
)
deftest_grdproject_no_outgrid(grid, projection, expected_grid):
"""
Test grdproject with no set outgrid.
Also check that providing the projection as an EPSG code orPROJ4 string works.
"""
> assert grid.gmt.gtype is GridType.GEOGRAPHIC
E AssertionError: assert 1 is <GridType.GEOGRAPHIC: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0cc83710>.gtype
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0cc83710> = <xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B\narray([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],\n [383. , 284.5, 344.5, 394. , 491. , 556.5, 578.5, 618.5],\n [373. , 367.5, 349. , 352.5, 419.5, 428. , 570. , 667.5],\n [557. , 435. , 385.5, 345.5, 413.5, 496. , 519.5, 833.5],\n [561.5, 539. , 446.5, 481.5, 439.5, 553. , 726.5, 981. ],\n [310. , 521.5, 757. , 570.5, 538.5, 524. , 686.5, 794. ],\n [521.5, 682.5, 796. , 886. , 571.5, 638.5, 739.5, 881.5],\n [308. , 595.5, 555.5, 556. , 580. , 770. , 927. , 920. ],\n [601. , 526.5, 535. , 299. , 398.5, 645. , 797.5, 964. ],\n [494.5, 488.5, 357. , 254.5, 286. , 484.5, 653.5, 930. ],\n [450.5, 395.5, 366. , 248. , 250. , 354.5, 550. , 797.5],\n [345.5, 320. , 335. , 292. , 207.5, 247. , 325. , 346.5],\n [349. , 313. , 325.5, 247. , 191. , 225. , 260. , 452.5],\n [347.
E + and <GridType.GEOGRAPHIC: 1> = GridType.GEOGRAPHIC
../pygmt/tests/test_grdproject.py:74: AssertionError
_____________________ test_grdsample_registration_changes ______________________
grid = <xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B
array([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],
...d by Gaussian Cartesian filtering (111.2 km fullwidt...
actual_range: [190. 981.]
long_name: elevation (m)
deftest_grdsample_registration_changes(grid):
"""
Test grdsample with no set outgrid and applying registration changes.
"""
> assert grid.gmt.registration is GridRegistration.PIXEL
E AssertionError: assert 1 is <GridRegistration.PIXEL: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0edc2630>.registration
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0edc2630> = <xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B\narray([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],\n [383. , 284.5, 344.5, 394. , 491. , 556.5, 578.5, 618.5],\n [373. , 367.5, 349. , 352.5, 419.5, 428. , 570. , 667.5],\n [557. , 435. , 385.5, 345.5, 413.5, 496. , 519.5, 833.5],\n [561.5, 539. , 446.5, 481.5, 439.5, 553. , 726.5, 981. ],\n [310. , 521.5, 757. , 570.5, 538.5, 524. , 686.5, 794. ],\n [521.5, 682.5, 796. , 886. , 571.5, 638.5, 739.5, 881.5],\n [308. , 595.5, 555.5, 556. , 580. , 770. , 927. , 920. ],\n [601. , 526.5, 535. , 299. , 398.5, 645. , 797.5, 964. ],\n [494.5, 488.5, 357. , 254.5, 286. , 484.5, 653.5, 930. ],\n [450.5, 395.5, 366. , 248. , 250. , 354.5, 550. , 797.5],\n [345.5, 320. , 335. , 292. , 207.5, 247. , 325. , 346.5],\n [349. , 313. , 325.5, 247. , 191. , 225. , 260. , 452.5],\n [347.
E + and <GridRegistration.PIXEL: 1> = GridRegistration.PIXEL
../pygmt/tests/test_grdsample.py:92: AssertionError
____________________________ test_io_load_dataarray ____________________________
deftest_io_load_dataarray():
"""
Check that load_dataarray works to read a netCDF grid with GMTDataArrayAccessor
information loaded.
"""with GMTTempFile(suffix=".nc") as tmpfile:
rng = np.random.default_rng()
grid = xr.DataArray(
data=rng.random((2, 2)), coords=[[0.1, 0.2], [0.3, 0.4]], dims=("x", "y")
)
grid.to_netcdf(tmpfile.name)
with pytest.warns(FutureWarning):
dataarray = load_dataarray(tmpfile.name)
> assert dataarray.gmt.gtype is GridType.CARTESIAN
E assert 0 is <GridType.CARTESIAN: 0>
E + where 0 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0cab5010>.gtype
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b0cab5010> = <xarray.DataArray (x: 2, y: 2)> Size: 32B\narray([[0.24800596, 0.16975465],\n [0.54603337, 0.1887691 ]])\nCoordinates:\n * x (x) float64 16B 0.1 0.2\n * y (y) float64 16B 0.3 0.4.gmt
E + and <GridType.CARTESIAN: 0> = GridType.CARTESIAN
../pygmt/tests/test_io.py:31: AssertionError
_______________________ test_surface_with_outgrid_param ________________________
data = array([[3.00000000e-01, 6.10000000e+00, 8.70000000e+02],
[1.40000000e+00, 6.20000000e+00, 7.93000000e+02],
...02],
[5.40000000e+00, 4.00000000e-01, 8.90000000e+02],
[6.00000000e+00, 1.00000000e-01, 8.60000000e+02]])
region = [0, 4, 0, 8], spacing = '1'
expected_grid = <xarray.DataArray (y: 9, x: 5)> Size: 360B
array([[962.2361, 909.526 , 872.2578, 876.5983, 950.573 ],
[944.369...2, 651.4994, 565.9981]])
Coordinates:
* y (y) int64 72B 0 1 2 3 4 5 6 7 8
* x (x) int64 40B 0 1 2 3 4
deftest_surface_with_outgrid_param(data, region, spacing, expected_grid):
"""
Run surface with the -Goutputfile.nc parameter.
"""
data = data.to_numpy() # convert pandas.DataFrame to numpy.ndarraywith GMTTempFile(suffix=".nc") as tmpfile:
output = surface(
data=data,
spacing=spacing,
region=region,
outgrid=tmpfile.name,
verbose="e", # Suppress warnings for IEEE 754 rounding
)
assert output isNone# check that output is None since outgrid is setassert Path(tmpfile.name).stat().st_size >0# check that outgrid exists
grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
> check_values(grid, expected_grid)
../pygmt/tests/test_surface.py:149: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ grid = <xarray.DataArray 'z' (y: 9, x: 5)> Size: 180B
array([[962.23615, 909.526 , 872.2578 , 876.5983 , 950.573 ],
...-N-000000-G/tmp/pygmt-t5e8k...
description: actual_range: [565.99810791962.23614502]
long_name: z
expected_grid = <xarray.DataArray (y: 9, x: 5)> Size: 360B
array([[962.2361, 909.526 , 872.2578, 876.5983, 950.573 ],
[944.369...2, 651.4994, 565.9981]])
Coordinates:
* y (y) int64 72B 0 1 2 3 4 5 6 7 8
* x (x) int64 40B 0 1 2 3 4
defcheck_values(grid, expected_grid):
"""
Check the attributes and values of the DataArray returned by surface.
"""assertisinstance(grid, xr.DataArray)
> assert grid.gmt.registration is GridRegistration.GRIDLINE
E AssertionError: assert 0 is <GridRegistration.GRIDLINE: 0>
E + where 0 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a516db0>.registration
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a516db0> = <xarray.DataArray 'z' (y: 9, x: 5)> Size: 180B\narray([[962.23615, 909.526 , 872.2578 , 876.5983 , 950.573 ],\n [944.369 , 905.82526, 872.1614 , 901.85834, 943.6854 ],\n [911.05994, 865.305 , 845.5058 , 855.7317 , 867.16376],\n [878.5973 , 851.71 , 814.6884 , 812.11273, 819.9591 ],\n [842.05225, 815.28955, 788.2292 , 777.0031 , 785.63446],\n [854.25146, 813.30347, 781. , 742.36414, 735.64966],\n [882.97205, 818.46356, 773.611 , 718.7798 , 685.4824 ],\n [897.45325, 822.9642 , 756.4472 , 687.594 , 626.22986],\n [910.29315, 823.33075, 737.9952 , 651.4994 , 565.9981 ]],\n dtype=float32)\nCoordinates:\n * y (y) float64 72B 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0\n * x (x) float64 40B 0.0 1.0 2.0 3.0 4.0\nAttributes:\n Conventions: CF-1.7\n title: Data gridded with continuous surface splines in tension\n history: gm
E + and <GridRegistration.GRIDLINE: 0> = GridRegistration.GRIDLINE
../pygmt/tests/test_surface.py:76: AssertionError
_____________________ test_regular_grid_with_outgrid_param _____________________
dataframe = x y z
0 0.30 6.1 870.0
1 1.40 6.2 793.0
2 2.40 6.1 755.0
3 3.40 5.7 710.0
4 3.60 6.1 697.5
5 4.80 5.6 780.0
6 5.70 6.2 800.0
7 1.95 5.0 781.0
8 3.15 5.2 729.0
9 4.10 4.6 760.0
expected_grid = <xarray.DataArray (y: 2, x: 3)> Size: 48B
array([[779.6264 , 752.1539 , 749.38776],
[771.2882 , 726.9792 , 722.1368 ]])
Coordinates:
* y (y) int64 16B 5 6
* x (x) int64 24B 2 3 4
deftest_regular_grid_with_outgrid_param(dataframe, expected_grid):
"""
Run triangulate.regular_grid with the -Goutputfile.nc parameter.
"""
data = dataframe.to_numpy()
with GMTTempFile(suffix=".nc") as tmpfile:
output = triangulate.regular_grid(
data=data, spacing=1, region=[2, 4, 5, 6], outgrid=tmpfile.name
)
assert output isNone# check that output is None since outgrid is setassert Path(tmpfile.name).stat().st_size >0# check that outgrid exists
grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
assertisinstance(grid, xr.DataArray)
> assert grid.gmt.registration is GridRegistration.GRIDLINE
E AssertionError: assert 0 is <GridRegistration.GRIDLINE: 0>
E + where 0 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a57df10>.registration
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a57df10> = <xarray.DataArray 'z' (y: 2, x: 3)> Size: 24B\narray([[779.6264 , 752.1539 , 749.38776],\n [771.2882 , 726.9792 , 722.1368 ]], dtype=float32)\nCoordinates:\n * y (y) float64 16B 5.0 6.0\n * x (x) float64 24B 2.0 3.0 4.0\nAttributes:\n Conventions: CF-1.7\n title: \n history: gmt triangulate @GMTAPI@-S-I-D-M-T-N-000000 -G/tmp/pygmt-q...\n description: \n actual_range: [722.13677979 779.62640381]\n long_name: z.gmt
E + and <GridRegistration.GRIDLINE: 0> = GridRegistration.GRIDLINE
../pygmt/tests/test_triangulate.py:161: AssertionError
___________________ test_xarray_accessor_gridline_cartesian ____________________
deftest_xarray_accessor_gridline_cartesian():
"""
Check that the accessor returns the correct registration and gtype values for a
Cartesian, gridline-registered grid.
"""
grid = xr.load_dataarray("@test.dat.nc", engine="gmt", raster_kind="grid")
> assert grid.gmt.registration is GridRegistration.GRIDLINE
E AssertionError: assert 0 is <GridRegistration.GRIDLINE: 0>
E + where 0 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a5aabd0>.registration
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a5aabd0> = <xarray.DataArray 'z' (y: 161, x: 181)> Size: 117kB\narray([[0.06260489, 0.06219847, 0.06212078, ..., 0.06322736, 0.06311526,\n 0.06260489],\n [0.06197159, 0.06201339, 0.06203938, ..., 0.06328446, 0.06329362,\n 0.06330974],\n [0.06170128, 0.06184416, 0.06192509, ..., 0.06339139, 0.06346501,\n 0.06360032],\n ...,\n [0.02527592, 0.02562295, 0.02590327, ..., 0.09023768, 0.09039675,\n 0.0906041 ],\n [0.02368068, 0.02447367, 0.02499465, ..., 0.09035734, 0.0905341 ,\n 0.0907779 ],\n [0.02094602, 0.02359646, 0.02453409, ..., 0.09039814, 0.09060441,\n 0.09099283]], dtype=float32)\nCoordinates:\n * y (y) float64 1kB 0.0 0.5 1.0 1.5 2.0 ... 78.0 78.5 79.0 79.5 80.0\n * x (x) float64 1kB 0.0 1.0 2.0 3.0 4.0 ... 177.0 178.0 179.0 180.0\nAttributes:\n Conventions: CF-1.7\n title: Data gridded with continuous surfac
E + and <GridRegistration.GRIDLINE: 0> = GridRegistration.GRIDLINE
../pygmt/tests/test_xarray_accessor.py:27: AssertionError
____________________ test_xarray_accessor_pixel_geographic _____________________
deftest_xarray_accessor_pixel_geographic():
"""
Check that the accessor returns the correct registration and gtype values for a
geographic, pixel-registered grid.
"""
grid = xr.load_dataarray("@earth_relief_01d_p", engine="gmt", raster_kind="grid")
> assert grid.gmt.registration is GridRegistration.PIXEL
E AssertionError: assert 1 is <GridRegistration.PIXEL: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a5ab890>.registration
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a5ab890> = <xarray.DataArray 'z' (lat: 180, lon: 360)> Size: 259kB\narray([[ 3040.5, 3039. , 3037.5, ..., 3043. , 3042. , 3041.5],\n [ 3117. , 3117.5, 3118. , ..., 3116.5, 3117. , 3117. ],\n [ 3035.5, 3034. , 3030.5, ..., 3035.5, 3035.5, 3036. ],\n ...,\n [-3867. , -3852.5, -3851.5, ..., -3889.5, -3879.5, -3874.5],\n [-3367.5, -3370. , -3382.5, ..., -3294. , -3314.5, -3346.5],\n [-3108.5, -3086.5, -3073.5, ..., -3148.5, -3141. , -3138. ]],\n dtype=float32)\nCoordinates:\n * lat (lat) float64 1kB -89.5 -88.5 -87.5 -86.5 ... 86.5 87.5 88.5 89.5\n * lon (lon) float64 3kB -179.5 -178.5 -177.5 -176.5 ... 177.5 178.5 179.5\nAttributes:\n Conventions: CF-1.7\n title: SRTM15 Earth Relief v2.7 at 01 arc degree\n history: \n description: Reduced by Gaussian Cartesian filtering (314.5 km fullwidt...\n actual_range: [-7057. 5326.5
E + and <GridRegistration.PIXEL: 1> = GridRegistration.PIXEL
../pygmt/tests/test_xarray_accessor.py:37: AssertionError
_____________________ test_xarray_accessor_sliced_datacube _____________________
@pytest.mark.skipif(condition=not_HAS_NETCDF4, reason="netCDF4 is not installed")
@pytest.mark.xfail(
condition=sys.platform =="win32"and Version(__gmt_version__) < Version("6.5.0"),
reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/7573",
)
deftest_xarray_accessor_sliced_datacube():
"""
Check that a 2-D grid which is sliced from an n-dimensional datacube works with
accessor methods.
This is a regression test for
https://github.com/GenericMappingTools/pygmt/issues/1578.
"""try:
fname = which(
"https://github.com/pydata/xarray-data/raw/master/eraint_uvz.nc",
download="u",
)
with xr.open_dataset(fname, engine="netcdf4") as dataset:
grid = dataset.sel(level=500, month=1, drop=True).z
> assert grid.gmt.registration is GridRegistration.GRIDLINE
E AssertionError: assert 0 is <GridRegistration.GRIDLINE: 0>
E + where 0 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a5aac90>.registration
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a5aac90> = <xarray.DataArray 'z' (latitude: 241, longitude: 480)> Size: 925kB\n[115680 values with dtype=float64]\nCoordinates:\n * longitude (longitude) float32 2kB -180.0 -179.2 -178.5 ... 178.5 179.2\n * latitude (latitude) float32 964B 90.0 89.25 88.5 ... -88.5 -89.25 -90.0\nAttributes:\n number_of_significant_digits: 5\n units: m**2 s**-2\n long_name: Geopotential\n standard_name: geopotential.gmt
E + and <GridRegistration.GRIDLINE: 0> = GridRegistration.GRIDLINE
../pygmt/tests/test_xarray_accessor.py:129: AssertionError
----------------------------- Captured stderr call -----------------------------
Warning: WARNING]: No 3-D array in file /home/runner/work/pygmt/pygmt/tmp-test-dir-with-unique-name/eraint_uvz.nc. Selecting first 3-D slice in the 4-D array z
----------------------------- Captured stderr call -----------------------------
Warning: WARNING]: No 3-D array in file /home/runner/work/pygmt/pygmt/tmp-test-dir-with-unique-name/eraint_uvz.nc. Selecting first 3-D slice in the 4-D array z
----------------------------- Captured stderr call -----------------------------
Warning: WARNING]: No 3-D array in file /home/runner/work/pygmt/pygmt/tmp-test-dir-with-unique-name/eraint_uvz.nc. Selecting first 3-D slice in the 4-D array z
______________________ test_xarray_backend_load_dataarray ______________________
@pytest.mark.benchmark
@pytest.mark.skipif(condition=not_HAS_NETCDF4, reason="netCDF4 is not installed")
deftest_xarray_backend_load_dataarray():
"""
Check that xarray.load_dataarray works to read a netCDF grid with
GMTDataArrayAccessor information loaded.
"""with GMTTempFile(suffix=".nc") as tmpfile:
rng = np.random.default_rng()
grid = xr.DataArray(
data=rng.random((2, 2)), coords=[[0.1, 0.2], [0.3, 0.4]], dims=("x", "y")
)
grid.to_netcdf(tmpfile.name)
dataarray = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid")
> assert dataarray.gmt.gtype is GridType.CARTESIAN
E AssertionError: assert 0 is <GridType.CARTESIAN: 0>
E + where 0 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a5fc470>.gtype
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a5fc470> = <xarray.DataArray 'z' (y: 2, x: 2)> Size: 16B\narray([[0.93592805, 0.58116895],\n [0.6231694 , 0.8868017 ]], dtype=float32)\nCoordinates:\n * y (y) float64 16B 0.1 0.2\n * x (x) float64 16B 0.3 0.4\nAttributes:\n Conventions: CF-1.7\n title: \n history: \n description: \n actual_range: [0.58116895 0.93592805]\n long_name: __xarray_dataarray_variable__.gmt
E + and <GridType.CARTESIAN: 0> = GridType.CARTESIAN
../pygmt/tests/test_xarray_backend.py:35: AssertionError
_____________________ test_xarray_backend_gmt_open_nc_grid _____________________
deftest_xarray_backend_gmt_open_nc_grid():
"""
Ensure that passing engine='gmt' to xarray.open_dataarray works for opening NetCDF
grids.
"""with xr.open_dataarray(
"@static_earth_relief.nc", engine="gmt", raster_kind="grid"
) as da:
assert da.sizes == {"lat": 14, "lon": 8}
assert da.dtype =="float32"
> assert da.gmt.gtype is GridType.GEOGRAPHIC
E AssertionError: assert 1 is <GridType.GEOGRAPHIC: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a514290>.gtype
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a514290> = <xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B\n[112 values with dtype=float32]\nCoordinates:\n * lat (lat) float64 112B -23.5 -22.5 -21.5 -20.5 ... -12.5 -11.5 -10.5\n * lon (lon) float64 64B -54.5 -53.5 -52.5 -51.5 -50.5 -49.5 -48.5 -47.5\nAttributes:\n Conventions: CF-1.7\n title: Produced by grdcut\n history: grdcut @earth_relief_01d_p -R-55/-47/-24/-10 -Gstatic_eart...\n description: Reduced by Gaussian Cartesian filtering (111.2 km fullwidt...\n actual_range: [190. 981.]\n long_name: elevation (m).gmt
E + and <GridType.GEOGRAPHIC: 1> = GridType.GEOGRAPHIC
../pygmt/tests/test_xarray_backend.py:51: AssertionError
____________________ test_xarray_backend_gmt_open_tif_image ____________________
deftest_xarray_backend_gmt_open_tif_image():
"""
Ensure that passing engine='gmt' to xarray.open_dataarray works for opening GeoTIFF
images.
"""with xr.open_dataarray("@earth_day_01d", engine="gmt", raster_kind="image") as da:
assert da.sizes == {"band": 3, "y": 180, "x": 360}
assert da.dtype =="uint8"
> assert da.gmt.gtype is GridType.GEOGRAPHIC
E AssertionError: assert 1 is <GridType.GEOGRAPHIC: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a514290>.gtype
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a514290> = <xarray.DataArray 'z' (band: 3, y: 180, x: 360)> Size: 194kB\n[194400 values with dtype=uint8]\nCoordinates:\n * y (y) float64 1kB 89.5 88.5 87.5 86.5 ... -86.5 -87.5 -88.5 -89.5\n * x (x) float64 3kB -179.5 -178.5 -177.5 -176.5 ... 177.5 178.5 179.5\n * band (band) uint8 3B 1 2 3\nAttributes:\n long_name: z.gmt
E + and <GridType.GEOGRAPHIC: 1> = GridType.GEOGRAPHIC
../pygmt/tests/test_xarray_backend.py:63: AssertionError
____________________ test_xarray_backend_gmt_load_grd_grid _____________________
deftest_xarray_backend_gmt_load_grd_grid():
"""
Ensure that passing engine='gmt' to xarray.load_dataarray works for loading GRD
grids.
"""
da = xr.load_dataarray(
"@earth_relief_20m_holes.grd", engine="gmt", raster_kind="grid"
)
# Ensure data is in memory.assertisinstance(da.data, np.ndarray)
npt.assert_allclose(da.min(), -4929.5)
assert da.sizes == {"lat": 31, "lon": 31}
assert da.dtype =="float32"
> assert da.gmt.gtype is GridType.GEOGRAPHIC
E AssertionError: assert 1 is <GridType.GEOGRAPHIC: 1>
E + where 1 = <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a517d10>.gtype
E + where <pygmt.xarray.accessor.GMTDataArrayAccessor object at 0x7f1b2a517d10> = <xarray.DataArray 'z' (lat: 31, lon: 31)> Size: 4kB\narray([[-4.7465e+03, -4.8995e+03, -4.8780e+03, -4.8280e+03, -4.7670e+03,\n -4.2020e+03, -4.6125e+03, -4.2315e+03, -4.4280e+03, -4.3395e+03,\n -4.2695e+03, -4.2030e+03, -4.1315e+03, -4.0535e+03, -3.9445e+03,\n -3.8120e+03, -3.6175e+03, -3.3495e+03, -3.0330e+03, -2.3895e+03,\n -1.3630e+03, -2.9280e+03, -2.8605e+03, -2.6725e+03, -2.3855e+03,\n -1.8130e+03, -7.6550e+02, -7.5500e+01, 1.7500e+01, 4.1500e+01,\n 4.5000e+01],\n [-4.9250e+03, -4.9170e+03, -4.8665e+03, -4.8350e+03, -4.7235e+03,\n -4.6930e+03, -4.6240e+03, -4.4855e+03, -4.4570e+03, -4.3510e+03,\n -4.3045e+03, -4.1880e+03, -4.1070e+03, -4.0345e+03, -3.9685e+03,\n -3.8490e+03, -3.6620e+03, -3.4720e+03, -3.0075e+03, -2.2975e+03,\n 1.5450e+02, -2.7775e+03, -2.8660e+03, -2.7680e+03, -2.4625e+03,\n -1.9645e+03, -9.4000e+02,
E + and <GridType.GEOGRAPHIC: 1> = GridType.GEOGRAPHIC
../pygmt/tests/test_xarray_backend.py:80: AssertionError

Cherry-picking commit 07b2802 (from suggestion #3932 (comment)) should fix this I think.

Cherry-picked from 07b2802
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@weiji14weiji14 changed the title Change enum member comparison from == to isSet registration and gtype properly as enums on gmt accessor initMay 9, 2025
@weiji14
weiji14 marked this pull request as ready for review May 9, 2025 00:25
@seismanseisman added this to the 0.16.0 milestone May 9, 2025
Comment threadpygmt/xarray/accessor.py Outdated
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@seisman
seisman merged commit 84921a0 into mainMay 9, 2025
@seisman
seisman deleted the enum_is_compare branch May 9, 2025 02:38
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenanceBoring but important stuff for the core devs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@weiji14@seisman