In this project, you will gain familiarity with reading data from the Internet. You will read data on earthquakes and plot dots with different radiuses and colors on a map.
This is a pair project. Please inform your lab instructor ASAP who your partner is. Please only submit one copy of the code with both names written in project6.py as comments.
Warning: you may need to use Python 3.5 (or a version slightly older than 3.5) to be able to test your work related to urllib.request
for this project. This means that you and your partner may need to plan on using the computer lab machine (as opposed to your personal computers, if you have 3.6 installed) to work on this project.
For consistency, we ask you to use the following URL (containing earthquakes between Nov 2011 and Feb 2012, listed from the most recent to least recent) in your program: http://mcs177.github.io/projects/earthquakedata-2012-02-23.txt.
The data's header contains:
dateLessThan
that takes two dates in the format YYYY/MM/DD (as string objects).
It returns True if the 1st date
comes strictly earlier than the 2nd date.
Otherwise, it returns False.
>>> dateLessThan("1999/01/01", "2000/05/15") True >>> dateLessThan("2000/05/15", "2005/05/01") True >>> dateLessThan("2017/01/01", "2010/07/01") False >>> dateLessThan("2013/10/24", "2013/9/24") # October is not earlier than September False
betweenDates
that takes 3 dates (as string objects) in the format YYYY/MM/DD
and returns True if the 1st date is earlier/equal to the 2nd date AND
the 2nd date is strictly earlier than the 3rd date.
That is, if the 2nd date is between the 1st date (inclusive) and the 3rd date (exclusive).
Otherwise, it returns False.
dateLessThan
in a way that makes your implementation simpler.
>>> betweenDates("2000/05/15", "2000/05/15", "2005/05/01") True >>> betweenDates("2000/05/09", "2000/05/09", "2000/05/10") True >>> betweenDates("1999/05/09", "2000/05/09", "1999/05/09") True >>> betweenDates("2011/01/01", "2011/07/01", "2011/12/31") True >>> betweenDates("2000/05/15", "2000/05/15", "2000/05/15") False >>> betweenDates("2011/01/01", "2010/07/01", "2011/12/31") False >>> betweenDates("2011/01/01", "2011/12/31", "2011/12/31") False
makeDataDictionary
.
It takes in two string parameters.
The first one is in the form of a header (separated by commas), and the second one is the data (separated by commas).
It returns a dictionary with dictionary keys the words given from the header
and the dictionary values the data values (as string objects).
All leading and trailing spaces should be removed.
makeDictionary
function from Project 5
(in this case, you have to first turn the two strings into two lists of strings which are stripped of leading and trailing spaces)
>>> makeDataDictionary('State,HouseholdIncome,IQ,McCainVote,Region','Alaska,57071,99,0.602,W') {'Region': 'W', 'State': 'Alaska', 'IQ': '99', 'HouseholdIncome': '57071', 'McCainVote': '0.602'} >>> makeDataDictionary('day, time,someinfo, magntd, depth', '2012/01/12,19:55:51.2,52.605, , 50') {'someinfo': '52.605', 'depth': '50', 'time': '19:55:51.2', 'day': '2012/01/12', 'magntd': ''} >>> makeDataDictionary('1,2,3','val1, , val3') {'1': 'val1', '3': 'val3', '2': ''}
readEarthquakes
that takes two dates in the format YYYY/MM/DD,
accesses the earthquakes from
http://mcs177.github.io/projects/earthquakedata-2012-02-23.txt
(taken from USGS).
It returns
a list of dictionaries for all earthquakes between the two dates,
except for earthquakes with missing magnitude information.
>>> L = readEarthquakes("2012/01/03", "2012/01/04") >>> L[0] {'Date': '2012/01/03', 'Longitude': '143.786', 'TimeUTC': '23:30:07.0', 'Magnitude': '4.6', 'Depth': '10', 'Latitude': '26.938'}
betweenDates
. Assume only valid dates in the form of YYYY/MM/DD are entered.
makeDataDictionary
to create each dictionary,
so that your function is not overly complicated. In fact, you should not have more than one loop in your implementation of readEarthquakes
.
readEarthquakes
procedure,
it must use either
dateLessThan
or
betweenDates
to make sure that earthquakes are included in the returned list of dictionaries if and only if they have the correct dates.
urllib.request
instead of urllib
.
Second, remember that you need to decode anything you read from the web.
For example, if you read a string into a variable myString
, you can decode it by:
decodedString = myString.decode()
import urllib.request
within this function.
(Remember also that you would run into problems if you run this procedure in Python 3.6).
colorCode
that takes the depth of an earthquake and
returns the corresponding color for the earthquake.
Range | Color |
0-33 | 'orange' |
34-70 | 'yellow' |
71-150 | 'green' |
151-300 | 'blue' |
301-500 | 'purple' |
501-900 | 'red' |
>>> colorCode(33) 'orange' >>> colorCode(34) 'yellow'Your procedure should not be overly complicated. In particular, don't include more than 2 nested selection statements.
cTurtle
methods shown below to show a window showing the image link and draw several dots at 4-5 different locations with various colors and sizes.
speed
).
You should not require any methods not mentioned below.
>>> myTurtle = cTurtle.Turtle()
>>> import cTurtle
>>> myTurtle.bgpic('worldmap.gif')
which should cause the worldmap.gif image to be displayed.
goto
. To draw a dot, use dot
.
Method Contract | Description | Example |
# goto: number number -> void |
Moves the turtle to the given coordinate (x, y) | myTurtle.goto(10, 150) |
# dot: number string -> void |
Makea a dot of the given size with the given color at the current position | myTurtle.dot(30, 'blue') |
# down: -> void |
Put the turtle's tail down. Makes the turtle draw lines when it moves. | myTurtle.down() |
# up: -> void |
Raise the turtle's tail up. Prevents the turtle from drawing lines when it moves. | myTurtle.up() |
# speed: integer -> void |
Sets the speed of the turtle. 1 is slowest and 10 is fastest. | myTurtle.speed(10) |
# setWorldCoordinates: number number number number -> void |
Modify the world coordinates of the canvas that the turtle draws on. Automatically scale the canvas between (x1, y1) and (x2, y2). X values range from x1 to x2 while Y values range from y1 to y2. | myTurtle.setWorldCoordinates(-10, -5, 10, 5) |
# bgpic: string -> void |
Set the background of the canvas with the given image file | myTurtle.bgpic('worldmap.gif') |
# hideturtle: -> void |
Make the turtle invisible | myTurtle.hideturtle() |
plotEarthquakes
that takes two dates and plots all the earthquake data from
http://mcs177.github.io/projects/earthquakedata-2012-02-23.txt
(taken from USGS) between the given dates
with dots on the world map.
readEarthquakes
.
dot
from the cTurtle
library that takes the size and color.
int
and float
may also be useful for you here.
bgpic
is useful to put the world map image in the background.
setWorldCoordinates
can help you plot the dots more easily.Assume the entire map shows -180 to 180 degrees from left to right and -90 to 90 degrees from bottom to top.
cTurtle
object.
Hence, you may want to do import cTurtle
within this function.
It should take no more than a few seconds for the turtle to stop drawing the dots using these short practice URLs. It will go even faster if you remember to hide your tutle using the hideturtle
procedure.
The following command
>>> plotEarthquakes("2012/01/01", "2012/01/04")
should produce the following map with all the earthquakes for 3 days (January 1st, 2nd, and 3rd in 2012). It may take more than a few seconds for the turtle to finish drawing.
The following command (which may take several minutes)
>>> plotEarthquakes("2012/01/04", "2012/02/01")
should produce the following map with all the earthquakes from January 4 until January 31 (inclusive). It may take more than a couple minutes for the turtle to finish drawing.
Submit your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit one Python file called
Be sure that your module can run on IDLE on the computer lab machines (that is, IDLE 3.54).
If you submit your project early, you will get an early feedback on the accuracy of your work.
The grader will test your program against various input values. Check your work against the grading rubric project6gradesheet.pdf which the grader will use when grading your lab.