Skip to content

Commit c6b5621

Browse files
authored
Sensible array outputs for pygmt info (#575)
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. * Check that info outputs 2D np.ndarray using spacing="b" for bounding box * Use str.startswith instead of regex to handle -R and -T info outputs
1 parent 89857c3 commit c6b5621

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

pygmt/modules.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,17 @@ def info(table, **kwargs):
6060
"""
6161
Get information about data tables.
6262
63-
Reads from files and finds the extreme values in each of the columns.
64-
It recognizes NaNs and will print warnings if the number of columns vary
65-
from record to record. As an option, it will find the extent of the first
66-
n columns rounded up and down to the nearest multiple of the supplied
67-
increments. By default, this output will be in the form *-Rw/e/s/n*,
68-
or the output will be in column form for as many columns as there are
69-
increments provided. The *nearest_multiple* option will provide a
70-
*-Tzmin/zmax/dz* string for makecpt.
63+
Reads from files and finds the extreme values in each of the columns
64+
reported as min/max pairs. It recognizes NaNs and will print warnings if
65+
the number of columns vary from record to record. As an option, it will
66+
find the extent of the first two columns rounded up and down to the nearest
67+
multiple of the supplied increments given by *spacing*. Such output will be
68+
in a numpy.ndarray form ``[w, e, s, n]``, which can be used directly as the
69+
*region* argument for other modules (hence only dx and dy are needed). If
70+
the *per_column* option is combined with *spacing*, then the numpy.ndarray
71+
output will be rounded up/down for as many columns as there are increments
72+
provided in *spacing*. A similar option *nearest_multiple* option will
73+
provide a numpy.ndarray in the form of ``[zmin, zmax, dz]`` for makecpt.
7174
7275
Full option list at :gmt-docs:`gmtinfo.html`
7376
@@ -83,12 +86,21 @@ def info(table, **kwargs):
8386
spacing : str
8487
``'[b|p|f|s]dx[/dy[/dz...]]'``.
8588
Report the min/max of the first n columns to the nearest multiple of
86-
the provided increments and output results in the form *-Rw/e/s/n*
87-
(unless *per_column* is set).
89+
the provided increments and output results in the form
90+
``[w, e, s, n]``.
8891
nearest_multiple : str
8992
``'dz[+ccol]'``
9093
Report the min/max of the first (0'th) column to the nearest multiple
91-
of dz and output this as the string *-Tzmin/zmax/dz*.
94+
of dz and output this in the form ``[zmin, zmax, dz]``.
95+
96+
Returns
97+
-------
98+
output : np.ndarray or str
99+
Return type depends on whether any of the 'per_column', 'spacing', or
100+
'nearest_multiple' parameters are set.
101+
102+
- np.ndarray if either of the above parameters are used.
103+
- str if none of the above parameters are used.
92104
"""
93105
kind = data_kind(table)
94106
with Session() as lib:
@@ -108,7 +120,16 @@ def info(table, **kwargs):
108120
[fname, build_arg_string(kwargs), "->" + tmpfile.name]
109121
)
110122
lib.call_module("info", arg_str)
111-
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+
if result.startswith(("-R", "-T")): # e.g. -R0/1/2/3 or -T0/9/1
129+
result = result[2:].replace("/", " ")
130+
result = np.loadtxt(result.splitlines())
131+
132+
return result
112133

113134

114135
@fmt_docstring

pygmt/tests/test_info.py

Lines changed: 22 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
@@ -57,25 +58,42 @@ def test_info_1d_array():
5758
def test_info_per_column():
5859
"Make sure the per_column option works"
5960
output = info(table=POINTS_DATA, per_column=True)
60-
assert output == "11.5309 61.7074 -2.9289 7.8648 0.1412 0.9338\n"
61+
npt.assert_allclose(
62+
actual=output, desired=[11.5309, 61.7074, -2.9289, 7.8648, 0.1412, 0.9338]
63+
)
6164

6265

6366
def test_info_spacing():
6467
"Make sure the spacing option works"
6568
output = info(table=POINTS_DATA, spacing=0.1)
66-
assert output == "-R11.5/61.8/-3/7.9\n"
69+
npt.assert_allclose(actual=output, desired=[11.5, 61.8, -3, 7.9])
70+
71+
72+
def test_info_spacing_bounding_box():
73+
"Make sure the spacing option for writing a bounding box works"
74+
output = info(table=POINTS_DATA, spacing="b")
75+
npt.assert_allclose(
76+
actual=output,
77+
desired=[
78+
[11.5309, -2.9289],
79+
[61.7074, -2.9289],
80+
[61.7074, 7.8648],
81+
[11.5309, 7.8648],
82+
[11.5309, -2.9289],
83+
],
84+
)
6785

6886

6987
def test_info_per_column_spacing():
7088
"Make sure the per_column and spacing options work together"
7189
output = info(table=POINTS_DATA, per_column=True, spacing=0.1)
72-
assert output == "11.5 61.8 -3 7.9 0.1412 0.9338\n"
90+
npt.assert_allclose(actual=output, desired=[11.5, 61.8, -3, 7.9, 0.1412, 0.9338])
7391

7492

7593
def test_info_nearest_multiple():
7694
"Make sure the nearest_multiple option works"
7795
output = info(table=POINTS_DATA, nearest_multiple=0.1)
78-
assert output == "-T11.5/61.8/0.1\n"
96+
npt.assert_allclose(actual=output, desired=[11.5, 61.8, 0.1])
7997

8098

8199
def test_info_fails():

0 commit comments

Comments
 (0)