#!/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 pandas as pd
import os
import glob
from configparser import ConfigParser

def file_len(fname):
    if os.path.exists(fname):
        with open(fname) as f:
            for i, l in enumerate(f):
                pass
        return i + 1
    else:
        return 1

conf_dir='/data/joan/c3s512-wp1-datachecker/dqc_wrapper/QAR_dqc/conf'

for cf in glob.glob(conf_dir+'/*.ini'):
    conf = ConfigParser()
    conf.read(cf)

    # Number of downloaded files
    nfiles = len(glob.glob(os.path.join(conf['general']['input'], conf['general']['fpattern'])))

    # Number of files in histogram
    nlines = file_len(os.path.join(conf['general']['res_dir'], os.path.basename(cf).replace('.ini','')+'-stats.csv'))

    # If we have the same histograms as number of files = OK, otherwise we need to re-do plots
    if nfiles > (nlines -1):
        print('{},{},{}'.format(nfiles, nlines-1, os.path.basename(cf)))
