In [1]:
# NBVAL_IGNORE_OUTPUT
from os import listdir
from os.path import join, dirname
from pprint import pprint
import pandas as pd
import pymagicc
from pymagicc.io import MAGICCData, read_cfg_file, NoReaderWriterError
import expectexception
In [2]:
import matplotlib.pyplot as plt
plt.style.use("bmh")
%matplotlib inline
In [3]:
MAGICC6_DIR = join("..", "pymagicc", "MAGICC6", "run")
TEST_DATA_DIR = join("..", "tests", "test_data")
In [4]:
print(MAGICCData.__init__.__doc__)
Here we load a file from disk and tell MAGICCData to set the scenario column to "Historical".
In [5]:
# NBVAL_IGNORE_OUTPUT
mdata = MAGICCData(
join(MAGICC6_DIR, "HISTRCP_CO2I_EMIS.IN"),
columns={"scenario": ["Historical"]},
)
mdata.head()
Out[5]:
Any metadata which is found in the file is included in MAGICCData
's metadata
attribute.
In [6]:
pprint(mdata.metadata)
In [7]:
# NBVAL_IGNORE_OUTPUT
mdata.filter(region="*R5ASIA").head()
Out[7]:
In [8]:
# NBVAL_IGNORE_OUTPUT
mdata = mdata.append(
join(MAGICC6_DIR, "RCP45.SCEN"), columns={"model": ["MiniCAM"]},
)
mdata.head()
Out[8]:
In [9]:
# NBVAL_IGNORE_OUTPUT
mdata.filter(variable="*BC", region="*ASIA").head()
Out[9]:
We can also read the RCP data files provided at http://www.pik-potsdam.de/~mmalte/rcps/. These are also tricky as the underlying data format is unique.
In the cell below we read a file from disk and we set the model column to be "IMAGE", the scenario column to be "RCP26" and the climate_model column to be "MAGICC6".
In [10]:
# NBVAL_IGNORE_OUTPUT
rcp_online_data = MAGICCData(
join(TEST_DATA_DIR, "RCP3PD_EMISSIONS.DAT"),
columns={
"model": ["IMAGE"],
"scenario": ["RCP26"],
"climate_model": ["MAGICC6"],
},
)
rcp_online_data.head()
Out[10]:
In [11]:
rcp_online_data.meta[["variable", "unit"]]
Out[11]:
In [12]:
print(rcp_online_data.append.__doc__)
In this case we want to append a file from disk and we want the appended file's model column to be "IMAGE", its scenario column to be "RCP26" and its climate_model column to be "MAGICC6".
In [13]:
# NBVAL_IGNORE_OUTPUT
rcp_online_data.append(
join(TEST_DATA_DIR, "RCP3PD_MIDYEAR_CONCENTRATIONS.DAT"),
columns={
"model": ["IMAGE"],
"scenario": ["RCP26"],
"climate_model": ["MAGICC6"],
},
inplace=True,
)
rcp_online_data.head()
Out[13]:
In [14]:
# NBVAL_IGNORE_OUTPUT
rcp_online_data.meta[["variable", "unit"]]
Out[14]:
We can also append the radiative forcing information.
In [15]:
# NBVAL_IGNORE_OUTPUT
rcp_online_data = rcp_online_data.append(
join(TEST_DATA_DIR, "RCP3PD_MIDYEAR_RADFORCING.DAT"),
columns={
"model": ["IMAGE"],
"scenario": ["RCP26"],
"climate_model": ["MAGICC6"],
},
)
rcp_online_data.head()
Out[15]:
In [16]:
# NBVAL_IGNORE_OUTPUT
rcp_online_data.meta[["variable", "unit"]]
Out[16]:
Making the most of MAGICCData
's data analysis features can allow all sorts of plots to be made with little difficulty.
In [17]:
# NBVAL_IGNORE_OUTPUT
rcp_online_data.filter(variable="*CO2*", region="World").filter(
variable="*Eq*", keep=False
).filter(variable="*,*", keep=False).timeseries().T.plot(
subplots=True, figsize=(16, 9)
);
In [18]:
all_run_files = {}
for file in listdir(MAGICC6_DIR):
if file.endswith((".exe", ".MON")):
continue
elif file.endswith(".CFG"):
all_run_files[file] = read_cfg_file(join(MAGICC6_DIR, file))
else:
all_run_files[file] = MAGICCData(join(MAGICC6_DIR, file))
In [19]:
sorted(all_run_files.keys())
Out[19]:
In [20]:
all_run_files["SRESB2.SCEN"].head()
Out[20]:
In [21]:
for g in all_run_files["SRESB2.SCEN"].groupby("variable"):
plt.figure(figsize=(12, 7.5))
g.lineplot(hue="region").set_title(g.get_unique_meta("variable", True))
In [22]:
print(MAGICCData.write.__doc__)
In [23]:
mdata = MAGICCData(join(MAGICC6_DIR, "HISTRCP_CO2I_EMIS.IN"))
mdata.write("HISTEXAMPLE_CO2I_EMIS.IN", magicc_version=7)
mdata = MAGICCData(join(MAGICC6_DIR, "RCP45.SCEN"))
mdata.write("SCENEXAMPLE.SCEN", magicc_version=7)
However, note that the format to write in is determined by the filename. Hence you can't just use any filename, it has to follow MAGICC's internal conventions, see the docs. Our error messaging will hopefully make this slightly clearer.
In [24]:
%%expect_exception NoReaderWriterError
mdata.write("histexample.txt", magicc_version=7)
In [ ]: