ha-noaa-space-weather/feeder/global-image.py

59 lines
1.5 KiB
Python
Raw Normal View History

import io
import logging
import pickle
2024-09-03 16:46:11 -06:00
import time
2024-09-03 17:54:07 -06:00
from datetime import datetime
2024-09-03 16:46:11 -06:00
import schedule
from PIL import Image
from redis import Redis
from lib.tecmap import plot_tec_map
logging.basicConfig(level=logging.INFO)
2024-09-03 17:34:23 -06:00
# Entire planet
LAT_RANGE_MIN = -90
LAT_RANGE_MAX = 90
LON_RANGE_MIN = -180
LON_RANGE_MAX = 180
2024-09-03 16:46:11 -06:00
def main():
redis = Redis(host='localhost', port=6379, db=0)
2024-09-03 17:09:36 -06:00
utc_hr = datetime.utcnow().hour
logging.info(f'Generating plot for hour {utc_hr}')
2024-09-03 17:11:22 -06:00
data = redis.get('tecmap_data')
while data is None:
2024-09-03 17:09:36 -06:00
logging.warning('Redis has not been populated yet. Is cache.py running? Sleeping 10s...')
time.sleep(10)
2024-09-03 17:21:14 -06:00
data = redis.get('tecmap_data')
2024-09-03 17:11:22 -06:00
ionex_data = pickle.loads(data)
2024-09-03 17:09:36 -06:00
for tecmap, epoch in ionex_data:
if epoch.hour == utc_hr:
2024-09-03 17:54:07 -06:00
plt = plot_tec_map(tecmap, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)], timestamp_utc=epoch)[1]
2024-09-03 17:09:36 -06:00
buf = io.BytesIO()
2024-09-03 17:37:28 -06:00
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0.1, dpi=110)
2024-09-03 17:09:36 -06:00
plt.close()
del plt
2024-09-03 17:09:36 -06:00
buf.seek(0)
img = Image.open(buf)
buf = io.BytesIO()
img.save(buf, format='PNG')
2024-09-03 16:46:11 -06:00
2024-09-03 17:09:36 -06:00
redis.set('global_map', buf.getvalue())
buf.close()
logging.info(f'Finished hour {utc_hr}')
2024-09-03 16:46:11 -06:00
if __name__ == '__main__':
main()
2024-09-03 17:09:36 -06:00
schedule.every().hour.at(':00').do(main)
2024-09-03 16:46:11 -06:00
while True:
schedule.run_pending()
time.sleep(1)