Skip to content

Commit fc834ca

Browse files
authored
Merge pull request #89 from NatLabRockies/copilot/fix-output-dir-error-for-tmy-data
Use default cache path for TMY data when `output_dir` is None
2 parents c69aee9 + 83c060e commit fc834ca

2 files changed

Lines changed: 43 additions & 20 deletions

File tree

routee/transit/thermal_energy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def add_HVAC_energy(
213213
Trips on selected date and route, including deadhead trips.
214214
output_dir : Path or None
215215
Directory used to store downloaded TMY weather files (in a ``TMY/``
216-
subdirectory). Must not be None.
216+
subdirectory). If None, defaults to ``~/cache/routee-transit/TMY``.
217217
218218
Returns
219219
-------
@@ -225,7 +225,7 @@ def add_HVAC_energy(
225225
if output_dir is not None:
226226
tmy_dir = output_dir / "TMY"
227227
else:
228-
raise Exception("Must specify output_dir if downloading TMY data")
228+
tmy_dir = Path.home() / "cache" / "routee-transit" / "TMY"
229229

230230
# Based on gtfs stops data, get counties served
231231
df_stops = feed.stops

tests/test_thermal_energy.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,48 +36,48 @@ def test_compute_HVAC_energy(self) -> None:
3636
# np.trapezoid on [0, ..., 0.99] with constant 10 gives 9.9.
3737
self.assertAlmostEqual(energy[0], 9.9, places=1)
3838

39-
@patch("routee.transit.thermal_energy.fetch_counties_gdf")
40-
@patch("routee.transit.thermal_energy.download_tmy_files")
41-
@patch("routee.transit.thermal_energy.get_hourly_temperature")
42-
def test_add_HVAC_energy(
39+
def _make_mock_feed_and_trips(
4340
self,
44-
mock_get_hourly: MagicMock,
45-
mock_download: MagicMock,
46-
mock_fetch_counties: MagicMock,
47-
) -> None:
48-
# Setup mock Feed
41+
) -> tuple[MagicMock, pd.DataFrame, gpd.GeoDataFrame, pd.DataFrame]:
4942
mock_feed = MagicMock()
5043
mock_feed.stops = pd.DataFrame(
5144
{"stop_id": ["S1"], "stop_lat": [40.0], "stop_lon": [-105.0]}
5245
)
53-
# Mock stop_times for HVAC calculation (integration needs start/end)
5446
mock_feed.stop_times = pd.DataFrame(
5547
{
5648
"trip_id": ["T1", "T1"],
5749
"arrival_time": [pd.Timedelta(hours=8), pd.Timedelta(hours=9)],
5850
"stop_id": ["S1", "S1"],
5951
}
6052
)
61-
62-
# Setup mock trips
6353
trips_df = pd.DataFrame({"trip_id": ["T1"]})
64-
65-
# Mock dependencies
6654
mock_county_gdf = gpd.GeoDataFrame(
6755
{
68-
"county_id": ["G0800130"], # Example FIPS
56+
"county_id": ["G0800130"],
6957
"STATEFP": ["08"],
7058
"COUNTYFP": ["013"],
7159
"geometry": [Point(-105.0, 40.0).buffer(1.0)],
7260
},
7361
crs="EPSG:4269",
7462
)
75-
mock_fetch_counties.return_value = mock_county_gdf
76-
77-
# Mock hourly temperature and power mapping
7863
mock_hourly_temp = pd.DataFrame(
7964
{"hour": list(range(24)), "Dry Bulb Temperature [°C]": [20.0] * 24}
8065
)
66+
return mock_feed, trips_df, mock_county_gdf, mock_hourly_temp
67+
68+
@patch("routee.transit.thermal_energy.fetch_counties_gdf")
69+
@patch("routee.transit.thermal_energy.download_tmy_files")
70+
@patch("routee.transit.thermal_energy.get_hourly_temperature")
71+
def test_add_HVAC_energy(
72+
self,
73+
mock_get_hourly: MagicMock,
74+
mock_download: MagicMock,
75+
mock_fetch_counties: MagicMock,
76+
) -> None:
77+
mock_feed, trips_df, mock_county_gdf, mock_hourly_temp = (
78+
self._make_mock_feed_and_trips()
79+
)
80+
mock_fetch_counties.return_value = mock_county_gdf
8181
mock_get_hourly.return_value = mock_hourly_temp
8282

8383
# use a temp directory for output
@@ -90,6 +90,29 @@ def test_add_HVAC_energy(
9090
# Should have results for 3 scenarios: summer, winter, median
9191
self.assertEqual(len(result), 3)
9292

93+
@patch("routee.transit.thermal_energy.fetch_counties_gdf")
94+
@patch("routee.transit.thermal_energy.download_tmy_files")
95+
@patch("routee.transit.thermal_energy.get_hourly_temperature")
96+
def test_add_HVAC_energy_no_output_dir(
97+
self,
98+
mock_get_hourly: MagicMock,
99+
mock_download: MagicMock,
100+
mock_fetch_counties: MagicMock,
101+
) -> None:
102+
"""add_HVAC_energy should work without output_dir using a default cache path."""
103+
mock_feed, trips_df, mock_county_gdf, mock_hourly_temp = (
104+
self._make_mock_feed_and_trips()
105+
)
106+
mock_fetch_counties.return_value = mock_county_gdf
107+
mock_get_hourly.return_value = mock_hourly_temp
108+
109+
# Call without specifying output_dir (previously raised an exception)
110+
result = add_HVAC_energy(mock_feed, trips_df, output_dir=None)
111+
112+
self.assertIn("hvac_energy_kWh", result.columns)
113+
self.assertIn("scenario", result.columns)
114+
self.assertEqual(len(result), 3)
115+
93116

94117
if __name__ == "__main__":
95118
unittest.main()

0 commit comments

Comments
 (0)