<jupyter_start><jupyter_text>This script compares the range of CDR pathways in the scenarios used in our study with all scenarios (that include CDR) from the IPCC Special Report on 1.5°C database<jupyter_code>#!{sys.executable} -m pip install geopandas
#!conda install --yes --prefix {sys.prefix} geopandas
%matplotlib inline
import numpy as np
import os
import matplotlib.pyplot as plt
import pandas as pd
from scipy import integrate
import seaborn as sns
import itertools
#from IAM_region_mapping import mappingDict
import json
# Paths
data_path = ""
plot_path = ""

SR15_data = pd.read_excel(data_path + 'SR15_data_afolu.xlsx')

main_remind=pd.read_excel(data_path+'REMIND_prepINPUT_use.xls')
main_image=pd.read_excel(data_path+'IMAGE_prepINPUT_use.xls')
main_message=pd.read_excel(data_path+'MESSAGE_prepINPUT_use.xls')

SR15_metadata = pd.read_excel(data_path + 'SR15_metadata_scens.xlsx')
SR15_metadata['mod_scen'] = SR15_metadata['model'] + '_' + SR15_metadata['scenario']

low15 = ['1.5C low overshoot', 'Below 1.5C']
high15 = ['1.5C high overshoot']
deg2 = ['Higher 2C', 'Lower 2C', 'Above 2C']

SR15_metadata_15lo = SR15_metadata.loc[SR15_metadata.category.isin(low15)]
SR15_metadata_15ho = SR15_metadata.loc[SR15_metadata.category.isin(high15)]
SR15_metadata_2 = SR15_metadata.loc[SR15_metadata.category.isin(deg2)]

SR15_metadata_15lo.category.unique()
list_15lo = SR15_metadata_15lo['mod_scen'].tolist()
SR15_data.head()


#SR15_data = SR15_data[['Model', 'Scenario', 'Region', 'Unit', 'Variable', '2020', '2030', '2040', '2050', '2060', '2070', '2080', '2090', '2100']]


SR15_data_M = SR15_data.set_index(['Model', 'Scenario', 'Region', 'Unit', 'Variable']).stack().reset_index()

SR15_data_M = SR15_data_M.rename(columns={"level_5": "Year", 0: "Value"})
SR15_data_M = SR15_data_M[['Model', 'Scenario', 'Region', 'Unit', 'Year','Value','Variable']]

#SR15_data_M = SR15_data_M.iloc[:,6].transpose()
SR15_data_M = SR15_data_M.pivot_table(index =['Model', 'Scenario', 'Region', 'Unit', 'Year'],  columns = 'Variable', values = 'Value')

SR15_data_M = SR15_data_M.reset_index() 
SR15_data_M = SR15_data_M.fillna(0)
SR15_data_M['Emissions|CO2|AFOLU'].values[SR15_data_M['Emissions|CO2|AFOLU'] > 0] = 0
SR15_data_M['Emissions|CO2|AFOLU'] = SR15_data_M['Emissions|CO2|AFOLU']*(-1)
SR15_data_M['Carbon Sequestration|Land Use'] = SR15_data_M['Carbon Sequestration|Land Use'].abs()
SR15_data_M['Carbon Sequestration|Direct Air Capture'] = SR15_data_M['Carbon Sequestration|Direct Air Capture'].abs()

SR15_data_M.loc[SR15_data_M.Scenario == 'SFCM_SSP2_combined_2Degree']
SR15_data_M['Total_CDR'] = 0
SR15_data_M['Year'] = SR15_data_M['Year'].astype(int)
SR15_data_M['Total_CDR'] = SR15_data_M['Carbon Sequestration|CCS|Biomass'] + SR15_data_M['Carbon Sequestration|Direct Air Capture'] + SR15_data_M['Carbon Sequestration|Land Use']+ SR15_data_M['Carbon Sequestration|Enhanced Weathering'] + SR15_data_M['Emissions|CO2|AFOLU'] 

main_remind['TOTAL_CDR'] = main_remind['TOTAL_CDR']/1000
main_image['TOTAL_CDR'] = main_image['TOTAL_CDR']/1000
main_message['TOTAL_CDR'] = main_message['TOTAL_CDR']/1000
SR15_data_M['Total_CDR'] = SR15_data_M['Total_CDR']/1000

indexNames = main_image[main_image['Scenario'] == 'SSP2-Ref-SPA0-V17'].index  #'SSP2-Ref-SPA0-V17' #'SSP1-Ref-SPA0-V17'
main_image.drop(indexNames , inplace=True)
indexNames = main_image[main_image['Scenario'] == 'SSP1-Ref-SPA0-V17'].index  #'SSP2-Ref-SPA0-V17' #'SSP1-Ref-SPA0-V17'
main_image.drop(indexNames , inplace=True)



for mod in SR15_data_M.Model.unique():
    SR15_F = SR15_data_M.loc[SR15_data_M.Model == mod]
    for scen in SR15_F.Scenario.unique():
        SR15_F_S = SR15_F.loc[SR15_F.Scenario == scen]
        sns.lineplot(x='Year',y='Total_CDR',data=SR15_F_S, linewidth = 0.5, color = 'gray')
        
for scen in main_remind.Scenario.unique():
    remind_S = main_remind.loc[(main_remind.Scenario == scen)& (main_remind.Region == 'World')]
    sns.lineplot(x='Year',y='TOTAL_CDR',data=remind_S, linewidth = 1, color = 'blue')
    
for scen in main_message.Scenario.unique():
    message_S = main_message.loc[(main_message.Scenario == scen) & (main_message.Region == 'World')]
    sns.lineplot(x='Year',y='TOTAL_CDR',data=message_S, linewidth = 1, color = 'green')
    
for scen in main_image.Scenario.unique():
    image_S = main_image.loc[(main_image.Scenario == scen) & (main_image.Region == 'World')]
    sns.lineplot(x='Year',y='TOTAL_CDR',data=image_S, linewidth = 1, color = 'red')
    


#plt.savefig(plot_path + 'Total_CDR_15.png')





SR15_box = pd.melt(SR15_data, id_vars =['Model', 'Scenario', 'Region', 'Variable', 'Unit'], value_vars =['2020', '2030', '2040', '2050', '2060', '2070', '2080', '2090', '2100'])

SR15_box = SR15_box.rename(columns={"variable": "Year"})

SR15_box['model_scena'] = SR15_box['Model'] + '_' + SR15_box['Scenario']
SR15_box
#for scen in SR15_box.model_scena.unique():
#    if scen.isin(list_15lo):
#       SR15_box['scen_cat'] = '15lo'

SR15_box = SR15_box.merge(SR15_metadata, left_on='model_scena', right_on='mod_scen', how='inner')
SR15_box = SR15_box.drop(columns = ['model', 'scenario', 'mod_scen'])
SR15_box['Total_CDR'] = 0
afolu_mask = (SR15_box.Variable == 'Emissions|CO2|AFOLU') & (SR15_box.value > 0)

SR15_box['value'][afolu_mask] = 0

SR15_box['value'] = SR15_box['value'].abs()
SR15_box.loc[SR15_box.Scenario == 'SSP5-19']
SR15_box_2050 = SR15_box.loc[SR15_box.Year == '2050', :]
SR15_box_2100 = SR15_box.loc[SR15_box.Year == '2100', :]

for mod in SR15_box_2050.model_scena.unique():
    SR15_box_2050.loc[SR15_box_2050.model_scena == mod, 'Total_CDR'] = SR15_box_2050.loc[SR15_box_2050.model_scena == mod, 'value'].sum()

for mod in SR15_box_2100.model_scena.unique():
    SR15_box_2100.loc[SR15_box_2100.model_scena == mod, 'Total_CDR'] = SR15_box_2100.loc[SR15_box_2100.model_scena == mod, 'value'].sum()
SR15_box_2050.loc[SR15_box_2050.Scenario == 'SSP5-19']
SR15_box_2050['category'] = SR15_box_2050['category'].map({'1.5C low overshoot': '15lo', 'Higher 2C': 'deg2',
                                                          'Lower 2C': 'deg2', 'Above 2C': 'deg2', '1.5C high overshoot': '15ho',
                                                          'Below 1.5C': '15lo'})

SR15_box_2100['category'] = SR15_box_2100['category'].map({'1.5C low overshoot': '15lo', 'Higher 2C': 'deg2',
                                                          'Lower 2C': 'deg2', 'Above 2C': 'deg2', '1.5C high overshoot': '15ho',
                                                          'Below 1.5C': '15lo'})
SR15_box_2100.category.unique()
SR15_boxplot = SR15_box_2050.append(SR15_box_2100)

SR15_boxplot['Total_CDR'] = SR15_boxplot['Total_CDR']/1000


from matplotlib.patches import Patch


sns.set_context("notebook", font_scale=1, rc={"axes.labelsize":12})

f, axes = plt.subplots(1, 1, figsize=(8,6))


colours = sns.color_palette("Paired",12)
colour_palette = [colours[5], colours[5]]


box1 = sns.boxplot(x="category", y="Total_CDR", data=SR15_boxplot, order = ['15lo', 'deg2', '15ho'], hue = 'Year', hue_order = ['2050', '2100'], color = 'gray')
box1 = sns.swarmplot(x="category", y="TOTAL_CDR", data=boxplot_ourmodels, order = ['15lo', 'deg2', '15ho'], palette = colour_palette, hue = 'Year', marker = 'o',facecolors='none', s = 7, dodge = True)


l = plt.legend(handles =  [Patch(facecolor='lightgray', alpha = 0.6, edgecolor='k',label='2050'), 
                           Patch(facecolor='grey', edgecolor='k',label='2100')], fontsize = 13)


box1.set_ylabel("CDR (GtCO2/yr)", fontsize = 14)
box1.set_xlabel('')
#box1.set(ylim =(-25,450))
box1.set_xticklabels(['1.5°C low OS', '2°C', '1.5°C high OS'])
box1.set_title("Range of CDR pathways in the SR1.5 database", fontsize=20)  #Cumulative CDR obligations in 2100 | equity: 
box1.tick_params(labelsize=14)

plt.savefig(plot_path + 'Final_Final_Plots/Plots_Colourblind/Boxplot_Total_CDR.png')
box_ourmodels = main_remind.append([main_image, main_message])
box_ourmodels['category'] = box_ourmodels['Scenario'].map({'SSP1-19-SPA0-V17': '15lo', 'LowEnergyDemand': '15lo', 'SSP1-19': '15lo', 'SSP2-26-SPA2-V17': 'deg2','SSP1-26-SPA1-V17': 'deg2', 'SSP5-26-SPA5-V17': 'deg2',
                                                           'SSP2-LF-SPA2-V17': '15ho', 'SSP1-19-SPA1-V17': '15ho', 'SSP2-19-SPA2-V17': '15ho', 'SSP5-19-SPA5-V17': '15ho', 'SSP1-26': 'deg2', 'SSP2-19': '15lo', 'SSP2-26': 'deg2'})
box_ourmodels_2050 = box_ourmodels.loc[(box_ourmodels.Year == 2050) & (box_ourmodels.Region == 'World'), :]
box_ourmodels_2100 = box_ourmodels.loc[(box_ourmodels.Year == 2100) & (box_ourmodels.Region == 'World'), :]

boxplot_ourmodels = box_ourmodels_2050.append(box_ourmodels_2100)
box_ourmodels_2050
box_ourmodels_2050.to_excel(data_path + 'box_ourmodels_2050.xls')
SR15_boxplot.to_excel(data_path + 'SR15_boxplot_afolu.xls')
SR15_box_2100.to_excel(data_path + 'SR15_box_2100.xls')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17153120790300852, 'fraction_numerical': 0.08419398293668612, 'mean_word_length': 5.390961262553802, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '130713', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 4041, 'n_tokens_neox': 3604, 'n_words': 557, 'path': '04_CDR_total_calc.ipynb', 'repo_name': 'SusanneBaurCA/CDR_equity', 'size': '8908'}