mirror of https://gitlab.com/pamhyr/pamhyr2
Model, LC: Add SQL export for lateral contribution.
parent
36f792d432
commit
a4acbdce16
|
|
@ -2,14 +2,24 @@
|
|||
|
||||
from tools import trace, timer, old_pamhyr_date_to_timestamp
|
||||
|
||||
from Model.DB import SQLSubModel
|
||||
from Model.Except import NotImplementedMethodeError
|
||||
|
||||
class LateralContribution(object):
|
||||
def __init__(self, name:str = "", status = None):
|
||||
class LateralContribution(SQLSubModel):
|
||||
_sub_classes = []
|
||||
_id_cnt = 0
|
||||
|
||||
def __init__(self, id:int = -1, name:str = "", status = None):
|
||||
super(LateralContribution, self).__init__()
|
||||
|
||||
self._status = status
|
||||
|
||||
if id == -1:
|
||||
self.id = type(self)._id_cnt
|
||||
else:
|
||||
self.id = id
|
||||
|
||||
|
||||
self._name = name
|
||||
self._type = ""
|
||||
self._edge = None
|
||||
|
|
@ -19,6 +29,132 @@ class LateralContribution(object):
|
|||
self._header = []
|
||||
self._types = [float, float]
|
||||
|
||||
type(self)._id_cnt = max(type(self)._id_cnt + 1, self.id)
|
||||
|
||||
@classmethod
|
||||
def _sql_create(cls, execute):
|
||||
execute("""
|
||||
CREATE TABLE lateral_contribution(
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
tab TEXT NOT NULL,
|
||||
edge INTEGER,
|
||||
begin_kp REAL NOT NULL,
|
||||
end_kp REAL NOT NULL,
|
||||
FOREIGN KEY(edge) REFERENCES river_reach(id)
|
||||
)
|
||||
""")
|
||||
|
||||
execute("""
|
||||
CREATE TABLE lateral_contribution_data(
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
ind INTEGER NOT NULL,
|
||||
data0 TEXT NOT NULL,
|
||||
data1 TEXT NOT NULL,
|
||||
lc INTEGER,
|
||||
FOREIGN KEY(lc) REFERENCES lateral_contribution(id)
|
||||
)
|
||||
""")
|
||||
|
||||
return cls._create_submodel(execute)
|
||||
|
||||
@classmethod
|
||||
def _sql_update(cls, execute, version):
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def _get_ctor_from_type(cls, t):
|
||||
from Model.LateralContribution.LateralContributionTypes import (
|
||||
NotDefined, LateralContrib, Rain, Evaporation
|
||||
)
|
||||
|
||||
res = NotDefined
|
||||
if t == "LC":
|
||||
res = LateralContrib
|
||||
elif t == "RA":
|
||||
res = Rain
|
||||
elif t == "EV":
|
||||
res = Evaporation
|
||||
return res
|
||||
|
||||
@classmethod
|
||||
def _sql_load(cls, execute, data = None):
|
||||
new = []
|
||||
tab = data["tab"]
|
||||
|
||||
table = execute(
|
||||
"SELECT id, name, type, edge, begin_kp, end_kp " +
|
||||
f"FROM lateral_contribution WHERE tab = '{tab}'"
|
||||
)
|
||||
|
||||
for row in table:
|
||||
t = row[2]
|
||||
ctor = cls._get_ctor_from_type(t)
|
||||
lc = ctor(
|
||||
id = row[0],
|
||||
name = row[1],
|
||||
status = data['status']
|
||||
)
|
||||
lc.edge = next(filter(lambda e: e.id == row[3], data["edges"]))
|
||||
lc._begin_kp = row[4]
|
||||
lc._end_kp = row[5]
|
||||
|
||||
values = execute(
|
||||
"SELECT ind, data0, data1 FROM lateral_contribution_data " +
|
||||
f"WHERE lc = '{lc.id}'"
|
||||
)
|
||||
# Create dummy data list
|
||||
for _ in values:
|
||||
lc.add(0)
|
||||
# Write data
|
||||
for v in values:
|
||||
ind = v[0]
|
||||
data0 = lc._types[0](v[1])
|
||||
data1 = lc._types[1](v[2])
|
||||
# Replace data at pos ind
|
||||
lc._data[ind] = (data0, data1)
|
||||
|
||||
new.append(lc)
|
||||
|
||||
return new
|
||||
|
||||
def _sql_save(self, execute, data = None):
|
||||
tab = data["tab"]
|
||||
|
||||
execute(f"DELETE FROM lateral_contribution WHERE id = {self.id}")
|
||||
execute(f"DELETE FROM lateral_contribution_data WHERE lc = {self.id}")
|
||||
|
||||
edge = -1
|
||||
if self._edge is not None:
|
||||
edge = self._edge.id
|
||||
|
||||
sql = (
|
||||
"INSERT INTO " +
|
||||
"lateral_contribution(id, name, type, tab, edge, begin_kp, end_kp) "+
|
||||
"VALUES (" +
|
||||
f"{self.id}, '{self._sql_format(self._name)}', " +
|
||||
f"'{self._sql_format(self._type)}', '{tab}', {edge}, " +
|
||||
f"{self._begin_kp}, {self._end_kp}" +
|
||||
")"
|
||||
)
|
||||
execute(sql)
|
||||
|
||||
ind = 0
|
||||
for d in self._data:
|
||||
data0 = self._sql_format(str(d[0]))
|
||||
data1 = self._sql_format(str(d[1]))
|
||||
|
||||
sql = (
|
||||
"INSERT INTO " +
|
||||
"lateral_contribution_data(ind, data0, data1, lc) "+
|
||||
f"VALUES ({ind}, '{data0}', {data1}, {self.id})"
|
||||
)
|
||||
execute(sql)
|
||||
ind += 1
|
||||
|
||||
return True
|
||||
|
||||
def __len__(self):
|
||||
return len(self._data)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,13 +3,19 @@
|
|||
from copy import copy
|
||||
from tools import trace, timer
|
||||
|
||||
from Model.DB import SQLSubModel
|
||||
from Model.Except import NotImplementedMethodeError
|
||||
|
||||
from Model.LateralContribution.LateralContribution import LateralContribution
|
||||
from Model.LateralContribution.LateralContributionTypes import (
|
||||
NotDefined, LateralContrib, Rain, Evaporation,
|
||||
)
|
||||
|
||||
class LateralContributionList(object):
|
||||
class LateralContributionList(SQLSubModel):
|
||||
_sub_classes = [
|
||||
LateralContribution,
|
||||
]
|
||||
|
||||
def __init__(self, status = None):
|
||||
super(LateralContributionList, self).__init__()
|
||||
|
||||
|
|
@ -21,6 +27,40 @@ class LateralContributionList(object):
|
|||
"suspenssion" : []
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _sql_create(cls, execute):
|
||||
return cls._create_submodel(execute)
|
||||
|
||||
@classmethod
|
||||
def _sql_update(cls, execute, version):
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def _sql_load(cls, execute, data = None):
|
||||
new = cls(status = data['status'])
|
||||
|
||||
if data is None:
|
||||
data = {}
|
||||
|
||||
for tab in new._tabs:
|
||||
data["tab"] = tab
|
||||
new._tabs[tab] = LateralContribution._sql_load(
|
||||
execute, data
|
||||
)
|
||||
|
||||
return new
|
||||
|
||||
def _sql_save(self, execute, data = None):
|
||||
if data is None:
|
||||
data = {}
|
||||
|
||||
for tab in self._tabs:
|
||||
data["tab"] = tab
|
||||
for lc in self._tabs[tab]:
|
||||
lc._sql_save(execute, data = data)
|
||||
|
||||
return True
|
||||
|
||||
def len(self, lst):
|
||||
return len(self._tabs[lst])
|
||||
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ from Model.LateralContribution.LateralContribution import LateralContribution
|
|||
|
||||
|
||||
class NotDefined(LateralContribution):
|
||||
def __init__(self, name:str = "", status=None):
|
||||
super(NotDefined, self).__init__(name=name, status=status)
|
||||
def __init__(self, id:int = -1, name:str = "", status=None):
|
||||
super(NotDefined, self).__init__(id=id, name=name, status=status)
|
||||
|
||||
self._type = "ND"
|
||||
self._header = ["x", "y"]
|
||||
|
|
@ -17,8 +17,8 @@ class NotDefined(LateralContribution):
|
|||
return 0.0
|
||||
|
||||
class LateralContrib(LateralContribution):
|
||||
def __init__(self, name:str = "", status=None):
|
||||
super(LateralContrib, self).__init__(name=name, status=status)
|
||||
def __init__(self, id:int = -1, name:str = "", status=None):
|
||||
super(LateralContrib, self).__init__(id=id, name=name, status=status)
|
||||
|
||||
self._type = "LC"
|
||||
self._header = ["time", "discharge"]
|
||||
|
|
@ -29,8 +29,8 @@ class LateralContrib(LateralContribution):
|
|||
return ["liquid"]
|
||||
|
||||
class Rain(LateralContribution):
|
||||
def __init__(self, name:str = "", status=None):
|
||||
super(Rain, self).__init__(name=name, status=status)
|
||||
def __init__(self, id:int = -1, name:str = "", status=None):
|
||||
super(Rain, self).__init__(id=id, name=name, status=status)
|
||||
|
||||
self._type = "RA"
|
||||
self._header = ["time", "discharge"]
|
||||
|
|
@ -41,8 +41,8 @@ class Rain(LateralContribution):
|
|||
return ["liquid"]
|
||||
|
||||
class Evaporation(LateralContribution):
|
||||
def __init__(self, name:str = "", status=None):
|
||||
super(Evaporation, self).__init__(name=name, status=status)
|
||||
def __init__(self, id:int = -1, name:str = "", status=None):
|
||||
super(Evaporation, self).__init__(id=id, name=name, status=status)
|
||||
|
||||
self._type = "EV"
|
||||
self._header = ["time", "discharge"]
|
||||
|
|
|
|||
|
|
@ -172,7 +172,7 @@ class River(Graph, SQLSubModel):
|
|||
RiverNode,
|
||||
RiverReach,
|
||||
BoundaryConditionList,
|
||||
# LateralContributionList,
|
||||
LateralContributionList,
|
||||
# InitialConditionsDict,
|
||||
# StricklersList,
|
||||
# SolverParametersList,
|
||||
|
|
@ -223,12 +223,17 @@ class River(Graph, SQLSubModel):
|
|||
execute,
|
||||
data
|
||||
)
|
||||
new._lateral_contribution = LateralContributionList._sql_load(
|
||||
execute,
|
||||
data
|
||||
)
|
||||
|
||||
return new
|
||||
|
||||
def _sql_save(self, execute, data = None):
|
||||
objs = (self._nodes + self._edges)
|
||||
objs.append(self._boundary_condition)
|
||||
objs.append(self._lateral_contribution)
|
||||
|
||||
self._save_submodel(execute, objs, data)
|
||||
return True
|
||||
|
|
|
|||
Loading…
Reference in New Issue