Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read/write timeseries section of inp #113

Merged
merged 3 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions swmmio/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def __init__(self, filePath):
SWMMIOFile.__init__(self, filePath)

meta_data = get_rpt_metadata(filePath)

self.swmm_version = meta_data['swmm_version']
self.simulationStart = meta_data['simulation_start']
self.simulationEnd = meta_data['simulation_end']
Expand Down Expand Up @@ -497,6 +497,7 @@ def __init__(self, file_path):
self._inp_section_details = None
self._inflows_df = None
self._curves_df = None
self._timeseries_df = None

SWMMIOFile.__init__(self, file_path) # run the superclass init

Expand All @@ -518,7 +519,8 @@ def __init__(self, file_path):
'[CURVES]',
'[COORDINATES]',
'[INFLOWS]',
'[Polygons]'
'[Polygons]',
'[TIMESERIES]'
]

def save(self, target_path=None):
Expand Down Expand Up @@ -906,8 +908,8 @@ def inflows(self):
return self._inflows_df

@inflows.setter
def polygons(self, df):
"""Set inp.polygons DataFrame."""
def inflows(self, df):
"""Set inp.inflows DataFrame."""
self._inflows_df = df

@property
Expand All @@ -924,7 +926,7 @@ def polygons(self):
@polygons.setter
def polygons(self, df):
"""Set inp.polygons DataFrame."""
self._curves_df = df
self._polygons_df = df
aerispaha marked this conversation as resolved.
Show resolved Hide resolved

@property
def curves(self):
Expand All @@ -939,9 +941,24 @@ def curves(self):

@curves.setter
def curves(self, df):
"""Set inp.polygons DataFrame."""
"""Set inp.curves DataFrame."""
self._curves_df = df

@property
def timeseries(self):
"""
get/set timeseries section of model
:return: multi-index dataframe of model curves
"""
if self._timeseries_df is not None:
return self._timeseries_df
self._timeseries_df = create_dataframe_multi_index(self.path, '[TIMESERIES]')
return self._timeseries_df

@timeseries.setter
def timeseries(self, df):
"""Set inp.timeseries DataFrame."""
self._timeseries_df = df

def drop_invalid_model_elements(inp):
"""
Expand Down
3 changes: 2 additions & 1 deletion swmmio/defs/inp_sections.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ inp_file_objects:
XSECTIONS: [Link, Shape, Geom1, Geom2, Geom3, Geom4, Barrels, XX]
INFLOWS: [Node, Constituent, Time Series, Type, Mfactor, Sfactor, Baseline, Pattern]


TIMESERIES: [Name, Date, Time, Value]
COORDINATES: [Name, X, Y]
VERTICES: [Name, X, Y]
Polygons: [Name, X, Y]
Expand All @@ -63,6 +63,7 @@ inp_file_objects:
CURVES: [Name, Type, X-Value, Y-Value]



inp_section_tags:
['[TITLE', '[OPTION', '[FILE', '[RAINGAGE', '[TEMPERATURE', '[EVAP',
'[SUBCATCHMENT', '[SUBAREA', '[INFIL', '[AQUIFER', '[GROUNDWATER', '[SNOWPACK',
Expand Down
2 changes: 1 addition & 1 deletion swmmio/defs/sectionheaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'[TAGS]':'ElementType Name Tag',
'[MAP]': 'x1 y1 x2 y2',
'[CURVES]': 'Name Type X-Value Y-Value',
#'[TIMESERIES]':'Name Date Time Value'
'[TIMESERIES]': 'Name Date Time Value'
}


Expand Down
1 change: 1 addition & 0 deletions swmmio/tests/data/model_full_features_network.inp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ SCS_24h_Type_I_1in 23:15 0.0185
SCS_24h_Type_I_1in 23:30 0.0185
SCS_24h_Type_I_1in 23:45 0.0185
SCS_24h_Type_I_1in 24:00 0
TS2 FILE "C:\Rotations\R3 - Vue\Quasi2D\2005_Pure2D\inflow_timeseries.txt"

[REPORT]
;;Reporting Options
Expand Down
13 changes: 11 additions & 2 deletions swmmio/tests/test_model_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ def test_example_1():
elem_dict = {element: model.__getattribute__(element).geojson for element in element_types}
swmm_version = model.rpt.swmm_version
assert(swmm_version['major'] == 5)
assert(swmm_version['minor'] == 1)
assert(swmm_version['minor'] == 1)
assert(swmm_version['patch'] == 12)

model_b = swmmio.Model(MODEL_EX_1B)
swmm_version = model_b.rpt.swmm_version
swmm_version = model_b.rpt.swmm_version
assert(swmm_version['patch'] == 13)
elem_dict = {element: model_b.__getattribute__(element).geojson for element in element_types}

Expand All @@ -162,3 +162,12 @@ def test_example_1():
peak_runoff = model.rpt.subcatchment_runoff_summary['PeakRunoff']
assert peak_runoff.values == pytest.approx([4.66, 4.52, 2.45, 2.45, 6.56, 1.5, 0.79, 1.33], rel=0.001)
assert peak_runoff.values == pytest.approx(subs['PeakRunoff'].values, rel=0.001)

def test_get_set_timeseries(test_model_02):

ts = test_model_02.inp.timeseries
assert(all(ts.columns == ['Date', 'Time', 'Value']))
assert(ts.loc['TS2'].Date == 'FILE')
assert('"' in ts.loc['TS2'].Value)
assert(ts.Value.isnull().sum() == 0)
print (ts)
19 changes: 14 additions & 5 deletions swmmio/utils/dataframes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from io import StringIO
import warnings
import pandas as pd

import re

def dataframe_from_bi(bi_path, section='[CONDUITS]'):
"""
Expand Down Expand Up @@ -41,14 +41,23 @@ def create_dataframe_multi_index(inp_path, section='CURVES'):
f = StringIO(s)
data = []
for line in f.readlines():
items = line.strip().split()
if len(items) == 3:
items = [items[0], None, items[1], items[2]]
if "FILE" in line:
filename = re.findall(r'"([^"]*)"', line)[0]
items = line.strip().split()[:2]
items = [items[0], items[1], None, '"{}"'.format(filename)]
else:
items = line.strip().split()
if len(items) == 3:
items = [items[0], None, items[1], items[2]]
if len(items) == 4:
data.append(items)


df = pd.DataFrame(data=data, columns=cols)
df = df.set_index(['Name', 'Type'])
if sect == 'CURVES':
df = df.set_index(['Name', 'Type'])
elif sect == 'TIMESERIES':
df = df.set_index(['Name'])

return df

Expand Down