Skip to content

Commit 4361af0

Browse files
Output files to original working directory if absolute path not specified (otherwise defaults to src/geophires_x which is undesirable)
1 parent cafd454 commit 4361af0

File tree

4 files changed

+34
-51
lines changed

4 files changed

+34
-51
lines changed

src/geophires_x/GEOPHIRESv3.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def main(enable_geophires_logging_config=True):
1919
logging will be configured in the Model class.
2020
:return: None
2121
"""
22+
original_cwd:Path = Path.cwd().absolute()
23+
2224
# set the starting directory to be the directory that this file is in
2325
os.chdir(os.path.dirname(os.path.abspath(__file__)))
2426

@@ -33,7 +35,7 @@ def main(enable_geophires_logging_config=True):
3335
model = Model.Model(enable_geophires_logging_config=enable_geophires_logging_config)
3436

3537
# read the parameters that apply to the model
36-
model.read_parameters()
38+
model.read_parameters(default_output_path=original_cwd)
3739

3840
# Calculate the entire model
3941
model.Calculate()

src/geophires_x/Model.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from email.policy import default
23
from pathlib import Path
34
import logging
45
import time
@@ -196,9 +197,10 @@ def __init__(self, enable_geophires_logging_config=True, input_file=None):
196197
def __str__(self):
197198
return "Model"
198199

199-
def read_parameters(self) -> None:
200+
def read_parameters(self, default_output_path: Path = None) -> None:
200201
"""
201202
The read_parameters function reads the parameters from the input file and stores them in a dictionary.
203+
:param default_output_path: Relative path for non-absolute output path parameters
202204
:return: None
203205
"""
204206
self.logger.info(f'Init {__class__}: {__name__}')
@@ -209,17 +211,17 @@ def read_parameters(self) -> None:
209211
self.wellbores.read_parameters(self)
210212
self.surfaceplant.read_parameters(self)
211213
self.economics.read_parameters(self)
212-
self.outputs.read_parameters(self)
214+
self.outputs.read_parameters(self, default_output_path=default_output_path)
213215

214216
# having read in the parameters, we now need to set up the objects that are specific to the user's choices
215217
# if we find out we have an add-ons, read the parameters
216218
if self.economics.DoAddOnCalculations.value:
217219
self.addeconomics.read_parameters(self)
218-
self.addoutputs.read_parameters(self)
220+
self.addoutputs.read_parameters(self, default_output_path=default_output_path)
219221
# if we find out we have an S-DAC-GT calculation, read for the parameters
220222
if self.economics.DoSDACGTCalculations.value:
221223
self.sdacgteconomics.read_parameters(self)
222-
self.sdacgtoutputs.read_parameters(self)
224+
self.sdacgtoutputs.read_parameters(self, default_output_path=default_output_path)
223225

224226
# Once we are done reading and processing parameters,
225227
# we reset the objects to more specific objects based on user choices

src/geophires_x/Outputs.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from geophires_x.OptionList import EndUseOptions, EconomicModel, ReservoirModel, FractureShape, ReservoirVolume, \
2323
PlantType
2424
from geophires_x.GeoPHIRESUtils import UpgradeSymbologyOfUnits, render_default, InsertImagesIntoHTML
25+
from geophires_x.Parameter import Parameter
2526

2627
NL = '\n'
2728
validFilenameChars = "-_.() %s%s" % (string.ascii_letters, string.digits)
@@ -639,24 +640,29 @@ def __init__(self, model:Model, output_file:str ='HDR.out'):
639640
model.logger.info(f'Init {__class__!s}: {__name__}')
640641
self.ParameterDict = {}
641642
self.OutputParameterDict = {}
643+
self.filepath_parameter_names = []
642644

643-
self.text_output_file = self.ParameterDict[self.text_output_file.Name] = strParameter(
645+
def file_path_parameter(p: Parameter) -> Parameter:
646+
self.filepath_parameter_names.append(p.Name)
647+
return p
648+
649+
self.text_output_file = self.ParameterDict[self.text_output_file.Name] = file_path_parameter(strParameter(
644650
'Improved Text Output File',
645651
DefaultValue='GEOPHIRES_Text.html',
646652
Required=False,
647653
Provided=False,
648654
ErrMessage='assume no improved text output',
649655
ToolTipText='Provide a improved text output name if you want to have improved text output (no output if not provided)',
650-
)
656+
))
651657

652-
self.html_output_file = self.ParameterDict[self.html_output_file.Name] = strParameter(
658+
self.html_output_file = self.ParameterDict[self.html_output_file.Name] = file_path_parameter(strParameter(
653659
'HTML Output File',
654660
DefaultValue='GEOPHIRES.html',
655661
Required=False,
656662
Provided=False,
657663
ErrMessage='assume no HTML output',
658664
ToolTipText='Provide a HTML output name if you want to have HTML output (no output if not provided)',
659-
)
665+
))
660666

661667
self.printoutput = self.ParameterDict[self.printoutput.Name] = boolParameter(
662668
'Print Output to Console',
@@ -677,7 +683,7 @@ def __init__(self, model:Model, output_file:str ='HDR.out'):
677683
def __str__(self):
678684
return 'Outputs'
679685

680-
def read_parameters(self, model:Model) -> None:
686+
def read_parameters(self, model: Model, default_output_path: Path = None) -> None:
681687
"""
682688
The read_parameters function reads in the parameters from a dictionary and stores them in the parameters.
683689
It also handles special cases that need to be handled after a value has been read in and checked.
@@ -692,6 +698,7 @@ def read_parameters(self, model:Model) -> None:
692698
to call this method from you class, which can effectively modify all these superclass parameters in your class.
693699
:param model: The container class of the application, giving access to everything else, including the logger
694700
:type model: :class:`~geophires_x.Model.Model`
701+
:param default_output_path: Relative path for non-absolute output path parameters
695702
:return: None
696703
"""
697704
model.logger.info(f'Init {__class__!s}: {__name__}')
@@ -702,6 +709,15 @@ def read_parameters(self, model:Model) -> None:
702709
key = ParameterToModify.Name.strip()
703710
if key in model.InputParameters:
704711
ParameterReadIn = model.InputParameters[key]
712+
713+
if key in self.filepath_parameter_names:
714+
if not Path(ParameterReadIn.sValue).is_absolute():
715+
original_val = ParameterReadIn.sValue
716+
ParameterReadIn.sValue = str(
717+
default_output_path.joinpath(ParameterReadIn.sValue).absolute())
718+
model.logger.info(f'Adjusted {key} path to {ParameterReadIn.sValue} because original value '
719+
f'({original_val}) was not an absolute path.')
720+
705721
# Before we change the parameter, let's assume that the unit preferences will match
706722
# - if they don't, the later code will fix this.
707723
ParameterToModify.CurrentUnits = ParameterToModify.PreferredUnits

src/geophires_x/SUTRAOutputs.py

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import geophires_x
66
import numpy as np
77
import geophires_x.Model as Model
8-
from .Parameter import LookupUnits
8+
from geophires_x.Outputs import Outputs
99
from .OptionList import EconomicModel
1010

1111
NL="\n"
1212

13-
class SUTRAOutputs:
14-
"""TODO should inherit from Outputs"""
13+
14+
class SUTRAOutputs(Outputs):
1515

1616
def __init__(self, model:Model, output_file:str ='HDR.out'):
1717
"""
@@ -35,45 +35,8 @@ def __init__(self, model:Model, output_file:str ='HDR.out'):
3535
model.logger.info(f'Complete {str(__class__)}: {sys._getframe().f_code.co_name}')
3636

3737
def __str__(self):
38-
return 'Outputs'
39-
40-
def read_parameters(self, model:Model) -> None:
41-
"""
42-
The read_parameters function reads in the parameters from a dictionary and stores them in the parameters.
43-
It also handles special cases that need to be handled after a value has been read in and checked.
44-
If you choose to subclass this master class, you can also choose to override this method (or not), and if you do
45-
Deals with all the parameter values that the user has provided. They should really only provide values that
46-
they want to change from the default values, but they can provide a value that is already set because it is a
47-
default value set in __init__. It will ignore those.
48-
This also deals with all the special cases that need to be taken care of after a value has been read in
49-
and checked.
50-
If you choose to subclass this master class, you can also choose to override this method (or not),
51-
and if you do, do it before or after you call you own version of this method. If you do, you can also choose
52-
to call this method from you class, which can effectively modify all these superclass parameters in your class.
53-
:param model: The container class of the application, giving access to everything else, including the logger
54-
:type model: :class:`~geophires_x.Model.Model`
55-
:return: None
56-
"""
57-
model.logger.info(f'Init {str(__class__)}: {sys._getframe().f_code.co_name}')
58-
59-
if len(model.InputParameters) > 0:
60-
# if the user wants it, we need to know if the user wants to copy the contents of the
61-
# output file to the screen - this serves as the screen report
62-
if "Print Output to Console" in model.InputParameters:
63-
ParameterReadIn = model.InputParameters["Print Output to Console"]
64-
if ParameterReadIn.sValue == "0":
65-
self.printoutput = False
38+
return 'SUTRAOutputs'
6639

67-
# loop through all the parameters that the user wishes to set, looking for parameters that contain the
68-
# prefix "Units:" - that means we want to set a special case for converting this
69-
# output parameter to new units
70-
for key in model.InputParameters.keys():
71-
if key.startswith("Units:"):
72-
self.ParameterDict[key.replace("Units:", "")] = LookupUnits(model.InputParameters[key].sValue)[0]
73-
74-
# handle special cases
75-
76-
model.logger.info(f'Complete {str(__class__)}: {sys._getframe().f_code.co_name}')
7740

7841
def PrintOutputs(self, model: Model):
7942
"""

0 commit comments

Comments
 (0)