$24
Background
Hurricane season is back and it came with a vengeance. Over five hurricanes have passed so far and two of them have done massive damage to the Caribbean, including Puerto Rico. In order to prepare countermeasures against the destruction these atmospheric phenomena leave on their way, it is imperative to gather information about their wind speeds and trajectory for the creation of better predictive models. The National Hurricane Center is responsible to record these disasters and create these predictive models (http://www.nhc.noaa.gov/).
For this project, you are tasked to create a program that stores a record of all the hurricanes that occurred within a range of years into a dictionary of dictionaries. Using this dictionary, it is required to extract all the hurricanes that occurred in a year and print the peak wind speed of each hurricane with their coordinates and dates, plot the trajectory of each hurricane, and plot a line chart with the peak wind speeds and show the category each hurricane reached.
Project Specifications
1. You must implement the following functions:
open_file() prompts the user to enter a filename containing the hurricane data. The program will try to open a file. An error message should be shown if the file can not be opened (use the try-except construct in Chapter 6). This function will loop until it receives proper input and successfully opens the file. It returns a file pointer.
update_dictionary(dictionary, year, hurricane_name, data) receives the dictionary, the year, the name of the hurricane, and the tuple (data) with the coordinates, date, wind speed, and pressure. The updated dictionary is returned. In this project we use a dictionary with year as the key and whose value is another dictionary. The nested dictionary will have the name of the hurricane as its key and a list of tuples as the value. For example
Dict = {'2017':{'MARIA': hurricane_data1, 'IRMA': hurricane_data2} }
where hurricane_data1 and hurricane_data2 are lists of tuples.
The value of expression Dict['2017']['MARIA'] is the list hurricane_data1
If the dictionary[year] is not defined, assign an empty dictionary to this entry and then fill that empty dictionary using hurricane_ name as the key and that hurricane’s data as its value. Remember to put that data in a list (so you can append more data for that hurricane when you read it). If the dictionary[year] is defined but the dictionary[year][hurricane_name] is not defined (that is, this is a new hurricane name for that year), add this new hurricane to the year (similar to the previous when we put the first hurricane in for the year) using hurricane _name as the key and that hurricane’s data as its value (in a list, as before). Otherwise (that is, both the year and the hurricane are already in the dictionary) append the data tuple to the existing list for that year and hurricane.
create_dictionary(fp) takes one parameter fp, the file pointer, reads the file, and creates the dictionary containing the hurricane records. All the work of updating the dictionary is done in the update_dictionary function. Each line in the file contains the following:
year = line[0]
hurricane_name = line[1]
lat = float(line[3])
long = float(line[4])
date = line[5]
wind = float(line[6]) if the value is a number, 0 otherwise pressure = float(line[7]) if the value is a number, 0 otherwise
Create a data tuple: (lat, long, date, wind, pressure); then call the update_dictionary function to add the tuple to the dictionary. See the dictionary created for the data file small.txt in novacky_dictionary.txt to make sure you are creating the correct dictionary.
display_table(dictionary, year) This function receives a dictionary and a year value and for every hurricane in that year it displays the name of the hurricane, the coordinate, date, and value where the hurricane reached the peak wind speed. If two data points have the same peak wind speed, print the one with the larger lat value. If they have the same peak wind spend and lat, print the one with larger long. (Hint: sort the data for a storm first on wind speed, then on lat and then on long. largest first; itemgetter is your friend.)
get_years(dictionary) Returns the oldest year and most recent year (min and max year) in the dictionary. Return a tuple (min_year, max_year). Hint: sort the keys! Use this function to find the range of years and print it.
main() The main function use the functions mentioned above.
Extra Credit. (2 %)
You will need to implement
prepare _plot(dictionary, year) Call this function to prepare for plotting hurricanes for the specified year. This function should create the following lists. Each list should be ordered by hurricane name, e.g. max_speed is a list of peak speeds, but should be ordered by hurricane name (see the plot): (1) names : a sorted list of all the names of the hurricanes in that year—sorted alphabetically, (2) max_speed : a list of maximum speeds of all the hurricanes. Finally return the two lists in a tuple: (names,max_speed). You should call the prepare _plot function inside the main function. It should prompt the user for whether to plot. I provide plot_wind_chart(year,size,names,max _speed) function. Call it if the user decides to plot. Another issue you may experience is (I did), your program may not recognize pylab – this is one of the many resources available to python users. I downloaded python 3.6 (includes pylab) from this website https://www.anaconda.com/download/
then choose windows or mac download. This version of python will replace the one you
are currently using but contains lots of resources like pylab, matplotlib, …
Hints and Suggestions
I’m giving you the data file called storm_track.txt use it rather than
storm_track1.txt used in the sample runs. Dictionary values can be any object such as floats and lists, but can also be another dictionary. In this project we use a dictionary with year as the key and whose value is another dictionary. The nested dictionary will have the name of the hurricane as its key and a list of tuples as the value. For example
D = {'2017':{'MARIA': hurricane_data1, 'IRMA': hurricane_data2} }
where hurricane_data1 and hurricane_data2 are lists of tuples. The value of expression D['2017']['MARIA'] is hurricane_data1
Dictionaries are unordered, so how do we sort keys (or values) of a dictionary? One way is to create a list of keys and sort that list. To get the list of keys on a dictionary, you can use the keys() method.
Test Case 1:
Enter hurricane data: storm_track1.txt
Hurricane Record
Software
Records from 2007 to 2017
Enter the year
to show hurricane data or 'quit': 2017
Peak Wind Speed for the Hurricanes in 2017
Name
Coordinates
Wind Speed (knots)
Date
ARLENE
(
40.00,-48.00)
45.00
04/21/06Z
BRET
(
11.60,-64.40)
40.00
06/20/12Z
CINDY
(
27.30,-91.90)
50.00
06/21/06Z
DON
(
11.50,-56.20)
45.00
07/18/06Z
FOUR
(
15.60,-50.90)
25.00
07/07/12Z
FRANKLIN
(
20.20,-96.10)
75.00
08/10/00Z
GERT
(
40.10,-58.40)
90.00
08/17/00Z
HARVEY
(
28.00,-97.00)
115.00
08/26/00Z
IRMA
(
19.40,-66.80)
160.00
09/07/00Z
JOSE
(
16.90,-59.30)
135.00
09/09/11Z
KATIA
(
21.00,-96.50)
90.00
09/08/18Z
LEE
(
31.20,-57.10)
100.00
09/27/18Z
MARIA
(
17.30,-64.70)
150.00
09/20/00Z
NATE
(
28.40,-89.10)
80.00
10/07/18Z
OPHELIA
(
37.30,-21.50)
100.00
10/15/00Z
SIX
(
27.70,-83.20)
40.00
07/31/06Z
Do you want to
plot? Yes
Test Case 2:
Enter hurricane data: storm_track1.txt
Hurricane Record
Software
Records from 2007 to 2017
Enter the year
to show hurricane data or 'quit': abc
Error with the
year key! Try another year
Enter the year
to show hurricane data or 'quit': 2000
Error with the
year key! Try another year
Enter the year
to show hurricane data or 'quit': 2010
Peak Wind Speed for the Hurricanes in 2010
Name
Coordinates
Wind Speed (knots)
Date
ALEX
(
24.20,-97.70)
95.00
07/01/02Z
BONNIE
(
23.80,-77.80)
40.00
07/23/06Z
COLIN
(
25.60,-66.60)
50.00
08/06/00Z
DANIELLE
(
27.10,-60.10)
115.00
08/27/18Z
EARL
(
28.60,-74.30)
125.00
09/02/06Z
FIONA
(
19.50,-62.50)
55.00
09/01/18Z
FIVE
(
26.50,-85.00)
30.00
08/11/06Z
GASTON
(
12.90,-36.10)
35.00
09/01/18Z
HERMINE
(
25.30,-97.40)
60.00
09/07/02Z
IGOR
(
18.90,-53.50)
135.00
09/15/00Z
JULIA
(
17.70,-32.20)
120.00
09/15/12Z
KARL
(
19.60,-95.60)
110.00
09/17/12Z
LISA
(
20.40,-27.80)
75.00
09/25/00Z
MATTHEW
(
15.20,-84.60)
50.00
09/25/00Z
NICOLE
(
27.40,-78.50)
40.00
09/30/12Z
OTTO
(
28.50,-59.70)
75.00
10/09/06Z
PAULA
(
19.60,-86.00)
90.00
10/13/00Z
RICHARD
(
17.20,-88.20)
85.00
10/25/00Z
SHARY
(
35.10,-57.20)
65.00
10/30/12Z
TOMAS
(
13.80,-62.40)
85.00
10/31/06Z
TWO
(
26.10,-96.60)
30.00
07/08/12Z
Do you want to
plot? n
Program execution complete!