Skip to content

Commit

Permalink
[1.0] End of major refactoring (to fit to cpp)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucduron committed Jan 9, 2020
1 parent df866bb commit d094849
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 67 deletions.
14 changes: 7 additions & 7 deletions cli/crue10_model_for_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

def crue10_model_for_maps(args):
study = Etude(args.etu_path)
model = study.get_modele(args.mo_name)
modele = study.get_modele(args.mo_name)
modele.read_all()
logger.info(modele.summary())

Expand Down Expand Up @@ -63,7 +63,7 @@ def crue10_model_for_maps(args):
schema = {
'geometry': 'Polygon',
'properties': OrderedDict([('NOM', 'str'), ('modele', 'str'), ('sousmodele', 'str'),
('noeud_reference', 'str'), ('is_active', 'bool')]),
('noeud', 'str'), ('is_active', 'bool')]),
}
with fiona.open(os.path.join(args.out_folder, 'casiers.shp'), 'w', 'ESRI Shapefile', schema) as out_shp:
for sous_modele in modele.sous_modeles:
Expand All @@ -74,7 +74,7 @@ def crue10_model_for_maps(args):
'NOM': casier.id,
'modele': model_name,
'sousmodele': sous_modele.id,
'noeud_reference': casier.noeud_reference.id,
'noeud': casier.noeud_reference.id,
'is_active': casier.is_active,
}
}
Expand Down Expand Up @@ -144,11 +144,11 @@ def crue10_model_for_maps(args):
# Build list of coordinates following upstream section, left ending points, downstream section and
# right ending point
coords = []
coords += branche.sections[0].geom_trace.coords
for section in branche.sections[1:-1]:
coords += branche.get_section_amont().geom_trace.coords
for section in branche.liste_sections_dans_branche[1:-1]:
coords.append(section.geom_trace.coords[-1])
coords += reversed(branche.sections[-1].geom_trace.coords)
for section in reversed(branche.sections[1:-1]):
coords += reversed(branche.get_section_aval().geom_trace.coords)
for section in reversed(branche.liste_sections_dans_branche[1:-1]):
coords.append(section.geom_trace.coords[0])

polygon = Polygon(coords).buffer(args.sm_buffer)
Expand Down
2 changes: 1 addition & 1 deletion cli/crue10_model_topographical_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* il s'agit d'une vue schématique avec tous les noeuds/casiers et les branches
(aucune information géographique)
* l'orientation des branches correspond au sens de la ligne qui se termine par
une flèche (branche fluviale) ou un symbole qui est "côté noeud_reference aval"
une flèche (branche fluviale) ou un symbole qui est "côté noeud aval"
(la seule exception concerne les branches orifices dont la position du symbole
tient compte du sens de l'orifice)
* les branches ou sous-modèles inactifs sont en pointillés
Expand Down
2 changes: 1 addition & 1 deletion cli/crue9_dc_topographical_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* il s'agit d'une vue schématique avec tous les noeuds/casiers et les branches
(aucune information géographique)
* l'orientation des branches correspond au sens de la ligne qui se termine par
une flèche (branche fluviale) ou un symbole qui est "côté noeud_reference aval"
une flèche (branche fluviale) ou un symbole qui est "côté noeud aval"
* la coloration des lignes dépend du type de branches
* la forme des noeuds et des casiers (avec leur nom associé) sont différentes
* les parties commentées ou shunter (par un GOTO) sont ignorées
Expand Down
6 changes: 3 additions & 3 deletions crue10/data/templates/drso.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<NdAm NomRef="{{ branche.noeud_amont.id }}"/>
<NdAv NomRef="{{ branche.noeud_aval.id }}"/>
<BrancheSaintVenant-Sections>
{%- for section in branche.sections %}
{%- for section in branche.liste_sections_dans_branche %}
<BrancheSaintVenant-Section NomRef="{{ section.id }}">
<Pos>{% if loop.first %}Amont{% elif loop.last %}Aval{% else %}Interne{% endif %}</Pos>
<Xp>{{ section.xp|float2str }}</Xp>
Expand All @@ -76,11 +76,11 @@
<SectionPilote NomRef="{{ branche.section_pilote.id }}"/>
{%- endif %}
<Branche-Sections>
<Branche-Section NomRef="{{ branche.sections[0].id }}">
<Branche-Section NomRef="{{ branche.get_section_amont().id }}">
<Pos>Amont</Pos>
<Xp>0.0</Xp>
</Branche-Section>
<Branche-Section NomRef="{{ branche.sections[-1].id }}">
<Branche-Section NomRef="{{ branche.get_section_aval().id }}">
<Pos>Aval</Pos>
<Xp>{{ branche.length }}</Xp>
</Branche-Section>
Expand Down
28 changes: 17 additions & 11 deletions crue10/emh/branche.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Branche(ABC):
- geom <LineString>: polyline branch trace
- noeud_amont <crue10.emh.noeud.Noeud>: upstream node
- noeud_aval <crue10.emh.noeud.Noeud>: downstream node
- liste_sections <[crue10.emh.section.Section]>: list of sections
- liste_sections_dans_branche <[crue10.emh.section.Section]>: list of sections
- comment <str>: optional text explanation
"""

Expand Down Expand Up @@ -88,7 +88,7 @@ def __init__(self, nom_branche, noeud_amont, noeud_aval, branch_type, is_active=
self.geom = None
self.noeud_amont = noeud_amont
self.noeud_aval = noeud_aval
self.sections = []
self.liste_sections_dans_branche = []
self.comment = ''

@staticmethod
Expand All @@ -98,11 +98,17 @@ def get_id_type_from_name(branch_type_name):
return type_id
return None

def get_section_amont(self):
return self.liste_sections_dans_branche[0]

def get_section_aval(self):
return self.liste_sections_dans_branche[-1]

@property
def length(self):
"""Length displayed in FC (may differ from geometry)"""
if self.type in Branche.TYPES_WITH_LENGTH:
return self.sections[-1].xp
return self.get_section_aval().xp
else:
return 0.0

Expand All @@ -125,7 +131,7 @@ def ajouter_section_dans_branche(self, section, xp):
if not isinstance(section, SectionSansGeometrie):
raise CrueError("La %s ne peut porter que des SectionSansGeometrie" % self)
section.xp = xp
self.sections.append(section)
self.liste_sections_dans_branche.append(section)

def set_geom(self, geom):
check_isinstance(geom, LineString)
Expand All @@ -139,7 +145,7 @@ def shift_sectionprofil_to_extremity(self):
if self.geom is None:
raise CrueErrorGeometryNotFound(self)
for pos in (0, -1):
section = self.sections[pos]
section = self.liste_sections_dans_branche[pos]
if isinstance(section, SectionProfil):
if pos == 0:
node = self.noeud_amont.geom
Expand All @@ -151,28 +157,28 @@ def shift_sectionprofil_to_extremity(self):
if isinstance(section_point, Point):
dx = node.x - section_point.x
dy = node.y - section_point.y
self.sections[pos].set_trace(
self.liste_sections_dans_branche[pos].set_trace(
translate(section.geom_trace, xoff=dx, yoff=dy))

def normalize_sections_xp(self):
"""
Recompute section xp to correspond to geometric distance (original values are taken from drso).
Last section xp will correspond exactly to the branch length.
"""
xp_max = self.sections[-1].xp
xp_max = self.get_section_aval().xp
length = self.geom.length
if self.type in Branche.TYPES_WITH_LENGTH and abs(xp_max - length) > DIFF_XP_TO_WARN:
logger.warn("La longueur de la branche `%s` est estimée à %.2fm (non pas %.2fm)."
% (self.id, length, xp_max))
for i, section in enumerate(self.sections):
for i, section in enumerate(self.liste_sections_dans_branche):
try:
section.xp = section.xp * length / xp_max
except ZeroDivisionError:
section.xp = (i / (len(self.sections) - 1)) * length
section.xp = (i / (len(self.liste_sections_dans_branche) - 1)) * length

def __repr__(self):
return "Branche [%i] #%s: %s -> %s (%i sections)" % (self.type, self.id,
self.noeud_amont, self.noeud_aval, len(self.sections))
return "Branche [%i] #%s: %s -> %s (%i sections)" % (self.type, self.id, self.noeud_amont, self.noeud_aval,
len(self.liste_sections_dans_branche))


class BranchePdC(Branche):
Expand Down
6 changes: 3 additions & 3 deletions crue10/modele.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def ajouter_sous_modele(self, sous_modele):
self.liste_sous_modeles.append(sous_modele)

def ajouter_depuis_modele(self, modele):
"""Add sous_modele in current modele with the related initial conditions"""
"""Add modele in current modele with the related initial conditions"""
for sous_modele in modele.susoubmodels:
self.ajouter_sous_modele(sous_modele)

Expand Down Expand Up @@ -327,7 +327,7 @@ def write_mascaret_geometry(self, geo_path):
for i_branche, branche in enumerate(sous_modele.iter_on_branches([20])):
if branche.has_geom() and branche.is_active:
reach = Reach(i_branche, name=branche.id)
for section in branche.sections:
for section in branche.liste_sections_dans_branche:
if not isinstance(section, SectionProfil):
raise CrueError("The `%s`, which is not a SectionProfil, could not be written" % section)
masc_section = Section(i_section, section.xp, name=section.id)
Expand Down Expand Up @@ -357,7 +357,7 @@ def write_shp_limites_lits_numerotes(self, shp_path):
for branche in sous_modele.iter_on_branches():
for i_lit, lit_name in enumerate(LitNumerote.LIMIT_NAMES):
coords = []
for section in branche.sections:
for section in branche.liste_sections_dans_branche:
if isinstance(section, SectionProfil):
if i_lit == 0:
point = section.interp_point(section.lits_numerotes[0].xt_min)
Expand Down
10 changes: 4 additions & 6 deletions crue10/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class RunResults:
"""
Results data for a single Run
- rcal_root <ET..Element>: rcal XML element
- rcal_path <str>: path to rcal file
- rcal_folder <str>: folder path to rcal file
- nb_errors <int>: number of errors in ccal.csv
- nb_warnings <int>: number of warnings in ccal.csv
Expand All @@ -110,6 +111,7 @@ class RunResults:

def __init__(self, rcal_path):
self.rcal_root = ET.parse(rcal_path).getroot()
self.rcal_path = rcal_path
self.rcal_folder = os.path.dirname(rcal_path)
self.nb_errors = -1
self.nb_warnings = -1
Expand All @@ -128,11 +130,6 @@ def __init__(self, rcal_path):
self._read_rescalc()
self._set_res_pattern()

@property
def model_name(self):
"""Modele name with 'Mo_' preffix"""
return os.path.basename(self.rcal_folder)

@property
def run_id(self):
return os.path.basename(os.path.normpath(os.path.join(self.rcal_folder, '..')))
Expand All @@ -157,7 +154,8 @@ def _add_emh_names(self, elt, emh_sec):
self.emh[emh_sec].append(emh_name)

def _read_ccal(self):
ccal_path = os.path.join(self.rcal_folder, self.model_name[3:] + '.ccal.csv')
ccal_path = self.rcal_path[:-9] + '.ccal.csv' # to replace '.rcal.csv' => FIXME: it should be based on ocal
print(ccal_path)
if not os.path.exists(ccal_path):
raise CrueError("Le fichier de compte rendu de calcul `%s` est introuvable" % ccal_path)
self.nb_errors = 0
Expand Down
Loading

0 comments on commit d094849

Please sign in to comment.