Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command to create directory structure to run OSeMOSYS_step in desired location #84

Merged
merged 9 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ in importing coal, importing RL1, and if RLu is allowed to run or not. Add the f

## 4. Run the workflow
```bash
cd src
python main_ms.py --step_length 5 --input_data ../data/<datafile_name>.txt
step run --step_length 5 --input_data ../data/<datafile_name>.txt
```

## 6. View Results
Expand Down
1 change: 1 addition & 0 deletions env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies:
- pip
- pandas
- snakemake-minimal
- glpk>=5.0
- pip:
- otoole>=1.0.0
- click
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ dependencies = [
"otoole>=1.1",
"snakemake",
"click",
"tqdm"
"tqdm",
"glpk"
]

[project.urls]
Expand All @@ -38,7 +39,7 @@ Issues = "https://github.com/KTH-dESA/OSeMOSYS_step/issues"
Source = "https://github.com/KTH-dESA/OSeMOSYS_step"

[project.scripts]
step = "osemosys_step.main:main"
step = "osemosys_step.main:cli"

[tool.hatch.version]
source = "vcs"
Expand Down
72 changes: 57 additions & 15 deletions src/osemosys_step/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,34 @@
"""

import click
from . import data_split as ds
from osemosys_step import data_split as ds
from osemosys_step import main_utils as mu
from osemosys_step import (
utils,
preprocess_data,
solve
)
import os
from pathlib import Path
import pandas as pd
import shutil
from . import utils
from . import main_utils as mu
from . import preprocess_data
from . import solve
from tqdm import tqdm
import logging
import sys
import glob
import snakemake
import subprocess

from otoole import read, write

logger = logging.getLogger(__name__)

from snakemake.utils import min_version
min_version("8.0")

@click.group()
def cli():
pass

@click.command()
@click.option("--step_length", required=True, multiple=True,
help="""
Expand All @@ -52,7 +61,7 @@
saved elsewhere than '../data/scenarios/' on can use this option to
indicate the path.
""")
def main(input_data: str, step_length: int, path_param: str, cores: int, solver=None, foresight=None):
def run(input_data: str, step_length: int, path_param: str, cores: int, solver=None, foresight=None):
"""Main entry point for workflow"""

##########################################################################
Expand Down Expand Up @@ -314,13 +323,16 @@ def main(input_data: str, step_length: int, path_param: str, cores: int, solver=
# when the goal is to just parallize multiple function calls
#######

snakemake.snakemake(
"src/osemosys_step/snakefile",
config = {"solver":solver, "files":lps_to_solve},
cores = cores,
keepgoing=True
)

# pretty sure there is a way to directly use the SnakemakeApi class!
snakefile_args = [
"--snakefile src/osemosys_step/snakefile",
f"--config solver={solver} files={[','.join(lps_to_solve)]}",
f"--cores {cores}",
"--keep-going",
"--quiet"
]
subprocess.run(f"snakemake {' '.join(snakefile_args)}", shell = True)

######################################################################
# Check for solutions
######################################################################
Expand Down Expand Up @@ -621,5 +633,35 @@ def main(input_data: str, step_length: int, path_param: str, cores: int, solver=

next_step += 1

@click.command()
@click.option("--path", required=True, default= '.',
help="Path where the directory structure shall be created."
)
def setup(path: str):
"""Function to create directory structure in which OSeMOSYS_step can be run.
The created directory has the below structure:
```bash
OSeMOSYS_step
├── data
│ ├── scenarios
├── model
├── results
└── steps
```
"""

dirs = ['data', ['data', 'scenarios'], 'logs', 'model', 'results', 'steps']

for d in dirs:
if type(d) == list:
p = Path(path, *d)
else:
p = Path(path, d)
if not p.exists():
p.mkdir()

cli.add_command(run)
cli.add_command(setup)

if __name__ == '__main__':
main() #input_data,step_length,path_param,solver)
cli()
5 changes: 4 additions & 1 deletion src/osemosys_step/snakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

options = [x[12:-9] for x in config["files"]] # extract out only the option path
if isinstance(config["files"], str):
options = [config["files"][12:-9]]
else:
options = [x[12:-9] for x in config["files"]] # extract out only the option path

rule all:
input:
Expand Down
Loading