Skip to content

Commit 7d665ba

Browse files
EliEli
authored andcommitted
Moved read_th and write_th to th_io.py. Also migrated tests.
1 parent 92da5a7 commit 7d665ba

7 files changed

Lines changed: 598 additions & 483 deletions

File tree

schimpy/merge_th.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from vtools.data.timeseries import datetime_elapsed
77
from schimpy import schism_yaml as syml
8+
from schimpy.th_io import write_th as th_io_write_th
89

910

1011
role_to_sign = {"source_tracer": 1, "source_flux": 1, "sink_flux": -1}
@@ -46,18 +47,34 @@ def read_locations_from_input(fname, role):
4647
# Assume sourc
4748

4849

49-
def write_th(df, fname, elapsed, ref_time=None):
50+
def write_th_merge(df, fname, elapsed, ref_time=None):
51+
"""Write a time-series to a .th file using common th_io formatting."""
5052
print("writing ", fname)
5153
print(f"dataframe dimensions: {df.shape}")
54+
5255
if elapsed:
53-
dfe = datetime_elapsed(df, reftime=ref_time, dtype=float)
54-
dfe.index.name = "time"
55-
with open(fname, mode="w", newline="\n") as outfile:
56-
dfe.to_csv(outfile, sep=" ", float_format="%.3f", header=False)
57-
# outfile.write("#"+str(dfe.columns.values.join(","))+"\n")
56+
th_io_write_th(
57+
df,
58+
fname,
59+
elapsed=True,
60+
ref_time=ref_time,
61+
elapsed_format="%.3f",
62+
float_format="%.3f",
63+
include_header=False,
64+
)
5865
else:
59-
sep = " " if fname.endswith("th") else ","
60-
df.to_csv(fname, sep=sep, float_format="%.3f", date_format="%Y-%m-%dT%H:%M")
66+
th_io_write_th(
67+
df,
68+
fname,
69+
elapsed=False,
70+
time_format="%Y-%m-%dT%H:%M",
71+
float_format="%.3f",
72+
include_header=True,
73+
)
74+
75+
76+
# Backwards compatibility
77+
write_th = write_th_merge
6178

6279

6380
def read_data(fname, variable):

0 commit comments

Comments
 (0)