-
Notifications
You must be signed in to change notification settings - Fork 245
WIP: Add tutorial showing the usage of pygmt.grdmask #4525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yvonnefroehlich
wants to merge
57
commits into
main
Choose a base branch
from
add-gallery-grdmask
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−2
Draft
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
095c795
Add first draft for gallery example or tutorial on grdmask
yvonnefroehlich 5456a7f
Adjust regions
yvonnefroehlich 31d1b74
Try extracting a circle
yvonnefroehlich 4584da7
Remove execution permission
yvonnefroehlich 2b8a632
Select US staat
yvonnefroehlich b936f5d
Zoom in mask grid
yvonnefroehlich 2c1c264
Try inside and outside
yvonnefroehlich a62befc
Adjust area for US staat, fix typo, improve code
yvonnefroehlich 9cb4b75
Fix style
yvonnefroehlich c0ef611
Set outside parameter correctly
yvonnefroehlich 6326c19
Fix typo
yvonnefroehlich a4d8f5e
Adjust title - circles are no polygons
yvonnefroehlich 5173eff
Change back to None
yvonnefroehlich 30ae770
Revert "Change back to None"
yvonnefroehlich bc14c58
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich ac5403d
Fix typo, add folder for cmaps
yvonnefroehlich 98f32db
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich 3403501
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich 1b54365
Add first version of intro text
yvonnefroehlich 5938662
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich 86da3ab
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich 904b0fe
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich 8bc55d5
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich 33338a0
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich 544fc07
Remove white space to fix link to grdlandmask gallery example
yvonnefroehlich 667fbb5
Fix typo
yvonnefroehlich 1a57988
Improve formulation
yvonnefroehlich 4e1d653
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich 871c469
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich c95623c
Fix typo
yvonnefroehlich 114ec89
Adjust color for outline
yvonnefroehlich e9f0ed5
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich bfb2402
Remove colorbar
yvonnefroehlich 4bf9102
Move to advaced tutorials
yvonnefroehlich 7cd4495
Remove execution permission
yvonnefroehlich 70afe29
Reduce resolution of grid, reduce size of subplots
yvonnefroehlich 688f6d7
Merge remote-tracking branch 'origin/add-gallery-grdmask' into add-ga…
yvonnefroehlich 11ef768
Make file name more descriptive
yvonnefroehlich 3fc3bef
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich ffd2aec
Add higlighting
yvonnefroehlich 6e17927
Adjust comment to changed value
yvonnefroehlich d06567e
Improve comments
yvonnefroehlich 14d6f06
Make variable names more exact even they are now longer
yvonnefroehlich 5341e34
Fix highlighting
yvonnefroehlich a4c27ac
Fix highlighting
yvonnefroehlich 9d67e16
Add more highlighting
yvonnefroehlich d4d060c
Fix line length
yvonnefroehlich 15328f6
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich 8ebbfc8
Remove colorbars
yvonnefroehlich b372fce
Add note regarding not adding colorbars
yvonnefroehlich 7750874
Adjust structure of intro text
yvonnefroehlich ff6e861
Explain values for nodes in intro text
yvonnefroehlich e77df68
Improve formulation
yvonnefroehlich 9cc268c
Add missing word
yvonnefroehlich 1116e28
Add word
yvonnefroehlich 583c5ca
Improve intro text
yvonnefroehlich 05e8beb
Merge branch 'main' into add-gallery-grdmask
yvonnefroehlich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| """ | ||
| Create grid masks from spatial shapes | ||
| ===================================== | ||
|
|
||
| The functionn :func:`pygmt.grdmask` allows to create a grid mask based on spatial shapes | ||
| given as closed polygons. These polygons can be provided as :class:`numpy.ndarray` or | ||
| :class:`geopandas.GeoDataFrame`. For the nodes falling inside, outside, and on the | ||
| edges, different values can be defined. The created mask can then be applied to a | ||
| desired grid. | ||
|
|
||
| This value can be any number, or one of ``None``, ``"NaN"``, and ``np.nan`` for NaN. The | ||
| defaults are ``0`` for ``outside``, ``0`` for ``edge``, and ``1`` for ``inside``. To | ||
| treat edges as inside, use the same value as for ``inside``. When setting these values, | ||
| keep in mind you are creating the mask grid first which is thought to be applied to a | ||
| real grid in a second step. | ||
|
|
||
| To create a land-water mask based on the GMT built-in shoreline data you can directly | ||
| use the function :func:`pygmt.grdlandmask` explained in the gallery example | ||
| :doc:`Create 'wet-dry' mask grid </gallery/images/grdlandmask>`. As the focus of this | ||
| tutorial is on creating a mask grid and the related masked grid, no colorbars for the | ||
| elevation grids are added to the maps. | ||
| """ | ||
|
|
||
| # %% | ||
| import geopandas | ||
| import numpy as np | ||
| import pygmt | ||
| from shapely.geometry import Point | ||
|
|
||
| # %% | ||
| # Polygons based on a :class:`numpy.ndarray` | ||
| # ------------------------------------------ | ||
|
|
||
| # Define a study region | ||
| region = [125, 135, 25, 36] | ||
|
|
||
| # Define two closed polygons, here a square and a triangle. | ||
| # Use a record with np.nan to separate the polygons | ||
| polygon = np.array( | ||
| [ | ||
| [129, 31], | ||
| [134, 31], | ||
| [134, 35], | ||
| [129, 35], | ||
| [129, 31], | ||
| [np.nan, np.nan], | ||
| [126, 26], | ||
| [131, 26], | ||
| [131, 30], | ||
| [126, 26], | ||
| ], | ||
| ) | ||
|
|
||
| # Download elevation grid | ||
| grid = pygmt.datasets.load_earth_relief(region=region, resolution="01m") | ||
|
|
||
| # Create a grid mask based on the two polygons defined above | ||
| # Set all grid nodes outside the polygons to NaN | ||
| mask_outside = pygmt.grdmask(region=region, data=polygon, spacing="01m", outside="NaN") | ||
| # Set all grid nodes inside the polygons to NaN | ||
| # Set the outside parameter to a value larger 0 to keep the nodes outside unchanged | ||
| mask_inside = pygmt.grdmask( | ||
| region=region, data=polygon, spacing="01m", inside="NaN", outside=1 | ||
| ) | ||
|
Comment on lines
+57
to
+64
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we should explain the default values for inside/outside/edge to better understand the behavior. |
||
|
|
||
| # Apply the grid mask to the downloaded elevation grid by multiplying the two grids | ||
| grid_masked_outside = grid * mask_outside | ||
| grid_masked_inside = grid * mask_inside | ||
|
|
||
|
|
||
| fig = pygmt.Figure() | ||
| pygmt.makecpt(cmap="SCM/oleron", series=[-2000, 2000]) | ||
|
|
||
| # Plot the elevation grid | ||
| fig.basemap(region=region, projection="M10c", frame=True) | ||
| fig.grdimage(grid=grid, cmap=True) | ||
| fig.plot(data=polygon, pen="2p,cyan") | ||
|
|
||
| fig.shift_origin(xshift="+w+2c") | ||
|
|
||
| # Plot the masked elevation grid outside | ||
| fig.basemap(region=region, projection="M10c", frame=True) | ||
| fig.grdimage(grid=grid_masked_outside, cmap=True) | ||
| fig.plot(data=polygon, pen="2p,cyan") | ||
|
|
||
| fig.shift_origin(xshift="+w+2c") | ||
|
|
||
| # Plot the masked elevation grid inside | ||
| fig.basemap(region=region, projection="M10c", frame=True) | ||
| fig.grdimage(grid=grid_masked_inside, cmap=True) | ||
| fig.plot(data=polygon, pen="2p,cyan") | ||
|
|
||
| fig.show() | ||
|
|
||
|
|
||
| # %% | ||
|
seisman marked this conversation as resolved.
|
||
| # US state Missouri based on a :class:`geopandas.GeoDataFrame` polygon geometry | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| region = [-126, -66, 25, 49] | ||
|
|
||
| provider = "https://naciscdn.org/naturalearth" | ||
| states = geopandas.read_file( | ||
| f"{provider}/50m/cultural/ne_50m_admin_1_states_provinces.zip" | ||
| ) | ||
| missouri = states[states["name"] == "Missouri"] | ||
|
|
||
| grid = pygmt.datasets.load_earth_relief(region=region, resolution="01m") | ||
| mask = pygmt.grdmask(region=region, data=missouri, spacing="01m", outside="NaN") | ||
| mask_lonlat = mask.rename(new_name_or_name_dict={"x": "lon", "y": "lat"}) | ||
| grid_masked = grid * mask_lonlat | ||
|
|
||
|
|
||
| fig = pygmt.Figure() | ||
| pygmt.makecpt(cmap="SCM/oleron", series=[-2000, 2000]) | ||
|
|
||
| # Plot the elevation grid | ||
| fig.basemap(projection="L-96/35/33/41/12c", region=region, frame=True) | ||
| fig.grdimage(grid=grid, cmap=True) | ||
| fig.plot(data=missouri, pen="1p,cyan") | ||
|
|
||
| fig.shift_origin(xshift="+w+1c") | ||
|
|
||
| # Plot the masked elevation grid | ||
| # fig.basemap(projection="L-96/35/33/41/12c", region=region, frame=True) | ||
| fig.basemap(projection="M10c", region=[-96.5, -88.5, 35.8, 41], frame=True) | ||
| fig.grdimage(grid=grid_masked, cmap=True) | ||
| fig.plot(data=missouri, pen="1p,cyan") | ||
|
|
||
| fig.show() | ||
|
|
||
|
|
||
| # %% | ||
| # Circle based on GeoPandas polygon geometry | ||
| # ------------------------------------------ | ||
| # | ||
| # Note the distortion of the circle due the projection making it appear as an ellipse. | ||
|
|
||
| region = [125, 135, 25, 36] | ||
|
|
||
| # Create a point and buffer it | ||
| point = geopandas.GeoSeries([Point(126.5, 33.5)]) | ||
| circle = point.buffer(0.6) # 0.6 is the radius | ||
|
|
||
| # Create masked grid | ||
| grid = pygmt.datasets.load_earth_relief(region=region, resolution="30s") | ||
| mask = pygmt.grdmask(region=region, data=circle, spacing="30s", outside="NaN") | ||
| mask_lonlat = mask.rename(new_name_or_name_dict={"x": "lon", "y": "lat"}) | ||
| grid_masked = grid * mask_lonlat | ||
|
|
||
|
|
||
| fig = pygmt.Figure() | ||
| pygmt.makecpt(cmap="SCM/oleron", series=[-2000, 2000]) | ||
|
|
||
| # Plot the elevation grid | ||
| fig.basemap(region=region, projection="M12c", frame=True) | ||
| fig.grdimage(grid=grid, cmap=True) | ||
| fig.plot(data=circle, pen="2p,cyan") | ||
|
|
||
| fig.shift_origin(xshift="+w+2c") | ||
|
|
||
| # Plot the masked elevation grid | ||
| fig.basemap(region=[125.5, 127.5, 32.5, 34.5], projection="M12c", frame=True) | ||
| fig.grdimage(grid=grid_masked, cmap=True) | ||
| fig.plot(data=circle, pen="2p,cyan") | ||
|
|
||
| fig.show() | ||
|
|
||
| # sphinx_gallery_thumbnail_number = 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.