Skip to content

Commit d13a16d

Browse files
authored
Merge branch 'main' into refactor/image
2 parents 1394e83 + 34e49a4 commit d13a16d

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

pygmt/src/grdfilter.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515

1616

1717
@fmt_docstring
18-
@use_alias(D="distance", F="filter", N="nans", f="coltypes")
18+
@use_alias(D="distance", F="filter", f="coltypes")
1919
def grdfilter(
2020
grid: PathLike | xr.DataArray,
2121
outgrid: PathLike | None = None,
22-
toggle: bool = False,
2322
spacing: Sequence[float | str] | None = None,
23+
nans: Literal["ignore", "replace", "preserve"] | None = None,
24+
toggle: bool = False,
2425
region: Sequence[float | str] | str | None = None,
25-
registration: Literal["gridline", "pixel"] | bool = False,
2626
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
2727
| bool = False,
28+
registration: Literal["gridline", "pixel"] | bool = False,
2829
cores: int | bool = False,
2930
**kwargs,
3031
) -> xr.DataArray | None:
@@ -45,6 +46,7 @@ def grdfilter(
4546
4647
$aliases
4748
- I = spacing
49+
- N = nans
4850
- R = region
4951
- T = toggle
5052
- V = verbose
@@ -89,21 +91,22 @@ def grdfilter(
8991
calculation.
9092
- ``"5"``: grid (x,y) in Mercator ``projection="m1"`` img units,
9193
*width* in km, Spherical distance calculation.
92-
9394
$spacing
94-
nans : str or float
95-
**i**\|\ **p**\|\ **r**.
96-
Determine how NaN-values in the input grid affect the filtered output.
97-
Use **i** to ignore all NaNs in the calculation of the filtered value
98-
[Default]. **r** is same as **i** except if the input node was NaN then
99-
the output node will be set to NaN (only applies if both grids are
100-
co-registered). **p** will force the filtered value to be NaN if any
101-
grid nodes with NaN-values are found inside the filter circle.
102-
$region
95+
nans
96+
Determine how NaN-values in the input grid affect the filtered output grid.
97+
Choose one of:
98+
99+
- ``"ignore"``: Ignore all NaNs in the calculation of filtered value [Default].
100+
- ``"replace"``: Similar to ``"ignore"`` except if the input node was NaN then
101+
the output node will be set to NaN (only applied if both grids are
102+
co-registered).
103+
- ``"preserve"``: Force the filtered value to be NaN if any grid nodes with
104+
NaN-values are found inside the filter circle.
103105
toggle
104106
Toggle the node registration for the output grid so as to become the opposite of
105107
the input grid [Default gives the same registration as the input grid].
106108
Alternatively, use ``registration`` to set the registration explicitly.
109+
$region
107110
$verbose
108111
$coltypes
109112
$registration
@@ -122,8 +125,8 @@ def grdfilter(
122125
--------
123126
>>> from pathlib import Path
124127
>>> import pygmt
125-
>>> # Apply a filter of 600 km (full width) to the @earth_relief_30m_g file
126-
>>> # and return a filtered field (saved as netCDF)
128+
>>> # Apply a median filter of 600 km (full width) to the @earth_relief_30m_g grid
129+
>>> # and return a filtered grid (saved as netCDF file).
127130
>>> pygmt.grdfilter(
128131
... grid="@earth_relief_30m_g",
129132
... filter="m600",
@@ -133,13 +136,16 @@ def grdfilter(
133136
... outgrid="filtered_pacific.nc",
134137
... )
135138
>>> Path("filtered_pacific.nc").unlink() # Cleanup file
136-
>>> # Apply a Gaussian smoothing filter of 600 km to the input DataArray
137-
>>> # and return a filtered DataArray with the smoothed field
139+
>>> # Apply a Gaussian smoothing filter of 600 km to the input DataArray and return
140+
>>> # a filtered DataArray with the smoothed grid.
138141
>>> grid = pygmt.datasets.load_earth_relief()
139142
>>> smooth_field = pygmt.grdfilter(grid=grid, filter="g600", distance="4")
140143
"""
141144
aliasdict = AliasSystem(
142145
I=Alias(spacing, name="spacing", sep="/", size=2),
146+
N=Alias(
147+
nans, name="nans", mapping={"ignore": "i", "replace": "r", "preserve": "p"}
148+
),
143149
T=Alias(toggle, name="toggle"),
144150
).add_common(
145151
R=region,

pygmt/src/inset.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def inset(
7474
If not specified, defaults to the Bottom Left corner of the plot.
7575
width
7676
height
77-
Width and height of the inset. Width must be specified and height is set to be
78-
equal to width if not specified.
77+
Width and height of the inset. Width must be specified unless ``projection`` and
78+
``region`` are given, and height is set to be equal to width if not specified.
7979
box
8080
Draw a background box behind the inset. If set to ``True``, a simple rectangular
8181
box is drawn using :gmt-term:`MAP_FRAME_PEN`. To customize the box appearance,
@@ -132,8 +132,12 @@ def inset(
132132
default=Position((0, 0), cstype="plotcoords"), # Default to (0,0) in plotcoords
133133
)
134134

135-
# width is mandatory.
136-
if width is None and not isinstance(position, str):
135+
# width is mandatory unless both projection and region are given.
136+
if (
137+
not isinstance(position, str)
138+
and width is None
139+
and (projection is None or region is None)
140+
):
137141
msg = "Parameter 'width' must be specified."
138142
raise GMTInvalidInput(msg)
139143

pygmt/tests/test_inset.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ def test_inset_default_position():
5454
return fig
5555

5656

57+
@pytest.mark.mpl_image_compare(filename="test_inset_default_position.png")
58+
def test_inset_width_from_projection_region():
59+
"""
60+
Test that the inset can infer width from projection and region.
61+
"""
62+
fig = Figure()
63+
fig.basemap(region="MG+r2", frame="afg")
64+
with fig.inset(projection="G47/-20/3.5c", region="g", box=True):
65+
fig.basemap(region="g", projection="G47/-20/?", frame="afg")
66+
return fig
67+
68+
5769
@pytest.mark.mpl_image_compare(filename="test_inset_aliases.png")
5870
def test_inset_deprecated_position():
5971
"""

0 commit comments

Comments
 (0)