<s> import datetime as dt <EOL> import logging <EOL> import yaml <EOL> logging . basicConfig ( ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> def export_evaluation_to_config ( evaluation , file_path = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> config = { } <EOL> config [ '<STR_LIT>' ] = generate_evaluation_information ( evaluation ) <EOL> config [ '<STR_LIT>' ] = generate_dataset_information ( evaluation ) <EOL> config [ '<STR_LIT>' ] = generate_metric_information ( evaluation ) <EOL> config [ '<STR_LIT>' ] = generate_subregion_information ( evaluation ) <EOL> yaml . dump ( config , file ( file_path , '<STR_LIT:w>' ) ) <EOL> def generate_dataset_information ( evaluation ) : <EOL> '''<STR_LIT>''' <EOL> datasets = { } <EOL> if evaluation . ref_dataset : <EOL> datasets [ '<STR_LIT>' ] = generate_dataset_config ( evaluation . ref_dataset ) <EOL> if len ( evaluation . target_datasets ) > <NUM_LIT:0> : <EOL> datasets [ '<STR_LIT>' ] = [ <EOL> generate_dataset_config ( target ) <EOL> for target in evaluation . target_datasets <EOL> ] <EOL> return datasets <EOL> def generate_dataset_config ( dataset ) : <EOL> '''<STR_LIT>''' <EOL> dataset_source = dataset . origin [ '<STR_LIT:source>' ] <EOL> if dataset_source == '<STR_LIT>' : <EOL> info = _extract_local_dataset_info ( dataset ) <EOL> elif dataset_source == '<STR_LIT>' : <EOL> info = _extract_rcmed_dataset_info ( dataset ) <EOL> elif dataset_source == '<STR_LIT>' : <EOL> info = _extract_esgf_dataset_info ( dataset ) <EOL> elif dataset_source == '<STR_LIT>' : <EOL> info = _extract_dap_dataset_info ( dataset ) <EOL> else : <EOL> err = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) . format ( dataset_source ) <EOL> logger . error ( err ) <EOL> raise AttributeError ( err ) <EOL> info [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] = dataset . name <EOL> info [ '<STR_LIT>' ] [ '<STR_LIT>' ] = dataset . units <EOL> return info <EOL> def generate_metric_information ( evaluation ) : <EOL> '''<STR_LIT>''' <EOL> unary_metrics = [ x . __class__ . __name__ for x in evaluation . unary_metrics ] <EOL> binary_metrics = [ x . __class__ . __name__ for x in evaluation . metrics ] <EOL> return unary_metrics + binary_metrics <EOL> def generate_evaluation_information ( evaluation ) : <EOL> '''<STR_LIT>''' <EOL> eval_config = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : [ - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> } <EOL> datasets = [ ] <EOL> if evaluation . ref_dataset : <EOL> datasets . append ( evaluation . ref_dataset ) <EOL> if evaluation . target_datasets : <EOL> datasets += evaluation . target_datasets <EOL> if len ( datasets ) > <NUM_LIT:0> : <EOL> eval_config [ '<STR_LIT>' ] = _calc_temporal_bin_size ( datasets ) <EOL> lats , lons = _calc_spatial_lat_lon_grid ( datasets ) <EOL> eval_config [ '<STR_LIT>' ] = lats <EOL> eval_config [ '<STR_LIT>' ] = lons <EOL> eval_config [ '<STR_LIT>' ] = _calc_subset_config ( datasets ) <EOL> return eval_config <EOL> def generate_subregion_information ( evaluation ) : <EOL> '''<STR_LIT>''' <EOL> subregions = [ ] <EOL> for s in evaluation . subregions : <EOL> subregions . append ( [ s . lat_min , s . lat_max , s . lon_min , s . lon_max ] ) <EOL> return subregions <EOL> def _extract_local_dataset_info ( dataset ) : <EOL> '''<STR_LIT>''' <EOL> dataset_info = { '<STR_LIT>' : { } } <EOL> dataset_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> dataset_info [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> dataset_info [ '<STR_LIT:path>' ] = dataset . origin [ '<STR_LIT:path>' ] <EOL> dataset_info [ '<STR_LIT>' ] = dataset . variable <EOL> dataset_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] = dataset . origin [ '<STR_LIT>' ] <EOL> dataset_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] = dataset . origin [ '<STR_LIT>' ] <EOL> dataset_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] = dataset . origin [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in dataset . origin : <EOL> elev = dataset . origin [ '<STR_LIT>' ] <EOL> dataset_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] = elev <EOL> return dataset_info <EOL> def _extract_rcmed_dataset_info ( dataset ) : <EOL> '''<STR_LIT>''' <EOL> dataset_info = { '<STR_LIT>' : { } } <EOL> min_lat , max_lat , min_lon , max_lon = dataset . spatial_boundaries ( ) <EOL> start_time , end_time = dataset . time_range ( ) <EOL> dataset_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> dataset_info [ '<STR_LIT>' ] = dataset . origin [ '<STR_LIT>' ] <EOL> dataset_info [ '<STR_LIT>' ] = dataset . origin [ '<STR_LIT>' ] <EOL> dataset_info [ '<STR_LIT>' ] = min_lat <EOL> dataset_info [ '<STR_LIT>' ] = max_lat <EOL> dataset_info [ '<STR_LIT>' ] = min_lon <EOL> dataset_info [ '<STR_LIT>' ] = max_lon <EOL> dataset_info [ '<STR_LIT>' ] = str ( start_time ) <EOL> dataset_info [ '<STR_LIT>' ] = str ( end_time ) <EOL> return dataset_info <EOL> def _extract_esgf_dataset_info ( dataset ) : <EOL> '''<STR_LIT>''' <EOL> dataset_info = { '<STR_LIT>' : { } } <EOL> dataset_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> dataset_info [ '<STR_LIT>' ] = dataset . origin [ '<STR_LIT>' ] <EOL> dataset_info [ '<STR_LIT>' ] = dataset . origin [ '<STR_LIT>' ] <EOL> dataset_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> dataset_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return dataset_info <EOL> def _extract_dap_dataset_info ( dataset ) : <EOL> '''<STR_LIT>''' <EOL> dataset_info = { '<STR_LIT>' : { } } <EOL> dataset_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> dataset_info [ '<STR_LIT:url>' ] = dataset . origin [ '<STR_LIT:url>' ] <EOL> dataset_info [ '<STR_LIT>' ] = dataset . variable <EOL> return dataset_info <EOL> def _calc_temporal_bin_size ( datasets ) : <EOL> '''<STR_LIT>''' <EOL> times = datasets [ <NUM_LIT:0> ] . times <EOL> time_delta = times [ <NUM_LIT:1> ] - times [ <NUM_LIT:0> ] <EOL> if time_delta . days == <NUM_LIT:0> : <EOL> return <NUM_LIT:1> <EOL> elif time_delta . days <= <NUM_LIT> : <EOL> return <NUM_LIT> <EOL> elif time_delta . days <= <NUM_LIT> : <EOL> return <NUM_LIT> <EOL> else : <EOL> return <NUM_LIT> <EOL> def _calc_spatial_lat_lon_grid ( datasets ) : <EOL> '''<STR_LIT>''' <EOL> lat_min , lat_max , lon_min , lon_max = datasets [ <NUM_LIT:0> ] . spatial_boundaries ( ) <EOL> lats = datasets [ <NUM_LIT:0> ] . lats <EOL> lons = datasets [ <NUM_LIT:0> ] . lons <EOL> lat_step = float ( abs ( lats [ <NUM_LIT:1> ] - lats [ <NUM_LIT:0> ] ) ) <EOL> lon_step = float ( abs ( lons [ <NUM_LIT:1> ] - lons [ <NUM_LIT:0> ] ) ) <EOL> if lat_max != <NUM_LIT> : lat_max += lat_step <EOL> if lon_max != <NUM_LIT> : lon_max += lon_step <EOL> return ( ( lat_min , lat_max , lat_step ) , ( lon_min , lon_max , lon_step ) ) <EOL> def _calc_subset_config ( datasets ) : <EOL> '''<STR_LIT>''' <EOL> lat_min = <NUM_LIT> <EOL> lat_max = - <NUM_LIT> <EOL> lon_min = <NUM_LIT> <EOL> lon_max = - <NUM_LIT> <EOL> start = dt . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> end = dt . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> for ds in datasets : <EOL> ds_lat_min , ds_lat_max , ds_lon_min , ds_lon_max = ds . spatial_boundaries ( ) <EOL> ds_start , ds_end = ds . time_range ( ) <EOL> if ds_lat_min < lat_min : <EOL> lat_min = ds_lat_min <EOL> if ds_lat_max > lat_max : <EOL> lat_max = ds_lat_max <EOL> if ds_lon_min < lon_min : <EOL> lon_min = ds_lon_min <EOL> if ds_lon_max > lon_max : <EOL> lon_max = ds_lon_max <EOL> if ds_start < start : <EOL> start = ds_start <EOL> if ds_end > end : <EOL> end = ds_end <EOL> return [ lat_min , lat_max , lon_min , lon_max , str ( start ) , str ( end ) ] </s>
