Skip to content

Commit

Permalink
Fix sunrise/sunset scatter plots (#16)
Browse files Browse the repository at this point in the history
* Fix sunrise/sunset scatters

Sunrise/sunset scatter plots are misplaced for
* Date ranges that are a subset of the entire DB date range
* Species whose first/last detections are inside the bounds of the current date range.

This fixes those problems.

* Clean up lint errors

* Fix the lint errors created by fixing lint errors

* Restore old lat/lon determination

We'll enhance this separately
  • Loading branch information
cdkl authored Mar 12, 2024
1 parent 459fe5d commit 37b023b
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions scripts/plotly_streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import plotly.express as px
from sklearn.preprocessing import normalize
from suntime import Sun
from datetime import datetime

profile = False
if profile:
Expand Down Expand Up @@ -181,25 +180,21 @@ def time_resample(df, resample_time):
font_size = 15


def sunrise_sunset_scatter(num_days_to_display):
def sunrise_sunset_scatter(date_range):
latitude = df2['Lat'][0]
longitude = df2['Lon'][0]

sun = Sun(latitude, longitude)

sunrise_list = []
sunset_list = []
sunrise_week_list = []
sunset_week_list = []
sunrise_text_list = []
sunset_text_list = []
daysback_range = []

now = datetime.now()
current_date = start_date

for past_day in range(num_days_to_display):
d = timedelta(days=num_days_to_display - past_day - 1)

current_date = now - d
for current_date in date_range:
# current_date = datetime.fromisocalendar(2022, week + 1, 5)
# time_zone = datetime.now()
sun_rise = sun.get_local_sunrise_time(current_date)
Expand All @@ -214,17 +209,17 @@ def sunrise_sunset_scatter(num_days_to_display):
sunset_text_list.append(temp_time)
sunrise_list.append(sun_rise_time)
sunset_list.append(sun_dusk_time)
sunrise_week_list.append(past_day)
sunset_week_list.append(past_day)

sunrise_week_list.append(None)
daysback_range.append(current_date.strftime('%d-%m-%Y'))

sunrise_list.append(None)
sunrise_text_list.append(None)
sunrise_list.extend(sunset_list)
sunrise_week_list.extend(sunset_week_list)
sunrise_text_list.extend(sunset_text_list)
daysback_range.append(None)
daysback_range.extend(daysback_range)

return sunrise_week_list, sunrise_list, sunrise_text_list
return daysback_range, sunrise_list, sunrise_text_list


def hms_to_dec(t):
Expand Down Expand Up @@ -464,12 +459,7 @@ def hms_to_str(t):
# text=labels,
texttemplate="%{text}", autocolorscale=False, colorscale=selected_pal
)
num_days_to_display = len(fig_x)
sunrise_week_list, sunrise_list, sunrise_text_list = sunrise_sunset_scatter(num_days_to_display)
daysback_range = fig_x
daysback_range.append(None)
daysback_range.extend(daysback_range)
daysback_range = daysback_range[:-1]
daysback_range, sunrise_list, sunrise_text_list = sunrise_sunset_scatter(day_hour_freq.index.tolist())

sunrise_sunset = go.Scatter(x=daysback_range,
y=sunrise_list,
Expand Down

0 comments on commit 37b023b

Please sign in to comment.