From 7780d4c82ccf40d0337e1f4197f01ba772df3fa1 Mon Sep 17 00:00:00 2001 From: Mathias157 Date: Wed, 25 Sep 2024 13:31:51 +0200 Subject: [PATCH] Finished check for file before loading inc files --- src/pybalmorel/classes.py | 63 ++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/src/pybalmorel/classes.py b/src/pybalmorel/classes.py index d7518ca..b2e1598 100644 --- a/src/pybalmorel/classes.py +++ b/src/pybalmorel/classes.py @@ -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_ @@ -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