From 1749c5a02c4667acff75d44c89d26c4bd1406a2e Mon Sep 17 00:00:00 2001 From: Mathias Berg Rosendal <77012503+Mathias157@users.noreply.github.com> Date: Wed, 25 Sep 2024 13:36:39 +0200 Subject: [PATCH] Finished check for file before loading inc files (#17) * Started work on issue #12 by renaming the output .gdx file to %scenario_input_data.gdx instead of generic _gams_py_gdb0.gdx * Finished check for file before loading inc files * Release 0.3.10 * Merge branch 'master' into check_for_loaded_incfiles --- docs/conf.py | 2 +- docs/get_started/installation.md | 2 +- environment.yaml | 2 +- pyproject.toml | 2 +- src/pybalmorel/classes.py | 61 +++++++++++++++++++------------- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index e508be5..99ddb44 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,7 +1,7 @@ project = "pybalmorel" copyright = "2024, Mathias Berg Rosendal, Théodore Le Nalinec" author = "Mathias Berg Rosendal, Théodore Le Nalinec" -release = "0.3.9" +release = "0.3.10" exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", ".testenv", ".testenv/**"] diff --git a/docs/get_started/installation.md b/docs/get_started/installation.md index 45d09fc..0d77281 100644 --- a/docs/get_started/installation.md +++ b/docs/get_started/installation.md @@ -21,5 +21,5 @@ dependencies: - pip: - gamsapi[transfer]>=45.0.0 - eel>=0.17.0 - - pybalmorel==0.3.9 + - pybalmorel==0.3.10 ``` diff --git a/environment.yaml b/environment.yaml index 938d27f..c7cb5de 100644 --- a/environment.yaml +++ b/environment.yaml @@ -11,4 +11,4 @@ dependencies: - pip: - gamsapi[transfer]>=45.0.0 - eel>=0.17.0 - - pybalmorel==0.3.9 \ No newline at end of file + - pybalmorel==0.3.10 diff --git a/pyproject.toml b/pyproject.toml index 90f20d8..d6ae8ca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "pybalmorel" -version = "0.3.9" +version = "0.3.10" maintainers = [ { name="Mathias Berg Rosendal", email="mathiasros@gmail.com"}, { name="Théodore Le Nalinec"}, diff --git a/src/pybalmorel/classes.py b/src/pybalmorel/classes.py index c635cc7..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,33 +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') + + 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) - # 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) - - # Load the GAMS model - model_db = ws.add_job_from_file(os.path.join(model_folder, read_file)) - - # Run the GAMS file - model_db.run() + # 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