#!/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 configparser, glob, re
from dqc_wrapper.utils.utils_forms import get_metadata
from dqc_wrapper.utils.utils_grib import *

conf_dir = '/data/joan/c3s512-wp1-datachecker/dqc_wrapper/QAR_dqc/conf'
forms_dir = '/data/cds-forms-c3s'

# Regex for units
pattern = re.compile('[\W_]+')

with open('/tmp/units_info.csv', 'w') as fo:
    fo.write('dataset,variable,units_c3s,units_grib,same_units\n')

    for conf_file in sorted(glob.glob('{}/*.ini'.format(conf_dir))):

        try:
            # Read config
            c = configparser.ConfigParser()
            c.read(conf_file)
            cds_dataset = c['dataset']['cds_dataset']
            cds_variable = c['dataset']['cds_variable']
            cds_units = c['dataset']['cds_units']
            metadata = get_metadata(forms_dir, cds_dataset, cds_variable)

            # Pick a grib file and check units
            files = glob.glob(os.path.join(c['general']['input'], c['general']['fpattern']))
            if len(files):
                gc = GribFileCheck(files[0])
                grib_units = gc.get_grib_units(sample=True)
                eq = pattern.sub('', cds_units.lower()) == pattern.sub('', grib_units.lower())
                fo.write('{},{},{},{},{}\n'.format(cds_dataset, cds_variable, cds_units, grib_units, eq))
        except:
            pass
            #print('ERROR: '+conf_file)