%matplotlib inline

import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = 16, 9

import pyhector
from pyhector import rcp26, rcp45, rcp60, rcp85

pyhector.__version__

"""
Explanation: Pyhector Usage Examples
End of explanation
"""


rcp26.columns

rcp26.head()

"""
Explanation: Scenarios
The four RCP scenarios are already preloaded as DataFrames in pyhector. 
They contain the following emissions:
End of explanation
"""


pyhector.units

"""
Explanation: The units can also be looked up.
End of explanation
"""


emissions = ["ffi_emissions", "luc_emissions", "CH4_emissions", "N2O_emissions"]
rcp26[emissions].loc[:2150].plot(subplots=True);
plt.suptitle("RCP 2.6");

"""
Explanation: A plot of four categories in RCP 2.6.
End of explanation
"""


%time results = pyhector.run(rcp26)

"""
Explanation: Running Hector
A single Hector run doesn't take much time and returns a DataFrame with results as well the used parameters:
End of explanation
"""


pyhector._default_config

"""
Explanation: By default pyhector uses the default parameters from the INI files for the RCP runs in the Hector repository.
End of explanation
"""


rcps = [rcp26, rcp45, rcp60, rcp85]

# For Hector output variables we need to address them as 
# 'component.variable', to simplify this we define short cuts
SURFACE_TEMP = "temperature.Tgav"
CONCENTRATION_CO2 = "simpleNbox.Ca"
FORCING = "forcing.Ftot"

"""
Explanation: RCP comparisons
End of explanation
"""


temp = results[SURFACE_TEMP].loc[1850:2100] - results[SURFACE_TEMP].loc[1850:1900].mean()
temp.plot()
plt.title("RCP2.6: Global Mean Temperature Projection")
plt.ylabel("Deg. C over pre-industrial (1850-1900 mean)");

results[FORCING].loc[1850:2100].plot()
plt.title("RCP2.6: " + pyhector.output[FORCING]["description"])
plt.ylabel(pyhector.output[FORCING]["unit"]);

results[CONCENTRATION_CO2].loc[1850:2100].plot()
plt.title("RCP2.6: " + pyhector.output[CONCENTRATION_CO2]["description"])
plt.ylabel(pyhector.output[CONCENTRATION_CO2]["unit"]);

"""
Explanation: Let's plot the default output variables for the four RCPs:
End of explanation
"""


for rcp in rcps:
    output = pyhector.run(rcp, {"core": {"endDate": 2100}})
    temp = output[SURFACE_TEMP]
    temp = temp.loc[1850:] - temp.loc[1850:1900].mean()
    temp.plot(label=rcp.name.split("_")[0])
plt.title("Global mean temperature")
plt.ylabel("Deg. C over pre-industrial (1850-1900 mean)")
plt.legend(loc="best");

"""
Explanation: A comparison of global mean temperature for the four RCPs.
End of explanation
"""


low = pyhector.run(rcp45, {"temperature": {"S": 1.5}})
default = pyhector.run(rcp45, {"temperature": {"S": 3}})
high = pyhector.run(rcp45, {"temperature": {"S": 4.5}})

sel = slice(1850, 2100)
plt.fill_between(low[SURFACE_TEMP].loc[sel].index,
                 low[SURFACE_TEMP].loc[sel],
                 high[SURFACE_TEMP].loc[sel],
                 color="lightgray")
default[SURFACE_TEMP].loc[sel].plot()
plt.title("RCP 4.5 with equilibrium climate sensitivity set to 1.5, 3, and 4.5")
plt.ylabel("Deg. C");

"""
Explanation: Setting parameters
Let's change an input parameter and, as an example, change the equilibrium climate sensitivity.
End of explanation
"""


