#!/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)
#       Ivan Jose Cernicharo Ortiz (ivan.cernicharo@bsc.es)

import os, sys, logging, subprocess

# Local import
import utils.grib_checker.grib_cck as gcck
from utils.utils_files import *

"""
Class: Standard compliance checks
---
- Checks for file format correctness
"""
class standard_compliance_test:

    def __init__(self, logdir, files):
        self.testid = 'test01-standard_compliance'
        self.logger = logging.getLogger(self.testid)
        self.logdir = logdir   
        self.files = files     
        self.gridded_files = ''
        self.other_files = ''
        self.formats1 = ''
        self.formats2 = ''

    def run_cfchecker(self,item):
        """RUN CFCHECKER FOR ONE FILE
        """
        cmd0 = "cfchecks --version auto %s" %item
        p0 = subprocess.Popen(
            cmd0, shell=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT
            )
        out0, _ = p0.communicate()
        return out0

    """
    Check for CF compliance (netCDF)
    """
    def netcdf_test(self, ncfile):

        # Defaults
        res = 'ok'
        msg = 'File is CF compliant'
                        
        # Run external CF-checker
        out0 = self.run_cfchecker(ncfile)
        string = str(out0).replace('\'',"").replace("\"","").split('\\n')

        # Detect error messages
        if any('ERROR:' in e for e in string):
            err = [item.split('ERROR:')[-1].strip() for item in string if item.startswith('ERROR:')]
            errstring = ncfile.split('/')[-1] + ' has ' + str(len(err))
            errstring += ' error messages' + '\n'
            errstring += '\n'.join(err)
            string += errstring
            self.logger.error(string)  

            # Outputs          
            res = 'err'
            msg = errstring

        return res, msg

    """
    TODO: Implementation pending (GRIB definitions)
    """
    def grib_test(self, gribfile):
 
        # Run grib checker            
        # res = gcck.grib_cck_run(gribfile)
        return 'err', 'Function not implemented yet'

    def main(self):

        # Progress init
        i = 0
        n = len(self.files)
        print()

        # Do check
        msg = ''
        res = ''

        for f in self.files:
            # Progress update            
            i+=1
            print_progress(i, n) 

            # Grib checker or CF checker
            if f.endswith('.nc'):
                res, msg = self.netcdf_test(f)
            elif f.endswith('.grib'):
                res, msg = self.grib_test(f)                
            else:
                res = 'err'
                msg = 'File format is not supported, only .grib and .nc are ({})'.format(f)

            if res == 'err':
                self.logger.error(msg + ' ' + os.path.basename(f))

        # Final message
        if res == 'ok':
            msg = 'All checks passed (CF compliance / GRIB checker)'            
        else:
            msg = 'Files are not standard compliant. Check log for details'            

                # Prepare output
        return { 
            'res': res, 
            'msg': msg
        }
            

if __name__ == "__main__":
    sys.exit()
