Grdmask

In pygmt I am having problem with grdmask. When I run pygmt.grdmask, it says module not recognized. It appears grdmask is not available in pygmt; pygmt.grdlandmask is working but i need grdmask. Any help will be appreciated.

Hello @dowchu,

welcome to the GMT forum :slightly_smiling_face:.

So far, grdmask is unfortunately not wrapped in PyGMT. If you like, you can open an New issue with a feature request on GitHub at Issues · GenericMappingTools/pygmt · GitHub. Maybe someone has time to work on this.

In the meanwhile, you can try using pygmt.clib.Session and pygmt.clib.Session.call_module. This allows you to use any GMT module within PyGMT.

I have no experience with grdmask, however I tried to rewrite the first example in the GMT documentation at grdmask — GMT 6.6.0 documentation. Maybe this can serve as an orientation.

Code example:
Please place the file coastlines.txt in your working directory:
coastlines.txt (57 Bytes)

import pygmt as gmt
from pygmt.clib import Session


# Source: https://docs.generic-mapping-tools.org/dev/grdmask.html#examples
# Last access: 2023/05/05

# To set all nodes inside and on the polygon coastline_*.xy to 0,
# and outside points to 1, do
# gmt grdmask coastline_*.xy -R-60/-40/-40/-30 -I5m -N1/0/0 -Gland_mask.nc=nb -V

with Session() as lib:

    lib.call_module(
        "grdmask",
        "{} \
        -R-60/-40/-40/-30 \
        -I5m \
        -N1/0/0 \
        -Gland_mask.nc=nb" \
        .format("coastlines.txt")
    )


# Plot output grid via PyGMT
fig = gmt.Figure()

fig.grdimage(
    grid="land_mask.nc",
    projection="M10c",
    frame=True,
    cmap="vik",
)
       
fig.colorbar()
           
fig.show()
# fig.savefig(fname="grdmask_in_pygmt.png")

Output figure:

1 Like

Thank you…much appreciated