Skip to content

Commit

Permalink
path handling platform independent
Browse files Browse the repository at this point in the history
  • Loading branch information
MuellerSeb committed Feb 27, 2019
1 parent 5959caa commit b28fb08
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 158 deletions.
56 changes: 20 additions & 36 deletions welltestpy/data/campaignlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ 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.
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``
Expand All @@ -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
Expand All @@ -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])
Expand All @@ -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)

Expand Down Expand Up @@ -504,15 +496,15 @@ 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.
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``
Expand All @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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)

Expand Down
30 changes: 11 additions & 19 deletions welltestpy/data/testslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,15 @@ 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.
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``
Expand All @@ -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
Expand All @@ -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])
Expand All @@ -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)

Expand Down

0 comments on commit b28fb08

Please sign in to comment.