Skip to content

Commit a21b2df

Browse files
committed
Revert non-related changes and focus on the _GMT_DATASET class
1 parent 9ddca59 commit a21b2df

12 files changed

+260
-292
lines changed

pygmt/helpers/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@
2020
is_nonstr_iter,
2121
launch_external_viewer,
2222
non_ascii_to_octal,
23-
return_table,
2423
)
2524
from pygmt.helpers.validators import validate_output_table_type

pygmt/helpers/utils.py

-46
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import time
1212
import webbrowser
1313
from collections.abc import Iterable
14-
from typing import Literal
1514

1615
import xarray as xr
1716
from pygmt.exceptions import GMTInvalidInput
@@ -556,48 +555,3 @@ def args_in_kwargs(args, kwargs):
556555
return any(
557556
kwargs.get(arg) is not None and kwargs.get(arg) is not False for arg in args
558557
)
559-
560-
561-
def return_table(
562-
session,
563-
output_type: Literal["pandas", "numpy", "file"],
564-
vfile: str,
565-
column_names: list[str] | None = None,
566-
):
567-
"""
568-
Return an output table from a virtual file based on the output type.
569-
570-
Parameters
571-
----------
572-
session : :class:`pygmt.clib.Session`
573-
The current session.
574-
output_type
575-
The output type. Valid values are ``"pandas"``, ``"numpy"``, or ``"file"``.
576-
vfile
577-
The virtual file name.
578-
column_names
579-
The column names for the :class:`pandas.DataFrame` output.
580-
581-
Returns
582-
-------
583-
:class:`pandas.DataFrame` or :class:`numpy.ndarray` or None
584-
The output table. If ``output_type`` is ``"file"``, returns ``None``.
585-
"""
586-
if output_type == "file": # Already written to file, so return None
587-
return None
588-
# Read the virtual file as a GMT dataset and convert to pandas.DataFrame
589-
result = session.read_virtualfile(vfile, kind="dataset").contents.to_dataframe()
590-
# assign column names
591-
if column_names is not None:
592-
result.columns = column_names
593-
# convert text data from object dtype to string dtype
594-
result = result.convert_dtypes(
595-
convert_string=True,
596-
convert_integer=False,
597-
convert_floating=False,
598-
convert_boolean=False,
599-
)
600-
if output_type == "pandas":
601-
return result
602-
# NumPy.ndarray output
603-
return result.to_numpy()

pygmt/src/blockm.py

+32-55
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
import pandas as pd
66
from pygmt.clib import Session
77
from pygmt.helpers import (
8+
GMTTempFile,
89
build_arg_string,
910
fmt_docstring,
1011
kwargs_to_strings,
11-
return_table,
1212
use_alias,
13-
validate_output_table_type,
1413
)
1514

1615
__doctest_skip__ = ["blockmean", "blockmedian", "blockmode"]
1716

1817

19-
def _blockm(block_method, data, x, y, z, output_type, outfile, **kwargs):
18+
def _blockm(block_method, data, x, y, z, outfile, **kwargs):
2019
r"""
2120
Block average (x, y, z) data tables by mean, median, or mode estimation.
2221
@@ -42,26 +41,31 @@ def _blockm(block_method, data, x, y, z, output_type, outfile, **kwargs):
4241
- None if ``outfile`` is set (filtered output will be stored in file
4342
set by ``outfile``)
4443
"""
45-
output_type = validate_output_table_type(output_type, outfile=outfile)
46-
47-
with Session() as lib:
48-
with lib.virtualfile_from_data(
49-
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
50-
) as vintbl, lib.virtualfile_to_data(kind="dataset", fname=outfile) as vouttbl:
51-
lib.call_module(
52-
module=block_method,
53-
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
44+
with GMTTempFile(suffix=".csv") as tmpfile:
45+
with Session() as lib:
46+
table_context = lib.virtualfile_from_data(
47+
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
5448
)
55-
column_names = None
56-
if isinstance(data, pd.DataFrame):
57-
column_names = data.columns.to_list()
58-
59-
return return_table(
60-
session=lib,
61-
output_type=output_type,
62-
vfile=vouttbl,
63-
column_names=column_names,
64-
)
49+
# Run blockm* on data table
50+
with table_context as infile:
51+
if outfile is None:
52+
outfile = tmpfile.name
53+
lib.call_module(
54+
module=block_method,
55+
args=build_arg_string(kwargs, infile=infile, outfile=outfile),
56+
)
57+
58+
# Read temporary csv output to a pandas table
59+
if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame
60+
try:
61+
column_names = data.columns.to_list()
62+
result = pd.read_csv(tmpfile.name, sep="\t", names=column_names)
63+
except AttributeError: # 'str' object has no attribute 'columns'
64+
result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">")
65+
elif outfile != tmpfile.name: # return None if outfile set, output in outfile
66+
result = None
67+
68+
return result
6569

6670

6771
@fmt_docstring
@@ -82,9 +86,7 @@ def _blockm(block_method, data, x, y, z, output_type, outfile, **kwargs):
8286
w="wrap",
8387
)
8488
@kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma", o="sequence_comma")
85-
def blockmean(
86-
data=None, x=None, y=None, z=None, output_type="pandas", outfile=None, **kwargs
87-
):
89+
def blockmean(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
8890
r"""
8991
Block average (x, y, z) data tables by mean estimation.
9092
@@ -157,14 +159,7 @@ def blockmean(
157159
>>> data_bmean = pygmt.blockmean(data=data, region=[245, 255, 20, 30], spacing="5m")
158160
"""
159161
return _blockm(
160-
block_method="blockmean",
161-
data=data,
162-
x=x,
163-
y=y,
164-
z=z,
165-
output_type=output_type,
166-
outfile=outfile,
167-
**kwargs,
162+
block_method="blockmean", data=data, x=x, y=y, z=z, outfile=outfile, **kwargs
168163
)
169164

170165

@@ -185,9 +180,7 @@ def blockmean(
185180
w="wrap",
186181
)
187182
@kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma", o="sequence_comma")
188-
def blockmedian(
189-
data=None, x=None, y=None, z=None, output_type="pandas", outfile=None, **kwargs
190-
):
183+
def blockmedian(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
191184
r"""
192185
Block average (x, y, z) data tables by median estimation.
193186
@@ -253,14 +246,7 @@ def blockmedian(
253246
... )
254247
"""
255248
return _blockm(
256-
block_method="blockmedian",
257-
data=data,
258-
x=x,
259-
y=y,
260-
z=z,
261-
output_type=output_type,
262-
outfile=outfile,
263-
**kwargs,
249+
block_method="blockmedian", data=data, x=x, y=y, z=z, outfile=outfile, **kwargs
264250
)
265251

266252

@@ -281,9 +267,7 @@ def blockmedian(
281267
w="wrap",
282268
)
283269
@kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma", o="sequence_comma")
284-
def blockmode(
285-
data=None, x=None, y=None, z=None, output_type="pandas", outfile=None, **kwargs
286-
):
270+
def blockmode(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
287271
r"""
288272
Block average (x, y, z) data tables by mode estimation.
289273
@@ -347,12 +331,5 @@ def blockmode(
347331
>>> data_bmode = pygmt.blockmode(data=data, region=[245, 255, 20, 30], spacing="5m")
348332
"""
349333
return _blockm(
350-
block_method="blockmode",
351-
data=data,
352-
x=x,
353-
y=y,
354-
z=z,
355-
output_type=output_type,
356-
outfile=outfile,
357-
**kwargs,
334+
block_method="blockmode", data=data, x=x, y=y, z=z, outfile=outfile, **kwargs
358335
)

pygmt/src/filter1d.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
filter1d - Time domain filtering of 1-D data tables
33
"""
44

5+
import pandas as pd
56
from pygmt.clib import Session
67
from pygmt.exceptions import GMTInvalidInput
78
from pygmt.helpers import (
9+
GMTTempFile,
810
build_arg_string,
911
fmt_docstring,
10-
return_table,
1112
use_alias,
1213
validate_output_table_type,
1314
)
@@ -116,13 +117,23 @@ def filter1d(data, output_type="pandas", outfile=None, **kwargs):
116117

117118
output_type = validate_output_table_type(output_type, outfile=outfile)
118119

119-
with Session() as lib:
120-
with lib.virtualfile_from_data(
121-
check_kind="vector", data=data
122-
) as vintbl, lib.virtualfile_to_data(kind="dataset", fname=outfile) as vouttbl:
123-
lib.call_module(
124-
module="filter1d",
125-
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
126-
)
127-
128-
return return_table(session=lib, output_type=output_type, vfile=vouttbl)
120+
with GMTTempFile() as tmpfile:
121+
with Session() as lib:
122+
file_context = lib.virtualfile_from_data(check_kind="vector", data=data)
123+
with file_context as infile:
124+
if outfile is None:
125+
outfile = tmpfile.name
126+
lib.call_module(
127+
module="filter1d",
128+
args=build_arg_string(kwargs, infile=infile, outfile=outfile),
129+
)
130+
131+
# Read temporary csv output to a pandas table
132+
if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame
133+
result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">")
134+
elif outfile != tmpfile.name: # return None if outfile set, output in outfile
135+
result = None
136+
137+
if output_type == "numpy":
138+
result = result.to_numpy()
139+
return result

pygmt/src/grd2xyz.py

+26-18
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
grd2xyz - Convert grid to data table
33
"""
44

5+
import pandas as pd
56
import xarray as xr
67
from pygmt.clib import Session
78
from pygmt.exceptions import GMTInvalidInput
89
from pygmt.helpers import (
10+
GMTTempFile,
911
build_arg_string,
1012
fmt_docstring,
1113
kwargs_to_strings,
12-
return_table,
1314
use_alias,
1415
validate_output_table_type,
1516
)
@@ -149,24 +150,31 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs):
149150
)
150151

151152
# Set the default column names for the pandas dataframe header
152-
column_names = ["x", "y", "z"]
153+
dataframe_header = ["x", "y", "z"]
153154
# Let output pandas column names match input DataArray dimension names
154-
if isinstance(grid, xr.DataArray):
155+
if isinstance(grid, xr.DataArray) and output_type == "pandas":
155156
# Reverse the dims because it is rows, columns ordered.
156-
column_names = [grid.dims[1], grid.dims[0], grid.name]
157-
158-
with Session() as lib:
159-
with lib.virtualfile_from_data(
160-
check_kind="raster", data=grid
161-
) as vingrd, lib.virtualfile_to_data(kind="dataset", fname=outfile) as vouttbl:
162-
lib.call_module(
163-
module="grd2xyz",
164-
args=build_arg_string(kwargs, infile=vingrd, outfile=vouttbl),
157+
dataframe_header = [grid.dims[1], grid.dims[0], grid.name]
158+
159+
with GMTTempFile() as tmpfile:
160+
with Session() as lib:
161+
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
162+
with file_context as infile:
163+
if outfile is None:
164+
outfile = tmpfile.name
165+
lib.call_module(
166+
module="grd2xyz",
167+
args=build_arg_string(kwargs, infile=infile, outfile=outfile),
168+
)
169+
170+
# Read temporary csv output to a pandas table
171+
if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame
172+
result = pd.read_csv(
173+
tmpfile.name, sep="\t", names=dataframe_header, comment=">"
165174
)
175+
elif outfile != tmpfile.name: # return None if outfile set, output in outfile
176+
result = None
166177

167-
return return_table(
168-
session=lib,
169-
output_type=output_type,
170-
vfile=vouttbl,
171-
column_names=column_names,
172-
)
178+
if output_type == "numpy":
179+
result = result.to_numpy()
180+
return result

0 commit comments

Comments
 (0)