Skip to content

Commit 85d78d6

Browse files
willschlitzerweiji14michaelgrundseisman
authored
Wrap grdsample (#1380)
*Wrap GMT module grdsample *Add tests for grdsample Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Michael Grund <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent 097c807 commit 85d78d6

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Operations on grids:
9494
grdfilter
9595
grdlandmask
9696
grdgradient
97+
grdsample
9798
grdtrack
9899
xyz2grd
99100

pygmt/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
grdgradient,
4141
grdinfo,
4242
grdlandmask,
43+
grdsample,
4344
grdtrack,
4445
info,
4546
makecpt,

pygmt/src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pygmt.src.grdimage import grdimage
2020
from pygmt.src.grdinfo import grdinfo
2121
from pygmt.src.grdlandmask import grdlandmask
22+
from pygmt.src.grdsample import grdsample
2223
from pygmt.src.grdtrack import grdtrack
2324
from pygmt.src.grdview import grdview
2425
from pygmt.src.histogram import histogram

pygmt/src/grdsample.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
grdsample - Resample a grid onto a new lattice
3+
"""
4+
5+
import xarray as xr
6+
from pygmt.clib import Session
7+
from pygmt.helpers import (
8+
GMTTempFile,
9+
build_arg_string,
10+
fmt_docstring,
11+
kwargs_to_strings,
12+
use_alias,
13+
)
14+
15+
16+
@fmt_docstring
17+
@use_alias(
18+
G="outgrid",
19+
J="projection",
20+
I="spacing",
21+
R="region",
22+
T="translate",
23+
V="verbose",
24+
f="coltypes",
25+
n="interpolation",
26+
r="registration",
27+
x="cores",
28+
)
29+
@kwargs_to_strings(I="sequence", R="sequence")
30+
def grdsample(grid, **kwargs):
31+
r"""
32+
Change the registration, spacing, or nodes in a grid file.
33+
34+
This reads a grid file and interpolates it to create a new grid
35+
file. It can change the registration with ``translate`` or
36+
``registration``, change the grid-spacing or number of nodes with
37+
``spacing``, and set a new sub-region using ``region``. A bicubic
38+
[Default], bilinear, B-spline or nearest-neighbor interpolation is set
39+
with ``interpolation``.
40+
41+
When ``region`` is omitted, the output grid will cover the same region as
42+
the input grid. When ``spacing`` is omitted, the grid spacing of the
43+
output grid will be the same as the input grid. Either ``registration`` or
44+
``translate`` can be used to change the grid registration. When omitted,
45+
the output grid will have the same registration as the input grid.
46+
47+
{aliases}
48+
49+
Parameters
50+
----------
51+
grid : str or xarray.DataArray
52+
The file name of the input grid or the grid loaded as a DataArray.
53+
outgrid : str or None
54+
The name of the output netCDF file with extension .nc to store the grid
55+
in.
56+
{I}
57+
{R}
58+
translate : bool
59+
Translate between grid and pixel registration; if the input is
60+
grid-registered, the output will be pixel-registered and vice-versa.
61+
registration : str or bool
62+
[**g**\ |\ **p**\ ].
63+
Set registration to **g**\ ridline or **p**\ ixel.
64+
{V}
65+
{f}
66+
{n}
67+
{x}
68+
69+
Returns
70+
-------
71+
ret: xarray.DataArray or None
72+
Return type depends on whether the ``outgrid`` parameter is set:
73+
74+
- :class:`xarray.DataArray` if ``outgrid`` is not set
75+
- None if ``outgrid`` is set (grid output will be stored in file set by
76+
``outgrid``)
77+
"""
78+
with GMTTempFile(suffix=".nc") as tmpfile:
79+
with Session() as lib:
80+
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
81+
with file_context as infile:
82+
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
83+
kwargs.update({"G": tmpfile.name})
84+
outgrid = kwargs["G"]
85+
arg_str = " ".join([infile, build_arg_string(kwargs)])
86+
lib.call_module("grdsample", arg_str)
87+
88+
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
89+
with xr.open_dataarray(outgrid) as dataarray:
90+
result = dataarray.load()
91+
_ = result.gmt # load GMTDataArray accessor information
92+
else:
93+
result = None # if user sets an outgrid, return None
94+
95+
return result

pygmt/tests/test_grdsample.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Tests for grdsample.
3+
"""
4+
import os
5+
6+
import pytest
7+
from pygmt import grdinfo, grdsample
8+
from pygmt.datasets import load_earth_relief
9+
from pygmt.helpers import GMTTempFile
10+
11+
12+
@pytest.fixture(scope="module", name="grid")
13+
def fixture_grid():
14+
"""
15+
Load the grid data from the sample earth_relief file.
16+
"""
17+
return load_earth_relief(
18+
resolution="01d", region=[-5, 5, -5, 5], registration="pixel"
19+
)
20+
21+
22+
def test_grdsample_file_out(grid):
23+
"""
24+
grdsample with an outgrid set and the spacing is changed.
25+
"""
26+
with GMTTempFile(suffix=".nc") as tmpfile:
27+
result = grdsample(grid=grid, outgrid=tmpfile.name, spacing=[1, 0.5])
28+
assert result is None # return value is None
29+
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
30+
result = grdinfo(tmpfile.name, per_column=True).strip().split()
31+
assert float(result[6]) == 1 # x-increment
32+
assert float(result[7]) == 0.5 # y-increment
33+
34+
35+
def test_grdsample_no_outgrid(grid):
36+
"""
37+
Test grdsample with no set outgrid and applying registration changes.
38+
"""
39+
assert grid.gmt.registration == 1 # Pixel registration
40+
translated_grid = grdsample(grid=grid, translate=True)
41+
assert translated_grid.gmt.registration == 0 # Gridline registration
42+
registration_grid = grdsample(grid=translated_grid, registration="p")
43+
assert registration_grid.gmt.registration == 1 # Pixel registration

0 commit comments

Comments
 (0)