diff --git a/src/Solver/Mage.py b/src/Solver/Mage.py index e7d95811ea25b2312d1e97893eabf1c2734ecf01..125fcfffe766ecafbf3e60f30ea7439e34932b3a 100644 --- a/src/Solver/Mage.py +++ b/src/Solver/Mage.py @@ -7,6 +7,15 @@ from tools import timer from Solver.ASolver import AbstractSolver from Checker.Mage import MageNetworkGraphChecker +def mage_file_open(filepath, mode): + f = open(filepath, mode) + + if "w" in mode: + # Write header + f.write("* This file is generate by PAMHYR, please don't modify\n") + + return f + class Mage(AbstractSolver): _type = "mage" @@ -92,9 +101,8 @@ class Mage(AbstractSolver): if edge._name == "": name = f"Reach_{edge.id}" - with open(os.path.join(repertory, f"{name}.ST"), "w+") as f: + with mage_file_open(os.path.join(repertory, f"{name}.ST"), "w+") as f: files.append(f"{name}.ST") - f.write("* This file is generate by PAMHYR, please don't modify\n") for profile in edge.reach.profiles: num = f"{profile.num:>6}" @@ -128,11 +136,9 @@ class Mage(AbstractSolver): if qlog is not None: qlog.put(f"Export {t} file") - with open(os.path.join(repertory, f"0.{t}"), "w+") as f: + with mage_file_open(os.path.join(repertory, f"0.{t}"), "w+") as f: files.append(f"0.{t}") - f.write("* This file is generate by PAMHYR, please don't modify\n") - for bound in bounds: name = f"{bound.node.id:3}".replace(" ", "x") f.write(f"* {bound.node.name} ({name}) {bound.bctype}\n") @@ -176,11 +182,9 @@ class Mage(AbstractSolver): # if qlog is not None: # qlog.put(f"Export LAT file") - # with open(os.path.join(repertory, f"0.LAT"), "w+") as f: + # with mage_file_open(os.path.join(repertory, f"0.LAT"), "w+") as f: # files.append(f"0.LAT") - # f.write("* This file is generate by PAMHYR, please don't modify\n") - # name = f"{lateral.node.id:3}".replace(" ", "x") # f.write(f"* {lateral.node.name} ({name}) {lateral.bctype}\n") # f.write(f"${name}\n") @@ -211,9 +215,8 @@ class Mage(AbstractSolver): qlog.put("Export RUG file") # Write header - with open(os.path.join(repertory, "0.RUG"), "w+") as f: + with mage_file_open(os.path.join(repertory, "0.RUG"), "w+") as f: files.append("0.RUG") - f.write("* This file is generate by PAMHYR, please don't modify\n") edges = study.river.edges() edges = list( @@ -253,7 +256,7 @@ class Mage(AbstractSolver): qlog.put("Export INI file") # Write header - with open(os.path.join(repertory, "0.INI"), "w+") as f: + with mage_file_open(os.path.join(repertory, "0.INI"), "w+") as f: has_ini = False id = 1 reachs = study.river.edges() @@ -264,7 +267,6 @@ class Mage(AbstractSolver): ) ) - f.write("* This file is generate by PAMHYR, please don't modify\n") # TODO put real date... f.write(f"$ date en minutes : 0.00\n") f.write(f"* IB IS discharge elevation kp\n") @@ -301,9 +303,8 @@ class Mage(AbstractSolver): qlog.put("Export REP file") # Write header - with open(os.path.join(repertory, f"0.REP"), "w+") as f: + with mage_file_open(os.path.join(repertory, f"0.REP"), "w+") as f: f.write("confirmation=non\n") - f.write("* This file is generate by PAMHYR, please don't modify\n") for file in files: EXT = file.split('.')[1] @@ -372,11 +373,9 @@ class Mage8(Mage): if qlog is not None: qlog.put("Export PAR file") - with open(os.path.join(repertory, "0.PAR"), "w+") as f: + with mage_file_open(os.path.join(repertory, "0.PAR"), "w+") as f: files.append("0.PAR") - f.write("* This file is generate by PAMHYR, please don't modify\n") - params = study.river.get_params(self.type).parameters for p in params: name = p.name\ @@ -396,11 +395,9 @@ class Mage8(Mage): if qlog is not None: qlog.put("Export NET file") - with open(os.path.join(repertory, "0.NET"), "w+") as f: + with mage_file_open(os.path.join(repertory, "0.NET"), "w+") as f: files.append("0.NET") - f.write("* This file is generate by PAMHYR, please don't modify\n") - edges = study.river.edges() edges = list( filter(