Skip to content

Commit

Permalink
handle geolookup timeout error
Browse files Browse the repository at this point in the history
  • Loading branch information
grgmiller committed Dec 21, 2024
1 parent f408bb3 commit 1f656fc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/oge/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"WDS",
]

TIME_RESOLUTIONS = {"hourly": "H", "monthly": "M", "annual": "A"}
TIME_RESOLUTIONS = {"hourly": "h", "monthly": "M", "annual": "A"}

# derived from table 2.4-4 of the EPA's AP-42 document
nox_lb_per_mmbtu_flared_landfill_gas = 0.078
Expand Down
8 changes: 4 additions & 4 deletions src/oge/eia930.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def manual_930_adjust(raw: pd.DataFrame):
new = raw[cols].copy()
new.loc[raw.index < "2021-11-01 00:00:00+00", cols] = new.loc[
raw.index < "2021-11-01 00:00:00+00", cols
].shift(1, freq="H")
].shift(1, freq="h")
raw = raw.drop(columns=cols)
raw = pd.concat([raw, new], axis="columns")

Expand All @@ -466,7 +466,7 @@ def manual_930_adjust(raw: pd.DataFrame):
& (raw.index < "2022-06-16 07:00:00+00")
),
cols,
].shift(1, freq="H")
].shift(1, freq="h")
raw = raw.drop(columns=cols)
raw = pd.concat([raw, new], axis="columns")

Expand Down Expand Up @@ -500,7 +500,7 @@ def manual_930_adjust(raw: pd.DataFrame):
new = raw[cols].copy()
new.loc[raw.index < "2021-10-25 00:00:00+00", cols] = new.loc[
raw.index < "2021-10-25 00:00:00+00", cols
].shift(-7, freq="H")
].shift(-7, freq="h")
raw = raw.drop(columns=cols)
raw = pd.concat([raw, new], axis="columns")

Expand All @@ -509,7 +509,7 @@ def manual_930_adjust(raw: pd.DataFrame):
new = raw[col].copy()
new.loc["2021-01-01 08:00:00+00:00":"2022-01-01 07:00:00+00:00", col] = new.loc[
"2021-01-01 08:00:00+00:00":"2022-01-01 07:00:00+00:00", col
].shift(4, freq="H")
].shift(4, freq="h")
raw = raw.drop(columns=col)
raw = pd.concat([raw, new], axis="columns")

Expand Down
23 changes: 17 additions & 6 deletions src/oge/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,12 +1103,23 @@ def search_location_from_coordinates(latitude: float, longitude: float) -> tuple
Returns:
tuple[str]: state, county and city of the location.
"""
try:
address = geolocator.reverse(f"{latitude}, {longitude}").raw["address"]
if address["country_code"] != "us":
return pd.NA, pd.NA, pd.NA
except ReadTimeoutError:
return pd.NA, pd.NA, pd.NA

# try to look up the address. This often fails when contacting the server, so retry
# once. If it fails on the retry, return no value
for i in range(0, 2):
while True:
try:
address = geolocator.reverse(f"{latitude}, {longitude}").raw["address"]
if address["country_code"] != "us":
return pd.NA, pd.NA, pd.NA
except (ReadTimeoutError, GeocoderUnavailable) as error:
if i < 1:
logger.warning(f"{error} for reverse address lookup")
continue
else:
logger.warning(f"{error} for reverse address lookup, returning NA")
return pd.NA, pd.NA, pd.NA
break

# Check for State
state = (
Expand Down
16 changes: 15 additions & 1 deletion src/oge/output_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ def write_plant_data_to_results(
[
"plant_id_eia",
"plant_name_eia",
"ba_code",
"fuel_category",
"capacity_mw",
"ba_code",
Expand All @@ -344,6 +343,21 @@ def write_plant_data_to_results(
validate="m:1",
)

# rearrange columns
df = df[
[
"plant_id_eia",
"plant_name_eia",
"fuel_category",
"capacity_mw",
"ba_code",
"city",
"county",
"state",
]
+ DATA_COLUMNS
]

# calculate emission rates
df = add_generated_emission_rate_columns(df)

Expand Down
1 change: 0 additions & 1 deletion src/oge/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ def test_for_negative_values(df, year, small: bool = False):
logger.info("Checking that fuel and emissions values are positive... ")
columns_that_can_be_negative = ["net_generation_mwh"]
negative_warnings = 0
print(df.columns)
for column in df.columns:
# if the column is allowed to be negative, skip the test
if column in columns_that_can_be_negative:
Expand Down
2 changes: 1 addition & 1 deletion src/oge/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def day_hour_heatmap(timeseries: pd.Series, year: int = 2022):
timeseries.index = timeseries.index.tz_convert("EST")
hours_index = pd.DataFrame(
index=pd.date_range(
f"{year}-01-01 T00:00", f"{year}-12-31 T23:00", freq="H"
f"{year}-01-01 T00:00", f"{year}-12-31 T23:00", freq="h"
).tz_localize("EST")
)
hours_index = hours_index.merge(
Expand Down

0 comments on commit 1f656fc

Please sign in to comment.