#!/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/cds_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 = [
	'reanalysis-era5-single-levels',
	'reanalysis-era5-single-levels-monthly-means',
	'reanalysis-era5-land',
	'reanalysis-era5-land-monthly-means',
	'reanalysis-era5-pressure-levels',
	'reanalysis-era5-pressure-levels-monthly-means',
	'reanalysis-uerra-europe-single-levels',
	'reanalysis-uerra-europe-pressure-levels',
	'seasonal-monthly-single-levels',
	'seasonal-original-single-levels',
	'seasonal-monthly-pressure-levels',
	'seasonal-postprocessed-pressure-levels',
	'seasonal-original-pressure-levels',
	'seasonal-postprocessed-single-levels'
 ]

data = dict()
for dataset in datasets:
	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

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