esmvaltool/diag_scripts/climate_metrics/ecs.py
def plot_ecs_regression(cfg, dataset_name, tas_cube, rtmt_cube,
                        regression_stats):
    """Plot linear regression used to calculate ECS."""
    if not (cfg['write_plots'] and cfg.get('plot_ecs_regression')):
        return
    ecs = -regression_stats.intercept / (2 * regression_stats.slope)
    filepath = os.path.join(cfg['plot_dir'],
                            dataset_name + '.' + cfg['output_file_type'])

    # Regression line
    x_reg = np.linspace(-1.0, 8.0, 2)
    y_reg = regression_stats.slope * x_reg + regression_stats.intercept

    # Plot data
    text = 'r = {:.2f}, '.format(regression_stats.rvalue) + \
           r'$\alpha$ = {:.2f}, '.format(-regression_stats.slope) + \
           'F = {:.2f}, '.format(regression_stats.intercept) + \
           'ECS = {:.2f}'.format(ecs)
    plot.scatterplot(
        [tas_cube.data, x_reg],
        [rtmt_cube.data, y_reg],
        filepath,
        plot_kwargs=[{
            'linestyle': 'none',
            'markeredgecolor': 'b',
            'markerfacecolor': 'none',
            'marker': 's',
        }, {
            'color': 'k',
            'linestyle': '-',
        }],
        save_kwargs={
            'bbox_inches': 'tight',
            'orientation': 'landscape',
        },
        axes_functions={
            'set_title': dataset_name,
            'set_xlabel': 'tas / ' + tas_cube.units.name,
            'set_ylabel': 'rtmt / ' + rtmt_cube.units.name,
            'set_xlim': [0.0, 7.0],
            'set_ylim': [-2.0, 10.0],
            'text': {
                'args': [0.05, 0.9, text],
                'kwargs': {
                    'transform': 'transAxes'
                },
            },
        },
    )

    # Write netcdf file for every plot
    if not cfg['write_netcdf']:
        return
    tas_coord = iris.coords.AuxCoord(
        tas_cube.data,
        **extract_variables(cfg, as_iris=True)['tas'])
    attr = {
        'model': dataset_name,
        'regression_r_value': regression_stats.rvalue,
        'regression_slope': regression_stats.slope,
        'regression_interception': regression_stats.intercept,
        'climate_sensitivity': -regression_stats.slope,
        'ECS': ecs,
    }
    cube = iris.cube.Cube(
        rtmt_cube.data,
        attributes=attr,
        aux_coords_and_dims=[(tas_coord, 0)],
        **extract_variables(cfg, as_iris=True)['rtmt'])
    filepath = os.path.join(cfg['work_dir'],
                            'ecs_regression_' + dataset_name + '.nc')
    save_iris_cube(cube, filepath, cfg)
    return
Plot linear regression used to calculate ECS.
Plot linear regression used to calculate ECS.
Plot linear regression used to calculate ECS .
def plot_ecs_regression(cfg, dataset_name, tas_cube, rtmt_cube,
                        regression_stats):
    if not (cfg['write_plots'] and cfg.get('plot_ecs_regression')):
        return
    ecs = -regression_stats.intercept / (2 * regression_stats.slope)
    filepath = os.path.join(cfg['plot_dir'],
                            dataset_name + '.' + cfg['output_file_type'])
    x_reg = np.linspace(-1.0, 8.0, 2)
    y_reg = regression_stats.slope * x_reg + regression_stats.intercept
    text = 'r = {:.2f}, '.format(regression_stats.rvalue) + \
           r'$\alpha$ = {:.2f}, '.format(-regression_stats.slope) + \
           'F = {:.2f}, '.format(regression_stats.intercept) + \
           'ECS = {:.2f}'.format(ecs)
    plot.scatterplot(
        [tas_cube.data, x_reg],
        [rtmt_cube.data, y_reg],
        filepath,
        plot_kwargs=[{
            'linestyle': 'none',
            'markeredgecolor': 'b',
            'markerfacecolor': 'none',
            'marker': 's',
        }, {
            'color': 'k',
            'linestyle': '-',
        }],
        save_kwargs={
            'bbox_inches': 'tight',
            'orientation': 'landscape',
        },
        axes_functions={
            'set_title': dataset_name,
            'set_xlabel': 'tas / ' + tas_cube.units.name,
            'set_ylabel': 'rtmt / ' + rtmt_cube.units.name,
            'set_xlim': [0.0, 7.0],
            'set_ylim': [-2.0, 10.0],
            'text': {
                'args': [0.05, 0.9, text],
                'kwargs': {
                    'transform': 'transAxes'
                },
            },
        },
    )
    if not cfg['write_netcdf']:
        return
    tas_coord = iris.coords.AuxCoord(
        tas_cube.data,
        **extract_variables(cfg, as_iris=True)['tas'])
    attr = {
        'model': dataset_name,
        'regression_r_value': regression_stats.rvalue,
        'regression_slope': regression_stats.slope,
        'regression_interception': regression_stats.intercept,
        'climate_sensitivity': -regression_stats.slope,
        'ECS': ecs,
    }
    cube = iris.cube.Cube(
        rtmt_cube.data,
        attributes=attr,
        aux_coords_and_dims=[(tas_coord, 0)],
        **extract_variables(cfg, as_iris=True)['rtmt'])
    filepath = os.path.join(cfg['work_dir'],
                            'ecs_regression_' + dataset_name + '.nc')
    save_iris_cube(cube, filepath, cfg)
    return
def plot_ecs_regression ( cfg , dataset_name , tas_cube , rtmt_cube , regression_stats ) : if not ( cfg [ 'write_plots' ] and cfg . get ( 'plot_ecs_regression' ) ) : return ecs = - regression_stats . intercept / ( 2 * regression_stats . slope ) filepath = os . path . join ( cfg [ 'plot_dir' ] , dataset_name + '.' + cfg [ 'output_file_type' ] ) x_reg = np . linspace ( - 1.0 , 8.0 , 2 ) y_reg = regression_stats . slope * x_reg + regression_stats . intercept text = 'r = {:.2f}, ' . format ( regression_stats . rvalue ) + r'$\alpha$ = {:.2f}, ' . format ( - regression_stats . slope ) + 'F = {:.2f}, ' . format ( regression_stats . intercept ) + 'ECS = {:.2f}' . format ( ecs ) plot . scatterplot ( [ tas_cube . data , x_reg ] , [ rtmt_cube . data , y_reg ] , filepath , plot_kwargs = [ { 'linestyle' : 'none' , 'markeredgecolor' : 'b' , 'markerfacecolor' : 'none' , 'marker' : 's' , } , { 'color' : 'k' , 'linestyle' : '-' , } ] , save_kwargs = { 'bbox_inches' : 'tight' , 'orientation' : 'landscape' , } , axes_functions = { 'set_title' : dataset_name , 'set_xlabel' : 'tas / ' + tas_cube . units . name , 'set_ylabel' : 'rtmt / ' + rtmt_cube . units . name , 'set_xlim' : [ 0.0 , 7.0 ] , 'set_ylim' : [ - 2.0 , 10.0 ] , 'text' : { 'args' : [ 0.05 , 0.9 , text ] , 'kwargs' : { 'transform' : 'transAxes' } , } , } , ) if not cfg [ 'write_netcdf' ] : return tas_coord = iris . coords . AuxCoord ( tas_cube . data , ** extract_variables ( cfg , as_iris = True ) [ 'tas' ] ) attr = { 'model' : dataset_name , 'regression_r_value' : regression_stats . rvalue , 'regression_slope' : regression_stats . slope , 'regression_interception' : regression_stats . intercept , 'climate_sensitivity' : - regression_stats . slope , 'ECS' : ecs , } cube = iris . cube . Cube ( rtmt_cube . data , attributes = attr , aux_coords_and_dims = [ ( tas_coord , 0 ) ] , ** extract_variables ( cfg , as_iris = True ) [ 'rtmt' ] ) filepath = os . path . join ( cfg [ 'work_dir' ] , 'ecs_regression_' + dataset_name + '.nc' ) save_iris_cube ( cube , filepath , cfg ) return
Plot linear regression used to calculate ECS.
Plot linear regression used to calculate ECS .
"""Plot linear regression used to calculate ECS.""" # Regression line # Plot data # Write netcdf file for every plot
cfg
dataset_name
tas_cube
rtmt_cube
regression_stats