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

from dqc_wrapper.utils.utils_forms import *

# Input/Output
json_file = '../grib/datasets.json'
grib1_file = '../grib/grib1.json'
grib2_file = '../grib/grib2.json'
forms_dir = '/data/cds-forms-c3s'

grib_def = None
with open(grib1_file, 'r') as gf1, open(grib2_file, 'r') as gf2:
	grib1_def = json.load(gf1)
	grib2_def = json.load(gf2)
	
# Filter out products from cds-forms
datasets=[]
[datasets.append(d) for d in glob.glob(os.path.join(forms_dir, 'seasonal-*'))]
[datasets.append(d) for d in glob.glob(os.path.join(forms_dir, 'reanalysis-era5-*'))]
[datasets.append(d) for d in glob.glob(os.path.join(forms_dir, 'reanalysis-uerra-*'))]

data = dict()
for dataset in datasets:	
	try:
		key_d = os.path.basename(dataset)
		v,p = get_dataset_information(dataset, forms_dir, grib1_def, grib2_def)
		obj = {
			'variables': v,
			'product_types': p
		}
		data[key_d] = obj
	except Exception as e:
		print(e)


# Save to JSON
with open(json_file, 'w') as fo:
	json.dump(data, fo, indent=2)
