import glob
import os

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

from bokeh.layouts import column
from bokeh.io import save, output_file

'''
Script run in order merge monthly and daily html plots into one
'''
forms_dir = '/data/cds-forms-c3s'
csv_dir = '/shared/QAR_dqc/results'

for csv in glob.glob(os.path.join(csv_dir, '*hist.csv')):

    daily_csv = ''
    
    # Seasonal
    if 'seasonal-monthly-single-levels' in csv:
        daily_csv = csv.replace('monthly', 'original')
        cds_dataset1 = 'seasonal-monthly-single-levels'
        cds_dataset2 = 'seasonal-original-single-levels'
        out_name = 'seasonal-single-levels-merged'

    # Reanalysis-era5
    if 'reanalysis-era5-single-levels-monthly-means' in csv:
        daily_csv = csv.replace('-monthly-means', '')
        cds_dataset1 = 'reanalysis-era5-single-levels-monthly-means'
        cds_dataset2 = 'reanalysis-era5-single-levels'
        out_name = 'reanalysis-era5-single-levels-merged'

    # Reanalysis-land
    if 'reanalysis-era5-land-monthly-means' in csv:
        daily_csv = csv.replace('-monthly-means', '')
        cds_dataset1 = 'reanalysis-era5-land-monthly-means'
        cds_dataset2 = 'reanalysis-era5-land'
        out_name = 'reanalysis-era5-land-merged'

    # Let's merge if we can
    if os.path.exists(daily_csv):
        # Common fields
        cds_variable = os.path.basename(daily_csv).replace(cds_dataset2, '').split('-')[1]
        metadata = get_metadata(forms_dir, cds_dataset1, cds_variable)
        csv_hist1 = csv
        csv_hist2 = daily_csv
        csv_stats1 = csv.replace('hist', 'stats')
        csv_stats2 = daily_csv.replace('hist', 'stats')
        out_html = daily_csv.replace(cds_dataset2, out_name).replace('-hist.csv', '-valid_ranges.html')

        # Plot 1
        bp1 = BokehPlot('', csv_stats1, csv_hist1, cds_variable, cds_dataset1, None, None, metadata)
        figs1=bp1.plot_histograms(just_figures=True)

        # Plot 2
        bp2 = BokehPlot('', csv_stats2, csv_hist2, cds_variable, cds_dataset2, None, None, metadata)
        figs2=bp2.plot_histograms(just_figures=True)

        print('PLOT: '+out_html)
        output_file(out_html)
        save(column(figs1 + figs2))