Skip to content

Commit

Permalink
get_gams_path() improvements.
Browse files Browse the repository at this point in the history
gams_dir an argument in get_sim_results (closes #17)
  • Loading branch information
kavvkon committed Sep 12, 2018
1 parent f394188 commit 228141c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
23 changes: 15 additions & 8 deletions DispaSET/misc/gdx_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ def write_variables(gams_dir, gdx_out, list_vars):
:param gdx_out: (Relative) path to the gdx file to be written
:param list_vars: List with the sets and parameters to be written
"""
if not os.path.isdir(gams_dir):
gams_dir = get_gams_path()
if not os.path.isdir(gams_dir):
gams_dir = get_gams_path(gams_dir=gams_dir.encode())
if not gams_dir: # couldn't locate
logging.critical('GDXCC: Could not find the specified gams directory: ' + gams_dir)
sys.exit(1)
gams_dir = force_str(gams_dir)
Expand Down Expand Up @@ -328,14 +327,22 @@ def get_gdx(gams_dir, resultfile):
fixindex=True, verbose=True)


def get_gams_path():
def get_gams_path(gams_dir=None):
"""
Function that attempts to search for the GAMS installation path (required to write the GDX or run gams)
It returns the path if it has been found, or an empty string otherwise.
It returns the path if it has been found, or an empty string otherwise. If a gams_dir argument is passed
it tries to validate before searching
Currently works for Windows, Linux and OSX. More searching rules and patterns should be added in the future
"""

if gams_dir is not None:
if not os.path.exists(gams_dir):
logging.warn('The provided path for GAMS (' + gams_dir + ') does not exist. Trying to locate...')
else:
return os.path.dirname(gams_dir).encode()

import subprocess
out = ''
if sys.platform == 'linux2' or sys.platform == 'linux':
Expand Down Expand Up @@ -406,15 +413,15 @@ def get_gams_path():
out = tmp
else:
logging.critical('The provided path is not a valid windows gams folder')
sys.exit(1)
return False
elif sys.platform == 'linux2':
if os.path.isfile(tmp + os.sep + 'gamslib'): # does not always work... gamslib_ml
out = tmp
else:
logging.critical('The provided path is not a valid linux gams folder')
sys.exit(1)
return False
else:
if os.path.isdir(tmp):
out = tmp

return out
return out.encode()
17 changes: 8 additions & 9 deletions DispaSET/postprocessing/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def plot_country_capacities(inputs,plot=True):



def get_sim_results(path='.', cache=False, temp_path='.pickle'):
def get_sim_results(path='.', gams_dir=None, cache=False, temp_path='.pickle'):
"""
This function reads the simulation environment folder once it has been solved and loads
the input variables together with the results.
Expand All @@ -604,15 +604,14 @@ def get_sim_results(path='.', cache=False, temp_path='.pickle'):
if not 'param_df' in inputs:
inputs['param_df'] = ds_to_df(inputs)

if gams_dir is None: # Use user-defined gams_dir else try to use the one defined in config
gams_dir = inputs['config']['GAMS_folder'].encode()

gams_dir = inputs['config']['GAMS_folder'].encode() # We need to pass the dir in config if we run it in clusters. PBS script fail to autolocate
if not os.path.exists(gams_dir):
logging.warn('The provided path for GAMS (' + gams_dir + ') does not exist. Trying to locate...')
gams_dir = get_gams_path()
if not os.path.exists(gams_dir):
logging.error('GAMS path cannot be located. Simulation is stopped')
return False
gams_dir = gams_dir.encode()
gams_dir = get_gams_path(gams_dir)
# We need to pass the dir in config if we run it in clusters. PBS script fail to autolocate
if not gams_dir: # couldn't locate
logging.error('GAMS path cannot be located. Cannot parse gdx files')
return False

# Load results and store in cache file in the .pickle folder:
if cache:
Expand Down
10 changes: 4 additions & 6 deletions DispaSET/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,10 @@ def solve_GAMS(sim_folder, gams_folder=None, output_lst=False):
logging.warning('Could not import lower level APIs. Trying to locate local version')
if not import_local_lib('gams'):
return False
if not os.path.exists(gams_folder):
logging.warning('The provided path for GAMS (' + gams_folder + ') does not exist. Trying to locate...')
gams_folder = get_gams_path()
if not os.path.exists(gams_folder):
logging.error('GAMS path cannot be located. Simulation is stopped')
return False
gams_folder = get_gams_path(gams_folder)
if not gams_folder: # couldn't locate
logging.error('GAMS path cannot be located. Simulation is stopped')
return False
sim_folder = os.path.abspath(sim_folder)
gams_folder = os.path.abspath(gams_folder)

Expand Down

0 comments on commit 228141c

Please sign in to comment.