Skip to content

Commit

Permalink
Merge pull request #6 from NREL/demo-documentation-fix
Browse files Browse the repository at this point in the history
wrote and documented function for downloading demo files
  • Loading branch information
calbaker authored Oct 3, 2023
2 parents ed4bd55 + 476da09 commit 169d3ea
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<Shift> + <Enter>` 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 `<Ctrl> + <Shift> + 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 `<Shift> + <Enter>` 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 `<Ctrl> + <Shift> + 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

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies = [
"pyyaml",
"polars==0.18.7",
"pyarrow",
"requests",
]

[project.optional-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion python/altrios/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@


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

# 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",
Expand Down
28 changes: 27 additions & 1 deletion python/altrios/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
from pathlib import Path
import datetime
import requests


from altrios.altrios_core_py import (
Expand Down Expand Up @@ -43,7 +44,6 @@

WARM_START_DAYS = 7


def print_dt():
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

Expand Down Expand Up @@ -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")

0 comments on commit 169d3ea

Please sign in to comment.