Skip to content

Commit

Permalink
Use tempfile_from_dftrack instead of tempfile_from_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
weiji14 committed Sep 9, 2020
1 parent 2c41ca4 commit 0f4d5d7
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions pygmt/x2sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
GMT supplementary X2SYS module for crossover analysis.
"""
import contextlib
import io
import os
import re

Expand All @@ -17,11 +16,27 @@
dummy_context,
fmt_docstring,
kwargs_to_strings,
tempfile_from_buffer,
use_alias,
)


@contextlib.contextmanager
def tempfile_from_dftrack(track, suffix):
"""
Saves a pandas.DataFrame track table to a temporary tab-separated ASCII
text file with a suffix (e.g. 'xyz').
"""
with GMTTempFile(suffix=suffix) as tmpfile:
track.to_csv(
path_or_buf=tmpfile.name,
sep="\t",
index=False,
date_format="%Y-%m-%dT%H:%M:%S.%fZ",
)

yield tmpfile.name


@fmt_docstring
@use_alias(
D="fmtfile",
Expand Down Expand Up @@ -284,14 +299,7 @@ def x2sys_cross(tracks=None, outfile=None, **kwargs):
suffix = kwargs["T"] # tempfile suffix will be same as TAG name

# Save pandas DataFrame data to io.StringIO stream buffer
buf = io.StringIO()
track.to_csv(
path_or_buf=buf,
sep="\t",
index=False,
date_format="%Y-%m-%dT%H:%M:%S.%fZ",
)
file_contexts.append(tempfile_from_buffer(buf=buf, suffix=f".{suffix}"))
file_contexts.append(tempfile_from_dftrack(track=track, suffix=suffix))
else:
raise GMTInvalidInput(f"Unrecognized data type: {type(track)}")

Expand Down

0 comments on commit 0f4d5d7

Please sign in to comment.