SlideShare a Scribd company logo
Files to Use
http://www.cse.msu.edu/~cse231/Projects/Project05/RedCedarRiver.txt
http://www.cse.msu.edu/~cse231/Projects/Project05/plot.py
Assignment Overview
This assignment focuses on the implementation of Python programs to read files and process
data by using lists and functions.
Assignment Background
U.S. Geological Survey (USGS) provides scientific information to understand Earth, manage
resources and minimize loss from natural disasters. You can download interesting data from the
website https://www.usgs.gov/
We have downloaded the flow rate data for the Red Cedar River in East Lansing starting from
1932 . Your task is to design and implement a Python program that draws two plots from the
data (and displays a table of data for each plot).
Here is the first line of the file. The numbers that we are interested are the last three numbers
which are year, month and flow rate in cubic feet per second (CFS). Notice that the year and
month are ints and the flow rate is float:
Assignment Specifications
1. The program must provide following functions to extract some statistics.
a) open_file()prompts the user to enter a file name. The program will try to open the data
file. Appropriate error message should be shown if the data file cannot be opened. This function
will loop until it receives proper input and successfully opens the file. It returns a file pointer.
b) read_file()calls the open_file() function and uses the returned file pointer to read the data file.
This function returns a list of your choosing containing data you need for other parts of this
project.
c) draw_plot( x, y, plt_title, x_label, y_label) provided by us takes two equal-length lists of
numbers and plots them. You need to pass the label strings and plot title
to the function. Include the range of years in the title (Hint: use string concatenation and check
sample output).
d) annual_average(L) takes a list as an argument and returns a list of tuples in the form (year,
average_flow).
e) month_average(L) takes a list as an argument and returns a list of tuples in the form (year,
month_flow).
f) You may use extra functions, if you wish.
2. The program should read the file only once.
3. The program should plot the average flow for each year. That is, find the average flow for
each year and then generate a plot with years on the x-axis and average flow for that year on the
y-axis. Then generate a table that has the year and average flow on each line. Put a title on the
table and label each column.
4. Next, the program should prompt the user for a number in the range 1-12 and should plot the
flow rates for only that month. For example if user enters 1, the program should extract the flow
rate of Jan 1932, Jan 1933, ... , Jan 2015 and plot them. (Note: do not loop and ask for more.)
5. The program should re-prompt the user if an invalid input (either wrong type or wrong value),
was entered for the month. If something other than an integer is input, your error message should
include the phrase “not an integer.” If an integer is input but it is not in the proper range, your
error message should include the phrase “integer out of range.” Note: use exceptions.
6. The month name should be displayed in the title of the second plot instead of the month
number. For example, in the sample below you see “May” in the title. (Hint: use the month
number as an index into a list of strings.) As with the previous plot, display a table of values that
you plotted.
Assignment Notes
1. Note that data are separated using spaces. You can use the list method .split() to split the line
into a list of data.
2. It is much easier to convert the input data to int and float when the program reads the file and
creates the list.
3. To create a list L of data begin with an empty list (e.g. L = [] before the loop begins) and
within the loop append to the list one item at a time, e.g. L.append(item). The data item you
append may be a collection such as a tuple or another list.
Sample Output
1932 192.10
1933 175.87
1934 68.55
1935 133.70
1936 107.92
1937 210.64
1938 203.00
1939 130.68
1940 155.16
1941 111.76
1942 222.91
1943 322.06
1944 168.40
1945 222.66
1946 132.44
1947 371.00
1948 332.60
1949 206.60
1950 345.97
1951 255.01
1952 216.94
1953 135.51
1954 237.37
1955 142.05
1956 288.82
1957 228.67
1958 102.73
1959 309.37
1960 221.05
1961 125.65
1962 146.39
1963 97.81
1964 48.00
1965 173.39
1966 155.56
1967 215.01
1968 290.97
1969 219.15
1970 198.05
1971 183.03
1972 231.14
1973 284.19
1974 297.25
1975 358.62
1976 281.77
1977 131.30
1978 143.01
1979 157.99
1980 213.15
1981 265.15
1982 246.95
1983 247.17
1984 171.79
1985 339.39
1986 307.77
1987 139.50
1988 191.94
1989 229.23
1990 282.57
1991 211.63
1992 351.48
1993 347.92
1994 397.64
1995 202.06
1996 194.83
1997 242.16
1998 247.35
1999 134.24
2000 174.79
2001 326.02
2002 179.25
2003 135.74
2004 239.83
2005 167.75
2006 271.75
2007 227.99
2008 332.66
2009 377.72
2010 189.64
2011 353.33
2012 145.84
2013 247.27
2014 264.20
2015 191.46
1932 362.40
1933 354.50
1934 65.40
1935 190.00
1936 103.60
1937 376.30
1938 214.60
1939 100.40
1940 94.50
1941 99.90
1942 99.30
1943 1008.00
1944 325.30
1945 772.40
1946 143.00
1947 619.50
1948 959.30
1949 150.20
1950 252.50
1951 307.90
1952 262.70
1953 253.70
1954 134.70
1955 94.60
1956 1310.00
1957 482.80
1958 78.30
1959 201.70
1960 293.00
1961 195.00
1962 164.50
1963 176.80
1964 121.60
1965 95.50
1966 284.40
1967 167.20
1968 211.40
1969 474.10
1970 261.20
1971 101.10
1972 234.10
1973 305.80
1974 507.10
1975 341.10
1976 401.70
1977 146.60
1978 206.30
1979 182.30
1980 246.20
1981 354.40
1982 143.60
1983 591.90
1984 306.20
1985 187.60
1986 186.50
1987 87.90
1988 131.20
1989 197.30
1990 348.40
1991 218.50
1992 249.80
1993 316.60
1994 296.00
1995 272.60
1996 403.40
1997 271.80
1998 436.80
1999 186.40
2000 354.90
2001 446.70
2002 329.50
2003 255.80
2004 692.80
2005 158.90
2006 481.40
2007 387.70
2008 141.30
2009 544.70
2010 485.00
2011 839.20
2012 273.10
2013 294.20
2014 576.70
2015 165.10
Solution
import pylab
from time import sleep
def draw_plot( x, y, plt_title, x_label, y_label):
''' Draw x vs. y (lists should have the same length)
Sets the title of plot and the labels of x and y axis '''
pylab.title(plt_title)
pylab.xlabel(x_label)
pylab.ylabel(y_label)
pylab.plot(x, y)
pylab.show()
def open_file():
try:
file_name = raw_input("Please enter a file name: ")
f = open(file_name, "r")
except:
open_file()
return f.tell(), file_name
def read_file():
ftell, file_name = open_file()
f = open(file_name)
f.seek(ftell)
results = f.readlines()
data = []
for line in results:
data_point = line.split()
year = data_point[4]
month = data_point[5]
flow = data_point[6]
data_point = [year, month, flow]
data.append(data_point)
return data
def annual_average(L):
annual_list = []
for i in range(1932, 2016):
total = 0
for j in range(0, 12):
total += float(L[i-1932][2])
entry = (i, total)
annual_list.append(entry)
return annual_list
def month_average(L, month):
month_list = []
j = month-1
for i in range(1932, 2016):
entry = (float(L[j][0]), float(L[j][2]))
j+=12
month_list.append(entry)
return month_list
data = read_file()
month = input("please enter a month: ")
annual_list = annual_average(data)
month_list = month_average(data, month)
draw_plot([annual_list[i][0] for i in range(0, 84)], [annual_list[i][1] for i in range(0, 84)],
"Annual Average Flow", "Year", "Flow")
draw_plot([month_list[i][0] for i in range(0, 84)], [month_list[i][1] for i in range(0, 84)],
"Month Flow for month "+str(month), "Year", "Flow")

More Related Content

Similar to Files to Use httpwww.cse.msu.edu~cse231ProjectsProject05Red.pdf

Application of Graph Theory in Computer science using Data Structure.pdf
Application of Graph Theory in Computer science using Data Structure.pdfApplication of Graph Theory in Computer science using Data Structure.pdf
Application of Graph Theory in Computer science using Data Structure.pdf
Nancy Ideker
 
Environmental Engineering Assignment Help
Environmental Engineering Assignment HelpEnvironmental Engineering Assignment Help
Environmental Engineering Assignment Help
Matlab Assignment Experts
 
Guidelines for writing Competition and individual size lab report .docx
Guidelines for writing Competition and individual size lab report .docxGuidelines for writing Competition and individual size lab report .docx
Guidelines for writing Competition and individual size lab report .docx
whittemorelucilla
 
Guidelines for writing thesis technical reports 2
Guidelines for writing thesis technical reports 2Guidelines for writing thesis technical reports 2
Guidelines for writing thesis technical reports 2
Bijendra Prajapati
 
Python Data Types.pdf
Python Data Types.pdfPython Data Types.pdf
Python Data Types.pdf
NehaSpillai1
 
Python Data Types (1).pdf
Python Data Types (1).pdfPython Data Types (1).pdf
Python Data Types (1).pdf
NehaSpillai1
 
Linked List
Linked ListLinked List
Linked List
BHARATH KUMAR
 
JGrass-NewAge LongWave radiation Balance
JGrass-NewAge LongWave radiation BalanceJGrass-NewAge LongWave radiation Balance
JGrass-NewAge LongWave radiation Balance
Marialaura Bancheri
 
Week-3 – System RSupplemental material1Recap •.docx
Week-3 – System RSupplemental material1Recap •.docxWeek-3 – System RSupplemental material1Recap •.docx
Week-3 – System RSupplemental material1Recap •.docx
helzerpatrina
 
qc-tools.ppt
qc-tools.pptqc-tools.ppt
qc-tools.ppt
Alpharoot
 
ejercicios de e
ejercicios de eejercicios de e
ejercicios de e
sergio fonseca
 
Links to Estimation Techniques Tim Shaughnessy, Chapter 7 .docx
Links to Estimation Techniques Tim Shaughnessy, Chapter 7 .docxLinks to Estimation Techniques Tim Shaughnessy, Chapter 7 .docx
Links to Estimation Techniques Tim Shaughnessy, Chapter 7 .docx
smile790243
 
Symbolic Computation and Automated Reasoning in Differential Geometry
Symbolic Computation and Automated Reasoning in Differential GeometrySymbolic Computation and Automated Reasoning in Differential Geometry
Symbolic Computation and Automated Reasoning in Differential GeometryM Reza Rahmati
 
Exercise Problems for Chapter 5Numerical example on page 203Pe.docx
Exercise Problems for Chapter 5Numerical example on page 203Pe.docxExercise Problems for Chapter 5Numerical example on page 203Pe.docx
Exercise Problems for Chapter 5Numerical example on page 203Pe.docx
gitagrimston
 
The 7 QC Tools - English (19 Pages).ppt
The 7 QC Tools - English (19 Pages).pptThe 7 QC Tools - English (19 Pages).ppt
The 7 QC Tools - English (19 Pages).ppt
AnkitSharma509599
 
beginners_python_cheat_sheet -python cheat sheet description
beginners_python_cheat_sheet -python cheat sheet descriptionbeginners_python_cheat_sheet -python cheat sheet description
beginners_python_cheat_sheet -python cheat sheet description
NaveenVarma Chintalapati
 
Chapter 16-spreadsheet1 questions and answer
Chapter 16-spreadsheet1  questions and answerChapter 16-spreadsheet1  questions and answer
Chapter 16-spreadsheet1 questions and answer
RaajTech
 
Gse 213 lesson 3
Gse 213 lesson 3Gse 213 lesson 3

Similar to Files to Use httpwww.cse.msu.edu~cse231ProjectsProject05Red.pdf (20)

Sortsearch
SortsearchSortsearch
Sortsearch
 
Application of Graph Theory in Computer science using Data Structure.pdf
Application of Graph Theory in Computer science using Data Structure.pdfApplication of Graph Theory in Computer science using Data Structure.pdf
Application of Graph Theory in Computer science using Data Structure.pdf
 
Environmental Engineering Assignment Help
Environmental Engineering Assignment HelpEnvironmental Engineering Assignment Help
Environmental Engineering Assignment Help
 
Guidelines for writing Competition and individual size lab report .docx
Guidelines for writing Competition and individual size lab report .docxGuidelines for writing Competition and individual size lab report .docx
Guidelines for writing Competition and individual size lab report .docx
 
Guidelines for writing thesis technical reports 2
Guidelines for writing thesis technical reports 2Guidelines for writing thesis technical reports 2
Guidelines for writing thesis technical reports 2
 
Python Data Types.pdf
Python Data Types.pdfPython Data Types.pdf
Python Data Types.pdf
 
Python Data Types (1).pdf
Python Data Types (1).pdfPython Data Types (1).pdf
Python Data Types (1).pdf
 
Linked List
Linked ListLinked List
Linked List
 
JGrass-NewAge LongWave radiation Balance
JGrass-NewAge LongWave radiation BalanceJGrass-NewAge LongWave radiation Balance
JGrass-NewAge LongWave radiation Balance
 
Week-3 – System RSupplemental material1Recap •.docx
Week-3 – System RSupplemental material1Recap •.docxWeek-3 – System RSupplemental material1Recap •.docx
Week-3 – System RSupplemental material1Recap •.docx
 
qc-tools.ppt
qc-tools.pptqc-tools.ppt
qc-tools.ppt
 
ejercicios de e
ejercicios de eejercicios de e
ejercicios de e
 
Links to Estimation Techniques Tim Shaughnessy, Chapter 7 .docx
Links to Estimation Techniques Tim Shaughnessy, Chapter 7 .docxLinks to Estimation Techniques Tim Shaughnessy, Chapter 7 .docx
Links to Estimation Techniques Tim Shaughnessy, Chapter 7 .docx
 
Lect11
Lect11Lect11
Lect11
 
Symbolic Computation and Automated Reasoning in Differential Geometry
Symbolic Computation and Automated Reasoning in Differential GeometrySymbolic Computation and Automated Reasoning in Differential Geometry
Symbolic Computation and Automated Reasoning in Differential Geometry
 
Exercise Problems for Chapter 5Numerical example on page 203Pe.docx
Exercise Problems for Chapter 5Numerical example on page 203Pe.docxExercise Problems for Chapter 5Numerical example on page 203Pe.docx
Exercise Problems for Chapter 5Numerical example on page 203Pe.docx
 
The 7 QC Tools - English (19 Pages).ppt
The 7 QC Tools - English (19 Pages).pptThe 7 QC Tools - English (19 Pages).ppt
The 7 QC Tools - English (19 Pages).ppt
 
beginners_python_cheat_sheet -python cheat sheet description
beginners_python_cheat_sheet -python cheat sheet descriptionbeginners_python_cheat_sheet -python cheat sheet description
beginners_python_cheat_sheet -python cheat sheet description
 
Chapter 16-spreadsheet1 questions and answer
Chapter 16-spreadsheet1  questions and answerChapter 16-spreadsheet1  questions and answer
Chapter 16-spreadsheet1 questions and answer
 
Gse 213 lesson 3
Gse 213 lesson 3Gse 213 lesson 3
Gse 213 lesson 3
 

More from mdameer02

II. Please provide complete correct detailed solution. Thank you. Co.pdf
II. Please provide complete correct detailed solution. Thank you. Co.pdfII. Please provide complete correct detailed solution. Thank you. Co.pdf
II. Please provide complete correct detailed solution. Thank you. Co.pdf
mdameer02
 
In a Windows 8 file system there is a file named direct.awk in a fol.pdf
In a Windows 8 file system there is a file named direct.awk in a fol.pdfIn a Windows 8 file system there is a file named direct.awk in a fol.pdf
In a Windows 8 file system there is a file named direct.awk in a fol.pdf
mdameer02
 
If using an ANOVA Single factor and the alpha is set at .05 and the.pdf
If using an ANOVA Single factor and the alpha is set at .05 and the.pdfIf using an ANOVA Single factor and the alpha is set at .05 and the.pdf
If using an ANOVA Single factor and the alpha is set at .05 and the.pdf
mdameer02
 
I need help please with these 2 assignments. of ul Uus Oll ook.pdf
I need help please with these 2 assignments. of ul Uus Oll ook.pdfI need help please with these 2 assignments. of ul Uus Oll ook.pdf
I need help please with these 2 assignments. of ul Uus Oll ook.pdf
mdameer02
 
How is water transported across the membrane What are the two types.pdf
How is water transported across the membrane  What are the two types.pdfHow is water transported across the membrane  What are the two types.pdf
How is water transported across the membrane What are the two types.pdf
mdameer02
 
Gradebook ignment FULL Question 1 Which of the following types of ent.pdf
Gradebook ignment FULL Question 1 Which of the following types of ent.pdfGradebook ignment FULL Question 1 Which of the following types of ent.pdf
Gradebook ignment FULL Question 1 Which of the following types of ent.pdf
mdameer02
 
Explain the DuPont identity. How is it used in finance Locate the f.pdf
Explain the DuPont identity. How is it used in finance Locate the f.pdfExplain the DuPont identity. How is it used in finance Locate the f.pdf
Explain the DuPont identity. How is it used in finance Locate the f.pdf
mdameer02
 
explain why the jovian planet atmospheres are called primitive atmos.pdf
explain why the jovian planet atmospheres are called primitive atmos.pdfexplain why the jovian planet atmospheres are called primitive atmos.pdf
explain why the jovian planet atmospheres are called primitive atmos.pdf
mdameer02
 
During digestion, in the intestinal lumen certain glycerols, FFA an.pdf
During digestion, in the intestinal lumen certain glycerols, FFA an.pdfDuring digestion, in the intestinal lumen certain glycerols, FFA an.pdf
During digestion, in the intestinal lumen certain glycerols, FFA an.pdf
mdameer02
 
Describe the structures and functions associated with the water-vascu.pdf
Describe the structures and functions associated with the water-vascu.pdfDescribe the structures and functions associated with the water-vascu.pdf
Describe the structures and functions associated with the water-vascu.pdf
mdameer02
 
Considering the atoms bonded and the shape of an H_2O molecule, is th.pdf
Considering the atoms bonded and the shape of an H_2O molecule, is th.pdfConsidering the atoms bonded and the shape of an H_2O molecule, is th.pdf
Considering the atoms bonded and the shape of an H_2O molecule, is th.pdf
mdameer02
 
describe the differences and similarities between viruses, worms, an.pdf
describe the differences and similarities between viruses, worms, an.pdfdescribe the differences and similarities between viruses, worms, an.pdf
describe the differences and similarities between viruses, worms, an.pdf
mdameer02
 
A window has the rectangle surmounted by a semi circle the total per.pdf
A window has the rectangle surmounted by a semi circle the total per.pdfA window has the rectangle surmounted by a semi circle the total per.pdf
A window has the rectangle surmounted by a semi circle the total per.pdf
mdameer02
 
Can you identify how energy flows through an ecosystemCan you i.pdf
Can you identify how energy flows through an ecosystemCan you i.pdfCan you identify how energy flows through an ecosystemCan you i.pdf
Can you identify how energy flows through an ecosystemCan you i.pdf
mdameer02
 
You must use the full name, no abbreviations. Name the dynamic found.pdf
You must use the full name, no abbreviations.  Name the dynamic found.pdfYou must use the full name, no abbreviations.  Name the dynamic found.pdf
You must use the full name, no abbreviations. Name the dynamic found.pdf
mdameer02
 
You are burning the latest song you bought on ITunes to a disk. The .pdf
You are burning the latest song you bought on ITunes to a disk. The .pdfYou are burning the latest song you bought on ITunes to a disk. The .pdf
You are burning the latest song you bought on ITunes to a disk. The .pdf
mdameer02
 
Write a scientific research paper that investigates the properties of.pdf
Write a scientific research paper that investigates the properties of.pdfWrite a scientific research paper that investigates the properties of.pdf
Write a scientific research paper that investigates the properties of.pdf
mdameer02
 
Write a function that multiplies each element in the array myA.pdf
Write a function that multiplies each element in the array myA.pdfWrite a function that multiplies each element in the array myA.pdf
Write a function that multiplies each element in the array myA.pdf
mdameer02
 
Which of the following is NOT a common feature of monosaccharidesA..pdf
Which of the following is NOT a common feature of monosaccharidesA..pdfWhich of the following is NOT a common feature of monosaccharidesA..pdf
Which of the following is NOT a common feature of monosaccharidesA..pdf
mdameer02
 
Who are the owners of a cor[poration,and how is their ownership repr.pdf
Who are the owners of a cor[poration,and how is their ownership repr.pdfWho are the owners of a cor[poration,and how is their ownership repr.pdf
Who are the owners of a cor[poration,and how is their ownership repr.pdf
mdameer02
 

More from mdameer02 (20)

II. Please provide complete correct detailed solution. Thank you. Co.pdf
II. Please provide complete correct detailed solution. Thank you. Co.pdfII. Please provide complete correct detailed solution. Thank you. Co.pdf
II. Please provide complete correct detailed solution. Thank you. Co.pdf
 
In a Windows 8 file system there is a file named direct.awk in a fol.pdf
In a Windows 8 file system there is a file named direct.awk in a fol.pdfIn a Windows 8 file system there is a file named direct.awk in a fol.pdf
In a Windows 8 file system there is a file named direct.awk in a fol.pdf
 
If using an ANOVA Single factor and the alpha is set at .05 and the.pdf
If using an ANOVA Single factor and the alpha is set at .05 and the.pdfIf using an ANOVA Single factor and the alpha is set at .05 and the.pdf
If using an ANOVA Single factor and the alpha is set at .05 and the.pdf
 
I need help please with these 2 assignments. of ul Uus Oll ook.pdf
I need help please with these 2 assignments. of ul Uus Oll ook.pdfI need help please with these 2 assignments. of ul Uus Oll ook.pdf
I need help please with these 2 assignments. of ul Uus Oll ook.pdf
 
How is water transported across the membrane What are the two types.pdf
How is water transported across the membrane  What are the two types.pdfHow is water transported across the membrane  What are the two types.pdf
How is water transported across the membrane What are the two types.pdf
 
Gradebook ignment FULL Question 1 Which of the following types of ent.pdf
Gradebook ignment FULL Question 1 Which of the following types of ent.pdfGradebook ignment FULL Question 1 Which of the following types of ent.pdf
Gradebook ignment FULL Question 1 Which of the following types of ent.pdf
 
Explain the DuPont identity. How is it used in finance Locate the f.pdf
Explain the DuPont identity. How is it used in finance Locate the f.pdfExplain the DuPont identity. How is it used in finance Locate the f.pdf
Explain the DuPont identity. How is it used in finance Locate the f.pdf
 
explain why the jovian planet atmospheres are called primitive atmos.pdf
explain why the jovian planet atmospheres are called primitive atmos.pdfexplain why the jovian planet atmospheres are called primitive atmos.pdf
explain why the jovian planet atmospheres are called primitive atmos.pdf
 
During digestion, in the intestinal lumen certain glycerols, FFA an.pdf
During digestion, in the intestinal lumen certain glycerols, FFA an.pdfDuring digestion, in the intestinal lumen certain glycerols, FFA an.pdf
During digestion, in the intestinal lumen certain glycerols, FFA an.pdf
 
Describe the structures and functions associated with the water-vascu.pdf
Describe the structures and functions associated with the water-vascu.pdfDescribe the structures and functions associated with the water-vascu.pdf
Describe the structures and functions associated with the water-vascu.pdf
 
Considering the atoms bonded and the shape of an H_2O molecule, is th.pdf
Considering the atoms bonded and the shape of an H_2O molecule, is th.pdfConsidering the atoms bonded and the shape of an H_2O molecule, is th.pdf
Considering the atoms bonded and the shape of an H_2O molecule, is th.pdf
 
describe the differences and similarities between viruses, worms, an.pdf
describe the differences and similarities between viruses, worms, an.pdfdescribe the differences and similarities between viruses, worms, an.pdf
describe the differences and similarities between viruses, worms, an.pdf
 
A window has the rectangle surmounted by a semi circle the total per.pdf
A window has the rectangle surmounted by a semi circle the total per.pdfA window has the rectangle surmounted by a semi circle the total per.pdf
A window has the rectangle surmounted by a semi circle the total per.pdf
 
Can you identify how energy flows through an ecosystemCan you i.pdf
Can you identify how energy flows through an ecosystemCan you i.pdfCan you identify how energy flows through an ecosystemCan you i.pdf
Can you identify how energy flows through an ecosystemCan you i.pdf
 
You must use the full name, no abbreviations. Name the dynamic found.pdf
You must use the full name, no abbreviations.  Name the dynamic found.pdfYou must use the full name, no abbreviations.  Name the dynamic found.pdf
You must use the full name, no abbreviations. Name the dynamic found.pdf
 
You are burning the latest song you bought on ITunes to a disk. The .pdf
You are burning the latest song you bought on ITunes to a disk. The .pdfYou are burning the latest song you bought on ITunes to a disk. The .pdf
You are burning the latest song you bought on ITunes to a disk. The .pdf
 
Write a scientific research paper that investigates the properties of.pdf
Write a scientific research paper that investigates the properties of.pdfWrite a scientific research paper that investigates the properties of.pdf
Write a scientific research paper that investigates the properties of.pdf
 
Write a function that multiplies each element in the array myA.pdf
Write a function that multiplies each element in the array myA.pdfWrite a function that multiplies each element in the array myA.pdf
Write a function that multiplies each element in the array myA.pdf
 
Which of the following is NOT a common feature of monosaccharidesA..pdf
Which of the following is NOT a common feature of monosaccharidesA..pdfWhich of the following is NOT a common feature of monosaccharidesA..pdf
Which of the following is NOT a common feature of monosaccharidesA..pdf
 
Who are the owners of a cor[poration,and how is their ownership repr.pdf
Who are the owners of a cor[poration,and how is their ownership repr.pdfWho are the owners of a cor[poration,and how is their ownership repr.pdf
Who are the owners of a cor[poration,and how is their ownership repr.pdf
 

Recently uploaded

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 

Recently uploaded (20)

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 

Files to Use httpwww.cse.msu.edu~cse231ProjectsProject05Red.pdf

  • 1. Files to Use http://www.cse.msu.edu/~cse231/Projects/Project05/RedCedarRiver.txt http://www.cse.msu.edu/~cse231/Projects/Project05/plot.py Assignment Overview This assignment focuses on the implementation of Python programs to read files and process data by using lists and functions. Assignment Background U.S. Geological Survey (USGS) provides scientific information to understand Earth, manage resources and minimize loss from natural disasters. You can download interesting data from the website https://www.usgs.gov/ We have downloaded the flow rate data for the Red Cedar River in East Lansing starting from 1932 . Your task is to design and implement a Python program that draws two plots from the data (and displays a table of data for each plot). Here is the first line of the file. The numbers that we are interested are the last three numbers which are year, month and flow rate in cubic feet per second (CFS). Notice that the year and month are ints and the flow rate is float: Assignment Specifications 1. The program must provide following functions to extract some statistics. a) open_file()prompts the user to enter a file name. The program will try to open the data file. Appropriate error message should be shown if the data file cannot be opened. This function will loop until it receives proper input and successfully opens the file. It returns a file pointer. b) read_file()calls the open_file() function and uses the returned file pointer to read the data file. This function returns a list of your choosing containing data you need for other parts of this project. c) draw_plot( x, y, plt_title, x_label, y_label) provided by us takes two equal-length lists of numbers and plots them. You need to pass the label strings and plot title to the function. Include the range of years in the title (Hint: use string concatenation and check sample output). d) annual_average(L) takes a list as an argument and returns a list of tuples in the form (year, average_flow). e) month_average(L) takes a list as an argument and returns a list of tuples in the form (year, month_flow). f) You may use extra functions, if you wish. 2. The program should read the file only once. 3. The program should plot the average flow for each year. That is, find the average flow for
  • 2. each year and then generate a plot with years on the x-axis and average flow for that year on the y-axis. Then generate a table that has the year and average flow on each line. Put a title on the table and label each column. 4. Next, the program should prompt the user for a number in the range 1-12 and should plot the flow rates for only that month. For example if user enters 1, the program should extract the flow rate of Jan 1932, Jan 1933, ... , Jan 2015 and plot them. (Note: do not loop and ask for more.) 5. The program should re-prompt the user if an invalid input (either wrong type or wrong value), was entered for the month. If something other than an integer is input, your error message should include the phrase “not an integer.” If an integer is input but it is not in the proper range, your error message should include the phrase “integer out of range.” Note: use exceptions. 6. The month name should be displayed in the title of the second plot instead of the month number. For example, in the sample below you see “May” in the title. (Hint: use the month number as an index into a list of strings.) As with the previous plot, display a table of values that you plotted. Assignment Notes 1. Note that data are separated using spaces. You can use the list method .split() to split the line into a list of data. 2. It is much easier to convert the input data to int and float when the program reads the file and creates the list. 3. To create a list L of data begin with an empty list (e.g. L = [] before the loop begins) and within the loop append to the list one item at a time, e.g. L.append(item). The data item you append may be a collection such as a tuple or another list. Sample Output 1932 192.10 1933 175.87 1934 68.55 1935 133.70 1936 107.92 1937 210.64 1938 203.00 1939 130.68 1940 155.16 1941 111.76 1942 222.91 1943 322.06 1944 168.40
  • 3. 1945 222.66 1946 132.44 1947 371.00 1948 332.60 1949 206.60 1950 345.97 1951 255.01 1952 216.94 1953 135.51 1954 237.37 1955 142.05 1956 288.82 1957 228.67 1958 102.73 1959 309.37 1960 221.05 1961 125.65 1962 146.39 1963 97.81 1964 48.00 1965 173.39 1966 155.56 1967 215.01 1968 290.97 1969 219.15 1970 198.05 1971 183.03 1972 231.14 1973 284.19 1974 297.25 1975 358.62 1976 281.77 1977 131.30 1978 143.01 1979 157.99 1980 213.15
  • 4. 1981 265.15 1982 246.95 1983 247.17 1984 171.79 1985 339.39 1986 307.77 1987 139.50 1988 191.94 1989 229.23 1990 282.57 1991 211.63 1992 351.48 1993 347.92 1994 397.64 1995 202.06 1996 194.83 1997 242.16 1998 247.35 1999 134.24 2000 174.79 2001 326.02 2002 179.25 2003 135.74 2004 239.83 2005 167.75 2006 271.75 2007 227.99 2008 332.66 2009 377.72 2010 189.64 2011 353.33 2012 145.84 2013 247.27 2014 264.20 2015 191.46 1932 362.40
  • 5. 1933 354.50 1934 65.40 1935 190.00 1936 103.60 1937 376.30 1938 214.60 1939 100.40 1940 94.50 1941 99.90 1942 99.30 1943 1008.00 1944 325.30 1945 772.40 1946 143.00 1947 619.50 1948 959.30 1949 150.20 1950 252.50 1951 307.90 1952 262.70 1953 253.70 1954 134.70 1955 94.60 1956 1310.00 1957 482.80 1958 78.30 1959 201.70 1960 293.00 1961 195.00 1962 164.50 1963 176.80 1964 121.60 1965 95.50 1966 284.40 1967 167.20 1968 211.40
  • 6. 1969 474.10 1970 261.20 1971 101.10 1972 234.10 1973 305.80 1974 507.10 1975 341.10 1976 401.70 1977 146.60 1978 206.30 1979 182.30 1980 246.20 1981 354.40 1982 143.60 1983 591.90 1984 306.20 1985 187.60 1986 186.50 1987 87.90 1988 131.20 1989 197.30 1990 348.40 1991 218.50 1992 249.80 1993 316.60 1994 296.00 1995 272.60 1996 403.40 1997 271.80 1998 436.80 1999 186.40 2000 354.90 2001 446.70 2002 329.50 2003 255.80 2004 692.80
  • 7. 2005 158.90 2006 481.40 2007 387.70 2008 141.30 2009 544.70 2010 485.00 2011 839.20 2012 273.10 2013 294.20 2014 576.70 2015 165.10 Solution import pylab from time import sleep def draw_plot( x, y, plt_title, x_label, y_label): ''' Draw x vs. y (lists should have the same length) Sets the title of plot and the labels of x and y axis ''' pylab.title(plt_title) pylab.xlabel(x_label) pylab.ylabel(y_label) pylab.plot(x, y) pylab.show() def open_file(): try: file_name = raw_input("Please enter a file name: ") f = open(file_name, "r") except: open_file() return f.tell(), file_name def read_file(): ftell, file_name = open_file() f = open(file_name) f.seek(ftell)
  • 8. results = f.readlines() data = [] for line in results: data_point = line.split() year = data_point[4] month = data_point[5] flow = data_point[6] data_point = [year, month, flow] data.append(data_point) return data def annual_average(L): annual_list = [] for i in range(1932, 2016): total = 0 for j in range(0, 12): total += float(L[i-1932][2]) entry = (i, total) annual_list.append(entry) return annual_list def month_average(L, month): month_list = [] j = month-1 for i in range(1932, 2016): entry = (float(L[j][0]), float(L[j][2])) j+=12 month_list.append(entry) return month_list data = read_file() month = input("please enter a month: ") annual_list = annual_average(data) month_list = month_average(data, month) draw_plot([annual_list[i][0] for i in range(0, 84)], [annual_list[i][1] for i in range(0, 84)], "Annual Average Flow", "Year", "Flow") draw_plot([month_list[i][0] for i in range(0, 84)], [month_list[i][1] for i in range(0, 84)], "Month Flow for month "+str(month), "Year", "Flow")