2018-05-18 21:59:06 +02:00
|
|
|
from datetime import datetime, timedelta, time
|
|
|
|
|
|
|
|
import astropy.coordinates as coord
|
|
|
|
from astropy.time import Time
|
2019-04-28 19:26:31 +02:00
|
|
|
from pytz import timezone
|
2018-05-18 21:59:06 +02:00
|
|
|
|
|
|
|
import config
|
|
|
|
|
2019-04-28 19:26:31 +02:00
|
|
|
midday = time(12)
|
|
|
|
|
|
|
|
tz = timezone(config.tz)
|
|
|
|
|
2018-05-18 21:59:06 +02:00
|
|
|
|
|
|
|
def time2altitude(time: datetime) -> float:
|
2018-05-18 22:30:20 +02:00
|
|
|
astro_time = Time(tz.localize(time))
|
2018-05-18 21:59:06 +02:00
|
|
|
altaz = coord.AltAz(location=config.loc, obstime=astro_time)
|
|
|
|
sun = coord.get_sun(astro_time)
|
|
|
|
return sun.transform_to(altaz).alt.degree
|
|
|
|
|
|
|
|
|
2019-04-28 19:26:31 +02:00
|
|
|
def get_time(altitude):
|
2018-05-18 21:59:06 +02:00
|
|
|
lower = datetime.combine(datetime.now().date(), midday)
|
|
|
|
upper = lower + timedelta(hours=12)
|
|
|
|
|
2019-04-28 19:26:31 +02:00
|
|
|
while upper - lower > timedelta(seconds=1):
|
2018-05-18 21:59:06 +02:00
|
|
|
middle = lower + (upper - lower) / 2
|
|
|
|
if time2altitude(middle) > altitude:
|
|
|
|
lower = middle
|
|
|
|
else:
|
|
|
|
upper = middle
|
|
|
|
|
|
|
|
return lower + (upper - lower) / 2
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
target = 14.480046611643763
|
|
|
|
print(get_time(target))
|