#!/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
from dqc_wrapper.utils.utils_db import *

'''
Insert into the table filedatacheck all the known check results from the GRIB files (initialization dummy db)
'''
def update_datachecks(dataset):

    datachecks_warn = ['file_format']

    # 2m_temperature example
    if 'reanalysis' in dataset:
        if 'monthly' in dataset:
            datachecks_ok = [ 'standard_compliance_netcdf', 'temporal_completeness', 'spatial_completeness', 'spatial_consistency', 'valid_ranges']
            datachecks_err = ['standard_compliance_grib', 'temporal_consistency']
        else:
            datachecks_ok = [ 'standard_compliance_netcdf', 'spatial_completeness', 'temporal_consistency',  'spatial_consistency', 'valid_ranges']
            datachecks_err = [ 'standard_compliance_grib', 'temporal_completeness']
    else:
        datachecks_ok = ['temporal_consistency', 'temporal_completeness', 'spatial_completeness', 'spatial_consistency', 'valid_ranges']
        datachecks_err = ['standard_compliance_grib', 'standard_compliance_netcdf']

    for c in datachecks_ok:
        update_filedatacheck(conn, dataset, None, c, 1, '')
    for c in datachecks_err:
        update_filedatacheck(conn, dataset, None, c, -1, '')
    for c in datachecks_warn:
        update_filedatacheck(conn, dataset, None, c, 2, '')

# Create connection to SQLITE file
current_dir = os.path.dirname(os.path.realpath(__file__))
conn = create_connection('/data/joan/c3s512-wp1-datachecker/dqc_workflow/database/dqc_workflow.db')

# Get all grib files
dataset = 'reanalysis-era5-single-levels-monthly-means-2m_temperature'
update_datachecks(dataset)
print('Updated: {}'.format(dataset))

dataset = 'seasonal-monthly-single-levels-2m_temperature-ecmwf-5'
update_datachecks(dataset)
print('Updated: {}'.format(dataset))

dataset = 'reanalysis-era5-single-levels-2m_temperature'
update_datachecks(dataset)
print('Updated: {}'.format(dataset))

dataset = 'seasonal-original-single-levels-2m_temperature-meteo_france-7'
update_datachecks(dataset)
print('Updated: {}'.format(dataset))

