Introducing the Column Model

About this exercise:

There are two goals for this (hopefully straightforward) exercise:

  1. To introduce you to working with files and modules in Python
  2. To introduce the column radiation model

We will be working with this same model code for the next few weeks.

Your job is to go through the whole exercise. There are 3 questions clearly marked below for you to answer. Hand in ONLY your answers to those questions.

Please DO NOT just copy and paste the examples from here. Type them in yourself! This is MUCH better way to learn what you are doing.

Answers are due on Tuesday February 25, 2014

About Python modules:

Every time we use the import statement in Python, the interpreter loads instructions from a file, usually with the extension .py at the end of the file name.

For example if we typed 'import foo' the interpreter would try to open and read a file called 'foo.py'

This file would contain the same types of Python code that you have been entering manually on the command line, e.g. definitions of variables and functions.

The real beauty of the import statement is that it makes the Python language very modular. With one 'import' we can have access to all kinds of useful code.

To then access variables and functions in a module, we usually type the name of the module, followed by period (.), followed by the variable or function name. You've already seen a bit of this with the netCDF4 module you used in the last homework. You'll practice these skills again here.

Obtaining the code files for the Column Model

You will need to download two .py files from the class website. 'ColumnModel.py' has the actual model code, and 'ClimateUtils.py' has a bunch of useful constants and functions that we will use repeatedly throughout the semester. Find links to the code on the class web page: http://www.atmos.albany.edu/facstaff/brose/classes/ENV480_Spring2014/styled-5/index.html

You need to choose a directory on your computer to work in (or create a new one). Save both .py files to this directory.

Then, open up Canopy and try this:

In [1]:
import ColumnModel

What happened? Did you get an error message such as this:

--------------------------------------------------------------------------- ImportError Traceback (most recent call last) in () ----> 1 import ColumnModel ImportError: No module named ColumnModel

If so, the reason is that the Python interpreter is looking for a file called 'ColumnModel.py' but can't find it!

This is because we haven't told Python where to look for it. The easiest thing to do is set Python's current working directory to the same directory where you saved the code files.

Find the little arrow on the right-hand side of the Canopy window, just above your interpreter box. Clicking on this arrow should make a window pop up allowing you to "Change Working Directory..."

Choose the directory where the code files are sitting, and try the import statement again. No error message = a successful import!

Creating a new instance of the Column Model

To begin working with the model, we just need to tell Python to create a new object called a 'column' that is defined in the file ColumnModel.py

Try this:

In [2]:
mycolumn = ColumnModel.column( params = {'num_levels':2} )

You have just created a new column object. What is that? Let's take a look:

In [3]:
print mycolumn
Instance of column class with surface temperature 288.0 K, 2 atmospheric levels, and parameters: 
{'abs_coeff': 0.0001229, 'num_levels': 2, 'Q': 341.3, 'adj_lapse_rate': None, 'timestep': 86400.0, 'water_depth': 1.0, 'albedo': 0.299}

Try just typing mycolumn. and then hitting the tab key. You should see a list pop up. Everything on that list is the name of some object that is contained within mycolumn.

"Object" is a very general concept. Everything in Python that has a name is an object. This includes variables (numbers, character strings, etc.), but also functions.

Our object called mycolumn contains a bunch of data, and it also contains some functions that operate on those data.

Try this:

In [4]:
print mycolumn.Ts
print mycolumn.Tatm
288.0
[ 278.  200.]

We will use the column object to model temperature in a column. What you just did above was to look at the temperatures currently stored in mycolumn, for the surface (a single number), and the atmosphere.

When you created mycolumn a few lines ago, you specifically asked for a column with 2 levels in the atmosphere.

We can see where those levels are in the atmosphere as follows:

In [5]:
print mycolumn.p
[ 750.  250.]

which is actually the pressure (in mb or hPa) at which we are specifying the temperature -- in this case essentially the lower and upper troposphere.

So what does this code actually do?

It calculates the warming and cooling of the air and the surface based on the grey radition approximation that we have looked at in class.

I encourage you to look through the code. Try typing this:

edit ColumnModel

You should find that the code is now displayed in your editor window. Have a look, but obviously don't fret about not understanding it at this point.

Notice, though, that there are lots of comments sprinkled throughout the code. Comments in Python begin with # and are just words to help us understand the code. Comments are never executed by the Python interpreter.

Convince yourself of this by typing something like this:

In [6]:
# this is not valid Python code

You might want to try typing the same thing without the # in front.

In [7]:
this is not valid Python code
  File "<ipython-input-7-146db698be12>", line 1
    this is not valid Python code
                           ^
SyntaxError: invalid syntax

OK but what does this code actually do?

It calculates the warming and cooling of the air and the surface based on the grey radition approximation that we have looked at in class.

Try this:

In [8]:
mycolumn.Longwave_Heating()
print mycolumn.LW_absorbed_sfc
print mycolumn.LW_absorbed_atm
-205.778892273
[-116.44619732   87.88100973]

What you just did was to call a function Longwave_Heating() that calculates how much longwave radiation is emitted up and down from each layer, and how much is absorbed by each layer (and the surface).

You then printed out some quantities that were calculated and stored by the function, which are actually the heating rates at each level in W / m\(^2\)

Now try this:

In [9]:
mycolumn.Shortwave_Heating()
print mycolumn.SW_absorbed_sfc
print mycolumn.SW_absorbed_atm
239.2513
[ 0.  0.]

Hopefully this makes sense. Our code also calculates the energy absorbed due to shortwave (solar) radiation. The atmosphere is transparent to solar radiation in this model, so the absorption all occurs at the surface.

Look in the code file to find where the function Shortwave_Heating() is defined. It's really just calculating \((1-\alpha) Q\) based on some default parameter values for \(alpha\) and \(Q\). We'll think about changing the defaults later.

Energy (im)balance in the column

To get the total energy sources and sinks at each point, we just need to add up the shortwave and longwave terms:

In [10]:
print mycolumn.LW_absorbed_sfc +  mycolumn.SW_absorbed_sfc
print mycolumn.LW_absorbed_atm +  mycolumn.SW_absorbed_atm
33.472407727
[-116.44619732   87.88100973]

Evidently this column is NOT in energy balance! The surface is gaining energy at a rate 33 W / m\(^2\), the lower atmosphere is losing energy at 116 W / m\(^2\), and the upper atmosphere is gaining nearly 90 W / m\(^2\).

OK so what?

The code will not just calculate the energy imbalance, but also change the temperatures in response to the imbalance. It does this by time-stepping, just like we did with the zero-dimensional model in the first homework.

In [11]:
mycolumn.Step_Forward()
print mycolumn.Ts
print mycolumn.Tatm
288.691654755
[ 276.03590988  201.48228304]

Here you just called a function Step_Forward() that computes the energy imbalance as you just did above, and then uses that imbalance to adjust the temperatures.

HOMEWORK QUESTION 1

Have the temperatures gone up or down at the surface and at each level from where they started? Why?

(This is an easy question, not a trick question)

Timestepping to equilibrium

Just like we did with the zero-dimensional model in the first homework, we will use loops to time-step the model towards equilibrium. Try this:

In [12]:
for n in range(10):
    mycolumn.Step_Forward()
    print mycolumn.Ts
289.226209011
289.631495465
289.930790219
290.143524496
290.285892321
290.37136734
290.411140861
290.41449201
290.389099741
290.341305316

This little loop just repeated the call to Step_Forward 10 times, and printed out the surface temperature after each time step.

Notice that the temperature is changing each time. That means we are not at equilibrium. Try it again!

In [13]:
for n in range(10):
    mycolumn.Step_Forward()
    print mycolumn.Ts
290.276332746
290.19847371
290.111242519
290.017505877
289.9195915
289.819378974
289.718375753
289.617780704
289.518537221
289.421377618

Still changing, but not by as much.

Here's a trick:

In [14]:
mycolumn.Step_Forward(100)
print mycolumn.Ts
287.801762862

What you just did was to loop through the time-stepping procedure 100 times!

Look at the code and find the function Step_Forward(). Do you see how the code creates a loop?

In this case the function Step_Forward() takes an optional input argument which is the number of iterations through the loop. This number defaults to 1 if we don't specify it, which is what happened above!

Has the model reached equilibrium yet? We can always keep on time-stepping and see if anything changes:

In [15]:
mycolumn.Step_Forward(100)
print mycolumn.Ts
287.844448589

The changes are now minimal, and it is close to equilibrium.

Let's look at the whole column temperature:

In [16]:
print mycolumn.Ts
print mycolumn.Tatm
287.844448589
[ 252.94352146  229.42722634]

HOMEWORK QUESTION 2

Compare the temperatures you found here (after time-stepping to equilibrium) with the radiative equilibrium temperatures we derived in class for this same model. Do they agree?

Greenhouse warming in the 2-layer model

Now that our column is in equilibrium, let's look at the Outgoing Longwave Radiation. The model keeps track of this for us:

In [17]:
print mycolumn.OLR
239.21426676

This should hopefully be almost exactly equal to the shortwave absorption:

In [18]:
print mycolumn.SW_absorbed_sfc
239.2513

Now you are going to do a "global warming" experiment, like we started in class.

The following will increase the emissivity / absorptivity of each layer by 10%, which is analagous to an increase in greenhouse gases in the atmosphere:

In [19]:
mycolumn.eps = mycolumn.eps * 1.1

Let's now re-calculate the longwave radiation with this new value of eps:

In [20]:
mycolumn.Longwave_Heating()

HOMEWORK QUESTION 3

Find the new value of OLR after this change. Is it larger or smaller than it was before we added greenhouse gases? What do you think should happen to the surface temperature as a result? Why?