created get_next functions
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import logging
|
||||
from contextlib import suppress
|
||||
from dataclasses import InitVar, dataclass, field
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime, timedelta, tzinfo
|
||||
from typing import Dict, Iterable
|
||||
|
||||
import astral
|
||||
@@ -14,6 +14,7 @@ from IPython.display import display
|
||||
|
||||
HOME_TZ = datetime.now().astimezone().tzinfo
|
||||
|
||||
|
||||
def format_x_axis(fig):
|
||||
ax: plt.Axes = fig.axes[0]
|
||||
ax.xaxis.set_major_locator(mdates.HourLocator(byhour=range(0, 24, 2)))
|
||||
@@ -29,7 +30,44 @@ def normalize(s: pd.Series, min=None, max=None):
|
||||
return ((s - min) / rng) * 100
|
||||
|
||||
|
||||
def get_next_time_at_elevation(*args, **kwargs):
|
||||
time = time_at_elevation(*args, **kwargs)
|
||||
if time < (now := datetime.now(HOME_TZ)):
|
||||
time = time_at_elevation(
|
||||
*args,
|
||||
date=(now + timedelta(days=1)).date(),
|
||||
**kwargs
|
||||
)
|
||||
return time
|
||||
|
||||
|
||||
def get_next_sun_time(named_time: str, observer: Observer):
|
||||
sun_times_dict = sun(observer, datetime.today().date())
|
||||
try:
|
||||
time = sun_times_dict[named_time].astimezone()
|
||||
except KeyError:
|
||||
time = datetime.combine(
|
||||
datetime.today().date(),
|
||||
datetime.strptime(named_time, '%I:%M:%S%p').time()
|
||||
).astimezone()
|
||||
|
||||
if time < (now := datetime.now(HOME_TZ)):
|
||||
tomorrow = (now + timedelta(days=1)).date()
|
||||
sun_times_dict = sun(observer, tomorrow)
|
||||
try:
|
||||
time = sun_times_dict[named_time].astimezone()
|
||||
except KeyError:
|
||||
time = datetime.combine(
|
||||
tomorrow,
|
||||
datetime.strptime(named_time, '%I:%M:%S%p').time()
|
||||
).astimezone()
|
||||
|
||||
return time
|
||||
|
||||
|
||||
def parse_periods(observer: Observer, periods: Dict):
|
||||
now = datetime.now(HOME_TZ)
|
||||
|
||||
for period in periods:
|
||||
if 'elevation' in period:
|
||||
if period['direction'] == 'rising':
|
||||
@@ -38,24 +76,19 @@ def parse_periods(observer: Observer, periods: Dict):
|
||||
dir = SunDirection.SETTING
|
||||
|
||||
if isinstance(period['elevation'], int):
|
||||
time = time_at_elevation(
|
||||
time = get_next_time_at_elevation(
|
||||
observer,
|
||||
elevation=period['elevation'],
|
||||
direction=dir,
|
||||
tzinfo=HOME_TZ,
|
||||
)
|
||||
|
||||
elif period['elevation'] == 'min':
|
||||
pass
|
||||
elif period['elevation'] == 'max':
|
||||
pass
|
||||
elif 'time' in period:
|
||||
try:
|
||||
time = sun(observer, datetime.today().date())[period['time']].astimezone()
|
||||
except:
|
||||
time = datetime.combine(
|
||||
datetime.today().date(),
|
||||
datetime.strptime(period['time'], '%I:%M:%S%p').time()
|
||||
).astimezone()
|
||||
time = get_next_sun_time(period['time'], observer)
|
||||
|
||||
# res = {'time': time.replace(tzinfo=None)}
|
||||
# res = {'time': time.replace(tzinfo=HOME_TZ)}
|
||||
@@ -64,7 +97,6 @@ def parse_periods(observer: Observer, periods: Dict):
|
||||
yield res
|
||||
|
||||
|
||||
|
||||
@dataclass
|
||||
class DaylightAdjuster:
|
||||
latitude: float
|
||||
@@ -75,28 +107,30 @@ class DaylightAdjuster:
|
||||
|
||||
def __post_init__(self, periods: Dict, resolution: int):
|
||||
self.logger: logging.Logger = logging.getLogger(type(self).__name__)
|
||||
today = self.datetime.date()
|
||||
now = datetime.now().astimezone()
|
||||
times = pd.date_range(
|
||||
today, today + timedelta(days=1),
|
||||
start=now, end=now + timedelta(days=1),
|
||||
periods=resolution,
|
||||
tz=HOME_TZ
|
||||
)
|
||||
self.logger.debug(times)
|
||||
|
||||
pytimes = (dt.to_pydatetime() for dt in times)
|
||||
pytimes = [dt.to_pydatetime() for dt in times]
|
||||
|
||||
el = pd.Series(
|
||||
(elevation(self.observer, dt) for dt in pytimes),
|
||||
index=times, name='elevation'
|
||||
)
|
||||
self.logger.debug(el)
|
||||
|
||||
# el.index = el.index.tz_convert(HOME_TZ)
|
||||
# el.index = el.index.tz_convert(None)
|
||||
|
||||
self.periods = parse_periods(self.observer, periods)
|
||||
|
||||
# self.df = pd.DataFrame(el)
|
||||
self.df = pd.concat([
|
||||
pd.DataFrame(parse_periods(self.observer, periods)).set_index('time'),
|
||||
el
|
||||
], axis=1).sort_index().interpolate().bfill().ffill()
|
||||
self.df = pd.concat([pd.DataFrame(self.periods).set_index('time'), el], axis=1)
|
||||
self.df = self.df.sort_index().interpolate().bfill().ffill()
|
||||
|
||||
# self.df.index = self.df.index.to_series().dt.tz_localize(None)
|
||||
|
||||
@@ -110,12 +144,15 @@ class DaylightAdjuster:
|
||||
|
||||
def elevation_fig(self):
|
||||
fig, ax = plt.subplots(figsize=(10, 7))
|
||||
handles = ax.plot(self.df['elevation'])
|
||||
elevation = self.df['elevation']
|
||||
elevation.index = elevation.index.to_series().dt.tz_localize(None)
|
||||
handles = ax.plot(elevation)
|
||||
|
||||
ax.set_ylabel('Elevation')
|
||||
ax.set_ylim(-100, 100)
|
||||
format_x_axis(fig)
|
||||
ax.set_xlim(self.df.index[0], self.df.index[-1])
|
||||
ax.set_xlim(elevation.index[0], elevation.index[-1])
|
||||
print(elevation)
|
||||
|
||||
# ax.xaxis_date(HOME_TZ)
|
||||
|
||||
@@ -125,9 +162,9 @@ class DaylightAdjuster:
|
||||
ax2.set_ylabel('Brightness')
|
||||
ax2.set_ylim(0, 100)
|
||||
|
||||
handles.append(ax.axvline(datetime.now(),
|
||||
linestyle='--',
|
||||
color='g'))
|
||||
# handles.append(ax.axvline(datetime.now().astimezone(),
|
||||
# linestyle='--',
|
||||
# color='g'))
|
||||
|
||||
# handles.append(ax2.axhline(self.get_brightness(),
|
||||
# linestyle='--',
|
||||
|
||||
Reference in New Issue
Block a user