Adists: IC: Update load and save for scenario.

scenarios
Pierre-Antoine 2025-08-14 10:31:17 +02:00
parent a525d8a2b0
commit 4a0456cbce
2 changed files with 95 additions and 64 deletions

View File

@ -114,46 +114,70 @@ class InitialConditionsAdisTS(SQLSubModel):
@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, pollutant, name, concentration, eg, em, ed, " +
"enabled, scenario " +
"FROM initial_conditions_adists"
"FROM initial_conditions_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:
IC_id = row[0]
pollutant = row[1]
name = row[2]
concentration = row[3]
eg = row[4]
em = row[5]
ed = row[6]
enabled = (row[7] == 1)
it = iter(row)
IC = cls(
id=IC_id,
pid = next(it)
pollutant = next(it)
name = next(it)
concentration = next(it)
eg = next(it)
em = next(it)
ed = next(it)
enabled = (next(it) == 1)
owner_scenario = next(it)
ic = cls(
id=pid,
name=name,
status=data['status']
status=status,
owner_scenario=owner_scenario
)
IC.pollutant = pollutant
IC.concentration = concentration
IC.eg = eg
IC.em = em
IC.ed = ed
IC.enabled = enabled
ic.pollutant = pollutant
ic.concentration = concentration
ic.eg = eg
ic.em = em
ic.ed = ed
ic.enabled = enabled
data['ic_default_id'] = IC_id
IC._data = ICAdisTSSpec._db_load(execute, data)
data['ic_default_id'] = pid
ic._data = ICAdisTSSpec._db_load(execute, data)
loaded.add(pid)
new.append(IC)
data["scenario"] = scenario.parent
new += cls._db_load(execute, data)
data["scenario"] = scenario
return new
def _db_save(self, execute, data=None):
execute(f"DELETE FROM initial_conditions_adists WHERE id = {self.id}")
if not self.must_be_saved():
return True
execute(
"DELETE FROM initial_conditions_adists " +
f"WHERE pamhyr_id = {self.id} " +
f"AND scenario = {self._status.scenario_id}"
)
pollutant = -1
if self.pollutant is not None:
@ -179,11 +203,12 @@ class InitialConditionsAdisTS(SQLSubModel):
"INSERT INTO " +
"initial_conditions_adists(" +
"pamhyr_id, pollutant, name, concentration, " +
"eg, em, ed, enabled" +
"eg, em, ed, enabled, scenario" +
") " +
"VALUES (" +
f"{self.id}, {pollutant}, '{self._db_format(self._name)}', " +
f"{concentration}, {eg}, {em}, {ed}, {self._enabled}" +
f"{concentration}, {eg}, {em}, {ed}, {self._enabled}, " +
f"{self._status.scenario_id}" +
")"
)
@ -192,7 +217,8 @@ class InitialConditionsAdisTS(SQLSubModel):
data['ic_default_id'] = self.id
execute(
"DELETE FROM initial_conditions_spec " +
f"WHERE ic_default = {self.id}"
f"WHERE ic_default = {self.id} " +
f"AND scenario = {self._status.scenario_id}"
)
for ic_spec in self._data:

View File

@ -28,9 +28,7 @@ logger = logging.getLogger()
class ICAdisTSSpec(SQLSubModel):
_sub_classes = [
]
_id_cnt = 0
_sub_classes = []
def __init__(self, id: int = -1, name: str = "",
status=None):
@ -38,11 +36,6 @@ class ICAdisTSSpec(SQLSubModel):
self._status = status
if id == -1:
self.id = ICAdisTSSpec._id_cnt
else:
self.id = id
self._name_section = name
self._reach = None
self._start_rk = None
@ -54,8 +47,6 @@ class ICAdisTSSpec(SQLSubModel):
self._rate = None
self._enabled = True
ICAdisTSSpec._id_cnt = max(ICAdisTSSpec._id_cnt + 1, self.id)
@classmethod
def _db_create(cls, execute, ext=""):
execute(f"""
@ -89,7 +80,7 @@ class ICAdisTSSpec(SQLSubModel):
if int(release) < 6:
cls._db_create(execute)
if major == "0" and int(minor) < 2:
elif major == "0" and int(minor) < 2:
cls._db_update_to_0_2_0(execute, data)
return True
@ -121,35 +112,44 @@ class ICAdisTSSpec(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
new = []
status = data['status']
reachs = data["edges"]
scenario = data["scenario"]
loaded = data['loaded_pid']
if scenario is None:
return new
table = execute(
"SELECT id, ic_default, name, reach, start_rk, end_rk, " +
"concentration, eg, em, ed, rate, enabled " +
"SELECT id, name, reach, start_rk, end_rk, " +
"concentration, eg, em, ed, rate, enabled, scenario " +
"FROM initial_conditions_spec " +
f"WHERE ic_default = {data['ic_default_id']} "
f"WHERE ic_default = {data['ic_default_id']} " +
f"AND scenario = {scenario.id} " +
f"AND pamhyr_id NOT IN ({', '.join(map(str, loaded))}) "
)
if table is not None:
for row in table:
id = row[0]
name = row[2]
reach = row[3]
start_rk = row[4]
end_rk = row[5]
concentration = row[6]
eg = row[7]
em = row[8]
ed = row[9]
rate = row[10]
enabled = (row[11] == 1)
it = iter(row)
# new_spec = [name, reach, start_rk, end_rk,
# concentration, eg, em, ed, rate, enabled]
id = next(it)
name = next(it)
reach = next(it)
start_rk = next(it)
end_rk = next(it)
concentration = next(it)
eg = next(it)
em = next(it)
ed = next(it)
rate = next(it)
enabled = (next(it) == 1)
owner_scenario = next(it)
new_spec = cls(
id=id,
name=name,
status=data['status']
id=id, name=name,
status=status,
owner_scenario=owner_scenario
)
new_spec.reach = reach
@ -162,30 +162,35 @@ class ICAdisTSSpec(SQLSubModel):
new_spec.rate = rate
new_spec.enabled = enabled
loaded.add(pid)
new.append(new_spec)
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
ic_default = data['ic_default_id']
sql = (
"INSERT INTO " +
"initial_conditions_spec(id, ic_default, name, reach, " +
"start_rk, end_rk, concentration, eg, em, ed, rate, enabled) " +
"start_rk, end_rk, concentration, eg, em, ed, rate, " +
"enabled, scenario) " +
"VALUES (" +
f"{self.id}, " +
f"{ic_default}, " +
f"{self.id}, {ic_default}, " +
f"'{self._db_format(self._name_section)}', " +
f"{self._reach}, " +
f"{self._start_rk}, " +
f"{self._end_rk}, " +
f"{self._start_rk}, {self._end_rk}, " +
f"{self._concentration}, " +
f"{self._eg}, " +
f"{self._em}, " +
f"{self._ed}, " +
f"{self._rate}, " +
f"{self._enabled}" +
f"{self._eg}, {self._em}, {self._ed}, " +
f"{self._rate}, {self._enabled}, " +
f"{self._status.scenario_id}" +
")"
)
execute(sql)