mirror of https://gitlab.com/pamhyr/pamhyr2
Adists: LC: Put lca data into separate class (to setup correct pamhyr_id).
parent
b94c37dd3b
commit
1f79e6510d
|
|
@ -31,54 +31,25 @@ from Model.Scenario import Scenario
|
|||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
class LateralContributionAdisTS(SQLSubModel):
|
||||
class Data(SQLSubModel):
|
||||
_sub_classes = []
|
||||
|
||||
def __init__(self, id: int = -1, pollutant: int = -1,
|
||||
name: str = "", status=None,
|
||||
def __init__(self,
|
||||
data0, data1,
|
||||
id: int = -1,
|
||||
types=[float, float],
|
||||
status=None,
|
||||
owner_scenario=-1):
|
||||
super(LateralContributionAdisTS, self).__init__(
|
||||
super(Data, self).__init__(
|
||||
id=id, status=status,
|
||||
owner_scenario=owner_scenario
|
||||
)
|
||||
|
||||
self._status = status
|
||||
|
||||
self._pollutant = pollutant
|
||||
self._reach = None
|
||||
self._begin_rk = 0.0
|
||||
self._end_rk = 0.0
|
||||
self._data = []
|
||||
self._header = ["time", "rate"]
|
||||
self._types = [self.time_convert, float]
|
||||
self._types = types
|
||||
self._data = [data0, data1]
|
||||
|
||||
@classmethod
|
||||
def _db_create(cls, execute, ext=""):
|
||||
cls._db_create_lca(execute, ext)
|
||||
cls._db_create_lca_data(execute, ext)
|
||||
|
||||
return cls._create_submodel(execute)
|
||||
|
||||
@classmethod
|
||||
def _db_create_lca(cls, execute, ext=""):
|
||||
execute(f"""
|
||||
CREATE TABLE lateral_contribution_adists{ext}(
|
||||
{cls.create_db_add_pamhyr_id()},
|
||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
pollutant INTEGER NOT NULL,
|
||||
reach INTEGER NOT NULL,
|
||||
begin_rk REAL NOT NULL,
|
||||
end_rk REAL NOT NULL,
|
||||
{Scenario.create_db_add_scenario()},
|
||||
{Scenario.create_db_add_scenario_fk()},
|
||||
FOREIGN KEY(pollutant) REFERENCES Pollutants(pamhyr_id),
|
||||
FOREIGN KEY(reach) REFERENCES river_reach(pamhyr_id)
|
||||
)
|
||||
""")
|
||||
|
||||
@classmethod
|
||||
def _db_create_lca_data(cls, execute, ext=""):
|
||||
execute(f"""
|
||||
CREATE TABLE lateral_contribution_data_adists{ext}(
|
||||
{cls.create_db_add_pamhyr_id()},
|
||||
|
|
@ -108,6 +79,175 @@ class LateralContributionAdisTS(SQLSubModel):
|
|||
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def _db_update_to_0_2_0(cls, execute, data):
|
||||
table = "lateral_contribution_data_adists"
|
||||
|
||||
cls.update_db_add_pamhyr_id(execute, table, data)
|
||||
Scenario.update_db_add_scenario(execute, table)
|
||||
|
||||
cls._db_create(execute, ext="_tmp")
|
||||
|
||||
execute(
|
||||
f"INSERT INTO {table}_tmp " +
|
||||
"(pamhyr_id, data0, data1, lca, scenario) " +
|
||||
"SELECT pamhyr_id, data0, data1, lc, scenario " +
|
||||
f"FROM {table}"
|
||||
)
|
||||
|
||||
execute(f"DROP TABLE {table}")
|
||||
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
|
||||
cls._db_update_to_0_2_0_set_lca_pid(execute, data)
|
||||
|
||||
@classmethod
|
||||
def _db_update_to_0_2_0_set_lca_pid(cls, execute, data):
|
||||
pid_lca = data["id2pid"]["lateral_contribution_adists"]
|
||||
els = execute(
|
||||
"SELECT pamhyr_id, lca " +
|
||||
"FROM lateral_contribution_data_adists"
|
||||
)
|
||||
|
||||
for row in els:
|
||||
it = iter(row)
|
||||
pid = next(it)
|
||||
lca_id = next(it)
|
||||
|
||||
if lca_id == -1:
|
||||
continue
|
||||
|
||||
execute(
|
||||
f"UPDATE lateral_contribution_data_adists " +
|
||||
f"SET lca = {pid_lca[lca_id]} " +
|
||||
f"WHERE pamhyr_id = {pid}"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _db_load(cls, execute, data=None):
|
||||
new = []
|
||||
status = data['status']
|
||||
scenario = data["scenario"]
|
||||
loaded = data['loaded_pid']
|
||||
|
||||
if scenario is None:
|
||||
return new
|
||||
|
||||
table = execute(
|
||||
"SELECT pamhyr_id, deleted, data0, data1 " +
|
||||
"FROM lateral_contribution_data_adists " +
|
||||
f"WHERE scenario = {scenario.id} " +
|
||||
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
|
||||
f"AND lca = '{lca.id}'"
|
||||
)
|
||||
|
||||
if table is not None:
|
||||
for row in table:
|
||||
it = iter(row)
|
||||
|
||||
pid = next(it)
|
||||
deleted = (next(it) == 1)
|
||||
data0 = next(it)
|
||||
data1 = next(it)
|
||||
owner_scenario = next(it)
|
||||
|
||||
data = cls(
|
||||
data0, data1,
|
||||
id=pid, status=status,
|
||||
owner_scenario=owner_scenario
|
||||
)
|
||||
if deleted:
|
||||
data.set_as_deleted()
|
||||
|
||||
loaded.add(pid)
|
||||
new.append(data)
|
||||
|
||||
data["scenario"] = scenario.parent
|
||||
new += cls._db_load(execute, data)
|
||||
data["scenario"] = scenario
|
||||
|
||||
return new
|
||||
|
||||
def _db_save(self, execute, data=None):
|
||||
if not self.must_be_saved():
|
||||
return True
|
||||
|
||||
lca = data["lca"]
|
||||
|
||||
execute(
|
||||
"INSERT INTO " +
|
||||
"lateral_contribution_adists(pamhyr_id, deleted," +
|
||||
"data0, data1, lca, scenario) " +
|
||||
"VALUES (" +
|
||||
f"{self.id}, {self._db_format(self.is_deleted())}, " +
|
||||
f"{self._data[0]}, {self._data[1]}, " +
|
||||
f"{lca.id}, {self._status.scenario_id}" +
|
||||
")"
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self._types[key](self._data[key])
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self._data[key] = self._types[key](value)
|
||||
|
||||
|
||||
class LateralContributionAdisTS(SQLSubModel):
|
||||
_sub_classes = [Data]
|
||||
|
||||
def __init__(self, id: int = -1, pollutant: int = -1,
|
||||
name: str = "", status=None,
|
||||
owner_scenario=-1):
|
||||
super(LateralContributionAdisTS, self).__init__(
|
||||
id=id, status=status,
|
||||
owner_scenario=owner_scenario
|
||||
)
|
||||
|
||||
self._status = status
|
||||
|
||||
self._pollutant = pollutant
|
||||
self._reach = None
|
||||
self._begin_rk = 0.0
|
||||
self._end_rk = 0.0
|
||||
self._data = []
|
||||
self._header = ["time", "rate"]
|
||||
self._types = [self.time_convert, float]
|
||||
|
||||
@classmethod
|
||||
def _db_create(cls, execute, ext=""):
|
||||
execute(f"""
|
||||
CREATE TABLE lateral_contribution_adists{ext}(
|
||||
{cls.create_db_add_pamhyr_id()},
|
||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
pollutant INTEGER NOT NULL,
|
||||
reach INTEGER NOT NULL,
|
||||
begin_rk REAL NOT NULL,
|
||||
end_rk REAL NOT NULL,
|
||||
{Scenario.create_db_add_scenario()},
|
||||
{Scenario.create_db_add_scenario_fk()},
|
||||
FOREIGN KEY(pollutant) REFERENCES Pollutants(pamhyr_id),
|
||||
FOREIGN KEY(reach) REFERENCES river_reach(pamhyr_id)
|
||||
)
|
||||
""")
|
||||
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def _db_update(cls, execute, version, data=None):
|
||||
major, minor, release = version.strip().split(".")
|
||||
created = False
|
||||
|
||||
if major == "0" and int(minor) <= 1:
|
||||
if int(release) < 7:
|
||||
cls._db_create(execute)
|
||||
created = True
|
||||
|
||||
if major == "0" and int(minor) < 2:
|
||||
if not created:
|
||||
cls._db_update_to_0_2_0(execute, data)
|
||||
|
||||
return cls._update_submodel(execute, version, data)
|
||||
|
||||
@classmethod
|
||||
def _db_update_to_0_2_0(cls, execute, data):
|
||||
table = "lateral_contribution_adists"
|
||||
|
|
@ -116,7 +256,7 @@ class LateralContributionAdisTS(SQLSubModel):
|
|||
cls.update_db_add_pamhyr_id(execute, table, data)
|
||||
Scenario.update_db_add_scenario(execute, table)
|
||||
|
||||
cls._db_create_lca(execute, ext="_tmp")
|
||||
cls._db_create(execute, ext="_tmp")
|
||||
|
||||
execute(
|
||||
f"INSERT INTO {table}_tmp " +
|
||||
|
|
@ -153,48 +293,6 @@ class LateralContributionAdisTS(SQLSubModel):
|
|||
f"WHERE pamhyr_id = {pid}"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _db_update_to_0_2_0_data(cls, execute, data):
|
||||
table = "lateral_contribution_data_adists"
|
||||
|
||||
cls.update_db_add_pamhyr_id(execute, table, data)
|
||||
Scenario.update_db_add_scenario(execute, table)
|
||||
|
||||
cls._db_create_lca_data(execute, ext="_tmp")
|
||||
|
||||
execute(
|
||||
f"INSERT INTO {table}_tmp " +
|
||||
"(pamhyr_id, data0, data1, lca, scenario) " +
|
||||
"SELECT pamhyr_id, data0, data1, lc, scenario " +
|
||||
f"FROM {table}"
|
||||
)
|
||||
|
||||
execute(f"DROP TABLE {table}")
|
||||
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
|
||||
cls._db_update_to_0_2_0_set_ica_pid(execute, data)
|
||||
|
||||
@classmethod
|
||||
def _db_update_to_0_2_0_set_lca_pid(cls, execute, data):
|
||||
pid_lca = data["id2pid"]["lateral_contribution_adists"]
|
||||
els = execute(
|
||||
"SELECT pamhyr_id, lca " +
|
||||
"FROM lateral_contribution_data_adists"
|
||||
)
|
||||
|
||||
for row in els:
|
||||
it = iter(row)
|
||||
pid = next(it)
|
||||
lca_id = next(it)
|
||||
|
||||
if lca_id == -1:
|
||||
continue
|
||||
|
||||
execute(
|
||||
f"UPDATE lateral_contribution_data_adists " +
|
||||
f"SET lca = {pid_lca[lca_id]} " +
|
||||
f"WHERE pamhyr_id = {pid}"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _db_load(cls, execute, data=None):
|
||||
new = []
|
||||
|
|
@ -238,20 +336,8 @@ class LateralContributionAdisTS(SQLSubModel):
|
|||
lca.begin_rk = brk
|
||||
lca.end_rk = erk
|
||||
|
||||
values = execute(
|
||||
"SELECT data0, data1 " +
|
||||
"FROM lateral_contribution_data_adists " +
|
||||
f"WHERE scenario = {scenario.id} " +
|
||||
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
|
||||
f"AND lca = '{lca.id}'"
|
||||
)
|
||||
|
||||
# Write data
|
||||
for v in values:
|
||||
data0 = lca._types[0](v[1])
|
||||
data1 = lca._types[1](v[2])
|
||||
# Replace data at pos ind
|
||||
lca._data.append((data0, data1))
|
||||
data["lca"] = lca
|
||||
lca._data = Data._db_load(execute, data=data)
|
||||
|
||||
loaded.add(pid)
|
||||
new.append(lca)
|
||||
|
|
@ -289,21 +375,25 @@ class LateralContributionAdisTS(SQLSubModel):
|
|||
")"
|
||||
)
|
||||
|
||||
ind = 0
|
||||
for d in self._data:
|
||||
data0 = self._db_format(str(d[0]))
|
||||
data1 = self._db_format(str(d[1]))
|
||||
data["ind"] = ind
|
||||
|
||||
sql = (
|
||||
"INSERT INTO " +
|
||||
"lateral_contribution_data_adists(data0, data1, lca) " +
|
||||
f"VALUES ('{data0}', {data1}, {self.id})"
|
||||
)
|
||||
execute(sql)
|
||||
d._db_save(execute, data)
|
||||
|
||||
ind += 1
|
||||
|
||||
return True
|
||||
|
||||
def __len__(self):
|
||||
return len(self._data)
|
||||
return len(
|
||||
list(
|
||||
filter(
|
||||
lambda el: not el.is_deleted(),
|
||||
self._data
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def time_convert(cls, data):
|
||||
|
|
|
|||
Loading…
Reference in New Issue