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
)


Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
Not importing directory /home/zebedee/Documents/AGCEC/MCastle/pymagicc/venv/lib/python3.7/site-packages/sphinxcontrib: missing __init__
Not importing directory /home/zebedee/Documents/AGCEC/MCastle/pymagicc/venv/lib/python3.7/site-packages/mpl_toolkits: missing __init__

In [2]:
%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)


Out[3]:
pymagicc.io.MAGICCData

In [4]:
pprint(rcp26.metadata)


{'description': 'HARMONISED, EXTENDED FINAL RCP3-PD (Peak&Decline) NOV26; '
                'RCP3PD-Contact: IMAGE group, Detlef van Vuuren '
                '(Detlef.vanVuuren@pbl.nl)',
 'header': 'Final RCP3PD with constant emissions after 2100 using the default '
           'RCPtool MAGICC6.3 settings. Compiled by: '
           'malte.meinshausen@pik-potsdam.de',
 'notes': 'DATE: 26/11/2009 11:29:06; MAGICC-VERSION: 6.3.09, 25 November 2009'}

MAGICCData subclasses OpenSCM's ScmDataFrame so we can access many of the dataframe's attributes directly, e.g.


In [5]:
rcp26.__class__.__bases__


Out[5]:
(scmdata.dataframe.ScmDataFrame,)

In [6]:
rcp26.head()


Out[6]:
time 2000-01-01 00:00:00 2001-01-01 00:00:00 2002-01-01 00:00:00 2003-01-01 00:00:00 2004-01-01 00:00:00 2005-01-01 00:00:00 2006-01-01 00:00:00 2007-01-01 00:00:00 2010-01-01 00:00:00 2020-01-01 00:00:00 2030-01-01 00:00:00 2040-01-01 00:00:00 2050-01-01 00:00:00 2060-01-01 00:00:00 2070-01-01 00:00:00 2080-01-01 00:00:00 2090-01-01 00:00:00 2100-01-01 00:00:00 2125-01-01 00:00:00 2500-01-01 00:00:00
model scenario region variable unit climate_model todo
IMAGE RCP26 World|Bunkers Emissions|CO2|MAGICC Fossil and Industrial Gt C / yr unspecified SET 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Emissions|CO2|MAGICC AFOLU Gt C / yr unspecified SET 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Emissions|CH4 Mt CH4 / yr unspecified SET 0.4325 0.4422 0.4520 0.4618 0.4717 0.4817 0.4812 0.4806 0.4790 0.5252 0.5252 0.3191 0.000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Emissions|N2O Mt N2ON / yr unspecified SET 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Emissions|SOx Mt S / yr unspecified SET 5.5390 5.5824 5.6207 5.6587 5.6964 5.7337 5.4434 5.1544 4.2947 1.5534 1.0918 0.7379 0.336 0.3209 0.2856 0.2356 0.2127 0.1887 0.1887 0.1887

The rcp's contain the following emissions with the following units


In [7]:
rcp26[["variable", "unit"]].drop_duplicates()


Out[7]:
variable unit
0 Emissions|CO2|MAGICC Fossil and Industrial Gt C / yr
1 Emissions|CO2|MAGICC AFOLU Gt C / yr
2 Emissions|CH4 Mt CH4 / yr
3 Emissions|N2O Mt N2ON / yr
4 Emissions|SOx Mt S / yr
5 Emissions|CO Mt CO / yr
6 Emissions|NMVOC Mt NMVOC / yr
7 Emissions|NOx Mt N / yr
8 Emissions|BC Mt BC / yr
9 Emissions|OC Mt OC / yr
10 Emissions|NH3 Mt N / yr
11 Emissions|CF4 kt CF4 / yr
12 Emissions|C2F6 kt C2F6 / yr
13 Emissions|C6F14 kt C6F14 / yr
14 Emissions|HFC23 kt HFC23 / yr
15 Emissions|HFC32 kt HFC32 / yr
16 Emissions|HFC4310 kt HFC4310 / yr
17 Emissions|HFC125 kt HFC125 / yr
18 Emissions|HFC134a kt HFC134a / yr
19 Emissions|HFC143a kt HFC143a / yr
20 Emissions|HFC227ea kt HFC227ea / yr
21 Emissions|HFC245fa kt HFC245fa / yr
22 Emissions|SF6 kt SF6 / yr

The regions included are


In [8]:
rcp26["region"].unique()


Out[8]:
array(['World|Bunkers', 'World|R5LAM', 'World|R5MAF', 'World|R5ASIA',
       'World|R5REF', 'World|R5OECD', 'World'], dtype=object)

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"]
]
rcp26.filter(
    variable=categories_to_plot,
    year=range(1000, 2150)
).pivot_table(
    index="time", 
    columns=["variable", "unit", "region"],
    aggfunc="sum"
).groupby(level="variable", axis=1).plot(figsize=(12, 7));


Fossil fuel emissions for the four RCP scenarios.


In [10]:
rcps.filter(
    variable="Emissions|CO2|MAGICC Fossil and Industrial",
    region="World"
).line_plot(x="time", figsize=(16, 9));


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
%time results = pymagicc.run(rcp26)


CPU times: user 1.22 s, sys: 63.7 ms, total: 1.28 s
Wall time: 20.3 s

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
%time multiple_runs()


CPU times: user 781 ms, sys: 32.3 ms, total: 814 ms
Wall time: 1.41 s

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"
        ).line_plot(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");