Commit e81369cc authored by Klaus Zimmermann's avatar Klaus Zimmermann
Browse files

Add error detection and reporting to climix-editor (closes #130)

parent 268faa52
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging
import re import re
import pyexcel as pe import pyexcel as pe
...@@ -61,22 +62,6 @@ def tr_inputs(no_inputs, ...@@ -61,22 +62,6 @@ def tr_inputs(no_inputs,
return d return d
def split_parameter_definitions(parameter_definitions_string, parameter_names):
name_regex = r'(?P<name>{})'.format('|'.join(parameter_names))
op_regex = r'(?P<operator>{})'.format('|'.join(SUPPORTED_OPERATORS))
red_regex = r'(?P<reducer>{})'.format('|'.join(SUPPORTED_REDUCERS))
qty_regex = (r'\(var_name: (?P<var_name>[^,]*), '
r'standard_name: (?P<standard_name>[^,]*), '
r'value: (?P<value>[^,]*), '
r'unit: (?P<units>[^)]*)\)')
param_regex = r'{}: (?:{}|{}|{})'.format(
name_regex, red_regex, op_regex, qty_regex
)
matcher = re.compile(param_regex)
result = matcher.finditer(parameter_definitions_string)
return result
def tr_parameter(parameter): def tr_parameter(parameter):
if parameter['operator'] is not None: if parameter['operator'] is not None:
d = {'var_name': parameter['name'], d = {'var_name': parameter['name'],
...@@ -97,14 +82,37 @@ def tr_parameter(parameter): ...@@ -97,14 +82,37 @@ def tr_parameter(parameter):
return d return d
def tr_index_function(name, no_thresholds, def split_parameter_definitions(parameter_definitions_string, parameter_names):
name_regex = r'(?P<name>{})'.format('|'.join(parameter_names))
op_regex = r'(?P<operator>{})'.format('|'.join(SUPPORTED_OPERATORS))
red_regex = r'(?P<reducer>{})'.format('|'.join(SUPPORTED_REDUCERS))
qty_regex = (r'\(var_name: (?P<var_name>[^,]*), '
r'standard_name: (?P<standard_name>[^,]*), '
r'value: (?P<value>[^,]*), '
r'unit: (?P<units>[^)]*)\)')
param_regex = r'{}: (?:{}|{}|{})'.format(
name_regex, red_regex, op_regex, qty_regex
)
matcher = re.compile(param_regex)
result = [tr_parameter(p)
for p in matcher.finditer(parameter_definitions_string)]
return result
def tr_index_function(index_name, name, no_thresholds,
parameter_names_string, parameter_definitions_string): parameter_names_string, parameter_definitions_string):
parameter_names = split_parts(no_thresholds, parameter_names_string) parameter_names = split_parts(no_thresholds, parameter_names_string)
parameters = split_parameter_definitions(parameter_definitions_string, parameters = split_parameter_definitions(parameter_definitions_string,
parameter_names) parameter_names)
found_parameters = set(p['var_name'] for p in parameters)
if found_parameters != set(parameter_names):
logging.warn(f"For index {index_name}, the parameters listed in "
f"parameter_name ({parameter_names}) are different from "
f"those defined in PARAMETER_definition "
f"({found_parameters}). Please check the table!")
index_function = { index_function = {
'name': name, 'name': name,
'parameters': [tr_parameter(p) for p in parameters], 'parameters': parameters,
} }
return index_function return index_function
...@@ -131,6 +139,7 @@ def prepare_record(record): ...@@ -131,6 +139,7 @@ def prepare_record(record):
record['INPUT_standard_name'], record['INPUT_standard_name'],
record['INPUT_cell_methods']), record['INPUT_cell_methods']),
'index_function': tr_index_function( 'index_function': tr_index_function(
var_name,
record['index_function'], record['index_function'],
no_parameters, no_parameters,
record['parameter_name'], record['PARAMETER_definition']), record['parameter_name'], record['PARAMETER_definition']),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment