|
| 1 | +""" |
| 2 | +sphinterpolate - Spherical gridding in tension of data on a sphere |
| 3 | +""" |
| 4 | +from pygmt.clib import Session |
| 5 | +from pygmt.helpers import ( |
| 6 | + GMTTempFile, |
| 7 | + build_arg_string, |
| 8 | + fmt_docstring, |
| 9 | + kwargs_to_strings, |
| 10 | + use_alias, |
| 11 | +) |
| 12 | +from pygmt.io import load_dataarray |
| 13 | + |
| 14 | + |
| 15 | +@fmt_docstring |
| 16 | +@use_alias( |
| 17 | + G="outgrid", |
| 18 | + I="spacing", |
| 19 | + R="region", |
| 20 | + V="verbose", |
| 21 | +) |
| 22 | +@kwargs_to_strings(I="sequence", R="sequence") |
| 23 | +def sphinterpolate(data, **kwargs): |
| 24 | + r""" |
| 25 | + Create spherical grid files in tension of data. |
| 26 | +
|
| 27 | + Reads a table containing *lon, lat, z* columns and performs a Delaunay |
| 28 | + triangulation to set up a spherical interpolation in tension. Several |
| 29 | + options may be used to affect the outcome, such as choosing local versus |
| 30 | + global gradient estimation or optimize the tension selection to satisfy one |
| 31 | + of four criteria. |
| 32 | +
|
| 33 | + Full option list at :gmt-docs:`sphinterpolate.html` |
| 34 | +
|
| 35 | + {aliases} |
| 36 | +
|
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + data : str or {table-like} |
| 40 | + Pass in (x, y, z) or (longitude, latitude, elevation) values by |
| 41 | + providing a file name to an ASCII data table, a 2D |
| 42 | + {table-classes}. |
| 43 | + outgrid : str or None |
| 44 | + The name of the output netCDF file with extension .nc to store the grid |
| 45 | + in. |
| 46 | + {I} |
| 47 | + {R} |
| 48 | + {V} |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + ret: xarray.DataArray or None |
| 53 | + Return type depends on whether the ``outgrid`` parameter is set: |
| 54 | +
|
| 55 | + - :class:`xarray.DataArray` if ``outgrid`` is not set |
| 56 | + - None if ``outgrid`` is set (grid output will be stored in file set by |
| 57 | + ``outgrid``) |
| 58 | + """ |
| 59 | + with GMTTempFile(suffix=".nc") as tmpfile: |
| 60 | + with Session() as lib: |
| 61 | + file_context = lib.virtualfile_from_data(check_kind="vector", data=data) |
| 62 | + with file_context as infile: |
| 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 = " ".join([infile, build_arg_string(kwargs)]) |
| 67 | + lib.call_module("sphinterpolate", arg_str) |
| 68 | + |
| 69 | + return load_dataarray(outgrid) if outgrid == tmpfile.name else None |
0 commit comments