Ejemplo n.º 1
0
def mean_regstat(timeres, variable):
    uptake = fb_id.invf_uptake[timeres]

    df = FeedbackAnalysis.INVF(
                                fb_id.co2['year'],
                                fb_id.temp['year'],
                                uptake,
                                variable
                                )

    regstat = [df.regstats()[model] for model in uptake]

    index = regstat[0].index
    columns = regstat[0].columns
    array = np.zeros([len(index), len(columns)])
    for i, j in product(range(len(index)), range(len(columns))):
        values = []
        for model_regstat in regstat:
            val = model_regstat.iloc[i,j]
            if val != np.nan:
                values.append(val)
        array[i][j] = np.nanmean(values)

    mean_regstat = pd.DataFrame(array)
    mean_regstat.index = index
    mean_regstat.columns = columns

    return mean_regstat
Ejemplo n.º 2
0
    def window_af(self, emission_rate=0.02):
        """
        """

        land_df = FeedbackAnalysis.INVF(self.co2, self.temp, self.uptake,
                                        'Earth_Land')
        ocean_df = FeedbackAnalysis.INVF(self.co2, self.temp, self.uptake,
                                         'Earth_Ocean')

        land = land_df.params()
        ocean = ocean_df.params()
        beta = (land['beta'] + ocean['beta'])
        u_gamma = (land['u_gamma'] + ocean['u_gamma'])

        alpha = (beta + u_gamma)
        alpha_mean = alpha.mean(axis=1)
        alpha_std = alpha.std(axis=1)

        b = 1 / np.log(1 + emission_rate)
        u = 1 - b * alpha_mean

        af = 1 / u
        return {'af': af, 'alpha_mean': alpha_mean, 'alpha_std': alpha_std}
Ejemplo n.º 3
0
def all_regstat(timeres, variable):
    uptake = fb_id.invf_uptake[timeres]

    df = FeedbackAnalysis.INVF(
                                fb_id.co2['year'],
                                fb_id.temp['year'],
                                uptake,
                                variable
                                )

    all_regstats = {model : pd.DataFrame(df.regstats()[model])
                    for model in uptake
               }

    return all_regstats
Ejemplo n.º 4
0
def fb_seasonal_regional_inv2(save=False):
    timeres = ['winter', 'summer']
    uptake = {time : fb_id.invf_uptake[time] for time in timeres}

    all_subplots = ['42'+str(i) for i in range(1,9)]
    vars = product(['Earth', 'South', 'Tropical', 'North'], timeres)

    # Note u_gamma is used to compare with beta.
    parameters = ['beta', 'u_gamma']
    colors = ['green', 'red']
    bar_width = 1.5
    bar_position = np.arange(-1, 1) * bar_width

    fig = plt.figure(figsize=(14,10))
    ax = {}
    axl = fig.add_subplot(111, frame_on=False)
    axl.tick_params(labelcolor="none", bottom=False, left=False)

    fb_reg_inv = {}

    ymin_land, ymax_land = [], []
    ymin_ocean, ymax_ocean = [], []
    for subplot, var in zip(all_subplots, vars):
        variable = var[0] + '_Land'
        time = var[1]

        df = FeedbackAnalysis.INVF(
                                    fb_id.co2['year'],
                                    fb_id.temp['year'],
                                    uptake[time],
                                    variable
                                    )
        params = df.params()
        for parameter, color, bar_pos in zip(parameters, colors, bar_position):
            param = params[parameter]

            param_mean = param.mean(axis=1)
            param_std = param.std(axis=1)

            fb_reg_inv[variable, parameter] = pd.DataFrame({'Mean': param_mean,
                                                            '90_conf_interval': 1.645*param_std})

            ax[subplot] = fig.add_subplot(subplot)

            ax[subplot].bar(param.index + 5 + bar_pos,
                            param_mean,
                            yerr=1.645*param_std,
                            width=bar_width,
                            color=color)

            if time == 'winter' and subplot != "421":
                ymin_land.append((param_mean - 1.645*param_std).min())
                ymax_land.append((param_mean + 1.645*param_std).max())
            if time == 'summer' and subplot != "422":
                ymin_ocean.append((param_mean - 1.645*param_std).min())
                ymax_ocean.append((param_mean + 1.645*param_std).max())

    delta = 0.05
    for subplot in ["423", "425", "427"]:
        ax[subplot].set_ylim([min(ymin_land) - delta * abs(min(ymin_land)),
                              max(ymax_land) + delta * abs(max(ymax_land))])
    for subplot in ["424", "426", "428"]:
        ax[subplot].set_ylim([min(ymin_ocean) - delta * abs(min(ymin_ocean)),
                              max(ymax_ocean) + delta * abs(max(ymax_ocean))])

    ax["421"].set_xlabel("Winter", fontsize=14, labelpad=5)
    ax["421"].xaxis.set_label_position('top')
    ax["422"].set_xlabel("Summer", fontsize=14, labelpad=5)
    ax["422"].xaxis.set_label_position('top')
    for subplot, region in zip(["421", "423", "425", "427"],
                               ['Earth', 'South', 'Tropical', 'North']):
        ax[subplot].set_ylabel(region, fontsize=14, labelpad=5)

    axl.set_xlabel("Middle year of 10-year window", fontsize=16, labelpad=10)
    axl.set_ylabel(r'Feedback parameter   (yr$^{-1}$)',
                   fontsize=16,
                   labelpad=40
                  )

    if save:
        plt.savefig(fb_id.FIGURE_DIRECTORY + f"fb_inv_regional_seasonal2.png")

    return fb_reg_inv
Ejemplo n.º 5
0
def fb_seasonal_regional_inv(save=False):
    timeres = ['winter', 'summer']
    uptake = {time : fb_id.invf_uptake[time] for time in timeres}

    all_subplots = ['22'+str(i) for i in range(1,5)]
    # Note u_gamma is used to compare with beta.
    parameters = [['beta', '_Land'], ['u_gamma', '_Land']]

    vars = ['Earth', 'South', 'Tropical', 'North']
    colors = ['gray', 'blue', 'orange', 'green']
    bar_width = 1.25
    bar_position = np.arange(-2, 2) * bar_width

    fig = plt.figure(figsize=(14,10))
    ax = {}
    axl = fig.add_subplot(111, frame_on=False)
    axl.tick_params(labelcolor="none", bottom=False, left=False)

    fb_reg_inv = {}

    for subplot, pro in zip(all_subplots, product(parameters, timeres)):
        parameter, time = pro
        for var, color, bar_pos in zip(vars, colors, bar_position):
            variable = var + parameter[1]
            df = FeedbackAnalysis.INVF(
                                        fb_id.co2['year'],
                                        fb_id.temp['year'],
                                        uptake[time],
                                        variable
                                        )
            param = df.params()[parameter[0]]
            param_mean = param.mean(axis=1)
            param_std = param.std(axis=1)

            fb_reg_inv[(variable, parameter[0])] = pd.DataFrame({'Mean': param_mean,
                                                                 '90_conf_interval': 1.645*param_std})

            ax[subplot] = fig.add_subplot(subplot)
            ax[subplot].bar(param.index + 5 + bar_pos,
                            param_mean,
                            yerr=1.645*param_std,
                            width=bar_width,
                            color=color)

    ax["221"].set_xlabel("Winter", fontsize=14, labelpad=5)
    ax["221"].xaxis.set_label_position('top')
    ax["222"].set_xlabel("Summer", fontsize=14, labelpad=5)
    ax["222"].xaxis.set_label_position('top')
    for subplot, region in zip(["221", "223"],
                               [r'$\beta$', r'$u_{\gamma}$']):
        ax[subplot].set_ylabel(region, fontsize=14, labelpad=5)

    axl.set_xlabel("Middle year of 10-year window", fontsize=16, labelpad=10)
    axl.set_ylabel(r'Feedback parameter   (yr$^{-1}$)',
                   fontsize=16,
                   labelpad=40
                  )

    if save:
        plt.savefig(fb_id.FIGURE_DIRECTORY + f"fb_inv_regional_year.png")

    return fb_reg_inv
Ejemplo n.º 6
0
def fb_seasonal_inv(save=False):
    timeres = ['winter', 'summer']
    uptake = {time : fb_id.invf_uptake[time] for time in timeres}

    fig = plt.figure(figsize=(14,10))
    ax = {}
    axl = fig.add_subplot(111, frame_on=False)
    axl.tick_params(labelcolor="none", bottom=False, left=False)

    fb_inv_df = {}

    for subplot, time in zip(["211", "212"], timeres):
        df = FeedbackAnalysis.INVF(
                                    fb_id.co2['year'],
                                    fb_id.temp['year'],
                                    uptake[time],
                                    "Earth_Land"
                                    )
        whole_df = feedback_regression(time, "Earth_Land")[0].mean(axis=1)
        beta = df.params()['beta']
        gamma = df.params()['u_gamma'] # Note u_gamma is used to compare with beta.

        whole_beta = whole_df['beta']
        whole_gamma = whole_df['u_gamma']

        beta_mean = beta.mean(axis=1)
        beta_std = beta.std(axis=1)
        gamma_mean = gamma.mean(axis=1)
        gamma_std = gamma.std(axis=1)

        fb_inv_df[time] = pd.DataFrame(
            {
                'whole_beta': whole_beta, 'whole_gamma': whole_gamma,
                'beta_mean': beta_mean, 'beta_std': beta_std,
                'gamma_mean': gamma_mean, 'gamma_std': gamma_std
            }
        )

        ax[subplot] = fig.add_subplot(subplot)

        ax[subplot].axhline(ls='--', color='k', alpha=0.5, lw=0.8)
        ax[subplot].axhline(y= whole_beta, ls='--', color='green', alpha=0.8, lw=1)
        ax[subplot].axhline(y= whole_gamma, ls='--', color='red', alpha=0.8, lw=1)

        bar_width = 3
        ax[subplot].bar(beta.index + 5 - bar_width / 2,
                        beta_mean,
                        yerr=beta_std * 1.645,
                        width=bar_width,
                        color='green',
                        label=r'$\beta$')
        ax[subplot].bar(gamma.index + 5 + bar_width / 2,
                        gamma_mean,
                        yerr=gamma_std * 1.645,
                        width=bar_width,
                        color='red',
                        label=r'$u_{\gamma}$')

        ax[subplot].legend()
        ax[subplot].set_ylabel(time.title(), fontsize=14, labelpad=5)

    axl.set_xlabel("First year of 10-year window", fontsize=16, labelpad=10)
    axl.set_ylabel('Feedback parameter   (yr $^{-1}$)',
                   fontsize=16,
                   labelpad=40
                  )

    if save:
        plt.savefig(fb_id.FIGURE_DIRECTORY + f"fb_inv_seasonal.png")

    return fb_inv_df
Ejemplo n.º 7
0
temp = {
    "year": xr.open_dataset(OUTPUT_DIR + f'TEMP/spatial/output_all/HadCRUT/year.nc'),
    "month": xr.open_dataset(OUTPUT_DIR + f'TEMP/spatial/output_all/HadCRUT/month.nc')
}


invf_models = os.listdir(INV_DIRECTORY)
invf_uptake = {'year': {}, 'month': {}}
for timeres in invf_uptake:
    for model in invf_models:
        model_dir = INV_DIRECTORY + model + '/'
        invf_uptake[timeres][model] = xr.open_dataset(model_dir + f'{timeres}.nc')


variables = ['Earth_Land', 'South_Land', 'Tropical_Land', 'North_Land']
fa = {}
for var in variables:
    fa[var] = FeedbackAnalysis.INVF(co2['year'], temp['year'], invf_uptake['year'], var).params()['beta']


def check_sum(model, year):
     earth = fa['Earth_Land'][model][year]
     south = fa['South_Land'][model][year]
     tropical = fa['Tropical_Land'][model][year]
     north = fa['North_Land'][model][year]

     return earth - south - tropical - north

for m, y in product(['CAMS', 'Rayner', 'JENA_s76'], [1980, 1990, 2000]):
    print(check_sum(m,y))