From 4687210542b3ae668da0a2258bdfb8aee87da79c Mon Sep 17 00:00:00 2001 From: Cyberes Date: Sat, 17 Aug 2024 07:08:43 -0600 Subject: [PATCH] ionex data is actually predictions 1 day ahead so get the current day not just the last one in the file list --- README.md | 4 +++- lib/cddis_fetch.py | 5 ++++- main.py | 19 ++++++++++++------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index ed7a362..911c914 100644 --- a/README.md +++ b/README.md @@ -53,4 +53,6 @@ Unit: `(10^16 el) / m^2` VTEC, or Vertical TEC, is a specific type of TEC measurement that is taken along a path extending vertically from the Earth's surface to the edge of the atmosphere. So essentially, VTEC is a subset of TEC, with the -difference lying in the specific path along which the measurement is taken. \ No newline at end of file +difference lying in the specific path along which the measurement is taken. + +Updated hourly. \ No newline at end of file diff --git a/lib/cddis_fetch.py b/lib/cddis_fetch.py index 55902d4..c380359 100644 --- a/lib/cddis_fetch.py +++ b/lib/cddis_fetch.py @@ -1,4 +1,5 @@ import datetime +import logging import subprocess import sys import tempfile @@ -43,7 +44,9 @@ def fetch_latest_ionex(username: str, password: str): day_urls = [element.get_attribute('href') for element in day_elements] # Load the latest day. - driver.get(day_urls[-1]) + today_url = day_urls[-2] # last element is predictions for tomorrow so we want the second to last one + logging.info(f'Using day {today_url.split("/")[-1]}') + driver.get(today_url) # Find our file. file_elements = driver.find_elements(By.XPATH, '//a[@class="archiveItemText"]') diff --git a/main.py b/main.py index bb04918..961916e 100644 --- a/main.py +++ b/main.py @@ -2,12 +2,13 @@ import logging import os import sys import time +from datetime import datetime import numpy as np import paho.mqtt.client as mqtt from lib.cddis_fetch import fetch_latest_ionex -from lib.tecmap import get_tecmaps, plot_tec_map +from lib.tecmap import get_tecmaps, plot_tec_map, parse_ionex_datetime logging.basicConfig(level=logging.INFO) @@ -35,7 +36,6 @@ if not CDDIS_USERNAME or not CDDIS_PASSWORD: client = mqtt.Client(client_id=MQTT_CLIENT_ID) if MQTT_USERNAME and MQTT_PASSWORD: client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD) - logging.info("Username and password set.") client.will_set(MQTT_TOPIC_PREFIX + "/status", payload="Offline", qos=1, retain=True) # set LWT client.connect(MQTT_BROKER_HOST, port=MQTT_BROKER_PORT) client.loop_start() @@ -48,18 +48,23 @@ def publish(topic: str, msg): if status == 0: logging.info(f"Sent {msg} to topic {topic_expanded}") else: - logging.error(f"Failed to send message to topic {topic_expanded}") + logging.error(f"Failed to send message to topic {topic_expanded}: {result}") def main(): while True: + # TODO: tick every second and execute runs if it's time. + # TODO: get TEC map every 15 min and serve from URL https://services.swpc.noaa.gov/images/animations/natec-ustec/ustec_tec/latest.png?time=1716232652000 + utc_hr = datetime.utcnow().hour logging.info('Fetching latest IONEX data') + logging.info(f'Using hour {utc_hr}') ionex_data = fetch_latest_ionex(CDDIS_USERNAME, CDDIS_PASSWORD) - tec_data = [] + avg_tec = None for tecmap, epoch in get_tecmaps(ionex_data): - avg_tec = np.mean(plot_tec_map(tecmap, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)])) - tec_data.append(avg_tec) - latest = round(tec_data[-1], 1) + if parse_ionex_datetime(epoch).hour == utc_hr: + avg_tec = np.mean(plot_tec_map(tecmap, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)])) + break + latest = round(avg_tec, 1) publish('vtec', latest) time.sleep(1800)