#!/usr/bin/env python
#
# grib_checker.py checks Checks whatever you want to check in your life
#
# AUTHOR: BSC-CNS I. CERNICHARO
#

import os
import sys
import datetime
import time
import logging

import grib_content

VERSION = "ALPHA 0.2"

def run(args):
    time0 = time.time()
    # Initiate the logging
    default_path = 'LOG_' + args.split("/")[-1].replace('.grib','.log')
    logging.basicConfig(
        filename=default_path,
        format='%(levelname)s:%(asctime)s:%(message)s', 
        level=logging.INFO
    )

    # Initiate the grib checks creating the grib_content object
    g_c = grib_content.grib_content(args)

    # Start adding messages to the log
    string = "\n\tDATE AND TIME: " + datetime.datetime.now().isoformat() 
    string += "\n\tGRIB_CCK VERSION: " + VERSION 
    string += "\n\tFILENAME: " + g_c.pname
    logging.info(string)
    g_c.print_num_keys()
    
    # Metada checks iteration
    g_c.check_meta_grid()
    
    # Data checks iteration
    # Not useful right now. Have to be redefined
    # g_c.check_data_coords()

    dtime = time.time() - time0

    str_time = str(int(dtime//3600)) + ' hours, '
    str_time += str(int((dtime%3600)//60)) + ' minutes, '
    str_time += str(round((dtime%3600)%60,3)) + ' seconds '

    string = '\n\tTIME ELAPSED: "%s\n' %str_time

    logging.info(string)


if __name__ == "__main__":
    args = sys.argv[1:]

    for item in args:
        run(item)
