-
Notifications
You must be signed in to change notification settings - Fork 228
Allow pandas.DataFrame table and 1D/2D numpy array inputs into pygmt.info #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Non-plot GMT modules. | ||
""" | ||
import numpy as np | ||
import xarray as xr | ||
|
||
from .clib import Session | ||
|
@@ -55,7 +56,7 @@ def grdinfo(grid, **kwargs): | |
|
||
@fmt_docstring | ||
@use_alias(C="per_column", I="spacing", T="nearest_multiple") | ||
def info(fname, **kwargs): | ||
def info(table, **kwargs): | ||
""" | ||
Get information about data tables. | ||
|
||
|
@@ -74,8 +75,9 @@ def info(fname, **kwargs): | |
|
||
Parameters | ||
---------- | ||
fname : str | ||
The file name of the input data table file. | ||
table : pandas.DataFrame or np.ndarray or str | ||
Either a pandas dataframe, a 1D/2D numpy.ndarray or a file name to an | ||
ASCII data table. | ||
per_column : bool | ||
Report the min/max values per column in separate columns. | ||
spacing : str | ||
|
@@ -88,14 +90,25 @@ def info(fname, **kwargs): | |
Report the min/max of the first (0'th) column to the nearest multiple | ||
of dz and output this as the string *-Tzmin/zmax/dz*. | ||
""" | ||
if not isinstance(fname, str): | ||
raise GMTInvalidInput("'info' only accepts file names.") | ||
kind = data_kind(table) | ||
with Session() as lib: | ||
if kind == "file": | ||
file_context = dummy_context(table) | ||
elif kind == "matrix": | ||
_table = np.asanyarray(table) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just FYI, this line converts See also: |
||
if table.ndim == 1: # 1D arrays need to be 2D and transposed | ||
_table = np.transpose(np.atleast_2d(_table)) | ||
file_context = lib.virtualfile_from_matrix(_table) | ||
else: | ||
raise GMTInvalidInput(f"Unrecognized data type: {type(table)}") | ||
|
||
with GMTTempFile() as tmpfile: | ||
arg_str = " ".join([fname, build_arg_string(kwargs), "->" + tmpfile.name]) | ||
with Session() as lib: | ||
lib.call_module("info", arg_str) | ||
return tmpfile.read() | ||
with GMTTempFile() as tmpfile: | ||
with file_context as fname: | ||
arg_str = " ".join( | ||
[fname, build_arg_string(kwargs), "->" + tmpfile.name] | ||
) | ||
lib.call_module("info", arg_str) | ||
return tmpfile.read() | ||
|
||
|
||
@fmt_docstring | ||
|
Uh oh!
There was an error while loading. Please reload this page.