[update] html writer creates hourly figures of all stations

This commit is contained in:
Marcel Paffrath 2022-11-09 14:32:13 +01:00
parent 18dac062ef
commit c723a32274
4 changed files with 48 additions and 4 deletions

View File

@ -12,6 +12,7 @@ verbosity: 0
reread_parameters: True # reread parameters file (change parameters on runtime, not for itself/GUI refresh/datapath) reread_parameters: True # reread parameters file (change parameters on runtime, not for itself/GUI refresh/datapath)
track_changes: True # tracks all changes since GUI startup by text highlighting (GUI only) track_changes: True # tracks all changes since GUI startup by text highlighting (GUI only)
dt_thresh: [300, 1800] # threshold (s) for timing delay colourisation (yellow/red) dt_thresh: [300, 1800] # threshold (s) for timing delay colourisation (yellow/red)
html_figures: True # Create html figure directory and links
POWBOX: POWBOX:
pb_ok: 1 # Voltage for PowBox OK pb_ok: 1 # Voltage for PowBox OK

View File

@ -17,4 +17,4 @@ export MKL_NUM_THREADS=1
export NUMEXPR_NUM_THREADS=1 export NUMEXPR_NUM_THREADS=1
export OMP_NUM_THREADS=1 export OMP_NUM_THREADS=1
python survBot.py -html '/home/marcel/public_html/survBot_out.html' python survBot.py -html '/home/marcel/public_html/'

View File

@ -12,6 +12,7 @@ import argparse
import time import time
from datetime import timedelta from datetime import timedelta
import numpy as np import numpy as np
import matplotlib.pyplot as plt
from obspy import read, UTCDateTime, Stream from obspy import read, UTCDateTime, Stream
from obspy.clients.filesystem.sds import Client from obspy.clients.filesystem.sds import Client
@ -55,6 +56,7 @@ class SurveillanceBot(object):
self.parameter_path = parameter_path self.parameter_path = parameter_path
self.update_parameters() self.update_parameters()
self.starttime = UTCDateTime() self.starttime = UTCDateTime()
self.plot_hour = self.starttime.hour
self.outpath_html = outpath_html self.outpath_html = outpath_html
self.filenames = [] self.filenames = []
self.filenames_read = [] self.filenames_read = []
@ -220,16 +222,20 @@ class SurveillanceBot(object):
:param refresh_period: Update every x seconds :param refresh_period: Update every x seconds
:return: :return:
''' '''
first_exec = True
status = 'ok' status = 'ok'
while status == 'ok' and self.refresh_period > 0: while status == 'ok' and self.refresh_period > 0:
status = self.execute_qc() status = self.execute_qc()
if self.outpath_html: if self.outpath_html:
self.write_html_table() self.write_html_table()
if self.parameters.get('html_figures'):
self.write_html_figures(check_plot_time=not(first_exec))
else: else:
self.print_analysis() self.print_analysis()
time.sleep(self.refresh_period) time.sleep(self.refresh_period)
if not self.outpath_html: if not self.outpath_html:
self.clear_prints() self.clear_prints()
first_exec = False
def console_print(self, itemlist, str_len=21, sep='|', seplen=3): def console_print(self, itemlist, str_len=21, sep='|', seplen=3):
assert len(sep) <= seplen, f'Make sure seperator has less than {seplen} characters' assert len(sep) <= seplen, f'Make sure seperator has less than {seplen} characters'
@ -240,8 +246,41 @@ class SurveillanceBot(object):
string += item.center(str_len) + sr string += item.center(str_len) + sr
self.print(string, flush=False) self.print(string, flush=False)
def check_plot_hour(self):
''' Check if new hour started '''
current_hour = UTCDateTime().hour
if not current_hour > self.plot_hour:
return False
if current_hour == 23:
self.plot_hour = 0
else:
self.plot_hour += 1
return True
def get_fig_path_abs(self, st_id):
return pjoin(self.outpath_html, self.get_fig_path_rel(st_id))
def get_fig_path_rel(self, st_id):
return os.path.join('figures', f'{st_id.rstrip(".")}.png')
def write_html_figures(self, check_plot_time=True):
""" Write figures for html, right now hardcoded hourly """
if check_plot_time and not self.check_plot_hour():
return
for st_id in self.station_list:
fig = plt.figure(figsize=(16, 9))
fnout = self.get_fig_path_abs(st_id)
st = self.data.get(st_id)
if st:
st.plot(fig=fig, show=False, draw=False, block=False, equal_scale=False, method='full')
ax = fig.axes[0]
ax.set_title(f'Hourly refreshed plot at (UTC) {UTCDateTime.now().strftime("%Y-%m-%d %H:%M:%S")}')
fig.savefig(fnout, dpi=150., bbox_inches='tight')
plt.close(fig)
def write_html_table(self, default_color='#e6e6e6'): def write_html_table(self, default_color='#e6e6e6'):
fnout = self.outpath_html fnout = pjoin(self.outpath_html, 'survBot_out.html')
if not fnout: if not fnout:
return return
try: try:
@ -259,7 +298,8 @@ class SurveillanceBot(object):
# Write all cells # Write all cells
for st_id in self.station_list: for st_id in self.station_list:
col_items = [dict(text=st_id.rstrip('.'), color=default_color)] fig_name = self.get_fig_path_rel(st_id)
col_items = [dict(text=st_id.rstrip('.'), color=default_color, image_src=fig_name)]
for check_key in self.keys: for check_key in self.keys:
status_dict, detailed_dict = self.analysis_results.get(st_id) status_dict, detailed_dict = self.analysis_results.get(st_id)
status = status_dict.get(check_key) status = status_dict.get(check_key)

View File

@ -42,7 +42,10 @@ def write_html_row(fobj, items, html_key='td'):
color = item.get('color') color = item.get('color')
# check for black background of headers (shouldnt happen anymore) # check for black background of headers (shouldnt happen anymore)
color = '#e6e6e6' if color == '#000000' else color color = '#e6e6e6' if color == '#000000' else color
fobj.write(2 * default_space + f'<{html_key} bgcolor="{color}" title="{tooltip}">' + text + f'</{html_key}>\n') image_src = item.get('image_src')
image_str = f'<a href="{image_src}">' if image_src else ''
fobj.write(2 * default_space + f'<{html_key} bgcolor="{color}" title="{tooltip}"> {image_str}'
+ text + f'</{html_key}>\n')
fobj.write(default_space + '</tr>\n') fobj.write(default_space + '</tr>\n')
def get_print_title_str(parameters): def get_print_title_str(parameters):