#!/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)
#

from dqc_wrapper.utils.utils_files import *

'''
Read a configparser object
'''
def read_conf_file(res_file):
	config = configparser.ConfigParser()
	config.read(res_file)
	return config

'''
Script in order to convert res and log files to separate PDFs to be imported at the CDS / CMS
'''
# Folder with results
res_folder = '/shared/QAR_dqc/results'
log_folder = '/shared/QAR_dqc/logs'
test = 'valid_ranges_result'

ok=0
warn=0
err=0
msgs = []

for res_file in sorted(glob.glob(os.path.join(res_folder, '*.ini'))):
	try:
		# RES
		cf = read_conf_file(res_file)
		if cf[test]['res'] == 'ok':
			ok+=1
		elif cf[test]['res'] == 'warn':
			warn+=1
		elif cf[test]['res'] == 'err':
			err+=1
			print(os.path.basename(res_file))
		else:
			print('ERROR: Unrecognized test')

		# MSG
		msgs += cf[test]['msg'].split('\n')
	except:
		pass


for m in list(set(msgs)):
	if 'compliant with the CF standard' in m:
		continue
	if 'error messages' in m:
		continue
	if '----' in m:
		continue
	print(m)


tot = float(warn+err+ok)
print('{} -> ok: {} ({}%) warn: {} ({}%) err: {} ({}%)'.format(test, ok, ok/tot*100.0, warn, warn/tot*100.0, err, err/tot*100.0))