Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add timezone conversion #194

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/pvsite_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)

import plotly.graph_objects as go
import pytz


def pvsite_forecast_page():
Expand All @@ -30,6 +31,10 @@ def pvsite_forecast_page():
sites.site_uuid for sites in site_uuids if sites.site_uuid is not None
]
site_selection = st.sidebar.selectbox("Select sites by site_uuid", site_uuids,)

timezone_selected = st.sidebar.selectbox("Select timezone", ['UTC', 'Asia/Calcutta'])
timezone_selected = pytz.timezone(timezone_selected)

day_after_tomorrow = datetime.today() + timedelta(days=3)
starttime = st.sidebar.date_input("Start Date", min_value=datetime.today() - timedelta(days=365), max_value=datetime.today())
endtime = st.sidebar.date_input("End Date",day_after_tomorrow)
Expand All @@ -40,7 +45,7 @@ def pvsite_forecast_page():
created = st.sidebar.text_input("Created Before", pd.Timestamp.now().ceil('15min'))

if created == "":
created = datetime.now()
created = datetime.utcnow()
else:
created = datetime.fromisoformat(created)
st.write("Forecast for", site_selection, "starting on", starttime, "created by", created, "ended on", endtime)
Expand All @@ -55,7 +60,12 @@ def pvsite_forecast_page():
if forecast_type == "DA":
# TODO make these more flexible
day_ahead_hours = 9
day_ahead_timezone_delta_hours = 5.5

# find the difference in hours for the timezone
now = datetime.now()
d = timezone_selected.localize(now) - now.replace(tzinfo=timezone.utc)
day_ahead_timezone_delta_hours = (24 - d.seconds/3600) % 24

st.write(f"Forecast for {day_ahead_hours} oclock the day before "
f"with {day_ahead_timezone_delta_hours} hour timezone delta")
else:
Expand All @@ -65,6 +75,22 @@ def pvsite_forecast_page():
# an option to resample to the data
resample = st.sidebar.selectbox("Resample data", [None, "15T", "30T"], None)

# change date to datetime
starttime = datetime.combine(starttime, time.min)
endtime = datetime.combine(endtime, time.min)

# change to the correct timezone
starttime = timezone_selected.localize(starttime)
endtime = timezone_selected.localize(endtime)

# change to utc
starttime = starttime.astimezone(pytz.utc)
endtime = endtime.astimezone(pytz.utc)

if created is not None:
created = timezone_selected.localize(created)
created = created.astimezone(pytz.utc)

# get forecast values for selected sites and plot
with connection.get_session() as session:
forecasts = get_latest_forecast_values_by_site(
Expand All @@ -83,6 +109,10 @@ def pvsite_forecast_page():
x = [i.start_utc for i in forecast]
y = [i.forecast_power_kw for i in forecast]

# convert to timezone
x = [i.replace(tzinfo=pytz.utc) for i in x]
x = [i.astimezone(timezone_selected) for i in x]

# get generation values for selected sites and plot
with connection.get_session() as session:
generations = get_pv_generation_by_sites(
Expand All @@ -96,6 +126,10 @@ def pvsite_forecast_page():
yy = [generation.generation_power_kw for generation in generations if generation is not None]
xx = [generation.start_utc for generation in generations if generation is not None]

# convert to timezone
xx = [i.replace(tzinfo=pytz.utc) for i in xx]
xx = [i.astimezone(timezone_selected) for i in xx]

df_forecast = pd.DataFrame({'forecast_datetime': x, 'forecast_power_kw': y})
df_generation = pd.DataFrame({'generation_datetime': xx, 'generation_power_kw': yy})

Expand All @@ -117,7 +151,7 @@ def pvsite_forecast_page():
fig = go.Figure(
layout=go.Layout(
title=go.layout.Title(text="Latest Forecast for Selected Site"),
xaxis=go.layout.XAxis(title=go.layout.xaxis.Title(text="Time [UTC]")),
xaxis=go.layout.XAxis(title=go.layout.xaxis.Title(text=f"Time [{timezone_selected}]")),
yaxis=go.layout.YAxis(title=go.layout.yaxis.Title(text="KW")),
legend=go.layout.Legend(title=go.layout.legend.Title(text="Chart Legend")),
)
Expand Down
Loading