|
| 1 | +""" |
| 2 | +GMT modules for grid operations |
| 3 | +""" |
| 4 | + |
| 5 | +import xarray as xr |
| 6 | + |
| 7 | + |
| 8 | +from .clib import Session |
| 9 | +from .helpers import ( |
| 10 | + build_arg_string, |
| 11 | + fmt_docstring, |
| 12 | + kwargs_to_strings, |
| 13 | + GMTTempFile, |
| 14 | + use_alias, |
| 15 | + data_kind, |
| 16 | + dummy_context, |
| 17 | +) |
| 18 | +from .exceptions import GMTInvalidInput |
| 19 | + |
| 20 | + |
| 21 | +@fmt_docstring |
| 22 | +@use_alias( |
| 23 | + G="outgrid", |
| 24 | + R="region", |
| 25 | + J="projection", |
| 26 | + N="extend", |
| 27 | + S="circ_subregion", |
| 28 | + Z="z_subregion", |
| 29 | +) |
| 30 | +@kwargs_to_strings(R="sequence") |
| 31 | +def grdcut(grid, **kwargs): |
| 32 | + """ |
| 33 | + Extract subregion from a grid. |
| 34 | +
|
| 35 | + Produce a new *outgrid* file which is a subregion of *grid*. The |
| 36 | + subregion is specified with *region*; the specified range must not exceed |
| 37 | + the range of *grid* (but see *extend*). If in doubt, run |
| 38 | + :meth:`pygmt.grdinfo` to check range. Alternatively, define the subregion |
| 39 | + indirectly via a range check on the node values or via distances from a |
| 40 | + given point. Finally, you can give *projection* for oblique projections to |
| 41 | + determine the corresponding rectangular *region* setting that will give a |
| 42 | + grid that fully covers the oblique domain. |
| 43 | +
|
| 44 | + Full option list at :gmt-docs:`grdcut.html` |
| 45 | +
|
| 46 | + {aliases} |
| 47 | +
|
| 48 | + Parameters |
| 49 | + ---------- |
| 50 | + grid : str |
| 51 | + The name of the input grid file. |
| 52 | + outgrid : str or None |
| 53 | + The name of the output netCDF file with extension .nc to store the grid |
| 54 | + in. |
| 55 | + {J} |
| 56 | + {R} |
| 57 | + extend : bool or int or float |
| 58 | + Allow grid to be extended if new *region* exceeds existing boundaries. |
| 59 | + Give a value to initialize nodes outside current region. |
| 60 | + circ_subregion : str |
| 61 | + ``'lon/lat/radius[unit][+n]'``. |
| 62 | + Specify an origin (*lon* and *lat*) and *radius*; append a distance |
| 63 | + *unit* and we determine the corresponding rectangular region so that |
| 64 | + all grid nodes on or inside the circle are contained in the subset. |
| 65 | + If **+n** is appended we set all nodes outside the circle to NaN. |
| 66 | + z_subregion : str |
| 67 | + ``'[min/max][+n|N|r]'``. |
| 68 | + Determine a new rectangular region so that all nodes outside this |
| 69 | + region are also outside the given z-range [-inf/+inf]. To indicate no |
| 70 | + limit on *min* or *max* only, specify a hyphen (-). Normally, any NaNs |
| 71 | + encountered are simply skipped and not considered in the |
| 72 | + range-decision. Append **+n** to consider a NaN to be outside the given |
| 73 | + z-range. This means the new subset will be NaN-free. Alternatively, |
| 74 | + append **+r** to consider NaNs to be within the data range. In this |
| 75 | + case we stop shrinking the boundaries once a NaN is found [Default |
| 76 | + simply skips NaNs when making the range decision]. Finally, if your |
| 77 | + core subset grid is surrounded by rows and/or columns that are all |
| 78 | + NaNs, append **+N** to strip off such columns before (optionally) |
| 79 | + considering the range of the core subset for further reduction of the |
| 80 | + area. |
| 81 | +
|
| 82 | + Returns |
| 83 | + ------- |
| 84 | + ret: xarray.DataArray or None |
| 85 | + Return type depends on whether the *outgrid* parameter is set: |
| 86 | +
|
| 87 | + - xarray.DataArray if *outgrid* is not set |
| 88 | + - None if *outgrid* is set (grid output will be stored in *outgrid*) |
| 89 | + """ |
| 90 | + kind = data_kind(grid) |
| 91 | + |
| 92 | + with GMTTempFile(suffix=".nc") as tmpfile: |
| 93 | + with Session() as lib: |
| 94 | + if kind == "file": |
| 95 | + file_context = dummy_context(grid) |
| 96 | + elif kind == "grid": |
| 97 | + raise NotImplementedError( |
| 98 | + "xarray.DataArray is not supported as the input grid yet!" |
| 99 | + ) |
| 100 | + # file_context = lib.virtualfile_from_grid(grid) |
| 101 | + # See https://github.com/GenericMappingTools/gmt/pull/3532 |
| 102 | + # for a feature request. |
| 103 | + else: |
| 104 | + raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid))) |
| 105 | + |
| 106 | + with file_context as infile: |
| 107 | + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile |
| 108 | + kwargs.update({"G": tmpfile.name}) |
| 109 | + outgrid = kwargs["G"] |
| 110 | + arg_str = " ".join([infile, build_arg_string(kwargs)]) |
| 111 | + lib.call_module("grdcut", arg_str) |
| 112 | + |
| 113 | + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray |
| 114 | + with xr.open_dataarray(outgrid) as dataarray: |
| 115 | + result = dataarray.load() |
| 116 | + else: |
| 117 | + result = None # if user sets an outgrid, return None |
| 118 | + |
| 119 | + return result |
0 commit comments