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
def median_regstat(timeres, variable): uptake = fb_id.trendy_uptake median_regstats = {} for simulation in ["S1", "S3"]: df = FeedbackAnalysis.TRENDY( timeres, fb_id.co2['year'], fb_id.temp['year'], uptake, variable ) regstat = [df.regstats()[simulation][model] for model in uptake[simulation][timeres]] 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.nanmedian(values) median_regstat = pd.DataFrame(array) median_regstat.index = index median_regstat.columns = columns median_regstats[simulation] = median_regstat return median_regstats
def fb_regional_trendy(timeres='year', save=False): uptake = fb_id.trendy_uptake all_subplots = ['211', '212'] # Note u_gamma is used to compare with beta. parameters = [['beta', 'S1'], ['u_gamma', 'S3']] 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=(16,6)) ax = {} axl = fig.add_subplot(111, frame_on=False) axl.tick_params(labelcolor="none", bottom=False, left=False) fb_trendy_df = {} for subplot, parameter in zip(all_subplots, parameters): for var, color, bar_pos in zip(vars, colors, bar_position): df = FeedbackAnalysis.TRENDY( timeres, fb_id.co2['year'], fb_id.temp['year'], uptake, var + '_Land' ) param = df.params()[parameter[1]][parameter[0]] param_mean = param.mean(axis=1) param_std = param.std(axis=1) fb_trendy_df[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=param_std*1.645, width=bar_width, color=color) ax["211"].set_xlabel(r'$\beta$', fontsize=14, labelpad=5) ax["211"].xaxis.set_label_position('top') ax["212"].set_xlabel(r'$u_{\gamma}$', fontsize=14, labelpad=5) ax["212"].xaxis.set_label_position('top') axl.set_xlabel("Middle year of 10-year window", fontsize=16, labelpad=10) axl.set_ylabel(r'Feedback parameter (yr $^{-1}$)', fontsize=16, labelpad=20 ) if save: plt.savefig(fb_id.FIGURE_DIRECTORY + f"fb_trendy_regional_year.png") return fb_trendy_df
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}
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
def all_regstat(timeres, variable): uptake = fb_id.trendy_uptake all_regstats = {} for simulation in ['S1', 'S3']: df = FeedbackAnalysis.TRENDY( timeres, fb_id.co2['year'], fb_id.temp['year'], uptake, variable ) all_regstats[simulation] = {model : pd.DataFrame(df.regstats()[simulation][model]) for model in uptake[simulation][timeres]} return all_regstats
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
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
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
time_ranges.pop(-1) time_ranges.append((str(time_stop - 10), str(time_stop))) return time_ranges """ EXECUTION """ for input in tqdm(inputs): uptake, tempsink, time = input temp, sink = tempsink time_start, time_stop = timerange_inputs[uptake[0]][uptake[1]] time_start = int(time_start) time_stop = int(time_stop) time_ranges = build_time_ranges(time_start, time_stop) # [str(max(1959, time_start)), '2017'] for time_range in time_ranges: if time == "month": # Grab last month of previous year as end point. month_tr = str(int(time_range[1]) - 1) time_range = (f'{time_range[0]}-01', f'{month_tr}-12') slice_time_range = slice(*time_range) df = FA.FeedbackAnalysis(uptake=uptake, temp=temp, time=time, sink=sink, time_range=slice_time_range) df.feedback_output()
def window_af(self, emission_rate=0.02): """ """ land = FeedbackAnalysis.TRENDY('year', self.co2, self.temp, self.all_uptake, 'Earth_Land').params() # Ocean times = ( ('1960', '1969'), ('1970', '1979'), ('1980', '1989'), ('1990', '1999'), ('2000', '2009'), ('2008', '2017'), ) models = {} start, end = int(times[0][0]), int(times[-1][-1]) df = pd.DataFrame(data={ "sink": self.GCP['ocean sink'].loc[start:end], "CO2": self.co2.loc[start:end], "temp": self.temp.sel(time=slice(str(start), str(end))).Earth }, index=self.GCP.loc[start:end].index) for time in times: X = sm.add_constant(df[['CO2', 'temp']].loc[time[0]:time[1]]) Y = df['sink'].loc[time[0]:time[1]] model = sm.OLS(Y, X).fit() models[time[0]] = model.params.loc[['CO2', 'temp']] ocean = pd.DataFrame(models).T ocean.columns = ['beta', 'gamma'] ocean.index = [int(time[0]) for time in times] phi = 0.015 / 2.12 rho = 1.93 ocean['beta'] /= 2.12 ocean['u_gamma'] = ocean['gamma'] * phi / rho land_beta = land['S1']['beta'] land_ugamma = land['S3']['u_gamma'] land_beta.index.name = None land_ugamma.index.name = None beta = land_beta.add(ocean['beta'], axis=0) u_gamma = land_ugamma.add(ocean['u_gamma'], axis=0) 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}
fs = 12 fc = 365 / 667 w = fc / (fs / 2) # Normalize the frequency. b, a = signal.butter(5, w, 'low') return signal.filtfilt(b, a, x) trendy_duptake['S1']['month']['VISIT'].Earth_Land from importlib import reload reload(FeedbackAnalysis) df = FeedbackAnalysis.TRENDY(co2['month'], temp['month'], trendy_duptake, 'Earth_Land') df.regstats()['S1']['VISIT'] DIR = './../../../' OUTPUT_DIR = DIR + 'output/' co2 = { "year": pd.read_csv(DIR + f"data/CO2/co2_year.csv", index_col=["Year"]).CO2, "month": pd.read_csv(DIR + f"data/CO2/co2_month.csv", index_col=["Year", "Month"]).CO2 } temp = { "year":
def fb_regional_trendy_seasonal2(save=False): uptake = fb_id.trendy_uptake seasons = ['winter', 'summer'] all_subplots = [['421', '423', '425', '427'], ['422', '424', '426', '428']] vars = ['Earth', 'South', 'Tropical', 'North'] # Note u_gamma is used to compare with beta. parameters = ['beta', 'u_gamma'] simulations = ['S1', 'S3'] colors = ['green', 'red'] bar_width = 1.5 bar_position = np.arange(-1, 1) * bar_width fig = plt.figure(figsize=(16,14)) ax = {} axl = fig.add_subplot(111, frame_on=False) axl.tick_params(labelcolor="none", bottom=False, left=False) fb_trendy_df = {} for subplots, timeres in zip(all_subplots, seasons): ymin, ymax = [], [] fb_trendy_df[timeres] = {} for subplot, var in zip(subplots, vars): df = FeedbackAnalysis.TRENDY( timeres, fb_id.co2['year'], fb_id.temp['year'], uptake, var + '_Land' ) for parameter, sim, color, bar_pos in zip(parameters, simulations, colors, bar_position): param = df.params()[sim][parameter] param_mean = param.mean(axis=1) param_std = param.std(axis=1) fb_trendy_df[timeres][(var+'_Land', 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 subplot != "411": ymin.append((param_mean - 1.645*param_std).min()) ymax.append((param_mean + 1.645*param_std).max()) delta = 0.05 for subplot in subplots[1:]: ax[subplot].set_ylim([min(ymin) - delta * abs(min(ymin)), max(ymax) + delta * abs(max(ymax))]) for subplot, region in zip(["421", "423", "425", "427"], ['Earth', 'South', 'Tropical', 'North']): ax[subplot].set_ylabel(region, fontsize=14, labelpad=5) for subplot, season in zip(["421", "422"], ['Winter', 'Summer']): ax[subplot].set_xlabel(season, fontsize=14, labelpad=5) ax[subplot].xaxis.set_label_position('top') 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_trendy_regional_seasonal2.png") return fb_trendy_df
def fb_trendy_seasonal(save=False): uptake = fb_id.trendy_uptake fig = plt.figure(figsize=(20,11)) ax = {} axl = fig.add_subplot(111, frame_on=False) axl.tick_params(labelcolor="none", bottom=False, left=False) bar_width = 3 param_details = { 'beta': { 'bar_width': -bar_width, 'color': 'green', 'label': r'$\beta$' }, 'u_gamma': { 'bar_width': bar_width, 'color': 'red', 'label': r'$u_{\gamma}$' } } subplots = ['211', '212'] seasons = ['winter', 'summer'] fb_trendy_df = {} for subplot, timeres in zip(subplots, seasons): fb_trendy_df[timeres] = {} for simulation, parameter in zip(["S1", "S3"], ['beta', 'u_gamma']): bw = param_details[parameter]['bar_width'] color = param_details[parameter]['color'] label = param_details[parameter]['label'] df = FeedbackAnalysis.TRENDY( timeres, fb_id.co2['year'], fb_id.temp['year'], uptake, 'Earth_Land' ) whole_df = feedback_regression(timeres, "Earth_Land")[0][simulation].mean(axis=1) whole_param = whole_df[parameter] param = df.params()[simulation][parameter] param_mean = param.mean(axis=1) param_std = param.std(axis=1) fb_trendy_df[timeres][parameter] = pd.DataFrame({'Mean': param_mean, '90_conf_interval': 1.645*param_std}) ax[subplot] = fig.add_subplot(subplot) ax[subplot].axhline(ls='--', color='k', alpha=0.5, lw=0.8) ax[subplot].bar(param.index + 5 + bw / 2, param_mean, yerr=param_std * 1.645, width=abs(bar_width), color=color, label=label) ax[subplot].axhline(y= whole_param, ls='--', color=color, alpha=0.8, lw=1) ax[subplot].legend() ax[subplot].set_ylabel(f'{timeres.title()}', fontsize=16, labelpad=5) axl.set_xlabel("Middle 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_trendy_seasonal.png") return fb_trendy_df
import pandas as pd import numpy as np import matplotlib.pyplot as plt import statsmodels.api as sm from datetime import datetime import pandas as pd import sys from core import FeedbackAnalysis as FA from importlib import reload reload(FA) df = FA.FeedbackAnalysis(uptake=('TRENDY', 'LPJ-GUESS_S1_nbp'), temp='CRUTEM', time='year', sink="Earth_Land", time_range=slice("2008", "2017")) df.data df.U len(df.uptake), len(df.temp), len(df.CO2) df.data df.model.summary() df.data df.model.params df.confidence_intervals('const', alpha) df.model.summary()
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))
f'TRENDY/spatial/output_all/{model_name}_S3_nbp/year.nc') for model_name in trendy_models }, "month": { model_name: xr.open_dataset( OUTPUT_DIR + f'TRENDY/spatial/output_all/{model_name}_S3_nbp/month.nc') for model_name in trendy_models if model_name != "LPJ-GUESS" } } } temp_zero = {} for timeres in temp: temp_zero[timeres] = xr.Dataset( { key: (('time'), np.zeros(len(temp[timeres][key]))) for key in ['Earth', 'South', 'Tropical', 'North'] }, coords={'time': (('time'), temp[timeres].time)}) timeres = 'month' S1 = FeedbackAnalysis.TRENDY(co2[timeres], temp_zero[timeres], trendy_uptake['S1'][timeres], 'Earth_Land') S3 = FeedbackAnalysis.TRENDY(co2[timeres], temp[timeres], trendy_uptake['S3'][timeres], 'Earth_Land') S3_zero = FeedbackAnalysis.TRENDY(co2[timeres], temp_zero[timeres], trendy_uptake['S3'][timeres], 'Earth_Land') S3.params()['beta'] - S1.params()['beta']
""" Development for FeedbackOutput class functions. """ """ IMPORTS """ import pandas as pd import matplotlib.pyplot as plt import numpy as np from scipy import stats from matplotlib.dates import date2num import sys from core import FeedbackAnalysis from importlib import reload reload(FeedbackAnalysis) """ INPUTS """ model_type = 'TRENDY' sink = 'Earth_Land' timeres = 'month' """ EXECUTION """ df = FeedbackAnalysis.FeedbackOutput(model_type, sink, timeres) df.individual_plot('CAMS', 'beta', True) df.merge_plot('S1', 'gamma', True) df.merge_params('S3', 'gamma') df.difference_simulations('gamma', False)