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

all_samples_seasonal_daily = []
all_samples_seasonal_monthly = []
all_samples_reanalysis_daily = []
all_samples_reanalysis_monthly = []

for jj in glob.glob('./conf/*.json'):
    if not('all_samples' in jj) and jj != './conf/download_options.json':

        # Read and add 'sample'
        with open(jj, 'r') as json_file:
            data = json.load(json_file)
            for prod in data:
                prod['sample'] = 'yes'

            # Write/save
            if prod['group'] == 'seasonal':
                if 'original' in prod['dataset']:
                    all_samples_seasonal_daily.append(prod)
                else:
                    all_samples_seasonal_monthly.append(prod)
            else:
                if 'monthly' in prod['dataset']:
                    all_samples_reanalysis_monthly.append(prod)
                else:
                    all_samples_reanalysis_daily.append(prod)

with open('./conf/all_samples_seasonal_daily.json', 'w') as result_file:
    json.dump(all_samples_seasonal_daily, result_file, indent=4)

with open('./conf/all_samples_seasonal_monthly.json', 'w') as result_file:
    json.dump(all_samples_seasonal_monthly, result_file, indent=4)

with open('./conf/all_samples_reanalysis_daily.json', 'w') as result_file:
    json.dump(all_samples_reanalysis_daily, result_file, indent=4)

with open('./conf/all_samples_reanalysis_monthly.json', 'w') as result_file:
    json.dump(all_samples_reanalysis_monthly, result_file, indent=4)