import glob
import os

'''
Script run in order merge monthly and daily pdfs
'''
csv_dir = '/shared/QAR_dqc/results'

for monthly_pdf in glob.glob(os.path.join(csv_dir, '*monthly*.pdf')):
    if 'file_format' in monthly_pdf or 'standard_compliance' in monthly_pdf:
        # Seasonal
        if 'seasonal-monthly-single-levels' in monthly_pdf:
            daily_pdf = monthly_pdf.replace('monthly', 'original')
            out_name = monthly_pdf.replace('seasonal-monthly-single-levels', 'seasonal-single-levels-merged')

        # Reanalysis-era5
        if 'reanalysis-era5-single-levels-monthly-means' in monthly_pdf:
            daily_pdf = monthly_pdf.replace('-monthly-means', '')
            out_name = monthly_pdf.replace('reanalysis-era5-single-levels-monthly-means', 'reanalysis-era5-single-levels-merged')

        # Reanalysis-land
        if 'reanalysis-era5-land-monthly-means' in monthly_pdf:
            daily_pdf = monthly_pdf.replace('-monthly-means', '')
            out_name = monthly_pdf.replace('reanalysis-era5-land-monthly-means', 'reanalysis-era5-land-merged')

        # Let's merge if we can

        if os.path.exists(daily_pdf) and os.path.exists(monthly_pdf):
            cmd = 'pdfunite {d} {m} {o}'.format(d=daily_pdf, m=monthly_pdf, o=out_name)
            print(daily_pdf)
            print(monthly_pdf)
            print(out_name)
            os.system(cmd)
            print('------')