|
| 1 | +""" |
| 2 | +grdsample - Resample a grid onto a new lattice |
| 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 | + G="outgrid", |
| 19 | + J="projection", |
| 20 | + I="spacing", |
| 21 | + R="region", |
| 22 | + T="translate", |
| 23 | + V="verbose", |
| 24 | + f="coltypes", |
| 25 | + n="interpolation", |
| 26 | + r="registration", |
| 27 | + x="cores", |
| 28 | +) |
| 29 | +@kwargs_to_strings(I="sequence", R="sequence") |
| 30 | +def grdsample(grid, **kwargs): |
| 31 | + r""" |
| 32 | + Change the registration, spacing, or nodes in a grid file. |
| 33 | +
|
| 34 | + This reads a grid file and interpolates it to create a new grid |
| 35 | + file. It can change the registration with ``translate`` or |
| 36 | + ``registration``, change the grid-spacing or number of nodes with |
| 37 | + ``spacing``, and set a new sub-region using ``region``. A bicubic |
| 38 | + [Default], bilinear, B-spline or nearest-neighbor interpolation is set |
| 39 | + with ``interpolation``. |
| 40 | +
|
| 41 | + When ``region`` is omitted, the output grid will cover the same region as |
| 42 | + the input grid. When ``spacing`` is omitted, the grid spacing of the |
| 43 | + output grid will be the same as the input grid. Either ``registration`` or |
| 44 | + ``translate`` can be used to change the grid registration. When omitted, |
| 45 | + the output grid will have the same registration as the input grid. |
| 46 | +
|
| 47 | + {aliases} |
| 48 | +
|
| 49 | + Parameters |
| 50 | + ---------- |
| 51 | + grid : str or xarray.DataArray |
| 52 | + The file name of the input grid or the grid loaded as a DataArray. |
| 53 | + outgrid : str or None |
| 54 | + The name of the output netCDF file with extension .nc to store the grid |
| 55 | + in. |
| 56 | + {I} |
| 57 | + {R} |
| 58 | + translate : bool |
| 59 | + Translate between grid and pixel registration; if the input is |
| 60 | + grid-registered, the output will be pixel-registered and vice-versa. |
| 61 | + registration : str or bool |
| 62 | + [**g**\ |\ **p**\ ]. |
| 63 | + Set registration to **g**\ ridline or **p**\ ixel. |
| 64 | + {V} |
| 65 | + {f} |
| 66 | + {n} |
| 67 | + {x} |
| 68 | +
|
| 69 | + Returns |
| 70 | + ------- |
| 71 | + ret: xarray.DataArray or None |
| 72 | + Return type depends on whether the ``outgrid`` parameter is set: |
| 73 | +
|
| 74 | + - :class:`xarray.DataArray` if ``outgrid`` is not set |
| 75 | + - None if ``outgrid`` is set (grid output will be stored in file set by |
| 76 | + ``outgrid``) |
| 77 | + """ |
| 78 | + with GMTTempFile(suffix=".nc") as tmpfile: |
| 79 | + with Session() as lib: |
| 80 | + file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) |
| 81 | + with file_context as infile: |
| 82 | + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile |
| 83 | + kwargs.update({"G": tmpfile.name}) |
| 84 | + outgrid = kwargs["G"] |
| 85 | + arg_str = " ".join([infile, build_arg_string(kwargs)]) |
| 86 | + lib.call_module("grdsample", arg_str) |
| 87 | + |
| 88 | + if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray |
| 89 | + with xr.open_dataarray(outgrid) as dataarray: |
| 90 | + result = dataarray.load() |
| 91 | + _ = result.gmt # load GMTDataArray accessor information |
| 92 | + else: |
| 93 | + result = None # if user sets an outgrid, return None |
| 94 | + |
| 95 | + return result |
0 commit comments