Skip to content

Commit 628a458

Browse files
authored
Merge pull request #146 from upsonp/DFO_issues
added stream support to from_btl for issue #142
2 parents ca4f993 + ea03856 commit 628a458

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

ctd/read.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ def _open_compressed(fname):
5959

6060

6161
def _read_file(fname):
62-
"""Read file contents."""
62+
"""Read file contents, or read from StringIO object."""
63+
if isinstance(fname, StringIO):
64+
fname.seek(0)
65+
text = fname.read()
66+
return StringIO(text)
67+
6368
if not isinstance(fname, Path):
6469
fname = Path(fname).resolve()
6570

@@ -102,6 +107,7 @@ def _parse_seabird(lines, ftype):
102107
"""Parse searbird formats."""
103108
# Initialize variables.
104109
lon = lat = time = None, None, None
110+
fname = None
105111
skiprows = 0
106112

107113
metadata = {}
@@ -119,6 +125,9 @@ def _parse_seabird(lines, ftype):
119125
# Seabird headers starts with *.
120126
if line.startswith("*"):
121127
header.append(line)
128+
if "FileName" in line:
129+
file_path = line.split("=")[-1].strip()
130+
fname = Path(file_path).stem
122131

123132
# Seabird configuration starts with #.
124133
if line.startswith("#"):
@@ -172,6 +181,7 @@ def _parse_seabird(lines, ftype):
172181
names.append("Statistic")
173182
metadata.update(
174183
{
184+
"name": fname if fname else "unknown",
175185
"header": "\n".join(header),
176186
"config": "\n".join(config),
177187
"names": _remane_duplicate_columns(names),
@@ -261,7 +271,9 @@ def from_btl(fname):
261271

262272
df["Statistic"] = df["Statistic"].str.replace(r"\(|\)", "") # (avg) to avg
263273

264-
name = _basename(fname)[1]
274+
if "name" not in metadata:
275+
name = _basename(fname)[1]
276+
metadata["name"] = str(name)
265277

266278
dtypes = {
267279
"bpos": int,
@@ -282,7 +294,6 @@ def from_btl(fname):
282294
warnings.warn("Could not convert %s to float." % column)
283295

284296
df["Date"] = pd.to_datetime(df["Date"])
285-
metadata["name"] = str(name)
286297
setattr(df, "_metadata", metadata)
287298
return df
288299

tests/test_read.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ def btl():
5454

5555

5656
@pytest.fixture
57-
def btl_duplicate_header_name():
58-
yield ctd.from_btl(data_path.joinpath("btl", "alt_bottletest.BTL"))
57+
def btl_as_stream():
58+
file = open(mode="rb", file=data_path.joinpath("btl", "alt_bottletest.BTL"))
59+
stream = StringIO(file.read().decode("cp1252"))
60+
yield ctd.from_btl(stream)
5961

6062

6163
@pytest.fixture
@@ -83,10 +85,13 @@ def test_btl_is_dataframe(btl):
8385
assert not btl.empty
8486

8587

86-
def test_btl_with_dup_cols(btl_duplicate_header_name):
87-
assert all(
88-
col in btl_duplicate_header_name.columns for col in ["Bottle", "Bottle_"]
89-
)
88+
def test_btl_with_dup_cols(btl_as_stream):
89+
assert all(col in btl_as_stream.columns for col in ["Bottle", "Bottle_"])
90+
91+
92+
def test_btl_as_stringio(btl_as_stream):
93+
assert isinstance(btl_as_stream, pd.DataFrame)
94+
assert not btl_as_stream.empty
9095

9196

9297
def test_ros_is_dataframe(ros):

0 commit comments

Comments
 (0)