A201

Practice Practical Test

Global Temperature Data

Download and save in the directory you will use for your program the file temp_data.txt containing global mean annual temperature data, in degrees Celcius, for 1850 through 2006 (obtained from http://umassk12.net/~warming). Observe that each line contains a four digit year followed by a Celcius temperature number.

This exercise is to write Python code that helps to see trends in these numbers.

The following code skeleton is one approach to this problem. It reduces the amount of data by averaging the numbers for each decade. (There are only seven numbers for the last decade because the last year is 2006.) It breaks the solution into several functions that can be written and tested separately, and most of them are general enough that they could be of use to process other data.

If you get stuck in completing this code you may ask for help from your lab instructor. You may also use a simpler approach to summarizing this data if you are unable to complete this code, and you may use a more challenging approach if you complete this one with time to spare.

When your lab instructor indicates, submit your work via Oncourse. (Submissions should reflect reasonable effort, but will only be used for attendence purposes.)

# global_temps.py, by chaynes@indiana.edu

import fpformat

def fahrenheit(celcius):
    """Convert Celcius temperature to Fahrenheit using the formula
    f = 9/5 c + 32.

    >>> fahrenheit(2)
    35.600000000000001
    >>>
    """
    #.

def fahrenheit_temperature_list(file_name):
    """Read the indicated file, containing a Celcius temperature number
    from the fifth column to the end of each line. Return a list of these
    numbers, converted to degrees Fahrenheit, in the order of the lines.

    >>> print file('test_data.txt', 'r').read()
    1850 -0.439
    1851 -0.306
    1852 -0.309
    <BLANKLINE>
    >>> fahrenheit_temperature_list('test_data.txt')
    [31.209800000000001, 31.449200000000001, 31.4438]
    >>>
    """
    #.
    #.
    #.
    #.
    #.
    #.
    #.
    #.

def group(lst, n):
    """Return a list of lists containing the values in list lst, with n values
    per sublist (except perhaps the last). Maintain the order of the values in
    lst. If the length of lst is not evenly divisible by n, the last sublist will
    have less than n.

    >>> group([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)
    [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10]]
    >>>
    """
    # Hint: use a loop to accumulate slices. Though it is an error to do simple
    # indexing with an index that is too large, if the stop index of a slicing
    # is greater than the length of the sequence, the slice just extends to the
    # end of the sequence.
    #.
    #.
    #.
    #.
    #.

def average_groups(groups):
    """Given a list of lists of numbers, return a list of the averages of the
    sublists.

    >>> average_groups([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10]])
    [1.0, 4.0, 7.0, 9.5]
    >>>
    """
    # Hint: the built-in function sum takes a list of numbers and returns their sum.
    #.
    #.
    #.
    #.
    #.

def graph(lon, offset, multiplier):
    """Print a star graph of the numbers in lon. There is one line per number.
    Each line begins with the number printed right-justified in 6 columns with
    one digit after the decimal point. After the number and a space, a number
    of stars are printed. The number of stars is obtained by subtracting the
    given offset, multiplying the result by the given multiplier, and rounding
    the result.

    >>> graph([5.4, 5.6, 5.1], 5, 10)
       5.4 ****
       5.6 ******
       5.1 *
    >>>
    """
    # Hint: recall the built-in round and int functions, string replication
    # operator, and the function fpformat.fix(value, precision).
    #.
    #.
    #.
    #.
    #.

def main():
    temps = fahrenheit_temperature_list('temp_data.txt')
    decade_groups = group(temps, 10)
    average_temps = average_groups(decade_groups)
    graph(average_temps, 31, 10)

main()