|
| 1 | +""" |
| 2 | +grdlandmask - Create a "wet-dry" mask grid from shoreline data base |
| 3 | +""" |
| 4 | + |
| 5 | +import xarray as xr |
| 6 | +from pygmt.clib import Session |
| 7 | +from pygmt.exceptions import GMTInvalidInput |
| 8 | +from pygmt.helpers import ( |
| 9 | + GMTTempFile, |
| 10 | + build_arg_string, |
| 11 | + fmt_docstring, |
| 12 | + kwargs_to_strings, |
| 13 | + use_alias, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@fmt_docstring |
| 18 | +@use_alias( |
| 19 | + G="outgrid", |
| 20 | + I="spacing", |
| 21 | + R="region", |
| 22 | + r="registration", |
| 23 | +) |
| 24 | +@kwargs_to_strings(R="sequence") |
| 25 | +def grdlandmask(**kwargs): |
| 26 | + r""" |
| 27 | + Create a grid file with set values for land and water. |
| 28 | +
|
| 29 | + Read the selected shoreline database and create a grid to specify which |
| 30 | + nodes in the specified grid are over land or over water. The nodes defined |
| 31 | + by the selected region and lattice spacing |
| 32 | + will be set according to one of two criteria: (1) land vs water, or |
| 33 | + (2) the more detailed (hierarchical) ocean vs land vs lake |
| 34 | + vs island vs pond. |
| 35 | +
|
| 36 | + Full option list at :gmt-docs:`grdlandmask.html` |
| 37 | +
|
| 38 | + {aliases} |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + outgrid : str or None |
| 43 | + The name of the output netCDF file with extension .nc to store the grid |
| 44 | + in. |
| 45 | + {I} |
| 46 | + {R} |
| 47 | + {r} |
| 48 | +
|
| 49 | + Returns |
| 50 | + ------- |
| 51 | + ret: xarray.DataArray or None |
| 52 | + Return type depends on whether the ``outgrid`` parameter is set: |
| 53 | +
|
| 54 | + - :class:`xarray.DataArray` if ``outgrid`` is not set |
| 55 | + - None if ``outgrid`` is set (grid output will be stored in file set by |
| 56 | + ``outgrid``) |
| 57 | + """ |
| 58 | + if "I" not in kwargs.keys() or "R" not in kwargs.keys(): |
| 59 | + raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.") |
| 60 | + |
| 61 | + with GMTTempFile(suffix=".nc") as tmpfile: |
| 62 | + with Session() as lib: |
| 63 | + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile |
| 64 | + kwargs.update({"G": tmpfile.name}) |
| 65 | + outgrid = kwargs["G"] |
| 66 | + arg_str = build_arg_string(kwargs) |
| 67 | + lib.call_module("grdlandmask", arg_str) |
| 68 | + |
| 69 | + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray |
| 70 | + with xr.open_dataarray(outgrid) as dataarray: |
| 71 | + result = dataarray.load() |
| 72 | + _ = result.gmt # load GMTDataArray accessor information |
| 73 | + else: |
| 74 | + result = None # if user sets an outgrid, return None |
| 75 | + |
| 76 | + return result |
0 commit comments