Skip to content

Commit

Permalink
Major refactoring to be closer to Crue10 naming (in cpp)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucduron committed Jan 9, 2020
1 parent 5a7c2d1 commit df866bb
Show file tree
Hide file tree
Showing 45 changed files with 666 additions and 653 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ Crue10 tools
## Crue10 API - Parseur de fichiers

- Lecture d'une étude et la géométrie
- [Lire une étude](snippets/read_studies.py)
- [Lire un modèle](snippets/read_model.py)
- [Lire un sous-modèle et ses EMHs](snippets/read_submodel.py)
- [Lire une étude](snippets/lire_etudes.py)
- [Lire un modèle](snippets/lire_modele.py)
- [Lire un sous-modèle et ses EMHs](snippets/lire_sous_modele.py)
- Écriture d'une étude et la géométrie
- [Écrire un sous-modèle](snippets/write_submodel_from_scratch.py)
- [Écrire une étude](snippets/write_study_from_scratch.py)
- Calcul (= lancer un run)
- [Créer et lancer une série de runs](snippets/run_calculations.py)
- Post-traitement de résultats (d'un run)
- [Lire un run avec ses résultats](snippets/read_run_and_results.py)
- [Lire un modèle et les résultats d'un run associé](snippets/read_model_and_run.py)
- [Lire un run avec ses résultats](snippets/lire_run_et_resultats.py)
- [Lire un modèle et les résultats d'un run associé](snippets/lire_modele_et_run.py)

## Scripts en ligne de commande

Expand Down
22 changes: 11 additions & 11 deletions cli/crue10_merge_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"""
import sys

from crue10.study import Study
from crue10.etude import Etude
from crue10.utils import CrueError, logger
from crue10.utils.cli_parser import MyArgParse

Expand Down Expand Up @@ -50,33 +50,33 @@ def crue10_merge_models(args):
if not reference_found:
raise CrueError("Le noeud de référence n'a pas pu être trouvé pour : `%s`" % nodes_str)

study_out = Study(args.etu_path_out, access='w')
study_out.create_empty_scenario('Sc_%s' % args.out_name, 'Mo_%s' % args.out_name, submodel_name=None)
model_out = study_out.get_model('Mo_%s' % args.out_name)
study_out = Etude(args.etu_path_out, access='w')
study_out.create_empty_scenario('Sc_%s' % args.out_name, 'Mo_%s' % args.out_name, nom_sous_modele=None)
model_out = study_out.get_modele('Mo_%s' % args.out_name)

for i, (etu_path, mo_name, suffix) in enumerate(zip(args.etu_path_list, args.mo_name_list, args.suffix_list)):
study_in = Study(etu_path, access='r')
model_in = study_in.get_model(mo_name)
study_in = Etude(etu_path, access='r')
model_in = study_in.get_modele(mo_name)
model_in.read_all()

# Rename all EMHs and set optional shared nodes
model_in.rename_emhs(suffix, emhs_to_preserve=noeud_id_list[i])
for j, noeud_id in enumerate(noeud_id_list[i]):
if noeud_id != '':
if noeud_id not in [nd.id for nd in model_in.get_noeud_list()]:
if noeud_id not in [nd.id for nd in model_in.get_liste_noeuds()]:
raise CrueError("Le noeud %s n'est pas dans le modèle %s" % (noeud_id, model_in.id))
model_in.rename_noeud(noeud_id, noeud_id_references[j])

model_out.append_from_model(model_in)
model_out.ajouter_depuis_modele(model_in)

logger.info("~> Étude en sortie:")
logger.info(study_out)
logger.info(model_out.summary())
model_out.log_duplicated_emh()

for submodel in model_out.submodels:
study_out.add_submodel(submodel)
logger.info(submodel.summary())
for sous_modele in model_out.sous_modeles:
study_out.ajouter_sous_modele(sous_modele)
logger.info(sous_modele.summary())

study_out.write_all()

Expand Down
28 changes: 14 additions & 14 deletions cli/crue10_merge_submodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@
"""
import sys

from crue10.study import Study
from crue10.submodel import SubModel
from crue10.etude import Etude
from crue10.sous_modele import SousModele
from crue10.utils import CrueError, logger
from crue10.utils.cli_parser import MyArgParse


def crue10_merge_submodels(args):
def crue10_merge_sous_modeles(args):
if len(args.etu_path_list) != len(args.sm_name_list) != len(args.suffix_list):
raise CrueError("Les arguments `etu_path_list`, `suffix_list` et `sm_name_list` n'ont pas la même longueur !")

first = True
merged_submodel = None
merged_sous_modele = None
for etu_path, sm_name, suffix in zip(args.etu_path_list, args.sm_name_list, args.suffix_list):
study = Study(etu_path)
submodel = study.get_submodel(sm_name)
submodel.read_all()
print(submodel)
study = Etude(etu_path)
sous_modele = study.get_sous_modele(sm_name)
sous_modele.read_all()
print(sous_modele)

if first:
merged_submodel = SubModel(submodel.id, submodel.files)
merged_sous_modele = SousModele(sous_modele.id, sous_modele.files)
first = False
merged_submodel.add_emh_from_submodel(submodel, suffix)
merged_sous_modele.ajouter_emh_depuis_sous_modele(sous_modele, suffix)

print("~> Merged submodel:")
print(merged_submodel)
merged_submodel.write_all(args.output_folder)
print("~> Merged sous_modele:")
print(merged_sous_modele)
merged_sous_modele.write_all(args.output_folder)


parser = MyArgParse(description=__doc__)
Expand All @@ -44,7 +44,7 @@ def crue10_merge_submodels(args):
if __name__ == '__main__':
args = parser.parse_args()
try:
crue10_merge_submodels(args)
crue10_merge_sous_modeles(args)
except CrueError as e:
logger.critical(e)
sys.exit(1)
18 changes: 9 additions & 9 deletions cli/crue10_model_extract_shp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
"""
import sys

from crue10.study import Study
from crue10.etude import Etude
from crue10.utils import CrueError, logger
from crue10.utils.cli_parser import MyArgParse


def crue10_model_extract_shp(args):
study = Study(args.etu_path)
model = study.get_model(args.mo_name)
model.read_all()
print(model.summary())
study = Etude(args.etu_path)
model = study.get_modele(args.mo_name)
modele.read_all()
print(modele.summary())

if args.shp_sections:
model.write_shp_sectionprofil_as_points(args.shp_sections)
modele.write_shp_sectionprofil_as_points(args.shp_sections)

if args.shp_limits:
for submodel in model.submodels:
submodel.convert_sectionidem_to_sectionprofil()
model.write_shp_limites_lits_numerotes(args.shp_limits)
for sous_modele in modele.sous_modeles:
sous_modele.convert_sectionidem_to_sectionprofil()
modele.write_shp_limites_lits_numerotes(args.shp_limits)


parser = MyArgParse(description=__doc__)
Expand Down
62 changes: 31 additions & 31 deletions cli/crue10_model_for_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@

from crue10.emh.branche import Branche
from crue10.emh.section import SectionProfil
from crue10.study import Study
from crue10.etude import Etude
from crue10.utils.cli_parser import MyArgParse
from crue10.utils import CrueError, logger


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

if args.model_name:
model_name = args.model_name
else:
model_name = model.id
model_name = modele.id

if not os.path.exists(args.out_folder):
os.makedirs(args.out_folder)
Expand All @@ -47,14 +47,14 @@ def crue10_model_for_maps(args):
'properties': OrderedDict([('NOM', 'str'), ('modele', 'str'), ('sousmodele', 'str')]),
}
with fiona.open(os.path.join(args.out_folder, 'noeuds.shp'), 'w', 'ESRI Shapefile', schema) as out_shp:
for submodel in model.submodels:
for _, noeud in submodel.noeuds.items():
for sous_modele in modele.sous_modeles:
for _, noeud in sous_modele.noeuds.items():
layer = {
'geometry': mapping(noeud.geom),
'properties': {
'NOM': noeud.id,
'modele': model_name,
'sousmodele': submodel.id,
'sousmodele': sous_modele.id,
}
}
out_shp.write(layer)
Expand All @@ -63,18 +63,18 @@ def crue10_model_for_maps(args):
schema = {
'geometry': 'Polygon',
'properties': OrderedDict([('NOM', 'str'), ('modele', 'str'), ('sousmodele', 'str'),
('noeud', 'str'), ('is_active', 'bool')]),
('noeud_reference', 'str'), ('is_active', 'bool')]),
}
with fiona.open(os.path.join(args.out_folder, 'casiers.shp'), 'w', 'ESRI Shapefile', schema) as out_shp:
for submodel in model.submodels:
for _, casier in submodel.casiers.items():
for sous_modele in modele.sous_modeles:
for _, casier in sous_modele.casiers.items():
layer = {
'geometry': mapping(Polygon(casier.geom)),
'properties': {
'NOM': casier.id,
'modele': model_name,
'sousmodele': submodel.id,
'noeud': casier.noeud.id,
'sousmodele': sous_modele.id,
'noeud_reference': casier.noeud_reference.id,
'is_active': casier.is_active,
}
}
Expand All @@ -87,14 +87,14 @@ def crue10_model_for_maps(args):
('nd_aval', 'str'), ('type', 'int'), ('is_active', 'bool')]),
}
with fiona.open(os.path.join(args.out_folder, 'branches.shp'), 'w', 'ESRI Shapefile', schema) as out_shp:
for submodel in model.submodels:
for _, branche in submodel.branches.items():
for sous_modele in modele.sous_modeles:
for _, branche in sous_modele.branches.items():
layer = {
'geometry': mapping(branche.geom),
'properties': {
'NOM': branche.id,
'modele': model_name,
'sousmodele': submodel.id,
'sousmodele': sous_modele.id,
'nd_amont': branche.noeud_amont.id,
'nd_aval': branche.noeud_aval.id,
'type': branche.type,
Expand All @@ -110,36 +110,36 @@ def crue10_model_for_maps(args):
('id_branche', 'str'), ('xp', 'float')]),
}
with fiona.open(os.path.join(args.out_folder, 'sections.shp'), 'w', 'ESRI Shapefile', schema) as out_shp:
for submodel in model.submodels:
for section in submodel.iter_on_sections(section_type=SectionProfil, ignore_inactive=True):
for sous_modele in modele.sous_modeles:
for section in sous_modele.iter_on_sections(section_type=SectionProfil, ignore_inactive=True):
layer = {
'geometry': mapping(section.geom_trace),
'properties': {
'NOM': section.id,
'modele': model_name,
'sousmodele': submodel.id,
'sousmodele': sous_modele.id,
'id_branche': branche.id,
'xp': section.xp,
}
}
out_shp.write(layer)

# Submodels
# sous_modeles
mo_geom_list = []
schema = {
'geometry': 'Polygon',
'properties': OrderedDict([('NOM', 'str'), ('modele', 'str')]),
}
with fiona.open(os.path.join(args.out_folder, 'sous-modeles.shp'), 'w', 'ESRI Shapefile', schema) as out_shp:
for submodel in model.submodels:
submodel.remove_sectioninterpolee()
submodel.normalize_geometry() # replace SectionIdem by SectionProfil
for sous_modele in modele.sous_modeles:
sous_modele.remove_sectioninterpolee()
sous_modele.normalize_geometry() # replace SectionIdem by SectionProfil

sm_geom_list = []
for _, casier in submodel.casiers.items():
for _, casier in sous_modele.casiers.items():
sm_geom_list.append(Polygon(casier.geom).buffer(args.sm_buffer))

for branche in submodel.iter_on_branches():
for branche in sous_modele.iter_on_branches():
if branche.type in Branche.TYPES_WITH_GEOM:
# Build list of coordinates following upstream section, left ending points, downstream section and
# right ending point
Expand All @@ -159,7 +159,7 @@ def crue10_model_for_maps(args):
# Add geometry of the branch (necessary for branches without geometry)
sm_geom_list.append(branche.geom.buffer(args.sm_buffer))

for _, noeud in submodel.noeuds.items():
for _, noeud in sous_modele.noeuds.items():
sm_geom_list.append(noeud.geom.buffer(args.sm_buffer))

mo_geom_list += sm_geom_list
Expand All @@ -168,13 +168,13 @@ def crue10_model_for_maps(args):
layer = {
'geometry': mapping(sm_zone),
'properties': {
'NOM': submodel.id,
'NOM': sous_modele.id,
'modele': model_name,
}
}
out_shp.write(layer)

# Model
# Modele
schema = {
'geometry': 'Polygon',
'properties': OrderedDict([('NOM', 'str'), ('id_modele', 'str'), ('bief', 'str'),
Expand All @@ -187,7 +187,7 @@ def crue10_model_for_maps(args):
'geometry': mapping(mo_zone),
'properties': {
'NOM': model_name,
'id_modele': model.id,
'id_modele': modele.id,
'bief': args.bief,
'auteurs': args.auteurs,
'date': args.date,
Expand All @@ -202,7 +202,7 @@ def crue10_model_for_maps(args):
parser.add_argument('out_folder', help="chemin du dossier pour les fichiers shp de sortie")

parser_attributes = parser.add_argument_group("Arguments pour écrire les méta-données")
parser_attributes.add_argument('--model_name', help="nom du modèle", default='')
parser_attributes.add_argument('--nom_modele', help="nom du modèle", default='')
parser_attributes.add_argument('--bief', help="bigramme du bief", default='')
parser_attributes.add_argument('--auteurs', help="détails des auteurs", default='')
parser_attributes.add_argument('--date', help="date de valeur (format : JJ/MM/AAAA)", default='')
Expand Down
16 changes: 8 additions & 8 deletions cli/crue10_model_geom_to_mascaret.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
"""
import sys

from crue10.study import Study
from crue10.etude import Etude
from crue10.utils.cli_parser import MyArgParse
from crue10.utils import CrueError, logger


def crue10_model_geom_to_mascaret(args):
study = Study(args.etu_path)
model = study.get_model(args.mo_name)
model.read_all()
for submodel in model.submodels:
submodel.remove_sectioninterpolee()
submodel.normalize_geometry()
model.write_mascaret_geometry(args.georef_path)
study = Etude(args.etu_path)
model = study.get_modele(args.mo_name)
modele.read_all()
for sous_modele in modele.sous_modeles:
sous_modele.remove_sectioninterpolee()
sous_modele.normalize_geometry()
modele.write_mascaret_geometry(args.georef_path)


parser = MyArgParse(description=__doc__)
Expand Down
Loading

0 comments on commit df866bb

Please sign in to comment.