#!/usr/bin/env python # coding: utf-8 # # Pymagicc Usage Examples # In[1]: # NBVAL_IGNORE_OUTPUT from pprint import pprint import pymagicc from pymagicc import MAGICC6 from pymagicc.io import MAGICCData from pymagicc.scenarios import rcp26, rcp45, rcps # In[2]: get_ipython().run_line_magic('matplotlib', 'inline') from matplotlib import pyplot as plt plt.style.use("ggplot") plt.rcParams["figure.figsize"] = 16, 9 # ## Scenarios # # The four RCP scenarios are already preloaded in Pymagicc. They are loaded as `MAGICCData` objects with `metadata` attributes. `metadata` contains metadata # In[3]: type(rcp26) # In[4]: pprint(rcp26.metadata) # `MAGICCData` subclasses scmdata's `ScmRun` so we can access the `scmdata` helpers directly, e.g. # In[5]: rcp26.__class__.__bases__ # In[6]: rcp26.head() # The rcp's contain the following emissions with the following units # In[7]: rcp26.meta[["variable", "unit"]].drop_duplicates() # The regions included are # In[8]: rcp26["region"].unique() # A plot of four categories in RCP3PD # In[9]: categories_to_plot = [ "Emissions|" + v for v in [ "CO2|MAGICC Fossil and Industrial", "CO2|MAGICC AFOLU", "CH4", "N2O", ] ] for g in rcp26.filter( variable=categories_to_plot, year=range(1000, 2150) ).groupby("variable"): plt.figure(figsize=(12, 7)) g.lineplot(hue="region").set_title(g.get_unique_meta("variable", True)) # Fossil fuel emissions for the four RCP scenarios. # In[10]: rcps.filter( variable="Emissions|CO2|MAGICC Fossil and Industrial", region="World" ).lineplot(x="time"); # ## Running MAGICC # # A single `pymagicc` run takes under a second and returns the same object as used above. If not on Windows, the very first run might be slower due to setting up Wine. Multiple runs can be faster as setup times are reduced and other options speed things up even further e.g. limiting output to the subset of interest, using binary output formats. # In[11]: # NBVAL_IGNORE_OUTPUT get_ipython().run_line_magic('time', 'results = pymagicc.run(rcp26)') # In[12]: def multiple_runs(): with MAGICC6() as magicc: for name, sdf in rcps.timeseries().groupby(["scenario"]): results = magicc.run(MAGICCData(sdf.copy())) # In[13]: # NBVAL_IGNORE_OUTPUT get_ipython().run_line_magic('time', 'multiple_runs()') # In[14]: fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(16, 9)) with MAGICC6() as magicc: for name, sdf in rcps.timeseries().groupby(["scenario"]): results = magicc.run(MAGICCData(sdf.copy())) results.filter( variable="Surface Temperature", region="World" ).lineplot(ax=ax, x="time"); # The default parameters are the ones that were used to produce the RCP GHG concentrations (see also http://live.magicc.org/). Of course it's easy to change them. # In[15]: low = pymagicc.run(rcp45, core_climatesensitivity=1.5) default = pymagicc.run(rcp45, core_climatesensitivity=3) high = pymagicc.run(rcp45, core_climatesensitivity=4.5) # In[16]: filtering = { "variable": "Surface Temperature", "region": "World", "year": range(1850, 2101), } fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(16, 9)) default.filter(**filtering).line_plot(x="time", ax=ax) plt.fill_between( low.filter(**filtering)["time"].values, low.filter(**filtering).timeseries().values.squeeze(), high.filter(**filtering).timeseries().values.squeeze(), color="lightgray", ) plt.title( "RCP 4.5 with equilibrium climate sensitivity set to 1.5, 3, and 4.5" ) plt.ylabel("°C"); # In[ ]: