|
| 1 | +""" |
| 2 | +sph2grd - Compute grid from spherical harmonic coefficients |
| 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 | + b="binary", |
| 22 | + h="header", |
| 23 | + i="incols", |
| 24 | + r="registration", |
| 25 | + x="cores", |
| 26 | +) |
| 27 | +@kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma") |
| 28 | +def sph2grd(data, **kwargs): |
| 29 | + r""" |
| 30 | + Create spherical grid files in tension of data. |
| 31 | +
|
| 32 | + Reads a spherical harmonics coefficient table with records of L, M, |
| 33 | + C[L,M], S[L,M] and evaluates the spherical harmonic model on the |
| 34 | + specified grid. |
| 35 | +
|
| 36 | + Full option list at :gmt-docs:`sph2grd.html` |
| 37 | +
|
| 38 | + {aliases} |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + data : str or {table-like} |
| 43 | + Pass in data with L, M, C[L,M], S[L,M] values by |
| 44 | + providing a file name to an ASCII data table, a 2D |
| 45 | + {table-classes}. |
| 46 | + outgrid : str or None |
| 47 | + The name of the output netCDF file with extension .nc to store the grid |
| 48 | + in. |
| 49 | + {I} |
| 50 | + {R} |
| 51 | + {V} |
| 52 | + {b} |
| 53 | + {h} |
| 54 | + {i} |
| 55 | + {r} |
| 56 | + {x} |
| 57 | +
|
| 58 | + Returns |
| 59 | + ------- |
| 60 | + ret: xarray.DataArray or None |
| 61 | + Return type depends on whether the ``outgrid`` parameter is set: |
| 62 | +
|
| 63 | + - :class:`xarray.DataArray` if ``outgrid`` is not set |
| 64 | + - None if ``outgrid`` is set (grid output will be stored in file set by |
| 65 | + ``outgrid``) |
| 66 | + """ |
| 67 | + with GMTTempFile(suffix=".nc") as tmpfile: |
| 68 | + with Session() as lib: |
| 69 | + file_context = lib.virtualfile_from_data(check_kind="vector", data=data) |
| 70 | + with file_context as infile: |
| 71 | + if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile |
| 72 | + kwargs.update({"G": tmpfile.name}) |
| 73 | + outgrid = kwargs["G"] |
| 74 | + arg_str = " ".join([infile, build_arg_string(kwargs)]) |
| 75 | + lib.call_module("sph2grd", arg_str) |
| 76 | + |
| 77 | + return load_dataarray(outgrid) if outgrid == tmpfile.name else None |
0 commit comments