Skip to content

Commit

Permalink
Finished check for file before loading inc files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias157 committed Sep 25, 2024
1 parent 78d6840 commit 7780d4c
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions src/pybalmorel/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,15 @@ def run(self, scenario: str, cmd_line_options: dict = {}):
def load_incfiles(self,
scenario: str = 'base',
use_provided_read_files: bool = True,
read_file: str = 'Balmorel_ReadData'):
read_file: str = 'Balmorel_ReadData',
overwrite: bool = False):
"""Will load .inc files from the specific scenario
Args:
scenario (str, optional): The scenario that you . Defaults to 'base'.
use_provided_read_files (bool, optional): Use provided Balmorel_ReadData.gms and Balmorelbb4_ReadData.inc. Defaults to True.
read_file (str, optional): The name of the read file to be executed. Defaults to Balmorel_ReadData
overwrite (bool, optional): Will overwrite an existing %scenario%_input_data.gdx file from a previous .load_incfiles execution
Raises:
KeyError: _description_
Expand All @@ -368,37 +370,44 @@ def load_incfiles(self,
if not(scenario in self.scenarios):
raise KeyError('%s scenario wasnt found.\nRun this Balmorel(...) class again if you just created the %s scenario.'%(scenario, scenario))


# Path to the GAMS system directory
model_folder = os.path.join(self.path, scenario, 'model')

# Are you using the provided 'ReadData'-Balmorel files or a custom one?
use_provided_read_files = True
if use_provided_read_files:
pkgdir = sys.modules['pybalmorel'].__path__[0]
# Copy Balmorel_ReadData and Balmorelbb4_ReadData
# into the model folder if there isn't one already
for file in ['Balmorel_ReadData.gms', 'Balmorelbb4_ReadData.inc']:
if not(os.path.exists(os.path.join(model_folder, file))):
shutil.copyfile(os.path.join(pkgdir, file), os.path.join(model_folder, file))
print(os.path.join(model_folder, file))

# Initialize GAMS Workspace
ws = gams.GamsWorkspace(working_directory=model_folder)

# Set options
opt = ws.add_options()
opt.gdx = '%s_input_data.gdx'%scenario # Setting the output gdx name (note, could be overwritten by the cmd line options, which is intended)

# Load the GAMS model
model_db = ws.add_job_from_file(os.path.join(model_folder, read_file), job_name=scenario)
if os.path.exists(os.path.join(model_folder, '%s_input_data.gdx'%scenario)) and not(overwrite):
ws = gams.GamsWorkspace()
db = ws.add_database_from_gdx(os.path.join(model_folder, '%s_input_data.gdx'%scenario))
self.input_data[scenario] = db

else:
# Are you using the provided 'ReadData'-Balmorel files or a custom one?
use_provided_read_files = True
if use_provided_read_files:
pkgdir = sys.modules['pybalmorel'].__path__[0]
# Copy Balmorel_ReadData and Balmorelbb4_ReadData
# into the model folder if there isn't one already
for file in ['Balmorel_ReadData.gms', 'Balmorelbb4_ReadData.inc']:
if not(os.path.exists(os.path.join(model_folder, file))):
shutil.copyfile(os.path.join(pkgdir, file), os.path.join(model_folder, file))
print(os.path.join(model_folder, file))

# Initialize GAMS Workspace
ws = gams.GamsWorkspace(working_directory=model_folder)

# Set options
opt = ws.add_options()
opt.gdx = '%s_input_data.gdx'%scenario # Setting the output gdx name (note, could be overwritten by the cmd line options, which is intended)

# Load the GAMS model
model_db = ws.add_job_from_file(os.path.join(model_folder, read_file), job_name=scenario)

# Run the GAMS file
model_db.run(opt)
# Run the GAMS file
model_db.run(opt)

# Store the database (will take some minutes)
self.input_data[scenario] = model_db.get_out_db()


# Store the database (will take some minutes)
self.input_data[scenario] = model_db.get_out_db()
class GUI:
def __init__(self) -> None:
pass
Expand Down

0 comments on commit 7780d4c

Please sign in to comment.