Skip to content

Commit 0f4d5d7

Browse files
committed
Use tempfile_from_dftrack instead of tempfile_from_buffer
1 parent 2c41ca4 commit 0f4d5d7

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

pygmt/x2sys.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
GMT supplementary X2SYS module for crossover analysis.
33
"""
44
import contextlib
5-
import io
65
import os
76
import re
87

@@ -17,11 +16,27 @@
1716
dummy_context,
1817
fmt_docstring,
1918
kwargs_to_strings,
20-
tempfile_from_buffer,
2119
use_alias,
2220
)
2321

2422

23+
@contextlib.contextmanager
24+
def tempfile_from_dftrack(track, suffix):
25+
"""
26+
Saves a pandas.DataFrame track table to a temporary tab-separated ASCII
27+
text file with a suffix (e.g. 'xyz').
28+
"""
29+
with GMTTempFile(suffix=suffix) as tmpfile:
30+
track.to_csv(
31+
path_or_buf=tmpfile.name,
32+
sep="\t",
33+
index=False,
34+
date_format="%Y-%m-%dT%H:%M:%S.%fZ",
35+
)
36+
37+
yield tmpfile.name
38+
39+
2540
@fmt_docstring
2641
@use_alias(
2742
D="fmtfile",
@@ -284,14 +299,7 @@ def x2sys_cross(tracks=None, outfile=None, **kwargs):
284299
suffix = kwargs["T"] # tempfile suffix will be same as TAG name
285300

286301
# Save pandas DataFrame data to io.StringIO stream buffer
287-
buf = io.StringIO()
288-
track.to_csv(
289-
path_or_buf=buf,
290-
sep="\t",
291-
index=False,
292-
date_format="%Y-%m-%dT%H:%M:%S.%fZ",
293-
)
294-
file_contexts.append(tempfile_from_buffer(buf=buf, suffix=f".{suffix}"))
302+
file_contexts.append(tempfile_from_dftrack(track=track, suffix=suffix))
295303
else:
296304
raise GMTInvalidInput(f"Unrecognized data type: {type(track)}")
297305

0 commit comments

Comments
 (0)