<s> import datetime <EOL> import yaml <EOL> import os <EOL> import sys <EOL> import xlwt <EOL> import numpy as np <EOL> import numpy . ma as ma <EOL> import ocw . data_source . local as local <EOL> import ocw . dataset as ds <EOL> import ocw . dataset_processor as dsp <EOL> import ocw . statistical_downscaling as down <EOL> import ocw . plotter as plotter <EOL> import ssl <EOL> def spatial_aggregation ( target_dataset , lon_min , lon_max , lat_min , lat_max ) : <EOL> """<STR_LIT>""" <EOL> if target_dataset . lons . ndim == <NUM_LIT:1> and target_dataset . lats . ndim == <NUM_LIT:1> : <EOL> new_lon , new_lat = np . meshgrid ( target_dataset . lons , target_dataset . lats ) <EOL> elif target_dataset . lons . ndim == <NUM_LIT:2> and target_dataset . lats . ndim == <NUM_LIT:2> : <EOL> new_lon = target_datasets . lons <EOL> new_lat = target_datasets . lats <EOL> y_index , x_index = np . where ( ( new_lon >= lon_min ) & ( new_lon <= lon_max ) & ( new_lat >= lat_min ) & ( new_lat <= lat_max ) ) [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> return target_dataset . values [ : , y_index . min ( ) : y_index . max ( ) + <NUM_LIT:1> , x_index . min ( ) : x_index . max ( ) + <NUM_LIT:1> ] <EOL> def extract_data_at_nearest_grid_point ( target_dataset , longitude , latitude ) : <EOL> """<STR_LIT>""" <EOL> if target_dataset . lons . ndim == <NUM_LIT:1> and target_dataset . lats . ndim == <NUM_LIT:1> : <EOL> new_lon , new_lat = np . meshgrid ( target_dataset . lons , target_dataset . lats ) <EOL> elif target_dataset . lons . ndim == <NUM_LIT:2> and target_dataset . lats . ndim == <NUM_LIT:2> : <EOL> new_lon = target_datasets . lons <EOL> new_lat = target_datasets . lats <EOL> distance = ( new_lon - longitude ) ** <NUM_LIT> + ( new_lat - latitude ) ** <NUM_LIT> <EOL> y_index , x_index = np . where ( distance == np . min ( distance ) ) [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> return target_dataset . values [ : , y_index [ <NUM_LIT:0> ] , x_index [ <NUM_LIT:0> ] ] <EOL> if hasattr ( ssl , '<STR_LIT>' ) : <EOL> ssl . _create_default_https_context = ssl . _create_unverified_context <EOL> config_file = str ( sys . argv [ <NUM_LIT:1> ] ) <EOL> print '<STR_LIT>' , config_file <EOL> config = yaml . load ( open ( config_file ) ) <EOL> case_name = config [ '<STR_LIT>' ] <EOL> downscale_option_names = [ '<STR_LIT:U+0020>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> DOWNSCALE_OPTION = config [ '<STR_LIT>' ] <EOL> location = config [ '<STR_LIT:location>' ] <EOL> grid_lat = location [ '<STR_LIT>' ] <EOL> grid_lon = location [ '<STR_LIT>' ] <EOL> month_index = config [ '<STR_LIT>' ] <EOL> month_start = month_index [ <NUM_LIT:0> ] <EOL> month_end = month_index [ - <NUM_LIT:1> ] <EOL> ref_info = config [ '<STR_LIT>' ] <EOL> model_info = config [ '<STR_LIT>' ] <EOL> OUTPUT = "<STR_LIT>" % ( location [ '<STR_LIT:name>' ] , ref_info [ '<STR_LIT>' ] , model_info [ '<STR_LIT>' ] , ref_info [ '<STR_LIT>' ] , model_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> print ( "<STR_LIT>" + ref_info [ '<STR_LIT>' ] + "<STR_LIT>" ) <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" % ( ref_info [ '<STR_LIT:path>' ] , ) ) <EOL> ref_dataset = local . load_file ( ref_info [ '<STR_LIT:path>' ] , ref_info [ '<STR_LIT>' ] ) <EOL> print ( ref_info [ '<STR_LIT>' ] + "<STR_LIT>" % ( ref_dataset . values . shape , ) ) <EOL> print ( "<STR_LIT>" % ( model_info [ '<STR_LIT>' ] [ '<STR_LIT:path>' ] , ) ) <EOL> model_dataset_present = local . load_file ( model_info [ '<STR_LIT>' ] [ '<STR_LIT:path>' ] , model_info [ '<STR_LIT>' ] ) <EOL> print ( model_info [ '<STR_LIT>' ] + "<STR_LIT>" % ( model_dataset_present . values . shape , ) ) <EOL> dy = model_dataset_present . spatial_resolution ( ) [ <NUM_LIT:0> ] <EOL> dx = model_dataset_present . spatial_resolution ( ) [ <NUM_LIT:1> ] <EOL> model_dataset_future = local . load_file ( model_info [ '<STR_LIT>' ] [ '<STR_LIT:path>' ] , model_info [ '<STR_LIT>' ] ) <EOL> print ( model_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] + '<STR_LIT::>' + model_info [ '<STR_LIT>' ] + "<STR_LIT>" % ( model_dataset_future . values . shape , ) ) <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" ) <EOL> ref_temporal_subset = dsp . temporal_subset ( month_start , month_end , ref_dataset ) <EOL> model_temporal_subset_present = dsp . temporal_subset ( month_start , month_end , model_dataset_present ) <EOL> model_temporal_subset_future = dsp . temporal_subset ( month_start , month_end , model_dataset_future ) <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" % ( grid_lat , grid_lon ) ) <EOL> ref_subset = extract_data_at_nearest_grid_point ( ref_temporal_subset , grid_lon , grid_lat ) <EOL> model_subset_present = extract_data_at_nearest_grid_point ( model_temporal_subset_present , grid_lon , grid_lat ) <EOL> model_subset_future = extract_data_at_nearest_grid_point ( model_temporal_subset_future , grid_lon , grid_lat ) <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" ) <EOL> downscale = down . Downscaling ( ref_subset , model_subset_present , model_subset_future ) <EOL> print ( downscale_option_names [ DOWNSCALE_OPTION ] + "<STR_LIT>" ) <EOL> if DOWNSCALE_OPTION == <NUM_LIT:1> : <EOL> downscaled_model_present , downscaled_model_future = downscale . Delta_addition ( ) <EOL> elif DOWNSCALE_OPTION == <NUM_LIT:2> : <EOL> downscaled_model_present , downscaled_model_future = downscale . Delta_correction ( ) <EOL> elif DOWNSCALE_OPTION == <NUM_LIT:3> : <EOL> downscaled_model_present , downscaled_model_future = downscale . Quantile_mapping ( ) <EOL> elif DOWNSCALE_OPTION == <NUM_LIT:4> : <EOL> downscaled_model_present , downscaled_model_future = downscale . Asynchronous_regression ( ) <EOL> else : <EOL> sys . exit ( "<STR_LIT>" ) <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" ) <EOL> if not os . path . exists ( case_name ) : <EOL> os . system ( "<STR_LIT>" + case_name ) <EOL> os . chdir ( os . getcwd ( ) + "<STR_LIT:/>" + case_name ) <EOL> plotter . draw_marker_on_map ( grid_lat , grid_lon , fname = '<STR_LIT>' , location_name = config [ '<STR_LIT:location>' ] [ '<STR_LIT:name>' ] ) <EOL> plotter . draw_histogram ( [ ref_subset . ravel ( ) , model_subset_present . ravel ( ) , model_subset_future . ravel ( ) ] , <EOL> data_names = [ ref_info [ '<STR_LIT>' ] , model_info [ '<STR_LIT>' ] , model_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] , <EOL> fname = OUTPUT + '<STR_LIT>' ) <EOL> plotter . draw_histogram ( [ ref_subset . ravel ( ) , downscaled_model_present , downscaled_model_future ] , <EOL> data_names = [ ref_info [ '<STR_LIT>' ] , model_info [ '<STR_LIT>' ] , model_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] , <EOL> fname = OUTPUT + '<STR_LIT>' + downscale_option_names [ DOWNSCALE_OPTION ] ) <EOL> print ( "<STR_LIT>" ) <EOL> workbook = xlwt . Workbook ( ) <EOL> sheet = workbook . add_sheet ( downscale_option_names [ config [ '<STR_LIT>' ] ] ) <EOL> sheet . write ( <NUM_LIT:0> , <NUM_LIT:0> , config [ '<STR_LIT:location>' ] [ '<STR_LIT:name>' ] ) <EOL> sheet . write ( <NUM_LIT:0> , <NUM_LIT:2> , '<STR_LIT>' ) <EOL> sheet . write ( <NUM_LIT:0> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> sheet . write ( <NUM_LIT:0> , <NUM_LIT:6> , '<STR_LIT>' ) <EOL> sheet . write ( <NUM_LIT:0> , <NUM_LIT:3> , grid_lon ) <EOL> sheet . write ( <NUM_LIT:0> , <NUM_LIT:5> , grid_lat ) <EOL> for imonth , month in enumerate ( month_index ) : <EOL> sheet . write ( <NUM_LIT:0> , <NUM_LIT:7> + imonth , month ) <EOL> sheet . write ( <NUM_LIT:3> , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> sheet . write ( <NUM_LIT:4> , <NUM_LIT:1> , ref_info [ '<STR_LIT>' ] ) <EOL> for idata , data in enumerate ( ref_subset . ravel ( ) [ ~ ref_subset . ravel ( ) . mask ] ) : <EOL> sheet . write ( <NUM_LIT:5> + idata , <NUM_LIT:1> , data . item ( ) ) <EOL> sheet . write ( <NUM_LIT:3> , <NUM_LIT:2> , '<STR_LIT>' ) <EOL> sheet . write ( <NUM_LIT:4> , <NUM_LIT:2> , model_info [ '<STR_LIT>' ] ) <EOL> for idata , data in enumerate ( model_subset_present . ravel ( ) ) : <EOL> sheet . write ( <NUM_LIT:5> + idata , <NUM_LIT:2> , data . item ( ) ) <EOL> sheet . write ( <NUM_LIT:3> , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> sheet . write ( <NUM_LIT:4> , <NUM_LIT:3> , model_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> for idata , data in enumerate ( model_subset_future . ravel ( ) ) : <EOL> sheet . write ( <NUM_LIT:5> + idata , <NUM_LIT:3> , data . item ( ) ) <EOL> sheet . write ( <NUM_LIT:3> , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> sheet . write ( <NUM_LIT:4> , <NUM_LIT:4> , model_info [ '<STR_LIT>' ] ) <EOL> for idata , data in enumerate ( downscaled_model_present ) : <EOL> sheet . write ( <NUM_LIT:5> + idata , <NUM_LIT:4> , data . item ( ) ) <EOL> sheet . write ( <NUM_LIT:3> , <NUM_LIT:5> , '<STR_LIT>' ) <EOL> sheet . write ( <NUM_LIT:4> , <NUM_LIT:5> , model_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> for idata , data in enumerate ( downscaled_model_future ) : <EOL> sheet . write ( <NUM_LIT:5> + idata , <NUM_LIT:5> , data . item ( ) ) <EOL> workbook . save ( OUTPUT + '<STR_LIT>' ) </s>
