<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 ATM 623
Step1:  Contents
Introducing climlab
Using climlab to implement the zero-dimensional energy balance model
Run the zero-dimensional EBM out to equilibrium
A climate change scenario in the EBM
Further climlab resources
<a id='section1'></a>
1. Introducing climlab
climlab is a python package for process-oriented climate modeling.
It is based on a very general concept of a model as a collection of individual, 
interacting processes. climlab defines a base class called Process, which
can contain an arbitrarily complex tree of sub-processes (each also some 
sub-class of Process). Every climate process (radiative, dynamical, 
physical, turbulent, convective, chemical, etc.) can be simulated as a stand-alone
process model given appropriate input, or as a sub-process of a more complex model. 
New classes of model can easily be defined and run interactively by putting together an
appropriate collection of sub-processes.
climlab is an open-source community project. The latest code can always be found on github
Step2:  <a id='section2'></a>
2. Using climlab to implement the zero-dimensional energy balance model
Recall that we have worked with a zero-dimensional Energy Balance Model
$$ C \frac{dT_s}{dt} = (1-\alpha) Q - \tau \sigma T_s^4 $$ 
Here we are going to implement this exact model using climlab.
Yes, we have already written code to implement this model, but we are going to repeat this effort here as a way of learning how to use climlab.
There are tools within climlab to implement much more complicated models, but the basic interface will be the same.
Step3:  Here we have created a dictionary called state with a single item called Ts
Step4:  This dictionary holds the state variables for our model -- which is this case is a single number!  It is a temperature in degrees Celsius.
For convenience, we can access the same data as an attribute (which lets us use tab-autocomplete when doing interactive work)
Step5:  It is also possible to see this state dictionary as an xarray.Dataset object
Step6:  The object called ebm here is the entire model -- including its current state (the temperature Ts) as well as all the methods needed to integrated forward in time!
The current model state, accessed two ways
Step7:  Here is some internal information about the timestep of the model
Step8:  This says the timestep is 2592000 seconds (30 days!), and the model has taken 0 steps forward so far.
To take a single step forward
Step9:  The model got colder!
To see why, let's look at some useful diagnostics computed by this model
Step10:  This is another dictionary, now with two items. They should make sense to you.
Just like the state variables, we can access these diagnostics variables as attributes
Step11:  So why did the model get colder in the first timestep?
What do you think will happen next?
<a id='section3'></a>
3. Run the zero-dimensional EBM out to equilibrium
Let's look at how the model adjusts toward its equilibrium temperature.
Exercise
Step12:  The parameter tau is a property of the OutgoingLongwave subprocess
Step13:  and the parameter albedo is a property of the AbsorbedShortwave subprocess
Step14:  Let's make an exact clone of our model and then change these two parameters
Step15:  Now our model is out of equilibrium and the climate will change!
To see this without actually taking a step forward
Step16:  Shoud the model warm up or cool down?
Well, we can find out
Step17:  Automatic timestepping
Often we want to integrate a model forward in time to equilibrium without needing to store information about the transient state.
climlab offers convenience methods to do this easily
Step18:  <a id='section5'></a>
5. Further climlab resources
We will be using climlab extensively throughout this course. Lots of examples of more advanced usage are found here in the course notes. Here are some links to other resources
<ASSISTANT_TASK:>
Python Code:
#  Ensure compatibility with Python 2 and 3
from __future__ import print_function, division
Explanation: ATM 623: Climate Modeling
Brian E. J. Rose, University at Albany
Lecture 4: Building simple climate models using climlab
Warning: content out of date and not maintained
You really should be looking at The Climate Laboratory book by Brian Rose, where all the same content (and more!) is kept up to date.
Here you are likely to find broken links and broken code.
About these notes:
This document uses the interactive Jupyter notebook format. The notes can be accessed in several different ways:
The interactive notebooks are hosted on github at https://github.com/brian-rose/ClimateModeling_courseware
The latest versions can be viewed as static web pages rendered on nbviewer
A complete snapshot of the notes as of May 2017 (end of spring semester) are available on Brian's website.
Also here is a legacy version from 2015.
Many of these notes make use of the climlab package, available at https://github.com/brian-rose/climlab
End of explanation
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import climlab
Explanation: Contents
Introducing climlab
Using climlab to implement the zero-dimensional energy balance model
Run the zero-dimensional EBM out to equilibrium
A climate change scenario in the EBM
Further climlab resources
<a id='section1'></a>
1. Introducing climlab
climlab is a python package for process-oriented climate modeling.
It is based on a very general concept of a model as a collection of individual, 
interacting processes. climlab defines a base class called Process, which
can contain an arbitrarily complex tree of sub-processes (each also some 
sub-class of Process). Every climate process (radiative, dynamical, 
physical, turbulent, convective, chemical, etc.) can be simulated as a stand-alone
process model given appropriate input, or as a sub-process of a more complex model. 
New classes of model can easily be defined and run interactively by putting together an
appropriate collection of sub-processes.
climlab is an open-source community project. The latest code can always be found on github:
https://github.com/brian-rose/climlab
You can install climlab by doing
conda install -c conda-forge climlab
End of explanation
# create a zero-dimensional domain with a single surface temperature
state = climlab.surface_state(num_lat=1,  # a single point
                              water_depth = 100.,  # 100 meters slab of water (sets the heat capacity)
                             )
state
Explanation: <a id='section2'></a>
2. Using climlab to implement the zero-dimensional energy balance model
Recall that we have worked with a zero-dimensional Energy Balance Model
$$ C \frac{dT_s}{dt} = (1-\alpha) Q - \tau \sigma T_s^4 $$ 
Here we are going to implement this exact model using climlab.
Yes, we have already written code to implement this model, but we are going to repeat this effort here as a way of learning how to use climlab.
There are tools within climlab to implement much more complicated models, but the basic interface will be the same.
End of explanation
state['Ts']
Explanation: Here we have created a dictionary called state with a single item called Ts:
End of explanation
state.Ts
Explanation: This dictionary holds the state variables for our model -- which is this case is a single number!  It is a temperature in degrees Celsius.
For convenience, we can access the same data as an attribute (which lets us use tab-autocomplete when doing interactive work):
End of explanation
climlab.to_xarray(state)
#  create the longwave radiation process
olr = climlab.radiation.Boltzmann(name='OutgoingLongwave',
                                  state=state, 
                                  tau = 0.612,
                                  eps = 1.,
                                  timestep = 60*60*24*30.)
#  Look at what we just created
print(olr)
#  create the shortwave radiation process
asr = climlab.radiation.SimpleAbsorbedShortwave(name='AbsorbedShortwave',
                                                state=state, 
                                                insolation=341.3, 
                                                albedo=0.299,
                                                timestep = 60*60*24*30.)
#  Look at what we just created
print(asr)
#  couple them together into a single model
ebm = olr + asr
#  Give the parent process name
ebm.name = 'EnergyBalanceModel'
#  Examine the model object
print(ebm)
Explanation: It is also possible to see this state dictionary as an xarray.Dataset object:
End of explanation
ebm.state
ebm.Ts
Explanation: The object called ebm here is the entire model -- including its current state (the temperature Ts) as well as all the methods needed to integrated forward in time!
The current model state, accessed two ways:
End of explanation
print(ebm.time['timestep'])
print(ebm.time['steps'])
Explanation: Here is some internal information about the timestep of the model:
End of explanation
ebm.step_forward()
ebm.Ts
Explanation: This says the timestep is 2592000 seconds (30 days!), and the model has taken 0 steps forward so far.
To take a single step forward:
End of explanation
ebm.diagnostics
Explanation: The model got colder!
To see why, let's look at some useful diagnostics computed by this model:
End of explanation
ebm.OLR
ebm.ASR
Explanation: This is another dictionary, now with two items. They should make sense to you.
Just like the state variables, we can access these diagnostics variables as attributes:
End of explanation
for name, process in ebm.subprocess.items():
    print(name)
    print(process)
Explanation: So why did the model get colder in the first timestep?
What do you think will happen next?
<a id='section3'></a>
3. Run the zero-dimensional EBM out to equilibrium
Let's look at how the model adjusts toward its equilibrium temperature.
Exercise:
Using a for loop, take 500 steps forward with this model
Store the current temperature at each step in an array
Make a graph of the temperature as a function of time
<a id='section4'></a>
4. A climate change scenario
Suppose we want to investigate the effects of a small decrease in the transmissitivity of the atmosphere tau. 
Previously we used the zero-dimensional model to investigate a hypothetical climate change scenario in which:
- the transmissitivity of the atmosphere tau decreases to 0.57
- the planetary albedo increases to 0.32
How would we do that using climlab?
Recall that the model is comprised of two sub-components:
End of explanation
ebm.subprocess['OutgoingLongwave'].tau
Explanation: The parameter tau is a property of the OutgoingLongwave subprocess:
End of explanation
ebm.subprocess['AbsorbedShortwave'].albedo
Explanation: and the parameter albedo is a property of the AbsorbedShortwave subprocess:
End of explanation
ebm2 = climlab.process_like(ebm)
print(ebm2)
ebm2.subprocess['OutgoingLongwave'].tau = 0.57
ebm2.subprocess['AbsorbedShortwave'].albedo = 0.32
Explanation: Let's make an exact clone of our model and then change these two parameters:
End of explanation
#  Computes diagnostics based on current state but does not change the state
ebm2.compute_diagnostics()
ebm2.ASR - ebm2.OLR
Explanation: Now our model is out of equilibrium and the climate will change!
To see this without actually taking a step forward:
End of explanation
ebm2.Ts
ebm2.step_forward()
ebm2.Ts
Explanation: Shoud the model warm up or cool down?
Well, we can find out:
End of explanation
ebm3 = climlab.process_like(ebm2)
ebm3.integrate_years(50)
#  What is the current temperature?
ebm3.Ts
#  How close are we to energy balance?
ebm3.ASR - ebm3.OLR
#  We should be able to accomplish the exact same thing with explicit timestepping
for n in range(608):
    ebm2.step_forward()
ebm2.Ts
ebm2.ASR - ebm2.OLR
Explanation: Automatic timestepping
Often we want to integrate a model forward in time to equilibrium without needing to store information about the transient state.
climlab offers convenience methods to do this easily:
End of explanation
%load_ext version_information
%version_information numpy, matplotlib, climlab
Explanation: <a id='section5'></a>
5. Further climlab resources
We will be using climlab extensively throughout this course. Lots of examples of more advanced usage are found here in the course notes. Here are some links to other resources:
The documentation is hosted at https://climlab.readthedocs.io/en/latest/
Source code (for both software and docs) are at https://github.com/brian-rose/climlab
A video of a talk I gave about climlab at the 2018 AMS Python symposium (January 2018)
Slides from a talk and demonstration that I gave in Febrary 2018 (The Apple Keynote version contains some animations that will not show up in the pdf version)
Version information
End of explanation
<END_TASK>