Adists: Prepare for scenarios update (commit code is not working).

scenarios
Pierre-Antoine 2025-08-12 15:36:19 +02:00
parent c43a26870e
commit 37cf42c524
12 changed files with 362 additions and 122 deletions

View File

@ -52,30 +52,41 @@ class BoundaryConditionAdisTS(SQLSubModel):
self._types = [self.time_convert, float]
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE boundary_condition_adists(
id INTEGER NOT NULL PRIMARY KEY,
def _db_create(cls, execute, ext=""):
cls._db_create_bca(execute, ext)
cls._db_create_bca_data(execute, ext)
return cls._create_submodel(execute)
@classmethod
def _db_create_bca(cls, execute, ext=""):
execute(f"""
CREATE TABLE boundary_condition_adists{ext}(
{cls.create_db_add_pamhyr_id()},
pollutant INTEGER NOT NULL,
type TEXT NOT NULL,
node INTEGER,
FOREIGN KEY(pollutant) REFERENCES Pollutants(id),
FOREIGN KEY(node) REFERENCES river_node(id)
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(pollutant) REFERENCES Pollutants(pamhyr_id),
FOREIGN KEY(node) REFERENCES river_node(pamhyr_id)
)
""")
execute("""
CREATE TABLE boundary_condition_data_adists(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
@classmethod
def _db_create_bca_data(cls, execute, ext=""):
execute(f"""
CREATE TABLE boundary_condition_data_adists{ext}(
{cls.create_db_add_pamhyr_id()},
data0 TEXT NOT NULL,
data1 TEXT NOT NULL,
bc INTEGER,
FOREIGN KEY(bc) REFERENCES boundary_condition_adists(id)
bca INTEGER,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(bca) REFERENCES boundary_condition_adists(pamhyr_id)
)
""")
return cls._create_submodel(execute)
@classmethod
def _db_update(cls, execute, version, data=None):
major, minor, release = version.strip().split(".")
@ -83,8 +94,55 @@ class BoundaryConditionAdisTS(SQLSubModel):
if int(release) < 7:
cls._db_create(execute)
if major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "boundary_condition_adists"
nodes = data['id2pid']['river_node']
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_bca(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, pollutant, type, node, scenario) " +
"SELECT pamhyr_id, pollutant, type, node, 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_node_pid(execute, table, reachs)
@classmethod
def _db_update_to_0_2_0_data(cls, execute, data):
table = "boundary_condition_data_adists"
reachs = data['id2pid']['river_reach']
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_bca_data(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, data0, data1, bca, scenario) " +
"SELECT pamhyr_id, data0, data1, bc, 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_reach_pid(execute, table, reachs)
@classmethod
def _db_load(cls, execute, data=None):
new = []

View File

@ -45,14 +45,20 @@ class BoundaryConditionsAdisTSList(PamhyrModelList):
return new
def _db_save(self, execute, data=None):
execute("DELETE FROM boundary_condition_adists")
execute("DELETE FROM boundary_condition_data_adists")
execute(
"DELETE FROM boundary_condition_adists" +
f"WHERE scenario = {self._status.scenario_id}"
)
execute(
"DELETE FROM boundary_condition_data_adists" +
f"WHERE scenario = {self._status.scenario_id}"
)
if data is None:
data = {}
for bc in self._lst:
bc._db_save(execute, data=data)
for bca in self._lst:
bca._db_save(execute, data=data)
return True

View File

@ -50,13 +50,15 @@ class D90AdisTS(SQLSubModel):
self._data = []
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE d90_adists(
id INTEGER NOT NULL PRIMARY KEY,
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE d90_adists{ext}(
{cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL,
d90 REAL NOT NULL,
enabled BOOLEAN NOT NULL
enabled BOOLEAN NOT NULL,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()}
)
""")
@ -69,8 +71,30 @@ class D90AdisTS(SQLSubModel):
if int(release) < 6:
cls._db_create(execute)
if major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "d90_adists"
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, name, d90, enabled, scenario) " +
"SELECT pamhyr_id, name, d90, enabled, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod
def _db_load(cls, execute, data=None):
new = []

View File

@ -42,7 +42,10 @@ class D90AdisTSList(PamhyrModelList):
return new
def _db_save(self, execute, data=None):
execute("DELETE FROM d90_adists")
execute(
"DELETE FROM d90_adists" +
f"WHERE scenario = {self._status.scenario_id}"
)
if data is None:
data = {}

View File

@ -52,10 +52,10 @@ class D90AdisTSSpec(SQLSubModel):
D90AdisTSSpec._id_cnt = max(D90AdisTSSpec._id_cnt + 1, self.id)
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE d90_spec(
id INTEGER NOT NULL PRIMARY KEY,
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE d90_adists_spec{ext}(
{cls.create_db_add_pamhyr_id()},
d90_default INTEGER NOT NULL,
name TEXT NOT NULL,
reach INTEGER NOT NULL,
@ -63,8 +63,10 @@ class D90AdisTSSpec(SQLSubModel):
end_rk REAL NOT NULL,
d90 REAL NOT NULL,
enabled BOOLEAN NOT NULL,
FOREIGN KEY(d90_default) REFERENCES d90_adists(id),
FOREIGN KEY(reach) REFERENCES river_reach(id)
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(d90_default) REFERENCES d90_adists(pamhyr_id),
FOREIGN KEY(reach) REFERENCES river_reach(pamhyr_id)
)
""")
@ -77,8 +79,31 @@ class D90AdisTSSpec(SQLSubModel):
if int(release) < 6:
cls._db_create(execute)
if major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "d90_spec"
table_new = "d90_adists_spec"
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca(execute, ext="_tmp")
execute(
f"INSERT INTO {table_new}_tmp " +
"(pamhyr_id, name, d90, enabled, scenario) " +
"SELECT pamhyr_id, name, d90, enabled, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table_new}_tmp RENAME TO {table_new}")
@classmethod
def _db_load(cls, execute, data=None):
new = []

View File

@ -53,16 +53,18 @@ class DIFAdisTS(SQLSubModel):
self._data = []
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE dif_adists(
id INTEGER NOT NULL PRIMARY KEY,
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE dif_adists{ext}(
{cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL,
method TEXT NOT NULL,
dif REAL NOT NULL,
b REAL,
c REAL,
enabled BOOLEAN NOT NULL
enabled BOOLEAN NOT NULL,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()}
)
""")
@ -75,8 +77,30 @@ class DIFAdisTS(SQLSubModel):
if int(release) < 6:
cls._db_create(execute)
if major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "dif_adists"
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, name, method, dif, b, c, enabled, scenario) " +
"SELECT pamhyr_id, name, method, dif, b, c, enabled, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table}_tmp RENAME TO {table}")
@classmethod
def _db_load(cls, execute, data=None):
new = []
@ -137,7 +161,7 @@ class DIFAdisTS(SQLSubModel):
sql = (
"INSERT INTO " +
"dif_adists(" +
"id, name, method, dif, b, c, enabled" +
"pamhyr_id, name, method, dif, b, c, enabled" +
") " +
"VALUES (" +
f"{self.id}, '{self._db_format(self._name)}', " +
@ -150,7 +174,7 @@ class DIFAdisTS(SQLSubModel):
data['dif_default_id'] = self.id
execute(
"DELETE FROM dif_spec " +
"DELETE FROM dif_adists_spec " +
f"WHERE dif_default = {self.id}"
)

View File

@ -42,7 +42,10 @@ class DIFAdisTSList(PamhyrModelList):
return new
def _db_save(self, execute, data=None):
execute("DELETE FROM dif_adists")
execute(
"DELETE FROM dif_adists" +
f"WHERE scenario = {self._status.scenario_id}"
)
if data is None:
data = {}

View File

@ -55,9 +55,9 @@ class DIFAdisTSSpec(SQLSubModel):
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE dif_spec(
id INTEGER NOT NULL PRIMARY KEY,
execute(f"""
CREATE TABLE dif_adists_spec{ext}(
{cls.create_db_add_pamhyr_id()},
dif_default INTEGER NOT NULL,
method TEXT NOT NULL,
reach INTEGER NOT NULL,
@ -67,6 +67,8 @@ class DIFAdisTSSpec(SQLSubModel):
b REAL,
c REAL,
enabled BOOLEAN NOT NULL,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(dif_default) REFERENCES dif_adists(id),
FOREIGN KEY(reach) REFERENCES river_reach(id)
)
@ -81,8 +83,34 @@ class DIFAdisTSSpec(SQLSubModel):
if int(release) < 6:
cls._db_create(execute)
if major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "dif_spec"
table_new = "dif_adists_spec"
reachs = data['id2pid']['river_reach']
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca(execute, ext="_tmp")
execute(
f"INSERT INTO {table_new}_tmp " +
"(pamhyr_id, pollutant, reach, begin_rk, end_rk, scenario) " +
"SELECT pamhyr_id, pollutant, edge, begin_rk, end_rk, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table_new}_tmp RENAME TO {table_new}")
cls._db_update_to_0_2_0_set_reach_pid(execute, table, reachs)
@classmethod
def _db_load(cls, execute, data=None):
new = []

View File

@ -55,10 +55,10 @@ class InitialConditionsAdisTS(SQLSubModel):
self._data = []
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE initial_conditions_adists(
id INTEGER NOT NULL PRIMARY KEY,
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE initial_conditions_adists{ext}(
{cls.create_db_add_pamhyr_id()},
pollutant INTEGER NOT NULL,
name TEXT NOT NULL,
concentration REAL NOT NULL,
@ -66,7 +66,9 @@ class InitialConditionsAdisTS(SQLSubModel):
em REAL NOT NULL,
ed REAL NOT NULL,
enabled BOOLEAN NOT NULL,
FOREIGN KEY(pollutant) REFERENCES Pollutants(id)
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(pollutant) REFERENCES Pollutants(pamhyr_id)
)
""")
@ -79,8 +81,34 @@ class InitialConditionsAdisTS(SQLSubModel):
if int(release) < 6:
cls._db_create(execute)
if major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "initial_conditions_adists"
reachs = data['id2pid']['river_reach']
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, pollutant, reach, begin_rk, end_rk, scenario) " +
"SELECT pamhyr_id, pollutant, edge, begin_rk, end_rk, 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_reach_pid(execute, table, reachs)
@classmethod
def _db_load(cls, execute, data=None):
new = []

View File

@ -43,7 +43,10 @@ class InitialConditionsAdisTSList(PamhyrModelList):
return new
def _db_save(self, execute, data=None):
execute("DELETE FROM initial_conditions_adists")
execute(
"DELETE FROM initial_conditions_adists" +
f"WHERE scenario = {self._status.scenario_id}"
)
if data is None:
data = {}

View File

@ -56,10 +56,10 @@ class ICAdisTSSpec(SQLSubModel):
ICAdisTSSpec._id_cnt = max(ICAdisTSSpec._id_cnt + 1, self.id)
@classmethod
def _db_create(cls, execute):
execute("""
CREATE TABLE initial_conditions_spec(
id INTEGER NOT NULL PRIMARY KEY,
def _db_create(cls, execute, ext=""):
execute(f"""
CREATE TABLE initial_conditions_spec{ext}(
{cls.create_db_add_pamhyr_id()},
ic_default INTEGER NOT NULL,
name TEXT NOT NULL,
reach INTEGER NOT NULL,
@ -71,8 +71,11 @@ class ICAdisTSSpec(SQLSubModel):
ed REAL NOT NULL,
rate REAL NOT NULL,
enabled BOOLEAN NOT NULL,
FOREIGN KEY(ic_default) REFERENCES initial_conditions_adists(id),
FOREIGN KEY(reach) REFERENCES river_reach(id)
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()},
FOREIGN KEY(ic_default)
REFERENCES initial_conditions_adists(pamhyr_id),
FOREIGN KEY(reach) REFERENCES river_reach(pamhyr_id)
)
""")
@ -85,8 +88,35 @@ class ICAdisTSSpec(SQLSubModel):
if int(release) < 6:
cls._db_create(execute)
if major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "initial_conditions_spec"
reachs = data['id2pid']['river_reach']
cls.update_db_add_pamhyr_id(execute, table, data)
Scenario.update_db_add_scenario(execute, table)
cls._db_create_lca(execute, ext="_tmp")
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, ic_default, name, reach, start_rk, end_rk, " +
"concentration, eg, em, ed, rate, enabled, scenario) " +
"SELECT pamhyr_id, ic_default, name, reach, start_rk, end_rk, " +
"concentration, eg, em, ed, rate, enabled, 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_reach_pid(execute, table, reachs)
@classmethod
def _db_load(cls, execute, data=None):
new = []

View File

@ -96,8 +96,7 @@ class LateralContributionAdisTS(SQLSubModel):
if int(release) < 7:
cls._db_create(execute)
if major == "0" and int(minor) <= 2:
if int(release) <= 0:
if major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@ -115,7 +114,7 @@ class LateralContributionAdisTS(SQLSubModel):
execute(
f"INSERT INTO {table}_tmp " +
"(pamhyr_id, pollutant, reach, begin_rk, end_rk, scenario) " +
"SELECT pamhyr_id, pollutant, edge, begin_rk, end_rk, scenario) " +
"SELECT pamhyr_id, pollutant, edge, begin_rk, end_rk, scenario " +
f"FROM {table}"
)
@ -124,7 +123,6 @@ class LateralContributionAdisTS(SQLSubModel):
cls._db_update_to_0_2_0_set_reach_pid(execute, table, reachs)
@classmethod
def _db_update_to_0_2_0_data(cls, execute, data):
table = "lateral_contribution_data_adists"
@ -147,43 +145,53 @@ class LateralContributionAdisTS(SQLSubModel):
cls._db_update_to_0_2_0_set_reach_pid(execute, table, reachs)
@classmethod
def _db_load(cls, execute, data=None):
new = []
scenario = data["scenario"]
loaded = data['loaded_pid']
table = execute(
"SELECT id, pollutant, edge, begin_rk, end_rk " +
"FROM lateral_contribution_adists"
"FROM lateral_contribution_adists" +
f"WHERE scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
)
if table is not None:
for row in table:
lc = cls(
lca = cls(
id=row[0],
pollutant=row[1],
status=data['status']
)
lc.edge = row[2]
lc.begin_rk = row[3]
lc.end_rk = row[4]
lca.edge = row[2]
lca.begin_rk = row[3]
lca.end_rk = row[4]
values = execute(
"SELECT data0," +
" data1 FROM lateral_contribution_data_adists " +
f"WHERE lc = '{lc.id}'"
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 = lc._types[0](v[0])
data1 = lc._types[1](v[1])
data0 = lca._types[0](v[0])
data1 = lca._types[1](v[1])
# Replace data at pos ind
lc._data.append((data0, data1))
lca._data.append((data0, data1))
new.append(lc)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new
def _db_save(self, execute, data=None):