diff --git a/gen_lp.py b/gen_lp.py deleted file mode 100644 index 7d10957..0000000 --- a/gen_lp.py +++ /dev/null @@ -1,31 +0,0 @@ -"""Script to generate lp file from OSeMOSYS datafile, within a workflow and to provide output in case not possible avoiding breaking of the workflow. -""" -import os -import sys -import subprocess as sp - -def gen_lp(p_df: str, p_lp: str): - - str_cmd = "glpsol -m "+os.path.join('"model','osemosys.txt"') + ' -d "%(data)s" --wlp "%(lp)s" --check' % {'data': p_df, 'lp': p_lp} - - try: - sp.run(str_cmd, shell=True, capture_output=True) - except: - file_error = open(os.sep.join(p_lp.split('/')[:-1]+['lp-error.txt']), "w") - file_error.close() - -if __name__ == "__main__": - - args = sys.argv[1:] - - if len(args) != 2: - print("Usage: python gen_lp.py ") - exit(1) - - path_df = args[0] - path_lp = args[1] - - gen_lp(path_df, path_lp) - - file_done = open(os.sep.join(path_lp.split('/')[:-1]+['lp_done.txt']), "w") - file_done.close() \ No newline at end of file diff --git a/snakefile b/snakefile index c3c7599..0690c0c 100644 --- a/snakefile +++ b/snakefile @@ -1,17 +1,22 @@ import os +import pandas as pd -scenario_path = os.path.join("input_data", "diagnostic_scen") -SCENARIOS = [x.path for x in os.scandir(scenario_path) if x.is_dir()] +dp_files = pd.read_csv('config/dp_files.txt') + +scenario_path = os.path.join("input_data") +SCENARIOS = [x.name for x in os.scandir(scenario_path) if x.is_dir()] rule all: input: - expand("results/{scen}/{scen}.xlsx", scen=SCENARIOS) #for testing, to be changed during development + expand("results/{scen}.xlsx", scen=SCENARIOS) #for testing, to be changed during development rule convert_dp: + message: "Coverting datapackage for {wildcards.scen}" input: - dp_path = "{scen}/datapackage.json" + other = expand("input_data/{{scen}}/{files}", files=dp_files), + dp_path = "input_data/{scen}/datapackage.json" output: - df_path = "{scen}/{scen}.txt" + df_path = "working_directory/{scen}.txt" conda: "envs/otoole_env.yaml" shell: @@ -21,7 +26,7 @@ rule pre_process: input: "working_directory/{scen}.txt" output: - temporary("working_directory/{scen}.pre") + temp("working_directory/{scen}.pre") conda: "envs/otoole_env.yaml" shell: @@ -29,47 +34,48 @@ rule pre_process: rule build_lp: input: - df_path = "{scen}/{scen}.txt" + df_path = "working_directory/{scen}.pre" params: - model_path = "model/osemosys.txt", - lp_path = "{scen}/{scen}.lp" + model_path = "model/osemosys.txt" output: - lp_complete_path = "{scen}/lp_done.txt" + temp("working_directory/{scen}.lp") + log: + "working_directory/{scen}.log" shell: - "python gen_lp.py {input.df_path} {params.lp_path}" + "glpsol -m {params.model_path} -d {input.df_path} --wlp {output} --check > {log}" rule run_model: + message: "Solving the LP for '{input}'" input: - lp_complete_path = "{scen}/lp_done.txt" - params: - lp_path = "{scen}/{scen}.lp", - path = "results/{scen}/{scen}" + "working_directory/{scen}.lp", output: - done_path = "results/{scen}/{scen}-sol_done.txt" + temp("working_directory/{scen}.sol") conda: "envs/gurobi_env.yaml" - shell: - "python run.py {params.lp_path} {params.path}" + log: + "working_directory/gurobi/{scen}.log", + threads: 2 + script: + "run.py" rule convert_sol: input: - sol_complete_path = "results/{scen}/{scen}-sol_done.txt", - dp_path = "{scen}/datapackage.json" + sol_path = "working_directory/{scen}.sol", + dp_path = "input_data/{scen}/datapackage.json" params: - sol_path = "results/{scen}/{scen}.sol", res_folder = "results/{scen}/results_csv" output: res_path = "results/{scen}/res-csv_done.txt" conda: "envs/otoole_env.yaml" shell: - "python convert.py {params.sol_path} {params.res_folder} {input.dp_path}" + "python convert.py {input.sol_path} {params.res_folder} {input.dp_path}" rule create_configs: input: config_tmpl = "config.yaml" output: - config_scen = "config_{scen}.yaml" + config_scen = "working_directory/config_{scen}.yaml" conda: "envs/yaml_env.yaml" shell: @@ -78,12 +84,12 @@ rule create_configs: rule res_to_iamc: input: res_path = "results/{scen}/res-csv_done.txt", - config_file = "config_{scen}.yaml" + config_file = "working_directory/config_{scen}.yaml" params: - inputs_folder = "{scen}/data", + inputs_folder = "input_data/{scen}/data", res_folder = "results/{scen}/results_csv" output: - output_file = "results/{scen}/{scen}.xlsx" + output_file = "results/{scen}.xlsx" conda: "envs/openentrance_env.yaml" shell: