Skip to content

Commit 14c2903

Browse files
authored
Merge pull request #112 from Lachlan00/castaway
Reader for CastAway CSV format
2 parents b9254f1 + 143cf11 commit 14c2903

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed

ctd/read.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,50 @@ def rosette_summary(fname):
488488
ros["nbf"] = ros["nbf"].astype(int)
489489
ros.set_index("nbf", drop=True, inplace=True, verify_integrity=False)
490490
return ros
491+
492+
493+
def from_castaway_csv(fname):
494+
"""
495+
DataFrame constructor to open CastAway CSV format.
496+
497+
Example
498+
--------
499+
>>> import ctd
500+
>>> cast = ctd.from_castaway_csv('tests/data/castaway_data.csv')
501+
>>> downcast, upcast = cast.split() # Upcast often prefiltered
502+
>>> fig, ax = plt.subplots()
503+
>>> ax = downcast['temperature'].plot_cast()
504+
>>> fig.show()
505+
506+
"""
507+
with open(fname) as file:
508+
f = file.readlines()
509+
510+
# Strip newline characters
511+
f = [s.strip() for s in f]
512+
513+
# Separate meta data and CTD profile
514+
meta = [s for s in f if s[0] == "%"][0:-1]
515+
data = [s.split(",") for s in f if s[0] != "%"]
516+
df = pd.DataFrame(data[1:-1], columns=data[0])
517+
518+
# Convert to numeric
519+
for col in df.columns:
520+
df[col] = pd.to_numeric(df[col])
521+
522+
# Normalise column names and extract units
523+
units = [s[s.find("(") + 1 : s.find(")")] for s in df.columns]
524+
df.columns = [
525+
_normalize_names(s.split("(")[0]).lower().replace(" ", "_") for s in df.columns
526+
]
527+
df.set_index("pressure", drop=True, inplace=True, verify_integrity=False)
528+
529+
# Add metadata
530+
meta = [s.replace("%", "").strip().split(",") for s in meta]
531+
metadata = {}
532+
for line in meta:
533+
metadata[line[0]] = line[1]
534+
metadata["units"] = units
535+
setattr(df, "_metadata", metadata)
536+
537+
return df

0 commit comments

Comments
 (0)