Skip to content

Commit 05a3f23

Browse files
committed
Sensible array outputs for pygmt info
When either of per_column (C), spacing (I), or nearest_multiple (T) are used in `pygmt.info`, output the result as a numpy.ndarray which would be more usable that a raw string that is meant for the command line world. Also improve the docstring of `pygmt.info` to mention that numpy.ndarray outputs are being reported.
1 parent 147dac9 commit 05a3f23

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

pygmt/modules.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""
22
Non-plot GMT modules.
33
"""
4+
import re
5+
6+
import numpy as np
47
import xarray as xr
58

69
from .clib import Session
@@ -59,14 +62,17 @@ def info(table, **kwargs):
5962
"""
6063
Get information about data tables.
6164
62-
Reads from files and finds the extreme values in each of the columns.
63-
It recognizes NaNs and will print warnings if the number of columns vary
64-
from record to record. As an option, it will find the extent of the first
65-
n columns rounded up and down to the nearest multiple of the supplied
66-
increments. By default, this output will be in the form *-Rw/e/s/n*,
67-
or the output will be in column form for as many columns as there are
68-
increments provided. The *nearest_multiple* option will provide a
69-
*-Tzmin/zmax/dz* string for makecpt.
65+
Reads from files and finds the extreme values in each of the columns
66+
reported as min/max pairs. It recognizes NaNs and will print warnings if
67+
the number of columns vary from record to record. As an option, it will
68+
find the extent of the first two columns rounded up and down to the nearest
69+
multiple of the supplied increments given by *spacing*. Such output will be
70+
in a numpy.ndarray form ``[w, e, s, n]``, which can be used directly as the
71+
*region* argument for other modules (hence only dx and dy are needed). If
72+
the *per_column* option is combined with *spacing*, then the numpy.ndarray
73+
output will be rounded up/down for as many columns as there are increments
74+
provided in *spacing*. A similar option *nearest_multiple* option will
75+
provide a numpy.ndarray in the form of ``[zmin, zmax, dz]`` for makecpt.
7076
7177
Full option list at :gmt-docs:`gmtinfo.html`
7278
@@ -81,12 +87,21 @@ def info(table, **kwargs):
8187
spacing : str
8288
``'[b|p|f|s]dx[/dy[/dz...]]'``.
8389
Report the min/max of the first n columns to the nearest multiple of
84-
the provided increments and output results in the form *-Rw/e/s/n*
85-
(unless *per_column* is set).
90+
the provided increments and output results in the form
91+
``[w, e, s, n]``.
8692
nearest_multiple : str
8793
``'dz[+ccol]'``
8894
Report the min/max of the first (0'th) column to the nearest multiple
89-
of dz and output this as the string *-Tzmin/zmax/dz*.
95+
of dz and output this in the form ``[zmin, zmax, dz]``.
96+
97+
Returns
98+
-------
99+
output : np.ndarray or str
100+
Return type depends on whether any of the 'per_column', 'spacing', or
101+
'nearest_multiple' parameters are set.
102+
103+
- np.ndarray if either of the above parameters are used.
104+
- str if none of the above parameters are used.
90105
"""
91106
kind = data_kind(table)
92107
with Session() as lib:
@@ -105,7 +120,16 @@ def info(table, **kwargs):
105120
[fname, build_arg_string(kwargs), "->" + tmpfile.name]
106121
)
107122
lib.call_module("info", arg_str)
108-
return tmpfile.read()
123+
result = tmpfile.read()
124+
125+
if any(arg in kwargs for arg in ["C", "I", "T"]):
126+
# Converts certain output types into a numpy array
127+
# instead of a raw string that is less useful.
128+
result = np.loadtxt(
129+
re.sub(pattern="-R|-T|/", repl=" ", string=result).splitlines()
130+
)
131+
132+
return result
109133

110134

111135
@fmt_docstring

pygmt/tests/test_info.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55

66
import numpy as np
7+
import numpy.testing as npt
78
import pandas as pd
89
import pytest
910
import xarray as xr
@@ -40,25 +41,27 @@ def test_info_dataframe():
4041
def test_info_per_column():
4142
"Make sure the per_column option works"
4243
output = info(table=POINTS_DATA, per_column=True)
43-
assert output == "11.5309 61.7074 -2.9289 7.8648 0.1412 0.9338\n"
44+
npt.assert_allclose(
45+
actual=output, desired=[11.5309, 61.7074, -2.9289, 7.8648, 0.1412, 0.9338]
46+
)
4447

4548

4649
def test_info_spacing():
4750
"Make sure the spacing option works"
4851
output = info(table=POINTS_DATA, spacing=0.1)
49-
assert output == "-R11.5/61.8/-3/7.9\n"
52+
npt.assert_allclose(actual=output, desired=[11.5, 61.8, -3, 7.9])
5053

5154

5255
def test_info_per_column_spacing():
5356
"Make sure the per_column and spacing options work together"
5457
output = info(table=POINTS_DATA, per_column=True, spacing=0.1)
55-
assert output == "11.5 61.8 -3 7.9 0.1412 0.9338\n"
58+
npt.assert_allclose(actual=output, desired=[11.5, 61.8, -3, 7.9, 0.1412, 0.9338])
5659

5760

5861
def test_info_nearest_multiple():
5962
"Make sure the nearest_multiple option works"
6063
output = info(table=POINTS_DATA, nearest_multiple=0.1)
61-
assert output == "-T11.5/61.8/0.1\n"
64+
npt.assert_allclose(actual=output, desired=[11.5, 61.8, 0.1])
6265

6366

6467
def test_info_fails():

0 commit comments

Comments
 (0)