Skip to content

Commit 7d7baa1

Browse files
committed
pygmt.grdfilter: Support Pythonic arguments for the distance parameter
1 parent f97dce9 commit 7d7baa1

2 files changed

Lines changed: 77 additions & 26 deletions

File tree

pygmt/src/grdfilter.py

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@
1515

1616

1717
@fmt_docstring
18-
@use_alias(D="distance", F="filter", f="coltypes")
18+
@use_alias(F="filter", f="coltypes")
1919
def grdfilter(
2020
grid: PathLike | xr.DataArray,
2121
outgrid: PathLike | None = None,
22+
distance: Literal[
23+
"pixel",
24+
"cartesian",
25+
"geo_cartesian",
26+
"geo_flatearth1",
27+
"geo_flatearth2",
28+
"geo_spherical",
29+
"geo_mercator",
30+
]
31+
| None = None,
2232
spacing: Sequence[float | str] | None = None,
2333
nans: Literal["ignore", "replace", "preserve"] | None = None,
2434
toggle: bool = False,
@@ -45,6 +55,7 @@ def grdfilter(
4555
Full GMT docs at :gmt-docs:`grdfilter.html`.
4656
4757
$aliases
58+
- D = distance
4859
- G = outgrid
4960
- I = spacing
5061
- N = nans
@@ -71,27 +82,50 @@ def grdfilter(
7182
- **p**: Maximum Likelihood probability
7283
- **h**: Histogram
7384
74-
distance : str
75-
State how the grid (x,y) relates to the filter *width*:
76-
77-
- ``"p"``: grid (px,py) with *width* an odd number of pixels,
78-
Cartesian distances.
79-
- ``"0"``: grid (x,y) same units as *width*, Cartesian distances.
80-
- ``"1"``: grid (x,y) in degrees, *width* in kilometers, Cartesian
81-
distances.
82-
- ``"2"``: grid (x,y) in degrees, *width* in km, dx scaled by
83-
cos(middle y), Cartesian distances.
84-
85-
The above options are fastest because they allow weight matrix to be
86-
computed only once. The next three options are slower because they
87-
recompute weights for each latitude.
88-
89-
- ``"3"``: grid (x,y) in degrees, *width* in km, dx scaled by cos(y),
90-
Cartesian distance calculation.
91-
- ``"4"``: grid (x,y) in degrees, *width* in km, Spherical distance
92-
calculation.
93-
- ``"5"``: grid (x,y) in Mercator ``projection="m1"`` img units,
94-
*width* in km, Spherical distance calculation.
85+
distance
86+
Distance *flag* tells how grid (*x, y*) relates to filter *width* as follows:
87+
88+
.. list-table::
89+
:header-rows: 1
90+
:widths: 16 32 20 32
91+
92+
* - Name
93+
- Grid (x,y)
94+
- Width
95+
- Distance Calculation
96+
* - ``"pixel"``
97+
- Pixels (px, py)
98+
- Odd number of pixels
99+
- Cartesian
100+
* - ``"cartesian"``
101+
- Same units as *width*
102+
- Any
103+
- Cartesian
104+
* - ``"geo_cartesian"``
105+
- Degrees
106+
- km
107+
- Cartesian
108+
* - ``"geo_flatearth1"``
109+
- Degrees
110+
- km
111+
- Cartesian, dx scaled by cos(middle y)
112+
* - ``"geo_flatearth2"``
113+
- Degrees
114+
- km
115+
- Cartesian, dx scaled by cos(y) per row
116+
* - ``"geo_spherical"``
117+
- Degrees
118+
- km
119+
- Spherical (great circle)
120+
* - ``"geo_mercator"``
121+
- Mercator **-Jm1** img units
122+
- km
123+
- Spherical
124+
125+
The first four options are fastest because they allow weight matrix to be
126+
computed only once. The last three options are slower because they recompute
127+
weights for each latitude.
128+
95129
$spacing
96130
nans
97131
Determine how NaN-values in the input grid affect the filtered output grid.
@@ -131,7 +165,7 @@ def grdfilter(
131165
>>> pygmt.grdfilter(
132166
... grid="@earth_relief_30m_g",
133167
... filter="m600",
134-
... distance="4",
168+
... distance="geo_spherical",
135169
... region=[150, 250, 10, 40],
136170
... spacing=0.5,
137171
... outgrid="filtered_pacific.nc",
@@ -140,9 +174,22 @@ def grdfilter(
140174
>>> # Apply a Gaussian smoothing filter of 600 km to the input DataArray and return
141175
>>> # a filtered DataArray with the smoothed grid.
142176
>>> grid = pygmt.datasets.load_earth_relief()
143-
>>> smooth_field = pygmt.grdfilter(grid=grid, filter="g600", distance="4")
177+
>>> smoothed = pygmt.grdfilter(grid=grid, filter="g600", distance="geo_spherical")
144178
"""
145179
aliasdict = AliasSystem(
180+
D=Alias(
181+
distance,
182+
name="distance",
183+
mapping={
184+
"pixel": "p",
185+
"cartesian": 0,
186+
"geo_cartesian": 1,
187+
"geo_flatearth1": 2,
188+
"geo_flatearth2": 3,
189+
"geo_spherical": 4,
190+
"geo_mercator": 5,
191+
},
192+
),
146193
I=Alias(spacing, name="spacing", sep="/", size=2),
147194
N=Alias(
148195
nans, name="nans", mapping={"ignore": "i", "replace": "r", "preserve": "p"}

pygmt/tests/test_grdfilter.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ def test_grdfilter_dataarray_in_dataarray_out(grid, expected_grid):
4747
Test grdfilter with an input DataArray, and output as DataArray.
4848
"""
4949
result = grdfilter(
50-
grid=grid, filter="g600", distance="4", region=[-53, -49, -20, -17], cores=2
50+
grid=grid,
51+
filter="g600",
52+
distance="geo_spherical",
53+
region=[-53, -49, -20, -17],
54+
cores=2,
5155
)
5256
# check information of the output grid
5357
assert isinstance(result, xr.DataArray)
@@ -66,7 +70,7 @@ def test_grdfilter_dataarray_in_file_out(grid, expected_grid):
6670
grid,
6771
outgrid=tmpfile.name,
6872
filter="g600",
69-
distance="4",
73+
distance="geo_spherical",
7074
region=[-53, -49, -20, -17],
7175
)
7276
assert result is None # return value is None

0 commit comments

Comments
 (0)