#!/usr/bin/env python
#
# Data Quality Checker also known as DQC
#
# Read the README.md for more detailed information
#
#       Barcelona Supercomputing Center - Centro Nacional de Supercomputacion
#       Earth Sciences Department - Computational Earth Sciences
#
#     Authors:
#       Joan Sala Calero (joan.sala@bsc.es)
#

import glob
import os
import configparser

from dqc_wrapper.utils.utils_forms import get_metadata
from dqc_wrapper.utils.utils_bokeh import BokehPlot

'''
Script run in order to re-plot html files (example, if CSV's change)
'''
# Folder with results
res_folder = '/shared/QAR_dqc/results'
conf_folder = '/data/joan/c3s512-wp1-datachecker/dqc_wrapper/QAR_dqc/conf'
forms_dir = '/data/cds-forms-c3s'

for cf_file in glob.glob(os.path.join(conf_folder, '*.ini')):
    # Read config
    config = configparser.ConfigParser()
    config.read(cf_file)
    dataset = config['dataset']['cds_dataset']
    variable = config['dataset']['cds_variable']
    csv_hist = os.path.join(res_folder, os.path.basename(cf_file).replace('.ini', '-hist.csv'))
    csv_stats = csv_hist.replace('-hist.csv', '-stats.csv')
    out_html = csv_hist.replace('-hist.csv', '-valid_ranges.html')
    metadata = get_metadata(forms_dir, dataset, variable)

    # Re-do individual plots
    if os.path.exists(csv_hist) and os.path.exists(csv_stats):
        print(out_html)
        bp = BokehPlot(out_html, csv_stats, csv_hist, variable, dataset, None, None, metadata)
        figs = bp.plot_histograms()
