mirror of
https://github.com/SpudGunMan/meshing-around.git
synced 2026-05-01 02:52:18 +02:00
141 lines
6.3 KiB
Python
141 lines
6.3 KiB
Python
# helper functions to get HF band conditions, DRAP X-ray flux, and sunrise/sunset times
|
|
# some code from https://github.com/Murturtle/MeshLink
|
|
# K7MHI Kelly Keeton 2024
|
|
|
|
import requests # pip install requests
|
|
import xml.dom.minidom
|
|
from datetime import timedelta
|
|
from dateutil import tz # pip install python-dateutil
|
|
from datetime import datetime
|
|
import ephem # pip install pyephem
|
|
|
|
LATITUDE = 48.50
|
|
LONGITUDE = -123.0
|
|
|
|
def hf_band_conditions():
|
|
hf_cond = ""
|
|
band_cond = requests.get("https://www.hamqsl.com/solarxml.php", timeout=5)
|
|
if(band_cond.ok):
|
|
solarxml = xml.dom.minidom.parseString(band_cond.text)
|
|
for i in solarxml.getElementsByTagName("band"):
|
|
hf_cond += i.getAttribute("time")[0]+i.getAttribute("name") +"="+str(i.childNodes[0].data)+"\n"
|
|
else:
|
|
hf_cond += "error fetching"
|
|
hf_cond = hf_cond[:-1] # remove the last newline
|
|
return hf_cond
|
|
|
|
def solar_conditions():
|
|
solar_cond = ""
|
|
# get the solar conditions from the xml at "https://www.hamqsl.com/solarxml.php"
|
|
solar_cond = requests.get("https://www.hamqsl.com/solarxml.php", timeout=5)
|
|
if(solar_cond.ok):
|
|
solar_xml = xml.dom.minidom.parseString(solar_cond.text)
|
|
for i in solar_xml.getElementsByTagName("solardata"):
|
|
solar_a_index = i.getElementsByTagName("aindex")[0].childNodes[0].data
|
|
solar_k_index = i.getElementsByTagName("kindex")[0].childNodes[0].data
|
|
solar_xray = i.getElementsByTagName("xray")[0].childNodes[0].data
|
|
solar_flux = i.getElementsByTagName("solarflux")[0].childNodes[0].data
|
|
sunspots = i.getElementsByTagName("sunspots")[0].childNodes[0].data
|
|
signalnoise = i.getElementsByTagName("signalnoise")[0].childNodes[0].data
|
|
solar_cond = "A-Index: " + solar_a_index + "\nK-Index: " + solar_k_index + "\nSunspots: " + sunspots + "\nX-Ray Flux: " + solar_xray + "\nSolar Flux: " + solar_flux + "\nSignal Noise: " + signalnoise
|
|
else:
|
|
solar_cond += "error fetching"
|
|
return solar_cond
|
|
|
|
def drap_xray_conditions():
|
|
drap_cond = ""
|
|
drap_cond = requests.get("https://services.swpc.noaa.gov/text/drap_global_frequencies.txt", timeout=5)
|
|
if(drap_cond.ok):
|
|
drap_list = drap_cond.text.split('\n')
|
|
x_filter = '# X-RAY Message :'
|
|
for line in drap_list:
|
|
if x_filter in line:
|
|
xray_flux = line.split(": ")[1]
|
|
else:
|
|
xray_flux += "error fetching"
|
|
return xray_flux
|
|
|
|
# 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
|
|
|
|
def get_sun(lat=0, lon=0):
|
|
obs = ephem.Observer()
|
|
obs.date = datetime.now()
|
|
sun = ephem.Sun()
|
|
if lat != 0 and lon != 0:
|
|
obs.lat = str(lat)
|
|
obs.lon = str(lon)
|
|
else:
|
|
obs.lat = str(LATITUDE)
|
|
obs.lon = str(LONGITUDE)
|
|
|
|
sun.compute(obs)
|
|
sun_table = {}
|
|
sun_table['azimuth'] = sun.az
|
|
sun_table['altitude'] = sun.alt
|
|
sun_table['ra'] = sun.ra
|
|
sun_table['dec'] = sun.dec
|
|
# distance from earth in km
|
|
sun_table['earth_distance'] = sun.earth_distance * ephem.meters_per_au / 1000
|
|
sun_table['parallactic_angle'] = sun.parallactic_angle()
|
|
# get the next rise and set times
|
|
local_sunrise = ephem.localtime(obs.next_rising(sun)).strftime('%a %d %I:%M')
|
|
local_sunset = ephem.localtime(obs.next_setting(sun)).strftime('%a %d %I:%M')
|
|
sun_table['rise_time'] = local_sunrise
|
|
sun_table['set_time'] = local_sunset
|
|
|
|
sun_table['transit_time'] = ephem.localtime(obs.next_transit(sun)).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'] + "\nSet:" + sun_table['set_time'] \
|
|
+ "\nSun Azimuth:" + str(sun_table['azimuth']) + " Altitude:" + str(sun_table['altitude']) + "Heading: " + sun_table['direction']
|
|
return sun_data
|
|
|
|
def get_moon(lat=0, lon=0):
|
|
obs = ephem.Observer()
|
|
moon = ephem.Moon()
|
|
if lat != 0 and lon != 0:
|
|
obs.lat = str(lat)
|
|
obs.lon = str(lon)
|
|
else:
|
|
obs.lat = str(LATITUDE)
|
|
obs.lon = str(LONGITUDE)
|
|
|
|
obs.date = datetime.now()
|
|
moon.compute(obs)
|
|
moon_table = {}
|
|
moon_phase = ['New Moon', 'Waxing Crescent', 'First Quarter', 'Waxing Gibbous', 'Full Moon', 'Waning Gibbous', 'Last Quarter', 'Waning Crescent'][round(moon.phase / (2 * ephem.pi) * 8) % 8]
|
|
moon_table['phase'] = moon_phase
|
|
moon_table['illumination'] = moon.phase
|
|
moon_table['azimuth'] = moon.az
|
|
moon_table['altitude'] = moon.alt
|
|
moon_table['ra'] = moon.ra
|
|
moon_table['dec'] = moon.dec
|
|
# distance from earth in km
|
|
moon_table['earth_distance'] = moon.earth_distance * ephem.meters_per_au / 1000
|
|
moon_table['parallactic_angle'] = moon.parallactic_angle()
|
|
moon_table['rise_time'] = ephem.localtime(obs.next_rising(moon)).strftime('%a %d %I:%M')
|
|
moon_table['set_time'] = ephem.localtime(obs.next_rising(moon)).strftime('%a %d %I:%M')
|
|
moon_table['transit_time'] = ephem.localtime(obs.next_rising(moon)).strftime('%a %d %I:%M')
|
|
moon_table['direction'] = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'][round(moon.az / (2 * ephem.pi / 8)) % 8]
|
|
moon_table['next_full_moon'] = ephem.localtime(obs.next_rising(moon)).strftime('%a %d %I:%M')
|
|
moon_table['next_new_moon'] = ephem.localtime(obs.next_rising(moon)).strftime('%a %d %I:%M')
|
|
|
|
moon_data = f"Moon Rise:" + moon_table['rise_time'] + " Set:" + moon_table['set_time'] + "\nMoon Phase:" \
|
|
+ moon_table['phase'] + " @:" + str('{0:.2f}'.format(moon_table['illumination'])) + "%" + "\nMoon Azimuth:" + str(moon_table['azimuth']) \
|
|
+ "\nMoon Altitude:" + str(moon_table['altitude']) + "\nMoon Direction:" + moon_table['direction'] \
|
|
+ "\nNext Full Moon:" + moon_table['next_full_moon'] + "\nNext New Moon:" + moon_table['next_new_moon']
|
|
|
|
return moon_data |