def test_TRENDY_window(): TRENDYdf = AirborneFraction.TRENDY(co2_year, temp_year, trendy_uptake_mock) TRENDY_co2 = TRENDYdf.co2[2:].values TRENDY_temp = TRENDYdf.temp.sel(time=slice('1959', '2018')).Earth.values TRENDYdf.GCP['ocean sink'] = (TRENDY_co2 + TRENDY_temp) / 2 class_af, alpha = TRENDYdf.window_af() phi = 0.015 / 2.12 rho = 1.93 test_alpha = 1 / 2.12 + 1 * phi / rho for decade in alpha.index: assert alpha.mean(axis=1).loc[decade] == pytest.approx( test_alpha, 1e-4) assert alpha.std(axis=1).loc[decade] == pytest.approx(0.) emission_rate = 0.02 b = 1 / np.log(1 + emission_rate) u = 1 - b * test_alpha test_af = 1 / u for decade in class_af['mean'].index: assert class_af['mean'].loc[decade] == pytest.approx(test_af, 1e-4) assert class_af['std'].loc[decade] == pytest.approx(0.)
def test_trendy_airborne(): df = AirborneFraction.TRENDY(co2_year, temp_year, trendy_uptake_mock) GCPdf = AirborneFraction.GCP(co2_year, temp_year) GCPdf.GCP['land sink'] = (GCPdf.co2 + GCPdf.temp) / 2 GCPdf.GCP['ocean sink'] = (GCPdf.co2 + GCPdf.temp) / 2 phi = 0.015 / 2.12 rho = 1.93 land = df._feedback_parameters('Earth_Land') ocean = GCPdf._feedback_parameters()['ocean'] ocean.index = ['beta', 'gamma'] ocean['beta'] /= 2.12 ocean['u_gamma'] = ocean['gamma'] * phi / rho for parameter, divisor in zip(['beta', 'gamma'], [2.12, 1]): assert land.loc[parameter].mean() == pytest.approx(0.5 / divisor) assert land.loc[parameter].std() == pytest.approx(0.) assert ocean[parameter] == pytest.approx(0.5 / divisor) # Test function emission_rate = 2 b = 1 / np.log(1 + emission_rate / 100) beta = 0.5 * 2 / 2.12 u_gamma = 0.5 * 2 * phi / rho u = 1 - b * (beta + u_gamma) test_af = 1 / u # Class function params = land.add(ocean, axis=0) class_beta = params.loc['beta'] class_u_gamma = params.loc['u_gamma'] class_b = 1 / np.log(1 + emission_rate / 100) class_u = 1 - class_b * (class_beta + class_u_gamma) class_af = 1 / class_u assert class_af.mean() == pytest.approx(test_af)
def test_GCP_airborne(): df = AirborneFraction.GCP(co2_year, temp_year) df.GCP['land sink'] = (df.co2 + df.temp) / 2 df.GCP['ocean sink'] = (df.co2 + df.temp) / 2 params = df._feedback_parameters() for sink, param in product(['land', 'ocean'], ['CO2', 'temp']): assert params[sink][param] == pytest.approx(0.5) phi = 0.015 / 2.12 rho = 1.93 emission_rate = 2 b = 1 / np.log(1 + emission_rate / 100) beta = 0.5 * 2 / 2.12 u_gamma = 0.5 * 2 * phi / rho u = 1 - b * (beta + u_gamma) test_af = 1 / u assert df.airborne_fraction(emission_rate) == pytest.approx(test_af)
def test_GCP_window(): GCPdf = AirborneFraction.GCP(co2_year, temp_year) GCPdf.GCP['land sink'] = (GCPdf.co2 + GCPdf.temp) / 2 GCPdf.GCP['ocean sink'] = (GCPdf.co2 + GCPdf.temp) / 2 class_af, alpha = GCPdf.window_af() phi = 0.015 / 2.12 rho = 1.93 test_alpha = 1 / 2.12 + 1 * phi / rho assert alpha.mean() == pytest.approx(test_alpha) assert alpha.std() == pytest.approx(0) emission_rate = 0.02 b = 1 / np.log(1 + emission_rate) u = 1 - b * test_alpha test_af = 1 / u for decade in class_af.index: assert class_af.loc[decade] == pytest.approx(test_af)
def test_INVF_window(): INVdf = AirborneFraction.INVF(co2_year, temp_year, invf_uptake_mock) class_af, alpha = INVdf.window_af() phi = 0.015 / 2.12 rho = 1.93 test_alpha = 1 / 2.12 + 1 * phi / rho for decade in alpha.index: assert alpha.mean(axis=1).loc[decade] == pytest.approx( test_alpha, 1e-4) assert alpha.std(axis=1).loc[decade] == pytest.approx(0.) emission_rate = 0.02 b = 1 / np.log(1 + emission_rate) u = 1 - b * test_alpha test_af = 1 / u for decade in class_af['mean'].index: assert class_af['mean'].loc[decade] == pytest.approx(test_af, 1e-4) assert class_af['std'].loc[decade] == pytest.approx(0.)
def test_invf_airborne(): df = AirborneFraction.INVF(co2_year, temp_year, invf_uptake_mock) land = df._feedback_parameters('Earth_Land') ocean = df._feedback_parameters('Earth_Ocean') for region, param in product([land, ocean], ['beta', 'gamma']): assert region.loc[param].mean() == pytest.approx(0.5) assert region.loc[param].std() == pytest.approx(0.) phi = 0.015 / 2.12 rho = 1.93 emission_rate = 2 b = 1 / np.log(1 + emission_rate / 100) beta = 0.5 * 2 / 2.12 u_gamma = 0.5 * 2 * phi / rho u = 1 - b * (beta + u_gamma) test_af = 1 / u assert df.airborne_fraction(emission_rate)['mean'] == pytest.approx( test_af) assert df.airborne_fraction(emission_rate)['std'] == pytest.approx(0.)
return af.mean() """ RESULTS """ def af_dataframe(data): af_df = pd.Series(data.airborne_fraction()) af_df['alpha_5'] = af_df['alpha_mean'] - af_df['alpha_std'] * 1.645 af_df['alpha_95'] = af_df['alpha_mean'] + af_df['alpha_std'] * 1.645 return af_df """ EXECUTIONS """ GCPdf = AirborneFraction.GCP(co2['year'], temp['year']) GCPdf.airborne_fraction() pd.Series(GCPdf.airborne_fraction()) GCP_window = pd.DataFrame(GCPdf.window_af()) GCP_window stats.linregress(range(len(GCP_window.index)), GCP_window.af) INVdf = AirborneFraction.INVF(co2['year'], temp['year'], invf_uptake['year']) af_dataframe(INVdf) inv_window = pd.DataFrame(INVdf.window_af()) inv_window['alpha_cf'] = inv_window['alpha_std'] * 1.645 inv_window['alpha_5'] = inv_window['alpha_mean'] - inv_window['alpha_cf'] inv_window['alpha_95'] = inv_window['alpha_mean'] + inv_window['alpha_cf'] inv_window
"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 } } } phi, rho = 0.0071, 1.93 """ DEVS """ from importlib import reload reload(AirborneFraction) df = AirborneFraction.GCP(co2['year'], temp['year']) #, invf_uptake['year']) df.window_af() def plot(): fig = plt.figure(figsize=(10, 8)) alpha = beta + u_gamma ax1 = fig.add_subplot(211) ax1.bar(alpha.mean(axis=1).index - 3, alpha.mean(axis=1).values, width=3) ax1.axhline(linestyle='--', alpha=0.5, color='k') ax1.axhline(emission_rate, linestyle='--', alpha=0.5, color='r') ax2 = fig.add_subplot(212) x = af_results['mean'].index y = af_results['mean'].values