Skip to content

Commit 1046de1

Browse files
committed
feat: add csv_to_dss command to convert CSV files to DSS format
1 parent 9c7d016 commit 1046de1

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

pydsm/cli.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,38 @@ def pretty_print_input(input_file, output_file=None):
265265
pretty_print(output_file, tables[table], tableName=table, append=append)
266266
append = True
267267

268+
@click.command()
269+
@click.argument("csv_file", type=click.Path(exists=True))
270+
@click.argument("dss_file", type=click.Path(exists=False))
271+
@click.option("--index_col", default=0, help="Column to use as index")
272+
@click.option("--bpart", default="F", help="B part of the DSS path")
273+
@click.option("--fpart", default="F", help="F part of the DSS path")
274+
@click.option("--unit", default="UNK", help="Unit of the data")
275+
@click.option(
276+
"--period_type", default="INST-VAL", help="Period type for the time series"
277+
)
278+
@click.option(
279+
"--multiplier",
280+
default=1.0,
281+
type=float,
282+
help="Multiplier to apply to the data values",
283+
)
284+
def csv_to_dss(
285+
csv_file,
286+
dss_file,
287+
index_col=0,
288+
bpart="F",
289+
fpart="F",
290+
unit="UNK",
291+
period_type="INST-VAL",
292+
multiplier=1.0,
293+
):
294+
"""
295+
Convert a CSV file to a DSS file.
296+
"""
297+
dssutils.csv_to_dss(
298+
csv_file, dss_file, index_col, bpart, fpart, unit, period_type, multiplier
299+
)
268300

269301
# Add the commands to the group repeating
270302
repeating.add_command(create_repeating)
@@ -274,6 +306,7 @@ def pretty_print_input(input_file, output_file=None):
274306
main.add_command(extract_dss)
275307
main.add_command(compare_dss)
276308
main.add_command(copy_all_dss)
309+
main.add_command(csv_to_dss)
277310
main.add_command(slice_hydro)
278311
main.add_command(update_hydro_tidefile_with_inp)
279312
main.add_command(create_dsm2_input_for_cd)

pydsm/dssutils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from vtools.functions import filter
66
import pandas as pd
77
import numpy as np
8+
import tqdm
89

910
from pydsm.hydro_slicer import slice_hydro
1011
from pydsm.postpro import load_location_file, load_location_table
@@ -261,3 +262,37 @@ def do_catalog_with_lock(dssfile):
261262
'''
262263
with pyhecdss.DSSFile(dssfile) as dssh:
263264
dssh.do_catalog()
265+
266+
267+
def csv_to_dss(
268+
csv_file,
269+
dss_file,
270+
index_col=0,
271+
apart="A",
272+
cpart="C",
273+
fpart="F",
274+
unit="UNK",
275+
period_type="INST-VAL",
276+
multiplier=1.0,
277+
):
278+
df = pd.read_csv(csv_file, index_col=index_col, parse_dates=True)
279+
df = df * multiplier
280+
freq = pd.infer_freq(df.index)
281+
if freq is not None:
282+
df = df.asfreq(freq)
283+
else:
284+
print("Cannot infer frequency from the index.")
285+
print(df.head())
286+
raise ValueError(
287+
"Unable to infer frequency from the index. See file " + csv_file
288+
)
289+
for c in df.columns:
290+
with pyhecdss.DSSFile(dss_file, create_new=True) as f:
291+
for c in tqdm.tqdm(df.columns):
292+
bpart = c
293+
ts = df[c]
294+
epart = ts.index.freqstr
295+
pathname = f"/{apart}/{bpart}/{cpart}///{fpart}/"
296+
print("Writing to ", pathname)
297+
f.write_rts(pathname, ts, unit, period_type)
298+
print("Done")

0 commit comments

Comments
 (0)