#!/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 = []
[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()
dimesionless = []
fraction = []
percent = []
units01 = []
units11 = []

for dataset in datasets:

    try:
        key_d = os.path.basename(dataset)
        vars, p = get_dataset_information(dataset, forms_dir, grib1_def, grib2_def)
        for v in vars:
            m = get_metadata(forms_dir, dataset, v)
            if m['unit'].strip() == '%':
                percent.append(v)

            if m['unit'].strip().lower() == 'dimensionless':
                dimesionless.append(v)

            if m['unit'].strip().lower() == '(0 - 1)':
                units01.append(v)

            if m['unit'].strip().lower() == '(-1 to 1)':
                units11.append(v)

            if 'fraction' in m['unit'].strip().lower():
                fraction.append(v)

    except Exception as e:
        print(e)

# Unique percent vars
print('\n# Percent variables')
for d in list(set(percent)):
    print(d+',0,100')

# Unique dimensionless vars
print('\n# Dimensionless variables')
for d in list(set(dimesionless)):
    print(d)

# Unique fraction vars
print('\n# Fraction variables')
for d in list(set(fraction)):
    print(d)

# 0-1 vars
print('\n# Units (0-1) variables')
for d in list(set(units01)):
    print(d+',0,1')

# +-1 vars
print('\n# Units (+-1) variables')
for d in list(set(units11)):
    print(d+',-1,1')