Tools, scripts, apps and functions to run fast source and ground shaking estimation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.0 KiB

# GPLv3
#
# The Developers, 21st Century
import os
import os.path as op
import shutil
import logging
from .plot import (plot_classes, plot_classes_gnss,
GMTPlot, MPLPlot, ViewPlot)
logger = logging.getLogger('ewrica.si.ids.report.base')
path_event = op.join('{reportdir}', '{event_name}')
paths_main = [
op.join('{reportdir}', 'components'),
op.join('{reportdir}', 'scripts'),
path_event]
def _init_report(reportdir, event_name):
paths = [p.format(
reportdir=reportdir, event_name=event_name) for p in paths_main]
# TODO copy a template with all css, js and html scripts instead
for p in paths:
os.makedirs(p)
def _check_reports_exist(reportdir, event_name):
# Initialize report folder structure (if needed)
if not op.exists(reportdir):
_init_report(reportdir, event_name)
elif not os.listdir(reportdir):
_init_report(reportdir, event_name)
path = path_event.format(reportdir=reportdir, event_name=event_name)
if not op.exists(path):
os.makedirs(path)
def _get_reports(reportdir, event_name):
# TODO have list of reports available (yaml?)
_check_reports_exist(reportdir, event_name)
return os.listdir(path_event.format(
reportdir=reportdir, event_name=event_name))
def create_report(
reportdir,
run,
source,
waveform_result,
gnss_obs=None,
gnss_syn=None,
*args,
**kwargs):
# Check for existing report
runs = _get_reports(reportdir, event_name=source.name)
path = op.join(
path_event.format(
reportdir=reportdir,
event_name=source.name),
run)
if run in runs:
logger.warning('Overwrite report directory %s', run)
shutil.rmtree(path)
os.makedirs(path)
classes = plot_classes
if gnss_obs is not None and gnss_syn is not None:
classes += plot_classes_gnss
kwargs_plot = dict(
source=source,
waveform_result=waveform_result,
gnss_obs=gnss_obs,
gnss_syn=gnss_syn)
for cl in classes:
cl, group, items = cl().make(**kwargs_plot)
if items is None:
continue
logger.info('Working on %s ...', group.title)
cls_path = op.join(path, cl.name)
os.makedirs(cls_path)
fig_fn = op.join(cls_path, '{name}.png')
if isinstance(cl, MPLPlot):
for it, fig in items:
fig.savefig(fig_fn.format(name=it.name))
logger.info('Saved %s', fig_fn.format(name=it.name))
elif isinstance(cl, ViewPlot):
for it, fig in items:
fig.save(fig_fn.format(name=it.name))
logger.info('Saved %s', fig_fn.format(name=it.name))
elif isinstance(cl, GMTPlot):
for it, fig in items:
fig.save(fig_fn.format(name=it.name))
logger.info('Saved %s', fig_fn.format(name=it.name))
# Update all needed files (later index.html or similar)