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

Improve input variables (closes #152)

parent 7fd7cdb8
......@@ -17,10 +17,7 @@ indices:
- time: minimum within days
- time: minimum over days
input:
var_name: tasmin
standard_name: air_temperature
cell_methods:
- time: minimum
data: tasmin
index_function:
name: first_occurrence
parameters:
......@@ -57,10 +54,7 @@ indices:
- time: minimum within days
- time: maximum over days
input:
var_name: tasmin
standard_name: air_temperature
cell_methods:
- time: minimum
data: tasmin
index_function:
name: last_occurrence
parameters:
......
This diff is collapsed.
variables:
pr:
standard_name: precipitation_flux
cell_methods:
- time: mean
tas:
standard_name: air_temperature
cell_methods:
- time: mean
tasmax:
standard_name: air_temperature
cell_methods:
- time: maximum
tasmin:
standard_name: air_temperature
cell_methods:
- time: minimum
......@@ -74,6 +74,12 @@ class InputVariable:
self.cell_methods)
def build_variable(name, variable, path):
cell_methods = [CellMethod(*cm.popitem())
for cm in variable.pop('cell_methods')]
return InputVariable(name, variable['standard_name'], cell_methods)
class ParameterKind(Enum):
QUANTITY = 'quantity'
OPERATOR = 'operator'
......@@ -178,7 +184,7 @@ class IndexDefinition:
reference: str
period: Mapping[str, Union[str, Mapping[str, Optional[str]]]]
output: OutputVariable
input: InputVariable
input: Mapping[str, InputVariable]
index_function: IndexFunction
source: str
......@@ -187,7 +193,8 @@ class IndexDefinition:
self.reference,
self.period,
self.output.instantiate(parameters),
self.input.instantiate(parameters),
{key: iv.instantiate(parameters)
for key, iv in self.input.items()},
self.index_function.instantiate(parameters),
self.source)
return idx
......@@ -197,7 +204,7 @@ def build_parameter(name, metadata):
return PARAMETER_KINDS[metadata['kind']](name, **metadata)
def build_index(metadata, source=None):
def build_index(metadata, variables, source=None):
output = OutputVariable(
metadata['output']['var_name'],
metadata['output']['standard_name'],
......@@ -206,11 +213,11 @@ def build_index(metadata, source=None):
metadata['output']['units'],
[CellMethod(*cm.popitem())
for cm in metadata['output']['cell_methods']])
input = InputVariable(
metadata['input']['var_name'],
metadata['input']['standard_name'],
[CellMethod(*cm.popitem())
for cm in metadata['input']['cell_methods']])
if isinstance(metadata['input'], str):
input_metadata = {'data': metadata['input']}
else:
input_metadata = metadata['input']
input = {key: variables[name] for key, name in input_metadata.items()}
params = metadata['index_function']['parameters']
if params is None:
parameters = {}
......@@ -350,11 +357,24 @@ def find_metadata_files():
def load_metadata():
variables = {}
indices = {}
for metadata_path in find_metadata_files():
with open(metadata_path) as md_file:
variable_metadata = []
index_metadata = []
for path in find_metadata_files():
with open(path) as md_file:
metadata = yaml.safe_load(md_file)
index_metadata = metadata.get('indices', {})
for name, idx_meta in index_metadata.items():
indices[name] = build_index(idx_meta, metadata_path)
index_metadata.append((metadata.get('indices', {}), path))
variable_metadata.append((metadata.get('variables', {}), path))
for var_metadata, path in variable_metadata:
for name, var in var_metadata.items():
variables[name] = build_variable(name, var, path)
for idx_metadata, path in index_metadata:
for name, idx_meta in idx_metadata.items():
try:
indices[name] = build_index(idx_meta, variables, path)
except KeyError:
logging.error(
'Metadata error for index {} from {}.'.format(name, path))
raise
return IndexCatalog(indices)
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