Update solarconditions.py

This commit is contained in:
SpudGunMan
2024-06-19 21:15:18 -07:00
parent b6030c466b
commit 1f09bf8b2e

View File

@@ -6,7 +6,6 @@ import requests # pip install requests
import xml.dom.minidom
from datetime import timedelta
from dateutil import tz # pip install python-dateutil
from suntime import Sun, SunTimeException # pip install suntime
from datetime import datetime
import ephem # pip install pyephem
@@ -56,19 +55,20 @@ def drap_xray_conditions():
xray_flux += "error fetching"
return xray_flux
def get_sunrise_sunset(lat=0, lon=0):
if lat != 0 and lon != 0:
sun = Sun(lat, lon)
else:
sun = Sun(LATITUDE, LONGITUDE)
# def get_sunrise_sunset(lat=0, lon=0):
# from suntime import Sun, SunTimeException # pip install suntime
# if lat != 0 and lon != 0:
# sun = Sun(lat, lon)
# else:
# sun = Sun(LATITUDE, LONGITUDE)
to_zone = tz.tzlocal()
today_sr = sun.get_sunrise_time(datetime.now())
today_ss = sun.get_sunset_time(datetime.now())
if today_ss < today_sr: # if sunset is before sunrise, then it's tomorrow
today_ss = today_ss + timedelta(1)
todaysun = [today_sr.astimezone(to_zone).strftime('%a %d %I:%M'), today_ss.astimezone(to_zone).strftime('%a %d %I:%M')]
return todaysun
# to_zone = tz.tzlocal()
# today_sr = sun.get_sunrise_time(datetime.now())
# today_ss = sun.get_sunset_time(datetime.now())
# if today_ss < today_sr: # if sunset is before sunrise, then it's tomorrow
# today_ss = today_ss + timedelta(1)
# todaysun = [today_sr.astimezone(to_zone).strftime('%a %d %I:%M'), today_ss.astimezone(to_zone).strftime('%a %d %I:%M')]
# return todaysun
def get_sun(lat=0, lon=0):
obs = ephem.Observer()
@@ -99,7 +99,7 @@ def get_sun(lat=0, lon=0):
sun_table['transit_time'] = obs.next_transit(sun).datetime().strftime('%a %d %I:%M')
sun_table['direction'] = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'][round(sun.az / (2 * ephem.pi / 8)) % 8]
sun_data = f"Next Sun Rise:" + sun_table['rise_time'] + " Set:" + sun_table['set_time'] \
sun_data = f"Next Sun Rise:" + sun_table['rise_time'] + "\nSet:" + sun_table['set_time'] \
+ "\nSun Azimuth:" + str(sun_table['azimuth']) + " Altitude:" + str(sun_table['altitude']) + "Heading: " + sun_table['direction']
return sun_data