Skip to content

Commit

Permalink
Merge pull request #30 from NREL/feature/vehicle_import
Browse files Browse the repository at this point in the history
Vehicle Import Tool
  • Loading branch information
kylecarow authored Dec 5, 2023
2 parents 8ab54b7 + 9c94859 commit 59398a8
Show file tree
Hide file tree
Showing 20 changed files with 6,797 additions and 46 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = [
"typing_extensions",
"pyyaml",
"pytest",
"openpyxl>=3.1.2"
]

[project.urls]
Expand Down
24 changes: 2 additions & 22 deletions python/fastsim/demos/cav_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,11 @@

import fastsim as fsim
from fastsim.tests.test_coasting import make_coasting_plot
from fastsim.demos.utils import maybe_str_to_bool, DEMO_TEST_ENV_VAR

RAN_SUCCESSFULLY = False

def maybe_str_to_bool(x, default=True):
"""
Turn values of None or string to bool
- x: str | None, some parameter, a string or None
- default: Bool, the default if x is None or blank
RETURN: True or False
"""
if x is None:
return default
if x is True or x is False:
return x
try:
lower_cased = x.lower().strip()
if lower_cased == 'false':
return False
if lower_cased == 'true':
return True
return default
except:
return default

IS_INTERACTIVE = maybe_str_to_bool(os.getenv('FASTSIM_DEMO_IS_INTERACTIVE'))
IS_INTERACTIVE = maybe_str_to_bool(os.getenv(DEMO_TEST_ENV_VAR))


# %% [markdown]
Expand Down
25 changes: 25 additions & 0 deletions python/fastsim/demos/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Utility functions for demo code
"""
DEMO_TEST_ENV_VAR = 'FASTSIM_DEMO_IS_INTERACTIVE'

def maybe_str_to_bool(x, default=True):
"""
Turn values of None or string to bool
- x: str | None, some parameter, a string or None
- default: Bool, the default if x is None or blank
RETURN: True or False
"""
if x is None:
return default
if x is True or x is False:
return x
try:
lower_cased = x.lower().strip()
if lower_cased == 'false':
return False
if lower_cased == 'true':
return True
return default
except:
return default
91 changes: 91 additions & 0 deletions python/fastsim/demos/vehicle_import_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""
Vehicle Import Demonstration
This module demonstrates the vehicle import API
"""
# %%
# Preamble: Basic imports
import os, pathlib

import fastsim.fastsimrust as fsr
from fastsim.demos.utils import maybe_str_to_bool, DEMO_TEST_ENV_VAR

RAN_SUCCESSFULLY = False
IS_INTERACTIVE = maybe_str_to_bool(os.getenv(DEMO_TEST_ENV_VAR))

# %%
# Setup some directories
THIS_DIR = pathlib.Path(__file__).parent.absolute()
OUTPUT_DIR = pathlib.Path(THIS_DIR) / "test_output"
if not OUTPUT_DIR.exists():
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

# %%
# List available options for the given year / make / model
make = "Toyota"
model = "Corolla"
year = "2022"

# NOTE: two additional optional arguments can be passed to get_options_for_year_make_model.
# They are the cache_url which is the URL for a file download service to retrieve cache data by year
# and also a data directory where that cache data will be stored.
# If not provided, they default to NREL's cache URL and the OS specific config data directory for this application.
# Also note that, due to interop requirements, these must be passed in as python strings. For example, a
# Python pathlib.Path object will be rejected.

options = fsr.get_options_for_year_make_model(year, make, model)
if IS_INTERACTIVE:
for opt in options:
print(f"{opt.id}: {opt.transmission}")

# %%
# Get the data for the given option
data = options[1]
if IS_INTERACTIVE:
print(
f"{data.year} {data.make} {data.model}: {data.comb_mpg_fuel1} mpg ({data.city_mpg_fuel1} CITY / {data.highway_mpg_fuel1} HWY)"
)

# %%
# Import the vehicle
other_inputs = fsr.OtherVehicleInputs(
vehicle_width_in=68.0,
vehicle_height_in=58.0,
fuel_tank_gal=12.0,
ess_max_kwh=0.0,
mc_max_kw=0.0,
ess_max_kw=0.0,
fc_max_kw=None,
) # None -> calculate from EPA data

# NOTE: two additional optional arguments can be passed to vehicle_import_by_id_and_year.
# They are the cache_url which is the URL for a file download service to retrieve cache data by year
# and also a data directory where that cache data will be stored.
# If not provided, they default to NREL's cache URL and the OS specific config data directory for this application.
# Also note that, due to interop requirements, these must be passed in as python strings. For example, a
# Python pathlib.Path object will be rejected.

rv = fsr.vehicle_import_by_id_and_year(data.id, int(year), other_inputs)

fsr.export_vehicle_to_file(rv, str(OUTPUT_DIR / "demo-vehicle.yaml"))

# %%
# Alternative API for importing all vehicles at once
# This API will import all matching configurations for
# the given year, make, and model.

# NOTE: two additional optional arguments can be passed to import_all_vehicles.
# They are the cache_url which is the URL for a file download service to retrieve cache data by year
# and also a data directory where that cache data will be stored.
# If not provided, they default to NREL's cache URL and the OS specific config data directory for this application.
# Also note that, due to interop requirements, these must be passed in as python strings. For example, a
# Python pathlib.Path object will be rejected.

vehs = fsr.import_all_vehicles(int(year), make, model, other_inputs)
if IS_INTERACTIVE:
for v in vehs:
print(f"Imported {v.scenario_name}")


# %%
# Used for automated testing
RAN_SUCCESSFULLY = True
Loading

0 comments on commit 59398a8

Please sign in to comment.