From 95f28fc7fe66caebf77eefe045f45686a0d56b18 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 13 Oct 2021 15:48:20 +1300 Subject: [PATCH 1/2] Allow GMTDataArrayAccessor to work on sliced datacubes If `grdinfo` can't read the source NetCDF file as it is an n-dimensional datacube (instead of a 2D grid), we fallback to setting the default gridline registration and Cartesian grid type. --- pygmt/accessors.py | 2 +- pygmt/tests/test_accessor.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pygmt/accessors.py b/pygmt/accessors.py index 59fd7aa13e6..190524075f9 100644 --- a/pygmt/accessors.py +++ b/pygmt/accessors.py @@ -34,7 +34,7 @@ def __init__(self, xarray_obj): self._registration, self._gtype = map( int, grdinfo(self._source, per_column="n", o="10,11").split() ) - except KeyError: + except (KeyError, ValueError): self._registration = 0 # Default to Gridline registration self._gtype = 0 # Default to Cartesian grid type diff --git a/pygmt/tests/test_accessor.py b/pygmt/tests/test_accessor.py index a3b0dc4fafc..cb6bf432a9b 100644 --- a/pygmt/tests/test_accessor.py +++ b/pygmt/tests/test_accessor.py @@ -1,6 +1,8 @@ """ Test the behaviour of the GMTDataArrayAccessor class. """ +import os + import pytest import xarray as xr from pygmt import which @@ -66,3 +68,25 @@ def test_accessor_set_non_boolean(): with pytest.raises(GMTInvalidInput): grid.gmt.gtype = 2 + + +def test_accessor_sliced_datacube(): + """ + Check that a 2D 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", + ) + dataset = xr.open_dataset(fname) + grid = dataset.sel(level=500, month=1, drop=True).z + + assert grid.gmt.registration == 0 # gridline registration + assert grid.gmt.gtype == 0 # cartesian coordinate type + finally: + os.remove(fname) From 81145dbb9d62079046ae4f7d26d0dc53ff70cbf0 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Fri, 22 Oct 2021 13:57:59 +1300 Subject: [PATCH 2/2] Use with statement to ensure the grid is properly closed So that the file can be deleted on Windows. --- pygmt/tests/test_accessor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_accessor.py b/pygmt/tests/test_accessor.py index cb6bf432a9b..99ce5f7104a 100644 --- a/pygmt/tests/test_accessor.py +++ b/pygmt/tests/test_accessor.py @@ -83,8 +83,8 @@ def test_accessor_sliced_datacube(): "https://github.com/pydata/xarray-data/raw/master/eraint_uvz.nc", download="u", ) - dataset = xr.open_dataset(fname) - grid = dataset.sel(level=500, month=1, drop=True).z + 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 == 0 # cartesian coordinate type