Program 4 - Temperature Anomoly



Do this assignment individually. We will be handing in the first part of the program - as a "checkpoint" - on 2/7 and the second part on 2/14.

In this program, we'll look at some climate change data from the National Oceanic and Atmospheric Administration (the US government agency in charge of weather and climate data, among other things). In particular, we'll look at one of the "headline" datasets, global temperature anomoly over the last century. By "anomoly" they just mean the difference between the annual average temperature and an single baseline temperature. In this case they use the average from 1900-2000 as a baseline (although the data goes from 1880-2012).

They use an average as the baseline, rather than a single year, because the temperature varies from year to year. If you average over a number of years these variations smooth out. If you average over a century, the result is just one number and you can't see any trend at all.

Our program will caluclate a moving average of the temperature anomoly data. We'll let the user enter an integer k. Then for each year we calculate the average of the k years before, the year itself, and the k years after. that year. For instance, if the user picked k=3, for the year 1936 we would average the temperatures for 1933, 1934, 1935, 1936, 1937, 1938 and 1939. Remember that to average seven numbers, we add them up and divide the sum by seven.

Our program will let the user experiment with averaging over different time intervals, to get a nice balance between smoothing out annual variation and losing the trend entirely.

Requirements

Download the temperature data at the bottom of this page. Using Internet Explorer on Windows, right-click on the link and "save as.." temp.txt on your Desktop or in the folder where you want to work. You should be able to look at the data in Wordpad, in Windows, or TextEdit on the Mac (it looks messed up in Notepad, that's OK).

Your program should ask the user for the number of years over which to average, and either exit or keep asking if it does not get an integer between 0 and 60.

The output of your program should be a file called movingAvg.csv. The ending .csv means that unlike the input file, the output data is separated by commas. This allows programs like Microsoft Excel, or Google Docs, or the ManyEyes Website from IBM, to read the data and make a chart. You are required to hand in a chart (as a screenshot) as well as your program.

After you get your program running, try a few different choices of k, make some charts, and see which you think best presents the data.

Example output

Here is an example of the .csv file the program produces when the user chooses to average over 60 years; there are not too many rows since there are only 10 years in the middle of the century when there are 60 years before and 60 years after over which to average.

1940,-0.0349371900826
1941,-0.0291066115702
1942,-0.0232082644628
1943,-0.0171115702479
1944,-0.0109479338843
1945,-0.00365537190083
1946,0.00295371900826
1947,0.00933140495868
1948,0.0156661157025
1949,0.0219909090909
1950,0.0281867768595
1951,0.0351082644628
1952,0.0420537190083

On different machines the same program can produce slightly different floating point results. So if you get, for instance, "1940,-0.03317438016528924" as the first line, that is fine.

Step By Step

The overall structure of your program should be two loops: one that reads the input data, extracts the temperatures, and stores them in a list, and a second one that computes the moving averages for every item that is not too close to the ends of the list, and writes them to the output file.

This is a big enough program that you can't just type it in and hope it will work. We will break it down into two big steps, and submit the results of the first step on Tues 25 and the results of the second step on Thurs 2/7.

In the second week, we will do the averaging, write out the output file, and make a chart.

What to Submit

The first week, just submit your program (to "Program 4 checkpoint"). The second week, submit your program on SmartSite, and also a screenshot of the chart you made for your favorite value of k.