#!/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 os
import glob
import zipfile

'''
Script to add re-processed html or pdf to zip
'''
# Folder with results
res_folder = '/shared/QAR_dqc/results'

for zip_file in glob.glob(os.path.join(res_folder, '*.zip')):
    zip = zipfile.ZipFile(zip_file, 'a')
    html_file = zip_file.replace('.zip', '-valid_ranges.html')
    html_there = os.path.exists(html_file)
    html_found = False
    for zp in zip.filelist:
        if zp.filename.endswith('.html'):
            html_found = True

    # Add html to zip
    if not(html_found) and html_there:
        zip.write(html_file, os.path.basename(html_file))
