From b28fb0808874446749300c78ecf9dd3695e1bd25 Mon Sep 17 00:00:00 2001 From: MuellerSeb Date: Wed, 27 Feb 2019 20:56:24 +0100 Subject: [PATCH] path handling platform independent --- welltestpy/data/campaignlib.py | 56 ++++++--------- welltestpy/data/testslib.py | 30 +++----- welltestpy/data/varlib.py | 80 ++++++++------------- welltestpy/estimate/estimatelib.py | 110 +++++++++++++++-------------- 4 files changed, 118 insertions(+), 158 deletions(-) diff --git a/welltestpy/data/campaignlib.py b/welltestpy/data/campaignlib.py index d5c2646..d9d229c 100644 --- a/welltestpy/data/campaignlib.py +++ b/welltestpy/data/campaignlib.py @@ -107,7 +107,7 @@ def coordinates(self, coordinates): def __repr__(self): return self.name - def save(self, path="./", name=None): + def save(self, path="", name=None): """Save a field site to file. This writes the field site to a csv file. @@ -115,7 +115,7 @@ def save(self, path="./", name=None): Parameters ---------- path : :class:`str`, optional - Path where the variable should be saved. Default: ``"./"`` + Path where the variable should be saved. Default: ``""`` name : :class:`str`, optional Name of the file. If ``None``, the name will be generated by ``"Field_"+name``. Default: ``None`` @@ -124,16 +124,9 @@ def save(self, path="./", name=None): ----- The file will get the suffix ``".fds"``. """ - # ensure that 'path' is a string [ needed ?! ] - # path = _formstr(path) - # ensure that 'path' ends with a '/' if it's not empty - if path != "" and path[-1] != "/": - path += "/" - if path == "": - path = "./" + path = os.path.normpath(path) # create the path if not existing - if not os.path.exists(path): - os.makedirs(path) + os.makedirs(path, exist_ok=True) # create a standard name if None is given if name is None: name = "Field_" + self.name @@ -142,14 +135,13 @@ def save(self, path="./", name=None): name += ".fds" name = _formname(name) # create temporal directory for the included files - tmp = ".tmpfield/" - patht = path + tmp + patht = os.path.join(path, ".tmpfield") if os.path.exists(patht): shutil.rmtree(patht, ignore_errors=True) os.makedirs(patht) # write the csv-file # with open(patht+name[:-4]+".csv", 'w') as csvf: - with open(patht + "info.csv", "w") as csvf: + with open(os.path.join(patht, "info.csv"), "w") as csvf: writer = csv.writer(csvf, quoting=csv.QUOTE_NONNUMERIC) writer.writerow(["Fieldsite"]) writer.writerow(["name", self.name]) @@ -163,10 +155,10 @@ def save(self, path="./", name=None): else: writer.writerow(["coordinates", "None"]) # compress everything to one zip-file - with zipfile.ZipFile(path + name, "w") as zfile: - zfile.write(patht + "info.csv", "info.csv") + with zipfile.ZipFile(os.path.join(path, name), "w") as zfile: + zfile.write(os.path.join(patht, "info.csv"), "info.csv") if self._coordinates is not None: - zfile.write(patht + coordname, coordname) + zfile.write(os.path.join(patht, coordname), coordname) # delete the temporary directory shutil.rmtree(patht, ignore_errors=True) @@ -504,7 +496,7 @@ def plot_wells(self, **kwargs): """ WellPlot(self, **kwargs) - def save(self, path="./", name=None): + def save(self, path="", name=None): """Save the campaign to file. This writes the campaign to a csv file. @@ -512,7 +504,7 @@ def save(self, path="./", name=None): Parameters ---------- path : :class:`str`, optional - Path where the variable should be saved. Default: ``"./"`` + Path where the variable should be saved. Default: ``""`` name : :class:`str`, optional Name of the file. If ``None``, the name will be generated by ``"Cmp_"+name``. Default: ``None`` @@ -521,16 +513,9 @@ def save(self, path="./", name=None): ----- The file will get the suffix ``".cmp"``. """ - # ensure that 'path' is a string [ needed ?! ] - # path = _formstr(path) - # ensure that 'path' ends with a '/' if it's not empty - if path != "" and path[-1] != "/": - path += "/" - if path == "": - path = "./" + path = os.path.normpath(path) # create the path if not existing - if not os.path.exists(path): - os.makedirs(path) + os.makedirs(path, exist_ok=True) # create a standard name if None is given if name is None: name = "Cmp_" + self.name @@ -539,14 +524,13 @@ def save(self, path="./", name=None): name += ".cmp" name = _formname(name) # create temporal directory for the included files - tmp = ".tmpcmp/" - patht = path + tmp + patht = os.path.join(path, ".tmpcmp") if os.path.exists(patht): shutil.rmtree(patht, ignore_errors=True) os.makedirs(patht) # write the csv-file # with open(patht+name[:-4]+".csv", 'w') as csvf: - with open(patht + "info.csv", "w") as csvf: + with open(os.path.join(patht, "info.csv"), "w") as csvf: writer = csv.writer(csvf, quoting=csv.QUOTE_NONNUMERIC) writer.writerow(["Campaign"]) writer.writerow(["name", self.name]) @@ -578,14 +562,14 @@ def save(self, path="./", name=None): self.tests[k].save(patht, testsname[k]) # compress everything to one zip-file - with zipfile.ZipFile(path + name, "w") as zfile: - zfile.write(patht + "info.csv", "info.csv") + with zipfile.ZipFile(os.path.join(path, name), "w") as zfile: + zfile.write(os.path.join(patht, "info.csv"), "info.csv") if self._fieldsite is not None: - zfile.write(patht + fieldsname, fieldsname) + zfile.write(os.path.join(patht, fieldsname), fieldsname) for k in wkeys: - zfile.write(patht + wellsname[k], wellsname[k]) + zfile.write(os.path.join(patht, wellsname[k]), wellsname[k]) for k in tkeys: - zfile.write(patht + testsname[k], testsname[k]) + zfile.write(os.path.join(patht, testsname[k]), testsname[k]) # delete the temporary directory shutil.rmtree(patht, ignore_errors=True) diff --git a/welltestpy/data/testslib.py b/welltestpy/data/testslib.py index bbe842b..889d078 100644 --- a/welltestpy/data/testslib.py +++ b/welltestpy/data/testslib.py @@ -412,7 +412,7 @@ def _addplot(self, plt_ax, wells, exclude=None): # plt_ax.legend(loc='best', fancybox=True, framealpha=0.75) - def save(self, path="./", name=None): + def save(self, path="", name=None): """Save a pumping test to file. This writes the variable to a csv file. @@ -420,7 +420,7 @@ def save(self, path="./", name=None): Parameters ---------- path : :class:`str`, optional - Path where the variable should be saved. Default: ``"./"`` + Path where the variable should be saved. Default: ``""`` name : :class:`str`, optional Name of the file. If ``None``, the name will be generated by ``"Test_"+name``. Default: ``None`` @@ -429,16 +429,9 @@ def save(self, path="./", name=None): ----- The file will get the suffix ``".tst"``. """ - # ensure that 'path' is a string [ needed ?! ] - # path = _formstr(path) - # ensure that 'path' ends with a '/' if it's not empty - if path != "" and path[-1] != "/": - path += "/" - if path == "": - path = "./" + path = os.path.normpath(path) # create the path if not existing - if not os.path.exists(path): - os.makedirs(path) + os.makedirs(path, exist_ok=True) # create a standard name if None is given if name is None: name = "Test_" + self.name @@ -447,14 +440,13 @@ def save(self, path="./", name=None): name += ".tst" name = _formname(name) # create temporal directory for the included files - tmp = ".tmptest/" - patht = path + tmp + patht = os.path.join(path, ".tmptest") if os.path.exists(patht): shutil.rmtree(patht, ignore_errors=True) os.makedirs(patht) # write the csv-file # with open(patht+name[:-4]+".csv", 'w') as csvf: - with open(patht + "info.csv", "w") as csvf: + with open(os.path.join(patht, "info.csv"), "w") as csvf: writer = csv.writer(csvf, quoting=csv.QUOTE_NONNUMERIC) writer.writerow(["Testtype", "PumpingTest"]) writer.writerow(["name", self.name]) @@ -481,12 +473,12 @@ def save(self, path="./", name=None): self.observations[k].save(patht, obsname[k]) # compress everything to one zip-file with zipfile.ZipFile(path + name, "w") as zfile: - zfile.write(patht + "info.csv", "info.csv") - zfile.write(patht + pumprname, pumprname) - zfile.write(patht + aquidname, aquidname) - zfile.write(patht + aquirname, aquirname) + zfile.write(os.path.join(patht, "info.csv"), "info.csv") + zfile.write(os.path.join(patht, pumprname), pumprname) + zfile.write(os.path.join(patht, aquidname), aquidname) + zfile.write(os.path.join(patht, aquirname), aquirname) for k in okeys: - zfile.write(patht + obsname[k], obsname[k]) + zfile.write(os.path.join(patht, obsname[k]), obsname[k]) # delete the temporary directory shutil.rmtree(patht, ignore_errors=True) diff --git a/welltestpy/data/varlib.py b/welltestpy/data/varlib.py index 6f12f4a..70212b8 100644 --- a/welltestpy/data/varlib.py +++ b/welltestpy/data/varlib.py @@ -164,7 +164,7 @@ def __repr__(self): def __str__(self): return str(self.name) + " " + self.label - def save(self, path="./", name=None): + def save(self, path="", name=None): """Save a variable to file. This writes the variable to a csv file. @@ -172,7 +172,7 @@ def save(self, path="./", name=None): Parameters ---------- path : :class:`str`, optional - Path where the variable should be saved. Default: ``"./"`` + Path where the variable should be saved. Default: ``""`` name : :class:`str`, optional Name of the file. If ``None``, the name will be generated by ``"Var_"+name``. Default: ``None`` @@ -181,16 +181,9 @@ def save(self, path="./", name=None): ----- The file will get the suffix ``".var"``. """ - # ensure that 'path' is a string [ needed ?! ] - # path = _formstr(path) - # ensure that 'path' ends with a '/' if it's not empty - if path != "" and path[-1] != "/": - path += "/" - if path == "": - path = "./" + path = os.path.normpath(path) # create the path if not existing - if not os.path.exists(path): - os.makedirs(path) + os.makedirs(path, exist_ok=True) # create a standard name if None is given if name is None: name = "Var_" + self.name @@ -198,8 +191,9 @@ def save(self, path="./", name=None): if name[-4:] != ".var": name += ".var" name = _formname(name) + file_path = os.path.join(path, name) # write the csv-file - with open(path + name, "w") as csvf: + with open(file_path, "w") as csvf: writer = csv.writer(csvf, quoting=csv.QUOTE_NONNUMERIC) writer.writerow(["Variable"]) writer.writerow(["name", self.name]) @@ -639,7 +633,7 @@ def edit(self): if self.state == "transient" and len(np.shape(self.time)) == 1: Editor(self) - def save(self, path="./", name=None): + def save(self, path="", name=None): """Save an observation to file. This writes the observation to a csv file. @@ -647,7 +641,7 @@ def save(self, path="./", name=None): Parameters ---------- path : :class:`str`, optional - Path where the variable should be saved. Default: ``"./"`` + Path where the variable should be saved. Default: ``""`` name : :class:`str`, optional Name of the file. If ``None``, the name will be generated by ``"Obs_"+name``. Default: ``None`` @@ -656,16 +650,9 @@ def save(self, path="./", name=None): ----- The file will get the suffix ``".obs"``. """ - # ensure that 'path' is a string [ needed ?! ] - # path = _formstr(path) - # ensure that 'path' ends with a '/' if it's not empty - if path != "" and path[-1] != "/": - path += "/" - if path == "": - path = "./" + path = os.path.normpath(path) # create the path if not existing - if not os.path.exists(path): - os.makedirs(path) + os.makedirs(path, exist_ok=True) # create a standard name if None is given if name is None: name = "Obs_" + self.name @@ -674,14 +661,13 @@ def save(self, path="./", name=None): name += ".obs" name = _formname(name) # create temporal directory for the included files - tmp = ".tmpobserv/" - patht = path + tmp + patht = os.path.join(path, ".tmpobserv") if os.path.exists(patht): shutil.rmtree(patht, ignore_errors=True) os.makedirs(patht) # write the csv-file # with open(patht+name[:-4]+".csv", 'w') as csvf: - with open(patht + "info.csv", "w") as csvf: + with open(os.path.join(patht, "info.csv"), "w") as csvf: writer = csv.writer(csvf, quoting=csv.QUOTE_NONNUMERIC) writer.writerow(["Observation"]) writer.writerow(["name", self.name]) @@ -699,12 +685,12 @@ def save(self, path="./", name=None): self._time.save(patht, timname) self._observation.save(patht, obsname) # compress everything to one zip-file - with zipfile.ZipFile(path + name, "w") as zfile: + with zipfile.ZipFile(os.path.join(path, name), "w") as zfile: # zfile.write(patht+name[:-4]+".csv", name[:-4]+".csv") - zfile.write(patht + "info.csv", "info.csv") + zfile.write(os.path.join(patht, "info.csv"), "info.csv") if self.state == "transient": - zfile.write(patht + timname, timname) - zfile.write(patht + obsname, obsname) + zfile.write(os.path.join(patht, timname), timname) + zfile.write(os.path.join(patht, obsname), obsname) shutil.rmtree(patht, ignore_errors=True) @@ -1047,7 +1033,7 @@ def __rand__(self, well): def __abs__(self): return np.linalg.norm(self.coordinates) - def save(self, path="./", name=None): + def save(self, path="", name=None): """Save a well to file. This writes the variable to a csv file. @@ -1055,7 +1041,7 @@ def save(self, path="./", name=None): Parameters ---------- path : :class:`str`, optional - Path where the variable should be saved. Default: ``"./"`` + Path where the variable should be saved. Default: ``""`` name : :class:`str`, optional Name of the file. If ``None``, the name will be generated by ``"Well_"+name``. Default: ``None`` @@ -1064,16 +1050,9 @@ def save(self, path="./", name=None): ----- The file will get the suffix ``".wel"``. """ - # ensure that 'path' is a string [ needed ?! ] - # path = _formstr(path) - # ensure that 'path' ends with a '/' if it's not empty - if path != "" and path[-1] != "/": - path += "/" - if path == "": - path = "./" + path = os.path.normpath(path) # create the path if not existing - if not os.path.exists(path): - os.makedirs(path) + os.makedirs(path, exist_ok=True) # create a standard name if None is given if name is None: name = "Well_" + self.name @@ -1082,14 +1061,13 @@ def save(self, path="./", name=None): name += ".wel" name = _formname(name) # create temporal directory for the included files - tmp = ".tmpwell/" - patht = path + tmp + patht = os.path.join(path, ".tmpwell") if os.path.exists(patht): shutil.rmtree(patht, ignore_errors=True) os.makedirs(patht) # write the csv-file # with open(patht+name[:-4]+".csv", 'w') as csvf: - with open(patht + "info.csv", "w") as csvf: + with open(os.path.join(patht, "info.csv"), "w") as csvf: writer = csv.writer(csvf, quoting=csv.QUOTE_NONNUMERIC) writer.writerow(["Well"]) writer.writerow(["name", self.name]) @@ -1108,13 +1086,13 @@ def save(self, path="./", name=None): writer.writerow(["aquiferdepth", aquifname]) self._aquiferdepth.save(patht, aquifname) # compress everything to one zip-file - with zipfile.ZipFile(path + name, "w") as zfile: + with zipfile.ZipFile(os.path.join(path, name), "w") as zfile: # zfile.write(patht+name[:-4]+".csv", name[:-4]+".csv") - zfile.write(patht + "info.csv", "info.csv") - zfile.write(patht + radiuname, radiuname) - zfile.write(patht + coordname, coordname) - zfile.write(patht + welldname, welldname) - zfile.write(patht + aquifname, aquifname) + zfile.write(os.path.join(patht, "info.csv"), "info.csv") + zfile.write(os.path.join(patht, radiuname), radiuname) + zfile.write(os.path.join(patht, coordname), coordname) + zfile.write(os.path.join(patht, welldname), welldname) + zfile.write(os.path.join(patht, aquifname), aquifname) # delete the temporary directory shutil.rmtree(patht, ignore_errors=True) @@ -1272,7 +1250,7 @@ def _formstr(string): def _formname(string): # remove slashes - string = "".join(str(string).split("/")) + string = "".join(str(string).split(os.path.sep)) # remove spaces, tabs, linebreaks and other separators return _formstr(string) diff --git a/welltestpy/estimate/estimatelib.py b/welltestpy/estimate/estimatelib.py index c4ba719..0fad190 100644 --- a/welltestpy/estimate/estimatelib.py +++ b/welltestpy/estimate/estimatelib.py @@ -339,32 +339,34 @@ def run( # generate the filenames if folder is None: - folder = os.getcwd() + "/" - elif folder[-1] != "/": - folder += "/" - dire = os.path.dirname(folder) - os.makedirs(dire, exist_ok=True) + folder = os.getcwd() + folder = os.path.normpath(folder) + os.makedirs(folder, exist_ok=True) if dbname is None: - dbname = folder + act_time + "_stat2D_db" + dbname = os.path.join(folder, act_time + "_stat2D_db") else: - dbname = folder + dbname + dbname = os.path.join(folder, dbname) if plotname1 is None: - plotname1 = folder + act_time + "_stat2D_plot_paratrace.pdf" + plotname1 = os.path.join( + folder, act_time + "_stat2D_plot_paratrace.pdf" + ) else: - plotname1 = folder + plotname1 + plotname1 = os.path.join(folder, plotname1) if plotname2 is None: - plotname2 = folder + act_time + "_stat2D_plot_fit.pdf" + plotname2 = os.path.join(folder, act_time + "_stat2D_plot_fit.pdf") else: - plotname2 = folder + plotname2 + plotname2 = os.path.join(folder, plotname2) if plotname3 is None: - plotname3 = folder + act_time + "_stat2D_plot_parainteract.pdf" + plotname3 = os.path.join( + folder, act_time + "_stat2D_plot_parainteract.pdf" + ) else: - plotname3 = folder + plotname3 + plotname3 = os.path.join(folder, plotname3) if estname is None: - paraname = folder + act_time + "_estimate.txt" + paraname = os.path.join(folder, act_time + "_estimate.txt") else: - paraname = folder + estname + paraname = os.path.join(folder, estname) # generate the parameter-names for plotting paralabels = [] @@ -547,25 +549,27 @@ def sensitivity( # generate the filenames if folder is None: - folder = os.getcwd() + "/" - elif folder[-1] != "/": - folder += "/" - dire = os.path.dirname(folder) - os.makedirs(dire, exist_ok=True) + folder = os.getcwd() + folder = os.path.normpath(folder) + os.makedirs(folder, exist_ok=True) if dbname is None: - dbname = folder + act_time + "_sensitivity_db" + dbname = os.path.join(folder, act_time + "_sensitivity_db") else: - dbname = folder + dbname + dbname = os.path.join(folder, dbname) if plotname is None: - plotname = folder + act_time + "_stat2D_plot_sensitivity.pdf" + plotname = os.path.join( + folder, act_time + "_stat2D_plot_sensitivity.pdf" + ) else: - plotname = folder + plotname + plotname = os.path.join(folder, plotname) if plotname1 is None: - plotname1 = folder + act_time + "_stat2D_plot_senstrace.pdf" + plotname1 = os.path.join( + folder, act_time + "_stat2D_plot_senstrace.pdf" + ) else: - plotname1 = folder + plotname1 - sensname = folder + act_time + "_FAST_estimate.txt" + plotname1 = os.path.join(folder, plotname1) + sensname = os.path.join(folder, act_time + "_FAST_estimate.txt") # generate the parameter-names for plotting paralabels = [] @@ -881,32 +885,34 @@ def run( # generate the filenames if folder is None: - folder = os.getcwd() + "/" - elif folder[-1] != "/": - folder += "/" - dire = os.path.dirname(folder) - os.makedirs(dire, exist_ok=True) + folder = os.getcwd() + folder = os.path.normpath(folder) + os.makedirs(folder, exist_ok=True) if dbname is None: - dbname = folder + act_time + "_Theis_db" + dbname = os.path.join(folder, act_time + "_Theis_db") else: - dbname = folder + dbname + dbname = os.path.join(folder, dbname) if plotname1 is None: - plotname1 = folder + act_time + "_Theis_plot_paratrace.pdf" + plotname1 = os.path.join( + folder, act_time + "_Theis_plot_paratrace.pdf" + ) else: - plotname1 = folder + plotname1 + plotname1 = os.path.join(folder, plotname1) if plotname2 is None: - plotname2 = folder + act_time + "_Theis_plot_fit.pdf" + plotname2 = os.path.join(folder, act_time + "_Theis_plot_fit.pdf") else: - plotname2 = folder + plotname2 + plotname2 = os.path.join(folder, plotname2) if plotname3 is None: - plotname3 = folder + act_time + "_Theis_plot_parainteract.pdf" + plotname3 = os.path.join( + folder, act_time + "_Theis_plot_parainteract.pdf" + ) else: - plotname3 = folder + plotname3 + plotname3 = os.path.join(folder, plotname3) if estname is None: - paraname = folder + act_time + "_Theis_estimate.txt" + paraname = os.path.join(folder, act_time + "_Theis_estimate.txt") else: - paraname = folder + estname + paraname = os.path.join(folder, estname) # generate the parameter-names for plotting paralabels = [] @@ -1031,21 +1037,21 @@ def sensitivity( # generate the filenames if folder is None: - folder = os.getcwd() + "/" - elif folder[-1] != "/": - folder += "/" - dire = os.path.dirname(folder) - os.makedirs(dire, exist_ok=True) + folder = os.getcwd() + folder = os.path.normpath(folder) + os.makedirs(folder, exist_ok=True) if dbname is None: - dbname = folder + act_time + "_Theis_sensitivity_db" + dbname = os.path.join(folder, act_time + "_Theis_sensitivity_db") else: - dbname = folder + dbname + dbname = os.path.join(folder, dbname) if plotname is None: - plotname = folder + act_time + "_Theis_plot_sensitivity.pdf" + plotname = os.path.join( + folder, act_time + "_Theis_plot_sensitivity.pdf" + ) else: - plotname = folder + plotname - sensname = folder + act_time + "_Theis_FAST_estimate.txt" + plotname = os.path.join(folder, plotname) + sensname = os.path.join(folder, act_time + "_Theis_FAST_estimate.txt") # generate the parameter-names for plotting paralabels = []