diff --git a/README.md b/README.md index 2ff94136..8244d0dc 100644 --- a/README.md +++ b/README.md @@ -101,9 +101,13 @@ To release the package with GitHub Actions, you can follow these steps: # How to run ALTRIOS -With your activated Python environment with ALTRIOS fully installed, you can run several scripts in `ALTRIOS/applications/demos/`. +With your activated Python environment with ALTRIOS fully installed, you can download the demo scripts to the current working directory inside of a `demos/` folder with: +```python +import altrios as alt +alt.download_demo_files() +``` -You can run the Simulation Manager through a multi-week simulation of train operations with `ALTRIOS/applications/demos/sim_manager_demo.py` by running `python sim_manager_demo.py` in `ALTRIOS/applications/demos/`. This will create a `plots` subfolder in which the plots will be saved. To run interactively, fire up a Python IDE (e.g. [VS Code](https://code.visualstudio.com/Download), [Spyder](https://www.spyder-ide.org/)), and run the file. If you're in VS Code, you can run the file as a virtual jupyter notebook because of the "cells" that are marked with the `# %%` annotation. You can click on line 2, for example, and hit ` + ` to run the current cell in an interactive terminal (which will take several seconds to launch) and advance to the next cell. Alternatively, you can hit ` + + p` to enable interactive commands and type "run current cell". +You can run the Simulation Manager through a multi-week simulation of train operations in by running `python sim_manager_demo.py` in `demos/`. This will create a `plots/` subfolder in which the plots will be saved. To run interactively, fire up a Python IDE (e.g. [VS Code](https://code.visualstudio.com/Download), [Spyder](https://www.spyder-ide.org/)), and run the file. If you're in VS Code, you can run the file as a virtual jupyter notebook because of the "cells" that are marked with the `# %%` annotation. You can click on line 2, for example, and hit ` + ` to run the current cell in an interactive terminal (which will take several seconds to launch) and advance to the next cell. Alternatively, you can hit ` + + p` to enable interactive commands and type "run current cell". There are several other python files in the `demos/` folder to demonstrate various capabilities of ALTRIOS. # Acknowledgements   diff --git a/pyproject.toml b/pyproject.toml index 4917ff28..8ec54f63 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,7 @@ dependencies = [ "pyyaml", "polars==0.18.7", "pyarrow", + "requests", ] [project.optional-dependencies] diff --git a/python/altrios/__init__.py b/python/altrios/__init__.py index df846252..37771b38 100644 --- a/python/altrios/__init__.py +++ b/python/altrios/__init__.py @@ -5,6 +5,7 @@ from altrios.utilities import set_param_from_path # noqa: F401 +from altrios.utilities import download_demo_files # noqa: F401 from altrios import utilities as utils import logging @@ -12,7 +13,6 @@ # make everything in altrios_core_py available here from altrios.altrios_core_py import * - # Set up logging logging.basicConfig( format="%(asctime)s.%(msecs)03d | %(filename)s#%(lineno)s | %(levelname)s: %(message)s", diff --git a/python/altrios/utilities.py b/python/altrios/utilities.py index 1fa3fbfe..452b63bb 100644 --- a/python/altrios/utilities.py +++ b/python/altrios/utilities.py @@ -9,6 +9,7 @@ import logging from pathlib import Path import datetime +import requests from altrios.altrios_core_py import ( @@ -43,7 +44,6 @@ WARM_START_DAYS = 7 - def print_dt(): print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) @@ -267,3 +267,29 @@ def enable_logging(): set_log_level(logging.WARNING) +def download_demo_files(): + """ + Downloads demo files from github repo into local directory. + """ + + api_url = "https://api.github.com/repos/NREL/altrios/contents/applications/demos" + response = requests.get(api_url) + + if response.status_code == 200: + contents = response.json() + + for item in contents: + if item["type"] == "file" and item["name"].endswith(".py"): + file_url = item["download_url"] + file_name = item["name"] + + demo_path = Path("demos") + demo_path.mkdir(exist_ok=True) + + with open(demo_path / file_name, "wb") as file: + file_content = requests.get(file_url).content + file.write(file_content) + + print(f"Saved {file_name} to {str(demo_path / file_name)}") + else: + print("Failed to download demo files")