Adists: Pollutants: Update for scenario.

scenarios
Pierre-Antoine 2025-08-14 17:49:21 +02:00
parent 468405d4ea
commit 739eb56006
2 changed files with 110 additions and 34 deletions

View File

@ -102,7 +102,7 @@ class InitialConditionsAdisTS(SQLSubModel):
"(pamhyr_id, pollutant, name, concentration, " + "(pamhyr_id, pollutant, name, concentration, " +
"eg, em, ed, scenario) " + "eg, em, ed, scenario) " +
"SELECT pamhyr_id, pollutant, name, concentration, " + "SELECT pamhyr_id, pollutant, name, concentration, " +
"eg, em, ed, scenario) " + "eg, em, ed, scenario " +
f"FROM {table}" f"FROM {table}"
) )

View File

@ -27,20 +27,20 @@ from tools import (
from Model.Tools.PamhyrDB import SQLSubModel from Model.Tools.PamhyrDB import SQLSubModel
from Model.Except import NotImplementedMethodeError from Model.Except import NotImplementedMethodeError
from Model.Scenario import Scenario
logger = logging.getLogger() logger = logging.getLogger()
class Pollutants(SQLSubModel): class Pollutants(SQLSubModel):
_sub_classes = [] _sub_classes = []
_id_cnt = 0
def __init__(self, id: int = -1, name: str = "", def __init__(self, id: int = -1, name: str = "",
status=None, status=None, owner_scenario=-1):
owner_scenario=-1):
super(Pollutants, self).__init__( super(Pollutants, self).__init__(
id=id, status=status, id=id, status=status,
owner_scenario=owner_scenario) owner_scenario=owner_scenario
)
self._status = status self._status = status
@ -52,9 +52,6 @@ class Pollutants(SQLSubModel):
self._data = [] self._data = []
Pollutants._id_cnt = max(
Pollutants._id_cnt + 1, self.id)
@property @property
def name(self): def name(self):
return self._name return self._name
@ -69,17 +66,26 @@ class Pollutants(SQLSubModel):
return self._data.copy() return self._data.copy()
@classmethod @classmethod
def _db_create(cls, execute): def _db_create(cls, execute, ext=""):
execute(""" cls._db_create_pol(execute, ext=ext)
CREATE TABLE Pollutants( cls._db_create_pol_char(execute, ext=ext)
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL UNIQUE @classmethod
def _db_create_pol(cls, execute, ext=""):
execute(f"""
CREATE TABLE pollutants{ext}(
{cls.create_db_add_pamhyr_id()},
name TEXT NOT NULL UNIQUE,
{Scenario.create_db_add_scenario()},
{Scenario.create_db_add_scenario_fk()}
) )
""") """)
execute(""" @classmethod
CREATE TABLE Pollutants_characteristics( def _db_create_pol_char(cls, execute, ext=""):
id INTEGER NOT NULL PRIMARY KEY, execute(f"""
CREATE TABLE pollutants_characteristics{ext}(
{cls.create_db_add_pamhyr_id()},
type INTEGER NOT NULL, type INTEGER NOT NULL,
diametre REAL NOT NULL, diametre REAL NOT NULL,
rho REAL NOT NULL, rho REAL NOT NULL,
@ -90,7 +96,9 @@ class Pollutants(SQLSubModel):
ac REAL NOT NULL, ac REAL NOT NULL,
bc REAL NOT NULL, bc REAL NOT NULL,
pollutant INTEGER NOT NULL, pollutant INTEGER 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)
) )
""") """)
@ -103,17 +111,70 @@ class Pollutants(SQLSubModel):
if int(release) < 7: if int(release) < 7:
cls._db_create(execute) cls._db_create(execute)
elif major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
cls._db_update_to_0_2_0_char(execute, data)
return True return True
@classmethod
def _db_update_to_0_2_0(cls, execute, data):
table = "Pollutants"
table_new = "pollutants"
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, scenario) " +
"SELECT pamhyr_id, name, scenario) " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table_new}_tmp RENAME TO {table_new}")
@classmethod
def _db_update_to_0_2_0_char(cls, execute, data):
table = "Pollutants_characteristics"
table_new = "pollutants_characteristics"
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, type, diametre, rho, porosity, " +
"cdc_riv, cdc_cas, apd, ac, bc, pollutant, scenario) " +
"SELECT pamhyr_id, type, diametre, rho, porosity, " +
"cdc_riv, cdc_cas, apd, ac, bc, pollutant, scenario " +
f"FROM {table}"
)
execute(f"DROP TABLE {table}")
execute(f"ALTER TABLE {table_new}_tmp RENAME TO {table_new}")
@classmethod @classmethod
def _db_load(cls, execute, data=None): def _db_load(cls, execute, data=None):
new = [] new = []
status = data["status"] status = data["status"]
scenario = data["scenario"]
loaded = data['loaded_pid']
if scenario is None:
return new
table = execute( table = execute(
"SELECT id, name " + "SELECT pamhyr_id, name FROM pollutants " +
f"FROM Pollutants" f"WHERE scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})"
) )
if table is not None: if table is not None:
@ -129,8 +190,10 @@ class Pollutants(SQLSubModel):
new_data = [] new_data = []
table = execute( table = execute(
"SELECT * " + "SELECT * " +
"FROM Pollutants_characteristics " + "FROM pollutants_characteristics " +
f"WHERE pollutant = {id}" f"WHERE pollutant = {id}" +
f"AND scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))})"
) )
if table is not None: if table is not None:
@ -139,36 +202,49 @@ class Pollutants(SQLSubModel):
new_pollutant._data.append(new_data) new_pollutant._data.append(new_data)
loaded.add(pid)
new.append(new_pollutant) new.append(new_pollutant)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new return new
def _db_save(self, execute, data=None): def _db_save(self, execute, data=None):
if not self.must_be_saved():
return True
execute(f"DELETE FROM Pollutants WHERE id = {self.id}") execute(
execute(f"DELETE FROM Pollutants_characteristics" + "DELETE FROM pollutants " +
f" WHERE pollutant = {self.id}") f"WHERE pamhyr_id = {self.id} " +
f"AND scenario = {self._status.scenario_id}"
)
execute(
f"DELETE FROM pollutants_characteristics " +
f"WHERE pollutant = {self.id} " +
f"AND scenario = {self._status.scenario_id}"
)
sql = ( execute(
"INSERT INTO " + "INSERT INTO " +
"Pollutants(id, name) " + "pollutants(id, name, scenario) " +
"VALUES (" + "VALUES (" +
f"{self.id}, " + f"{self.id}, " +
f"'{self._db_format(self._name)}'" + f"'{self._db_format(self._name)}', " +
f"{self._status.scenario_id}" +
")" ")"
) )
execute(sql)
for d in self._data: for d in self._data:
sql = ( execute(
"INSERT INTO " + "INSERT INTO " +
"Pollutants_characteristics(type, diametre, rho, porosity, " + "pollutants_characteristics(type, diametre, rho, porosity, " +
"cdc_riv, cdc_cas, apd, ac, bc, pollutant) " + "cdc_riv, cdc_cas, apd, ac, bc, pollutant, scenario) " +
f"VALUES ({d[0]}, {d[1]}, {d[2]},{d[3]}, {d[4]}, " f"VALUES ({d[0]}, {d[1]}, {d[2]},{d[3]}, {d[4]}, "
f"{d[5]}, {d[6]}, {d[7]}, {d[8]}, {self.id})" f"{d[5]}, {d[6]}, {d[7]}, {d[8]}, {self.id}, " +
f"{self._status.scenario_id})"
) )
execute(sql)
return True return True