Compare commits

..

3 Commits

3 changed files with 53 additions and 10 deletions

View File

@ -22,6 +22,8 @@ import numpy as np
from copy import deepcopy
from datetime import datetime
from tools import timer
from Model.Scenario import Scenario
from Model.Tools.PamhyrDB import SQLSubModel
from Model.Results.River.River import River
@ -167,7 +169,7 @@ class Results(SQLSubModel):
self._name = name
self._study = study
self._river = River(self._study)
self._river = River(self._study, self)
self._solver = solver
self._repertory = repertory
@ -195,6 +197,7 @@ class Results(SQLSubModel):
def study(self):
return self._study
@timer
def set(self, key, value):
self._meta_data[key] = value
@ -212,6 +215,12 @@ class Results(SQLSubModel):
def get(self, key):
return self._meta_data[key]
def set_timestamp_index(self, index):
self._ts_index = index
def get_timestamp_id(self, ts):
return self._ts_index[ts]
def reload(self):
return self._solver.results(
self._study,
@ -314,6 +323,7 @@ class Results(SQLSubModel):
new_results.set("timestamps", ts)
data["timestamps"] = sorted(ts)
data["parent"] = new_results
new_results._river = River._db_load(execute, data)
new_results.set(

View File

@ -20,7 +20,7 @@ import logging
import itertools
import numpy as np
from tools import flatten
from tools import flatten, timer
from functools import reduce
from datetime import datetime
@ -31,15 +31,17 @@ logger = logging.getLogger()
class Profile(SQLSubModel):
def __init__(self, profile, study):
def __init__(self, profile, study, parent):
super(Profile, self).__init__(
id=-1, status=study.status,
owner_scenario=study.status.scenario.id
)
self._parent = parent
self._study = study
self._profile = profile # Source profile in the study
self._data = {} # Dict of dict {<ts>: {<key>: <value>, ...}, ...}
self._global_index = -1
def __len__(self):
return len(self._data)
@ -56,15 +58,26 @@ class Profile(SQLSubModel):
def geometry(self):
return self._profile
@property
def global_index(self):
return self._global_index
@global_index.setter
def global_index(self, id):
self._global_index = id
@timer
def set(self, timestamp, key, data):
if timestamp not in self._data:
self._data[timestamp] = {}
self._data[timestamp][key] = data
@timer
def get_ts(self, timestamp):
return self._data[timestamp]
@timer
def get_key(self, key):
res = list(
map(lambda ts: self._data[ts][key],
@ -72,6 +85,7 @@ class Profile(SQLSubModel):
)
return res
@timer
def get_ts_key(self, timestamp, key):
if timestamp in self._data:
if key in self._data[timestamp]:
@ -126,6 +140,7 @@ class Profile(SQLSubModel):
new = {}
status = data['status']
parent = data['parent']
study = data['study']
reach = data['reach']
profile = data['profile']
@ -153,7 +168,7 @@ class Profile(SQLSubModel):
owner_scenario = next(it)
if profile not in new:
new_data = cls(profile, study)
new_data = cls(profile, study, parent)
new[profile] = new_data
else:
new_data = new[profile]
@ -266,19 +281,20 @@ class Profile(SQLSubModel):
class Reach(SQLSubModel):
_sub_classes = [Profile]
def __init__(self, reach, study, with_init=True):
def __init__(self, reach, study, parent, with_init=True):
super(Reach, self).__init__(
id=-1, status=study.status,
owner_scenario=study.status.scenario.id
)
self._parent = parent
self._study = study
self._reach = reach # Source reach in the study
self._profiles = []
if with_init:
self._profiles = list(
map(
lambda p: Profile(p, self._study),
lambda p: Profile(p, self._study, self._parent),
reach.profiles
)
)
@ -316,6 +332,11 @@ class Reach(SQLSubModel):
def profile(self, id):
return self._profiles[id]
def set_global_index(self, indexs):
for profile, ind in zip(self._profiles, indexs):
profile.global_index = ind
@timer
def set(self, profile_id, timestamp, key, data):
self._profiles[profile_id].set(timestamp, key, data)
@ -344,7 +365,8 @@ class Reach(SQLSubModel):
reach = data["reach"]
new_reach = cls(
data["reach"], data["study"], with_init=False
data["reach"], data["study"], data["parent"],
with_init=False
)
for i, profile in enumerate(reach.profiles):
@ -363,13 +385,14 @@ class Reach(SQLSubModel):
class River(SQLSubModel):
_sub_classes = [Reach]
def __init__(self, study):
def __init__(self, study, parent):
super(River, self).__init__(
id=-1, status=study.status,
owner_scenario=study.status.scenario.id
)
self._study = study
self._parent = parent
# Dict with timestamps as key
self._reachs = []
@ -390,7 +413,7 @@ class River(SQLSubModel):
def add(self, reach_id):
reachs = self._study.river.enable_edges()
new = Reach(reachs[reach_id].reach, self._study)
new = Reach(reachs[reach_id].reach, self._study, self._parent)
self._reachs.append(new)
return new
@ -414,7 +437,9 @@ class River(SQLSubModel):
@classmethod
def _db_load(cls, execute, data=None):
study = data["study"]
new_river = cls(study)
parent = data["parent"]
new_river = cls(study, parent)
for reach in study.river.reachs():
data["reach"] = reach.reach

View File

@ -1098,6 +1098,9 @@ class Mage8(Mage):
i1 = data[2*i] - 1
i2 = data[2*i+1] - 1
# Keep profiles index of all river
r.set_global_index(range(i1, i2 + 1))
# Add profile id correspondance to reach
for key in range(i1, i2 + 1):
iprofiles.append(i)
@ -1171,6 +1174,11 @@ class Mage8(Mage):
endline()
end = newline().size <= 0
ts_index = {}
for i, t in enumerate(sorted(ts)):
ts_index[t] = i
results.set_timestamp_index(ts_index)
table = {}
for k in tmp_table:
table[k] = np.array(tmp_table[k])