mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-06-24 05:35:10 +00:00
Solar condition monitoring improvements, mostly polling GIRO at a steady continual rate rather than bursting every hour, bug fixes and commenting improvements
This commit is contained in:
@@ -10,7 +10,12 @@ from core.constants import HTTP_HEADERS
|
||||
from solarconditionsproviders.ionosonde_utils import compute_band_states
|
||||
from solarconditionsproviders.solar_conditions_provider import SolarConditionsProvider
|
||||
|
||||
POLL_INTERVAL = 3600 # 1 hour
|
||||
# Each station gets polled roughly once every hour (3600 seconds). Note that to avoid a burst of requests to the server
|
||||
# every hour, the requests for data from each station are spaced out throughout the hour, leading to one request being
|
||||
# sent every 1-2 minutes.
|
||||
POLL_INTERVAL = 3600
|
||||
# To avoid looking up all stations in the GIRO system and working out which ones are providing live data, this has been
|
||||
# manually determined and a CSV provided of all the stations that we can query for live data.
|
||||
STATIONS_INDEX = "datafiles/didbase-stations.csv"
|
||||
LGDC_URL = "https://lgdc.uml.edu/common/DIDBGetValues"
|
||||
HISTORY_HOURS = 24
|
||||
@@ -19,8 +24,9 @@ HISTORY_HOURS = 24
|
||||
class GIROIonosonde(SolarConditionsProvider):
|
||||
"""Solar conditions provider using ionosonde data from the GIRO Data Center.
|
||||
Queries foF2, MUF, and LUF measurements for all stations in datafiles/didbase-stations.csv.
|
||||
Can run alongside KC2GProp: GIRO supplements KC2G's foF2/MUF data with LUF readings, and
|
||||
stations from each source that the other does not cover are preserved."""
|
||||
|
||||
Designed to run alongside KC2GProp even though they produce similar data. GIRO has more stations and includes LUF
|
||||
data, but is less reliable and often offline."""
|
||||
|
||||
def __init__(self, provider_config):
|
||||
super().__init__(provider_config)
|
||||
@@ -61,64 +67,59 @@ class GIROIonosonde(SolarConditionsProvider):
|
||||
self._stop_event.set()
|
||||
|
||||
def _run(self):
|
||||
# Real interval at which we poll is the "once per hour" divided by the number of stations, so each one gets
|
||||
# polled once per hour, just not all at once
|
||||
interval = POLL_INTERVAL / len(self._stations)
|
||||
station_index = 0
|
||||
while True:
|
||||
self._poll()
|
||||
if self._stop_event.wait(timeout=POLL_INTERVAL):
|
||||
self._poll_station(self._stations[station_index])
|
||||
station_index = (station_index + 1) % len(self._stations)
|
||||
if self._stop_event.wait(timeout=interval):
|
||||
break
|
||||
|
||||
def _poll(self):
|
||||
def _poll_station(self, station):
|
||||
ursi = station["ursi"]
|
||||
name = station["name"]
|
||||
try:
|
||||
logging.debug("Polling GIRO ionosonde data...")
|
||||
logging.debug(f"Polling GIRO ionosonde data for {ursi} ({name})...")
|
||||
now = datetime.now(timezone.utc)
|
||||
from_time = now - timedelta(hours=HISTORY_HOURS)
|
||||
cutoff_ts = from_time.timestamp()
|
||||
|
||||
fof2, muf, luf = self._fetch_station_data(ursi, from_time, now)
|
||||
if not fof2 or not muf:
|
||||
return
|
||||
|
||||
# Start from the existing ionosonde_data so stations provided by other providers
|
||||
# (e.g. KC2GProp) are preserved for stations GIRO does not cover.
|
||||
ionosonde_data = dict(self._solar_conditions.ionosonde_data or {})
|
||||
updated_count = 0
|
||||
|
||||
for station in self._stations:
|
||||
if self._stop_event.is_set():
|
||||
break
|
||||
ursi = station["ursi"]
|
||||
name = station["name"]
|
||||
try:
|
||||
fof2, muf, luf = self._fetch_station_data(ursi, from_time, now)
|
||||
if not fof2 or not muf:
|
||||
continue
|
||||
# Merge GIRO's readings into any existing data for this station.
|
||||
existing = ionosonde_data.get(ursi, {})
|
||||
merged_fof2 = {**{float(t): v for t, v in (existing.get("fof2") or {}).items()}, **fof2}
|
||||
merged_muf = {**{float(t): v for t, v in (existing.get("muf") or {}).items()}, **muf}
|
||||
merged_luf = dict(luf) if luf else {}
|
||||
|
||||
# Merge GIRO's readings into any existing data for this station.
|
||||
existing = ionosonde_data.get(ursi, {})
|
||||
merged_fof2 = {**{float(t): v for t, v in (existing.get("fof2") or {}).items()}, **fof2}
|
||||
merged_muf = {**{float(t): v for t, v in (existing.get("muf") or {}).items()}, **muf}
|
||||
merged_luf = dict(luf) if luf else {}
|
||||
|
||||
merged_fof2 = {t: v for t, v in merged_fof2.items() if t >= cutoff_ts}
|
||||
merged_muf = {t: v for t, v in merged_muf.items() if t >= cutoff_ts}
|
||||
merged_luf = {t: v for t, v in merged_luf.items() if t >= cutoff_ts}
|
||||
|
||||
band_states = compute_band_states(merged_fof2, merged_muf, merged_luf)
|
||||
ionosonde_data[ursi] = {
|
||||
"ursi": ursi, "name": name,
|
||||
"fof2": merged_fof2 or None,
|
||||
"muf": merged_muf or None,
|
||||
"luf": merged_luf or None,
|
||||
"band_states": band_states,
|
||||
}
|
||||
updated_count += 1
|
||||
except Exception:
|
||||
logging.warning(f"Could not fetch ionosonde data for {ursi} ({name})")
|
||||
merged_fof2 = {t: v for t, v in merged_fof2.items() if t >= cutoff_ts}
|
||||
merged_muf = {t: v for t, v in merged_muf.items() if t >= cutoff_ts}
|
||||
merged_luf = {t: v for t, v in merged_luf.items() if t >= cutoff_ts}
|
||||
|
||||
band_states = compute_band_states(merged_fof2, merged_muf, merged_luf)
|
||||
ionosonde_data[ursi] = {
|
||||
"ursi": ursi, "name": name,
|
||||
"fof2": merged_fof2 or None,
|
||||
"muf": merged_muf or None,
|
||||
"luf": merged_luf or None,
|
||||
"band_states": band_states,
|
||||
}
|
||||
self.update_data({"ionosonde_data": ionosonde_data})
|
||||
self.status = "OK"
|
||||
self.last_update_time = datetime.now(pytz.UTC)
|
||||
logging.debug(f"Updated ionosonde data for {updated_count} stations.")
|
||||
logging.debug(f"Updated ionosonde data for {ursi} ({name}).")
|
||||
|
||||
except Exception:
|
||||
self.status = "Error"
|
||||
logging.exception("Exception in GIRO Ionosonde data provider")
|
||||
self._stop_event.wait(timeout=1)
|
||||
logging.exception(f"Exception fetching GIRO ionosonde data for {ursi} ({name})")
|
||||
|
||||
def _fetch_station_data(self, ursi, from_time, to_time):
|
||||
"""Fetch foF2, MUF and LUF readings for a station. Returns (fof2_dict, muf_dict, luf_dict) keyed by UNIX timestamp."""
|
||||
|
||||
Reference in New Issue
Block a user