fix things

This commit is contained in:
Cyberes 2024-09-03 16:46:11 -06:00
parent 979381f365
commit aa472d98cf
6 changed files with 62 additions and 48 deletions

View File

@ -4,7 +4,8 @@ This is an MQTT sensor to send NOAA space weather data to Home Assistant. Fetchi
1. Create an account at <https://urs.earthdata.nasa.gov> 1. Create an account at <https://urs.earthdata.nasa.gov>
2. `pip install -r requirements.txt` 2. `pip install -r requirements.txt`
3. `sudo apt install p7zip-full` 3. `sudo apt install p7zip-full redis-server`
4. `sudo systemctl enable --now redis-server`
### Google Chrome ### Google Chrome

View File

@ -1,10 +1,11 @@
import io import io
import logging import logging
import pickle import pickle
import sys import time
from datetime import datetime from datetime import datetime
from typing import List from typing import List
import schedule
from PIL import Image from PIL import Image
from redis import Redis from redis import Redis
@ -17,19 +18,22 @@ LAT_RANGE_MAX = 90
LON_RANGE_MIN = -180 LON_RANGE_MIN = -180
LON_RANGE_MAX = 180 LON_RANGE_MAX = 180
def main():
redis = Redis(host='localhost', port=6379, db=0) redis = Redis(host='localhost', port=6379, db=0)
while True:
utc_hr = datetime.utcnow().hour utc_hr = datetime.utcnow().hour
logging.info(f'Generating plot for hour {utc_hr}') logging.info(f'Generating plot for hour {utc_hr}')
ionex_data: List = pickle.loads(redis.get('tecmap_data')) ionex_data: List = pickle.loads(redis.get('tecmap_data'))
if ionex_data is None: while ionex_data is None:
logging.critical('Redis has not been populated yet. Is cache.py running?') logging.warning('Redis has not been populated yet. Is cache.py running? Sleeping 10s...')
sys.exit(1) time.sleep(10)
for tecmap, epoch in ionex_data: for tecmap, epoch in ionex_data:
if epoch.hour == utc_hr: if epoch.hour == utc_hr:
plt = plot_tec_map(tecmap, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)])[1] plt = plot_tec_map(tecmap, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)], timestamp=epoch)[1]
buf = io.BytesIO() buf = io.BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, dpi=110) plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, dpi=110)
@ -38,9 +42,16 @@ for tecmap, epoch in ionex_data:
buf.seek(0) buf.seek(0)
img = Image.open(buf) img = Image.open(buf)
# img = img.resize((img.size[0], 500), Image.LANCZOS)
buf = io.BytesIO() buf = io.BytesIO()
img.save(buf, format='PNG') img.save(buf, format='PNG')
redis.set('global_map', buf.getvalue()) redis.set('global_map', buf.getvalue())
buf.close() buf.close()
if __name__ == '__main__':
main()
schedule.every().hour.at(":00").do(main)
while True:
schedule.run_pending()
time.sleep(1)

View File

@ -32,7 +32,7 @@ def get_tecmaps(ionex: str):
yield parse_map(tecmap), epoch yield parse_map(tecmap), epoch
def plot_tec_map(tecmap, lon_range: list, lat_range: list): def plot_tec_map(tecmap, lon_range: list, lat_range: list, timestamp: datetime = None):
proj = ccrs.PlateCarree() proj = ccrs.PlateCarree()
f, ax = plt.subplots(1, 1, subplot_kw=dict(projection=proj)) f, ax = plt.subplots(1, 1, subplot_kw=dict(projection=proj))
@ -54,7 +54,8 @@ def plot_tec_map(tecmap, lon_range: list, lat_range: list):
# Make graph pretty # Make graph pretty
ax.coastlines() ax.coastlines()
plt.title(datetime.now().strftime('%H:%M %d-%m-%Y'), fontsize=12, y=1.04) if timestamp:
plt.title(timestamp.strftime('%H:%M %d-%m-%Y'), fontsize=12, y=1.04)
plt.suptitle('Vertical Total Electron Count', fontsize=16, y=0.87) plt.suptitle('Vertical Total Electron Count', fontsize=16, y=0.87)
divider = make_axes_locatable(ax) divider = make_axes_locatable(ax)
ax_cb = divider.new_horizontal(size='5%', pad=0.1, axes_class=plt.Axes) ax_cb = divider.new_horizontal(size='5%', pad=0.1, axes_class=plt.Axes)

View File

@ -3,7 +3,6 @@ import os
import pickle import pickle
import sys import sys
import time import time
import traceback
from datetime import datetime from datetime import datetime
from typing import List from typing import List
@ -61,14 +60,18 @@ def publish(topic: str, msg):
def main(): def main():
try:
redis = Redis(host='localhost', port=6379, db=0) redis = Redis(host='localhost', port=6379, db=0)
while True: while True:
utc_hr = datetime.utcnow().hour utc_hr = datetime.utcnow().hour
logging.info('Fetching latest IONEX data') logging.info('Fetching latest IONEX data')
logging.info(f'Using hour {utc_hr}') logging.info(f'Using hour {utc_hr}')
ionex_data: List = pickle.loads(redis.get('tecmap_data')) ionex_data: List = pickle.loads(redis.get('tecmap_data'))
while ionex_data is None:
logging.warning('Redis has not been populated yet. Is cache.py running? Sleeping 10s...')
time.sleep(10)
avg_tec = None avg_tec = None
for tecmap, epoch in ionex_data: for tecmap, epoch in ionex_data:
parsed_dt = parse_ionex_datetime(epoch) parsed_dt = parse_ionex_datetime(epoch)
@ -79,9 +82,6 @@ def main():
latest = round(avg_tec, 1) latest = round(avg_tec, 1)
publish('vtec', latest) publish('vtec', latest)
time.sleep(60) time.sleep(60)
except:
logging.critical(traceback.format_exc())
sys.exit(1)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -9,3 +9,4 @@ redis==5.0.8
async-timeout==4.0.3 async-timeout==4.0.3
Pillow Pillow
flask==3.0.3 flask==3.0.3
schedule==1.2.2

View File

@ -11,7 +11,7 @@ redis_client = redis.Redis(host='localhost', port=6379)
def serve_global_map(): def serve_global_map():
global_map_data = redis_client.get('global_map') global_map_data = redis_client.get('global_map')
if global_map_data is None: if global_map_data is None:
return "No global map available", 404 return "No global map available", 400
buf = io.BytesIO(global_map_data) buf = io.BytesIO(global_map_data)
buf.seek(0) buf.seek(0)