#!/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:
#       Julian Rodrigo Berlin (julian.berlin@bsc.es)
#
import glob
import os
# Local imports
from dqc_wrapper.utils.utils_files import *
from dqc_wrapper.utils.utils_pdf import *


'''
Script to update the existing files with newly/current generated files, 
assumption: they already have the right name following the C3S naming convention and the set is complete
 (all qar files are there for a given key)
'''
# Folder with results
res_folder = '/shared/QAR_dqc/results_tmp_renaming/temp'
# Target output folder
out_folder = '/shared/QAR_dqc/results_tmp_renaming/temp/zips'

#get all zip files in the specified path
for zip_file in sorted(glob.glob(os.path.join(res_folder, '*.zip'))):

    try:
        # Find cms_name
        key = os.path.basename(zip_file).replace('.zip', '')
        out_file = os.path.join(out_folder, os.path.basename(zip_file))
        file_list = []
        file_list = sorted(glob.glob(os.path.join(res_folder, key + '!(*.zip)')))
        print(zip_file)
        print(file_list)
        # save the files
        if len(file_list) > 1:
            print(out_file)
            zip_results(out_file, file_list)

        print('----------------')
    except:
        print('WARNING: problems opening file {}'.format(zip_file))
        pass