Skip to content

Commit

Permalink
Merge pull request #10 from jpfleischer/namepatch
Browse files Browse the repository at this point in the history
Fix egrid data if name not in properties
  • Loading branch information
JGreenlee authored Oct 23, 2024
2 parents e8ae9dc + 5cee2d4 commit 2f4c678
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/emcommon/metrics/footprint/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ async def get_egrid_region(coords: list[float, float], year: int) -> str | None:
return None
region_feature = get_feature_containing_point(coords, geojson)
if region_feature is not None:
return region_feature['properties']['name']
if 'name' in region_feature['properties']:
return region_feature['properties']['name']
if 'SUBRGN' in region_feature['properties']:
return region_feature['properties']['SUBRGN']
Log.warn(f"An eGRID region was not found for coords {coords} in year {year}.")
return None

Expand Down
32 changes: 32 additions & 0 deletions test/metrics/test_footprint_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,38 @@ async def test_nyc_bus_footprint():
expectEqual(metadata[key], expected_metadata[key])


@jest_test
async def test_2021_nyc_ebike_footprint():
"""
Test kWh and kg CO2e for a 2021 ebike in NYC.
"""
fake_trip = {
"_id": "fake_trip_id",
"distance": 1000,
"start_fmt_time": "2021-01-01",
"start_loc": {"coordinates": [-74.006, 40.7128]},
"user_input": {"mode_confirm": "ebike"}
}
fake_label_options = {
"MODE": [
{"value": "ebike", "base_mode": "E_BIKE"}
]
}

(footprint, metadata) = await emcmff.calc_footprint_for_trip(fake_trip, fake_label_options)

expected_footprint = {'kwh': 0.01367, 'kg_co2': 0.005071}
expected_metadata = {
"data_sources": ["egrid2021"],
"is_provisional": False,
"requested_year": 2021,
}
for key in expected_footprint.keys():
expectAlmostEqual(footprint[key], expected_footprint[key], places=2)
for key in expected_metadata.keys():
expectEqual(metadata[key], expected_metadata[key])


@jest_test
async def test_impact_of_ebike_replacing_car():
"""
Expand Down

0 comments on commit 2f4c678

Please sign in to comment.