diff --git a/src/Solver/AdisTS.py b/src/Solver/AdisTS.py index 942afc166b3ccf86fbbdf89597d9e90dbe680d5b..d632d21f6625448aad904337ea19f0c5637f92df 100644 --- a/src/Solver/AdisTS.py +++ b/src/Solver/AdisTS.py @@ -16,8 +16,10 @@ # -*- coding: utf-8 -*- -import os +import os, glob import logging +from http.cookiejar import reach + import numpy as np import shutil @@ -457,6 +459,117 @@ class AdisTSlc(AdisTS): f.write(f"output = {id_reach} {kp} {title}\n") + @timer + def read_bin(self, study, repertory, results, qlog=None, name="0"): + repertory_results = os.path.join(repertory, "resultats") + fname = os.path.join(repertory, f"{name}.BIN") + fname = "AAA-silt.bin" + logger.info(f"read_bin: Start reading '{fname}' ...") + files_bin_names = [el.split("/")[-1] for el in glob.glob(repertory_results+"/*.bin")] + print("files names resultats: ", files_bin_names) + + ifilename = os.path.join(repertory_results, files_bin_names[0]) + + print("reading ", ifilename) + with open(ifilename, 'rb') as f: + # header + # first line + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (start) + data = np.fromfile(f, dtype=np.int32, count=3) + ibmax = data[0] # number of reaches + ismax = data[1] # total number of cross sections + kbl = data[2] * -1 # block size for .BIN header + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (end) + # second line + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (start) + ibu = np.fromfile(f, dtype=np.int32, count=ibmax) + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (end) + # third line + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (start) + data = np.fromfile(f, dtype=np.int32, count=2 * ibmax) + is1 = np.zeros(ibmax, dtype=np.int32) + is2 = np.zeros(ibmax, dtype=np.int32) + print("nombre de biefs : ", ibmax) + + logger.debug(f"read_bin: nb_reach = {ibmax}") + logger.debug(f"read_bin: nb_profile = {ismax}") + + results.set("nb_reach", f"{ibmax}") + results.set("nb_profile", f"{ismax}") + + reachs = [] + iprofiles = {} + reach_offset = {} + + for i in range(ibmax): + # Add results reach to reach list + r = results.river.add(i) + reachs.append(r) + + is1[i] = data[2 * i] # first section of reach i (FORTRAN numbering) + is2[i] = data[2 * i + 1] # last section of reach i (FORTRAN numbering) + + key = (is1[i], is2[i]) + iprofiles[key] = r + + reach_offset[r] = is1[i] + + logger.debug(f"read_bin: iprofiles = {iprofiles}") + + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (end) + # fourth line + pk = np.zeros(ismax, dtype=np.float32) + for k in range(0, ismax, kbl): + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (start) + pk[k:min(k + kbl, ismax)] = np.fromfile(f, dtype=np.float32, count=min(k + kbl, ismax) - k) + print("pk : ", pk) + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (end) + + # fifth line (useless) + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (start) + zmin_OLD = np.fromfile(f, dtype=np.float32, count=1)[0] + print("zmin_OLD : ", zmin_OLD) + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (end) + # sixth line + zf = np.zeros(ismax, dtype=np.float32) + z = np.zeros(ismax * 3, dtype=np.float32) + for k in range(0, ismax, kbl): + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (start) + z[3 * k:3 * min(k + kbl, ismax)] = np.fromfile(f, dtype=np.float32, + count=3 * (min(k + kbl, ismax) - k)) + # z[i*3+1] and z[i*3+2] are useless + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (end) + zf = [z[i * 3] for i in range(ismax)] + print("zf : ", zf) + # seventh line (useless) + for k in range(0, ismax, kbl): + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (start) + zero = np.fromfile(f, dtype=np.int32, count=ismax) + print("zero : ", zero) + data = np.fromfile(f, dtype=np.int32, count=1) # line length (bytes) (end) + # end header + + def ip_to_r(i): return iprofiles[ + next( + filter( + lambda k: k[0] <= i <= k[1], + iprofiles + ) + ) + ] + + def ip_to_ri(r, i): return i - reach_offset[r] + + @timer + def results(self, study, repertory, qlog=None, name=None): + self._study = study + if name is None: + name = study.name.replace(" ", "_") + + results = super(AdisTSlc, self).results(study, repertory, qlog, name=name) + + return results + def export_func_dict(self): return [ self._export_NUM,