From 02c7b0e765631d785a761dc792c32d1f9fa965a9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jun 2020 00:54:25 -0400 Subject: [PATCH 01/11] Wrap grdcut Wrap the `grdcut` function. Ideally it should take a grid file or a `xarray.DataArray` as input, and store output as a grid file or return a `xarray.DataArray`. Supported features: - [X] input: grid file; output: grid file - [X] input: grid file; output: `xarray.DataArray` - [ ] input: `xarray.DataArray`; output: grid file - [ ] input: `xarray.DataArray`; output: `xarray.DataArray` Passing `xarray.DataArray` to `grdcut` is not implenmented in the GMT 6.0.0. See https://github.com/GenericMappingTools/gmt/pull/3532 for a feature request to the upstream GMT. --- doc/api/index.rst | 1 + pygmt/__init__.py | 2 +- pygmt/modules.py | 104 +++++++++++++++++++++++++++++++++++++ pygmt/tests/test_grdcut.py | 64 +++++++++++++++++++++++ 4 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 pygmt/tests/test_grdcut.py diff --git a/doc/api/index.rst b/doc/api/index.rst index c2974884a65..7beb62651e5 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -72,6 +72,7 @@ Operations on grids: grdinfo grdtrack + grdcut GMT Defaults ------------ diff --git a/pygmt/__init__.py b/pygmt/__init__.py index b5db0fb4633..47496bd52d6 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -18,7 +18,7 @@ from .gridding import surface from .sampling import grdtrack from .mathops import makecpt -from .modules import config, info, grdinfo, which +from .modules import config, info, grdinfo, grdcut, which from . import datasets diff --git a/pygmt/modules.py b/pygmt/modules.py index 89c6a46db88..0d7b6b2f22b 100644 --- a/pygmt/modules.py +++ b/pygmt/modules.py @@ -1,10 +1,14 @@ """ Non-plot GMT modules. """ +import xarray as xr + + from .clib import Session from .helpers import ( build_arg_string, fmt_docstring, + kwargs_to_strings, GMTTempFile, use_alias, data_kind, @@ -147,6 +151,106 @@ def which(fname, **kwargs): return path +@fmt_docstring +@use_alias( + G="outgrid", + R="region", + J="projection", + N="extend", + S="circ_subregion", + Z="z_subregion", +) +@kwargs_to_strings(R="sequence") +def grdcut(ingrid, **kwargs): + """ + Extract subregion from a grid. + + Produce a new *outgrid* file which is a subregion of *ingrid*. The + subregion is specified with *region*; the specified range must not exceed + the range of *ingrid* (but see *extend*). If in doubt, run + :meth:`pygmt.grdinfo` to check range. Alternatively, define the subregion + indirectly via a range check on the node values or via distances from a + given point. Finally, you can give *projection* for oblique projections to + determine the corresponding rectangular *region* setting that will give a + grid that fully covers the oblique domain. + + Full option list at :gmt-docs:`grdcut.html` + + {aliases} + + Parameters + ---------- + ingrid : str + The name of the input grid file. + outgrid : str or None + The name of the output netCDF file with extension .nc to store the grid + in. + {J} + {R} + extend : bool or int or float + Allow grid to be extended if new *region* exceeds existing boundaries. + Give a value to initialize nodes outside current region. + circ_subregion : str + ``'lon/lat/radius[unit][+n]'``. + Specify an origin (*lon* and *lat*) and *radius*; append a distance + *unit* and we determine the corresponding rectangular region so that + all grid nodes on or inside the circle are contained in the subset. + If **+n** is appended we set all nodes outside the circle to NaN. + z_subregion : + ``'[min/max][+n|N|r]'``. + Determine a new rectangular region so that all nodes outside this + region are also outside the given z-range [-inf/+inf]. To indicate no + limit on *min* or *max* only, specify a hyphen (-). Normally, any NaNs + encountered are simply skipped and not considered in the + range-decision. Append **+n** to consider a NaN to be outside the given + z-range. This means the new subset will be NaN-free. Alternatively, + append **+r** to consider NaNs to be within the data range. In this + case we stop shrinking the boundaries once a NaN is found [Default + simply skips NaNs when making the range decision]. Finally, if your + core subset grid is surrounded by rows and/or columns that are all + NaNs, append **+N** to strip off such columns before (optionally) + considering the range of the core subset for further reduction of the + area. + + Returns + ------- + ret: xarray.DataArray or None + Return type depends on whether the *outgrid* parameter is set: + + - xarray.DataArray if *outgrid* is not set + - None if *outgrid* is set (grid output will be stored in *outgrid*) + """ + kind = data_kind(ingrid) + + with GMTTempFile(suffix=".nc") as tmpfile: + with Session() as lib: + if kind == "file": + file_context = dummy_context(ingrid) + elif kind == "grid": + raise GMTInvalidInput( + "xarray.DataArray is not supported as the input grid yet!" + ) + # file_context = lib.virtualfile_from_grid(ingrid) + # See https://github.com/GenericMappingTools/gmt/pull/3532 + # for a feature request. + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(ingrid))) + + with file_context as infile: + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile + kwargs.update({"G": tmpfile.name}) + outgrid = kwargs["G"] + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("grdcut", arg_str) + + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray + result = xr.open_dataarray(outgrid) + else: + result = None # if user sets an outgrid, return None + + return result + + class config: # pylint: disable=invalid-name """ Set GMT defaults globally or locally. diff --git a/pygmt/tests/test_grdcut.py b/pygmt/tests/test_grdcut.py new file mode 100644 index 00000000000..7ac8e877b79 --- /dev/null +++ b/pygmt/tests/test_grdcut.py @@ -0,0 +1,64 @@ +""" +Tests for grdcut +""" +import os +import numpy as np +import pytest +import xarray as xr + +from .. import grdcut, grdinfo +from ..datasets import load_earth_relief +from ..exceptions import GMTInvalidInput +from ..helpers import GMTTempFile + + +def test_grdcut_file_in_file_out(): + "grduct an input grid file, and output to a grid file" + with GMTTempFile(suffix=".nc") as tmpfile: + result = grdcut("@earth_relief_01d", outgrid=tmpfile.name, region="0/180/0/90") + assert result is None # return value is None + assert os.path.exists(path=tmpfile.name) # check that outgrid exists + result = grdinfo(tmpfile.name, C=True) + assert result == "0 180 0 90 -8182 5651.5 1 1 180 90 1\n" + + +def test_grdcut_file_in_dataarray_out(): + "grdcut an input grid file, and output as DataArray" + outgrid = grdcut("@earth_relief_01d", region="0/180/0/90") + assert isinstance(outgrid, xr.DataArray) + # check information of the output grid + # the '@earth_relief_01d' is in pixel registration, so the grid range is + # not exactly 0/180/0/90 + assert outgrid.coords["lat"].data.min() == 0.5 + assert outgrid.coords["lat"].data.max() == 89.5 + assert outgrid.coords["lon"].data.min() == 0.5 + assert outgrid.coords["lon"].data.max() == 179.5 + assert outgrid.data.min() == -8182.0 + assert outgrid.data.max() == 5651.5 + assert outgrid.sizes["lat"] == 90 + assert outgrid.sizes["lon"] == 180 + + +def test_grdcut_dataarray_in_file_out(): + "grdcut an input DataArray, and output to a grid file" + # Not supported yet. + # See https://github.com/GenericMappingTools/gmt/pull/3532 + + +def test_grdcut_dataarray_in_dataarray_out(): + "grdcut an input DataArray, and output to a grid file" + # Not supported yet. + # See https://github.com/GenericMappingTools/gmt/pull/3532 + + +def test_grdcut_dataarray_in_fail(): + "Make sure that grdcut fails correctly if DataArray is the input grid" + with pytest.raises(GMTInvalidInput): + grid = load_earth_relief() + grdcut(grid, region="0/180/0/90") + + +def test_grdcut_fails(): + "Check that grdcut fails correctly" + with pytest.raises(GMTInvalidInput): + grdcut(np.arange(10).reshape((5, 2))) From f1e27c41ecf76bc963d0594e9965977e3e001a5b Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jun 2020 18:53:31 -0400 Subject: [PATCH 02/11] Fix the grdcut test grdinfo in GMT 6.1.0 reports the grid registration in the last column, while GMT 6.0.0 doesn't. --- pygmt/tests/test_grdcut.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_grdcut.py b/pygmt/tests/test_grdcut.py index 7ac8e877b79..cd75e9c6a15 100644 --- a/pygmt/tests/test_grdcut.py +++ b/pygmt/tests/test_grdcut.py @@ -19,7 +19,7 @@ def test_grdcut_file_in_file_out(): assert result is None # return value is None assert os.path.exists(path=tmpfile.name) # check that outgrid exists result = grdinfo(tmpfile.name, C=True) - assert result == "0 180 0 90 -8182 5651.5 1 1 180 90 1\n" + assert result == "0 180 0 90 -8182 5651.5 1 1 180 90\n" def test_grdcut_file_in_dataarray_out(): From b578f858eea2b4915aba4bbe387db3265b4b4751 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jun 2020 19:09:06 -0400 Subject: [PATCH 03/11] Rename parameter ingrid to grid --- pygmt/modules.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pygmt/modules.py b/pygmt/modules.py index 0d7b6b2f22b..ec189b8f2ea 100644 --- a/pygmt/modules.py +++ b/pygmt/modules.py @@ -161,13 +161,13 @@ def which(fname, **kwargs): Z="z_subregion", ) @kwargs_to_strings(R="sequence") -def grdcut(ingrid, **kwargs): +def grdcut(grid, **kwargs): """ Extract subregion from a grid. - Produce a new *outgrid* file which is a subregion of *ingrid*. The + Produce a new *outgrid* file which is a subregion of *grid*. The subregion is specified with *region*; the specified range must not exceed - the range of *ingrid* (but see *extend*). If in doubt, run + the range of *grid* (but see *extend*). If in doubt, run :meth:`pygmt.grdinfo` to check range. Alternatively, define the subregion indirectly via a range check on the node values or via distances from a given point. Finally, you can give *projection* for oblique projections to @@ -180,7 +180,7 @@ def grdcut(ingrid, **kwargs): Parameters ---------- - ingrid : str + grid : str The name of the input grid file. outgrid : str or None The name of the output netCDF file with extension .nc to store the grid @@ -220,21 +220,21 @@ def grdcut(ingrid, **kwargs): - xarray.DataArray if *outgrid* is not set - None if *outgrid* is set (grid output will be stored in *outgrid*) """ - kind = data_kind(ingrid) + kind = data_kind(grid) with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: if kind == "file": - file_context = dummy_context(ingrid) + file_context = dummy_context(grid) elif kind == "grid": raise GMTInvalidInput( "xarray.DataArray is not supported as the input grid yet!" ) - # file_context = lib.virtualfile_from_grid(ingrid) + # file_context = lib.virtualfile_from_grid(grid) # See https://github.com/GenericMappingTools/gmt/pull/3532 # for a feature request. else: - raise GMTInvalidInput("Unrecognized data type: {}".format(type(ingrid))) + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) with file_context as infile: if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile From 071d46369eab86c752af8fde6ade04025ddd8974 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jun 2020 19:10:40 -0400 Subject: [PATCH 04/11] Raise a NotImplementedError Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/modules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/modules.py b/pygmt/modules.py index ec189b8f2ea..28e9678ec0a 100644 --- a/pygmt/modules.py +++ b/pygmt/modules.py @@ -227,7 +227,7 @@ def grdcut(grid, **kwargs): if kind == "file": file_context = dummy_context(grid) elif kind == "grid": - raise GMTInvalidInput( + raise NotImplementedError( "xarray.DataArray is not supported as the input grid yet!" ) # file_context = lib.virtualfile_from_grid(grid) From 6ce0450fb4cd2f38b810a41da8084049494a4b36 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jun 2020 19:11:36 -0400 Subject: [PATCH 05/11] Fix the NotImplementedError in the test --- pygmt/tests/test_grdcut.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_grdcut.py b/pygmt/tests/test_grdcut.py index cd75e9c6a15..1e1e283d1fe 100644 --- a/pygmt/tests/test_grdcut.py +++ b/pygmt/tests/test_grdcut.py @@ -53,7 +53,7 @@ def test_grdcut_dataarray_in_dataarray_out(): def test_grdcut_dataarray_in_fail(): "Make sure that grdcut fails correctly if DataArray is the input grid" - with pytest.raises(GMTInvalidInput): + with pytest.raises(NotImplementedError): grid = load_earth_relief() grdcut(grid, region="0/180/0/90") From 004c40cf8210010986157cc4e1ebe4a332c59359 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jun 2020 19:52:06 -0400 Subject: [PATCH 06/11] Force xarray to load the output grid into memory --- pygmt/modules.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pygmt/modules.py b/pygmt/modules.py index 28e9678ec0a..08db7f33ce7 100644 --- a/pygmt/modules.py +++ b/pygmt/modules.py @@ -244,7 +244,8 @@ def grdcut(grid, **kwargs): lib.call_module("grdcut", arg_str) if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray - result = xr.open_dataarray(outgrid) + with xr.open_dataarray(outgrid) as dataarray: + result = dataarray.load() else: result = None # if user sets an outgrid, return None From 93644692104ab365582b61195d268644c21bab45 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jun 2020 20:41:17 -0400 Subject: [PATCH 07/11] Update pygmt/modules.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/modules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/modules.py b/pygmt/modules.py index 08db7f33ce7..50899ad8d0a 100644 --- a/pygmt/modules.py +++ b/pygmt/modules.py @@ -196,7 +196,7 @@ def grdcut(grid, **kwargs): *unit* and we determine the corresponding rectangular region so that all grid nodes on or inside the circle are contained in the subset. If **+n** is appended we set all nodes outside the circle to NaN. - z_subregion : + z_subregion : str ``'[min/max][+n|N|r]'``. Determine a new rectangular region so that all nodes outside this region are also outside the given z-range [-inf/+inf]. To indicate no From 2a589205abd111db2764abf7775541ee4d8512e5 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 24 Jun 2020 20:41:25 -0400 Subject: [PATCH 08/11] Update doc/api/index.rst Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- doc/api/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/index.rst b/doc/api/index.rst index 7beb62651e5..28683d3b75c 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -70,9 +70,9 @@ Operations on grids: .. autosummary:: :toctree: generated + grdcut grdinfo grdtrack - grdcut GMT Defaults ------------ From 6ca442bef784673eb763d20cf6b578456c5f5f1c Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 26 Jun 2020 01:26:18 -0400 Subject: [PATCH 09/11] Move grdcut to gridops.py --- pygmt/__init__.py | 3 +- pygmt/gridops.py | 119 ++++++++++++++++++++++++++++++++++++++++++++++ pygmt/modules.py | 104 ---------------------------------------- 3 files changed, 121 insertions(+), 105 deletions(-) create mode 100644 pygmt/gridops.py diff --git a/pygmt/__init__.py b/pygmt/__init__.py index 47496bd52d6..e066a36035e 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -18,7 +18,8 @@ from .gridding import surface from .sampling import grdtrack from .mathops import makecpt -from .modules import config, info, grdinfo, grdcut, which +from .modules import config, info, grdinfo, which +from .gridops import grdcut from . import datasets diff --git a/pygmt/gridops.py b/pygmt/gridops.py new file mode 100644 index 00000000000..400a8d6f219 --- /dev/null +++ b/pygmt/gridops.py @@ -0,0 +1,119 @@ +""" +GMT modules for grid operations +""" + +import xarray as xr + + +from .clib import Session +from .helpers import ( + build_arg_string, + fmt_docstring, + kwargs_to_strings, + GMTTempFile, + use_alias, + data_kind, + dummy_context, +) +from .exceptions import GMTInvalidInput + + +@fmt_docstring +@use_alias( + G="outgrid", + R="region", + J="projection", + N="extend", + S="circ_subregion", + Z="z_subregion", +) +@kwargs_to_strings(R="sequence") +def grdcut(grid, **kwargs): + """ + Extract subregion from a grid. + + Produce a new *outgrid* file which is a subregion of *grid*. The + subregion is specified with *region*; the specified range must not exceed + the range of *grid* (but see *extend*). If in doubt, run + :meth:`pygmt.grdinfo` to check range. Alternatively, define the subregion + indirectly via a range check on the node values or via distances from a + given point. Finally, you can give *projection* for oblique projections to + determine the corresponding rectangular *region* setting that will give a + grid that fully covers the oblique domain. + + Full option list at :gmt-docs:`grdcut.html` + + {aliases} + + Parameters + ---------- + grid : str + The name of the input grid file. + outgrid : str or None + The name of the output netCDF file with extension .nc to store the grid + in. + {J} + {R} + extend : bool or int or float + Allow grid to be extended if new *region* exceeds existing boundaries. + Give a value to initialize nodes outside current region. + circ_subregion : str + ``'lon/lat/radius[unit][+n]'``. + Specify an origin (*lon* and *lat*) and *radius*; append a distance + *unit* and we determine the corresponding rectangular region so that + all grid nodes on or inside the circle are contained in the subset. + If **+n** is appended we set all nodes outside the circle to NaN. + z_subregion : str + ``'[min/max][+n|N|r]'``. + Determine a new rectangular region so that all nodes outside this + region are also outside the given z-range [-inf/+inf]. To indicate no + limit on *min* or *max* only, specify a hyphen (-). Normally, any NaNs + encountered are simply skipped and not considered in the + range-decision. Append **+n** to consider a NaN to be outside the given + z-range. This means the new subset will be NaN-free. Alternatively, + append **+r** to consider NaNs to be within the data range. In this + case we stop shrinking the boundaries once a NaN is found [Default + simply skips NaNs when making the range decision]. Finally, if your + core subset grid is surrounded by rows and/or columns that are all + NaNs, append **+N** to strip off such columns before (optionally) + considering the range of the core subset for further reduction of the + area. + + Returns + ------- + ret: xarray.DataArray or None + Return type depends on whether the *outgrid* parameter is set: + + - xarray.DataArray if *outgrid* is not set + - None if *outgrid* is set (grid output will be stored in *outgrid*) + """ + kind = data_kind(grid) + + with GMTTempFile(suffix=".nc") as tmpfile: + with Session() as lib: + if kind == "file": + file_context = dummy_context(grid) + elif kind == "grid": + raise NotImplementedError( + "xarray.DataArray is not supported as the input grid yet!" + ) + # file_context = lib.virtualfile_from_grid(grid) + # See https://github.com/GenericMappingTools/gmt/pull/3532 + # for a feature request. + else: + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) + + with file_context as infile: + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile + kwargs.update({"G": tmpfile.name}) + outgrid = kwargs["G"] + arg_str = " ".join([infile, build_arg_string(kwargs)]) + lib.call_module("grdcut", arg_str) + + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray + with xr.open_dataarray(outgrid) as dataarray: + result = dataarray.load() + else: + result = None # if user sets an outgrid, return None + + return result diff --git a/pygmt/modules.py b/pygmt/modules.py index 50899ad8d0a..2aaca5560c6 100644 --- a/pygmt/modules.py +++ b/pygmt/modules.py @@ -1,14 +1,11 @@ """ Non-plot GMT modules. """ -import xarray as xr - from .clib import Session from .helpers import ( build_arg_string, fmt_docstring, - kwargs_to_strings, GMTTempFile, use_alias, data_kind, @@ -151,107 +148,6 @@ def which(fname, **kwargs): return path -@fmt_docstring -@use_alias( - G="outgrid", - R="region", - J="projection", - N="extend", - S="circ_subregion", - Z="z_subregion", -) -@kwargs_to_strings(R="sequence") -def grdcut(grid, **kwargs): - """ - Extract subregion from a grid. - - Produce a new *outgrid* file which is a subregion of *grid*. The - subregion is specified with *region*; the specified range must not exceed - the range of *grid* (but see *extend*). If in doubt, run - :meth:`pygmt.grdinfo` to check range. Alternatively, define the subregion - indirectly via a range check on the node values or via distances from a - given point. Finally, you can give *projection* for oblique projections to - determine the corresponding rectangular *region* setting that will give a - grid that fully covers the oblique domain. - - Full option list at :gmt-docs:`grdcut.html` - - {aliases} - - Parameters - ---------- - grid : str - The name of the input grid file. - outgrid : str or None - The name of the output netCDF file with extension .nc to store the grid - in. - {J} - {R} - extend : bool or int or float - Allow grid to be extended if new *region* exceeds existing boundaries. - Give a value to initialize nodes outside current region. - circ_subregion : str - ``'lon/lat/radius[unit][+n]'``. - Specify an origin (*lon* and *lat*) and *radius*; append a distance - *unit* and we determine the corresponding rectangular region so that - all grid nodes on or inside the circle are contained in the subset. - If **+n** is appended we set all nodes outside the circle to NaN. - z_subregion : str - ``'[min/max][+n|N|r]'``. - Determine a new rectangular region so that all nodes outside this - region are also outside the given z-range [-inf/+inf]. To indicate no - limit on *min* or *max* only, specify a hyphen (-). Normally, any NaNs - encountered are simply skipped and not considered in the - range-decision. Append **+n** to consider a NaN to be outside the given - z-range. This means the new subset will be NaN-free. Alternatively, - append **+r** to consider NaNs to be within the data range. In this - case we stop shrinking the boundaries once a NaN is found [Default - simply skips NaNs when making the range decision]. Finally, if your - core subset grid is surrounded by rows and/or columns that are all - NaNs, append **+N** to strip off such columns before (optionally) - considering the range of the core subset for further reduction of the - area. - - Returns - ------- - ret: xarray.DataArray or None - Return type depends on whether the *outgrid* parameter is set: - - - xarray.DataArray if *outgrid* is not set - - None if *outgrid* is set (grid output will be stored in *outgrid*) - """ - kind = data_kind(grid) - - with GMTTempFile(suffix=".nc") as tmpfile: - with Session() as lib: - if kind == "file": - file_context = dummy_context(grid) - elif kind == "grid": - raise NotImplementedError( - "xarray.DataArray is not supported as the input grid yet!" - ) - # file_context = lib.virtualfile_from_grid(grid) - # See https://github.com/GenericMappingTools/gmt/pull/3532 - # for a feature request. - else: - raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) - - with file_context as infile: - if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile - kwargs.update({"G": tmpfile.name}) - outgrid = kwargs["G"] - arg_str = " ".join([infile, build_arg_string(kwargs)]) - lib.call_module("grdcut", arg_str) - - if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray - with xr.open_dataarray(outgrid) as dataarray: - result = dataarray.load() - else: - result = None # if user sets an outgrid, return None - - return result - - class config: # pylint: disable=invalid-name """ Set GMT defaults globally or locally. From 3e8530ea78de2e9a1d0cc382fdf5ecf9e24ed2ce Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 26 Jun 2020 01:29:48 -0400 Subject: [PATCH 10/11] Do not add a blank line in modules.py --- pygmt/modules.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/modules.py b/pygmt/modules.py index 2aaca5560c6..89c6a46db88 100644 --- a/pygmt/modules.py +++ b/pygmt/modules.py @@ -1,7 +1,6 @@ """ Non-plot GMT modules. """ - from .clib import Session from .helpers import ( build_arg_string, From 99f5d82620632d40cf657c87e9286a5725ad3764 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 27 Jun 2020 22:12:06 -0400 Subject: [PATCH 11/11] Fix the grdcut tests for the gridline-registered grids --- pygmt/tests/test_grdcut.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pygmt/tests/test_grdcut.py b/pygmt/tests/test_grdcut.py index 1e1e283d1fe..dfd06edc919 100644 --- a/pygmt/tests/test_grdcut.py +++ b/pygmt/tests/test_grdcut.py @@ -19,7 +19,7 @@ def test_grdcut_file_in_file_out(): assert result is None # return value is None assert os.path.exists(path=tmpfile.name) # check that outgrid exists result = grdinfo(tmpfile.name, C=True) - assert result == "0 180 0 90 -8182 5651.5 1 1 180 90\n" + assert result == "0 180 0 90 -8592.5 5559 1 1 181 91\n" def test_grdcut_file_in_dataarray_out(): @@ -29,14 +29,14 @@ def test_grdcut_file_in_dataarray_out(): # check information of the output grid # the '@earth_relief_01d' is in pixel registration, so the grid range is # not exactly 0/180/0/90 - assert outgrid.coords["lat"].data.min() == 0.5 - assert outgrid.coords["lat"].data.max() == 89.5 - assert outgrid.coords["lon"].data.min() == 0.5 - assert outgrid.coords["lon"].data.max() == 179.5 - assert outgrid.data.min() == -8182.0 - assert outgrid.data.max() == 5651.5 - assert outgrid.sizes["lat"] == 90 - assert outgrid.sizes["lon"] == 180 + assert outgrid.coords["lat"].data.min() == 0.0 + assert outgrid.coords["lat"].data.max() == 90.0 + assert outgrid.coords["lon"].data.min() == 0.0 + assert outgrid.coords["lon"].data.max() == 180.0 + assert outgrid.data.min() == -8592.5 + assert outgrid.data.max() == 5559 + assert outgrid.sizes["lat"] == 91 + assert outgrid.sizes["lon"] == 181 def test_grdcut_dataarray_in_file_out():