#!/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
import json

"""
Write fields to a JSON obj and reset
"""
def write_and_reset(json_obj, varname, key, key_u, key_cf, defs, val_k, val_v):
	json_obj[key] = {
		'varname': varname,
		'unit': key_u,
		'cf': key_cf,
		'grib_codes': defs
	}			

	# Reset values
	varname = None
	key = None
	val_k = None
	val_v = None	
	defs = dict()
	return key, val_k, val_v, defs

"""
Convert from pseudo-format to a python dictionary and save to file
"""
def convert_to_json(param_file, units_file, cf_file):

	with open(param_file, 'r') as fp, open(units_file, 'r') as fu, open(cf_file, 'r') as fc:

		# Skip first line (autogenerated comment)
		line = fp.readline()
		line_u = fu.readline()
		line_c = fc.readline()
		
		# Initial conditions
		json_obj = dict()
		varname = None
		key = None
		val_k = None
		val_v = None
		defs = dict()

		while line and line_u:

			# Read next line
			line = fp.readline()
			line_u = fu.readline()
			line_c = fc.readline()

			# Starts with variable name
			if '#' in line:

				# Add if fields complete
				if not(varname is None or key is None or val_k is None or val_v is None):
					key, val_k, val_v, defs = write_and_reset(
						json_obj, varname, key, key_u, key_cf, defs, val_k, val_v)	

				# Next varname
				varname = line.replace('#', '').strip()
			elif '= {' in line:
				key = line.split('=')[0].replace('\'', '').strip()
				key_u = line_u.split('=')[0].replace('\'', '').strip()
				key_cf = line_c.split('=')[0].replace('\'', '').strip()		
			elif ';' in line:
				val_k = line.split('=')[0].replace('\t', '').strip()
				val_v = line.split('=')[1].replace(';', '').strip()
				defs[val_k] = val_v


		# Last one
		key, val_k, val_v, defs = write_and_reset(json_obj, varname, key, key_u, key_cf, defs, val_k, val_v)

		return json_obj
		



# Directory where definitions are + script
grib_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'eccodes_definitions')

# GRIB1/2 standard tables
grib1=convert_to_json(
	os.path.join(grib_dir, 'grib1_paramId.def'), 
	os.path.join(grib_dir, 'grib1_units.def'), 
	os.path.join(grib_dir, 'grib1_cfVarName.def'))
grib2=convert_to_json(
	os.path.join(grib_dir, 'grib2_paramId.def'), 
	os.path.join(grib_dir, 'grib2_units.def'), 
	os.path.join(grib_dir, 'grib2_cfVarName.def'))

# GRIB1/2 standard tables (ECMWF parameters)git p
grib1_ecmwf=convert_to_json(
	os.path.join(grib_dir, 'grib1_paramId_ecmwf.def'), 
	os.path.join(grib_dir, 'grib1_units_ecmwf.def'), 
	os.path.join(grib_dir, 'grib1_cfVarName_ecmwf.def'))

grib2_ecmwf=convert_to_json(
	os.path.join(grib_dir, 'grib2_paramId_ecmwf.def'), 
	os.path.join(grib_dir, 'grib2_units_ecmwf.def'), 
	os.path.join(grib_dir, 'grib2_cfVarName_ecmwf.def'))

# Merge dictionaries
g1 = grib1_ecmwf.copy()
g1.update(grib1)

g2 = grib2_ecmwf.copy()
g2.update(grib2)

# Save to JSON
with open('grib1.json', 'w') as fo:
	json.dump(g1, fo, indent=2)

with open('grib2.json', 'w') as fo:
	json.dump(g2, fo, indent=2)