|
| 1 | +""" |
| 2 | +grdfill - Fill blank areas from a grid. |
| 3 | +""" |
| 4 | + |
| 5 | +import xarray as xr |
| 6 | +from pygmt.clib import Session |
| 7 | +from pygmt.helpers import ( |
| 8 | + GMTTempFile, |
| 9 | + build_arg_string, |
| 10 | + fmt_docstring, |
| 11 | + kwargs_to_strings, |
| 12 | + use_alias, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@fmt_docstring |
| 17 | +@use_alias( |
| 18 | + A="mode", |
| 19 | + G="outgrid", |
| 20 | + R="region", |
| 21 | +) |
| 22 | +@kwargs_to_strings(R="sequence") |
| 23 | +def grdfill(grid, **kwargs): |
| 24 | + r""" |
| 25 | + Fill blank areas from a grid file. |
| 26 | +
|
| 27 | + Read a grid that presumably has unfilled holes that the user |
| 28 | + wants to fill in some fashion. Holes are identified by NaN values but |
| 29 | + this criteria can be changed. There are several different algorithms that |
| 30 | + can be used to replace the hole values. |
| 31 | +
|
| 32 | + Full option list at :gmt-docs:`grdfill.html` |
| 33 | +
|
| 34 | + {aliases} |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + grid : str or xarray.DataArray |
| 39 | + The file name of the input grid or the grid loaded as a DataArray. |
| 40 | + outgrid : str or None |
| 41 | + The name of the output netCDF file with extension .nc to store the grid |
| 42 | + in. |
| 43 | + mode : str |
| 44 | + Specify the hole-filling algorithm to use. Choose from **c** for |
| 45 | + constant fill and append the constant value, **n** for nearest |
| 46 | + neighbor (and optionally append a search radius in |
| 47 | + pixels [default radius is :math:`r^2 = \sqrt{{ X^2 + Y^2 }}`, |
| 48 | + where (*X,Y*) are the node dimensions of the grid]). |
| 49 | + {R} |
| 50 | +
|
| 51 | + Returns |
| 52 | + ------- |
| 53 | + ret: xarray.DataArray or None |
| 54 | + Return type depends on whether the ``outgrid`` parameter is set: |
| 55 | +
|
| 56 | + - :class:`xarray.DataArray` if ``outgrid`` is not set |
| 57 | + - None if ``outgrid`` is set (grid output will be stored in file set by |
| 58 | + ``outgrid``) |
| 59 | + """ |
| 60 | + with GMTTempFile(suffix=".nc") as tmpfile: |
| 61 | + with Session() as lib: |
| 62 | + file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) |
| 63 | + with file_context as infile: |
| 64 | + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile |
| 65 | + kwargs.update({"G": tmpfile.name}) |
| 66 | + outgrid = kwargs["G"] |
| 67 | + arg_str = " ".join([infile, build_arg_string(kwargs)]) |
| 68 | + lib.call_module("grdfill", arg_str) |
| 69 | + |
| 70 | + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray |
| 71 | + with xr.open_dataarray(outgrid) as dataarray: |
| 72 | + result = dataarray.load() |
| 73 | + _ = result.gmt # load GMTDataArray accessor information |
| 74 | + else: |
| 75 | + result = None # if user sets an outgrid, return None |
| 76 | + |
| 77 | + return result |
0 commit comments