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.
You may choose to do this project with a partner (we highly recommend that you do). If you choose to work with a partner, Please only submit one copy of the code with both names written in plotEarthquakes.py as comments
http://www.choongsoo.info/teach/mcs177-sp12/projects/earthquake/earthquakeData-02-23-2012.txt
Make a copy of the URL for the data as you will need to use it in your procedure. The list contains:
betweenDates
that takes three dates in the format YYYY/MM/DD and returns
a boolean indicating whether the first date is on or after the second date and before the third date. You may assume the second date is before the third date.
>>> betweenDates("2011/07/01", "2011/01/01", "2011/12/31") True >>> betweenDates("2010/07/01", "2011/01/01", "2011/12/31") False >>> betweenDates("2011/01/01", "2011/01/01", "2011/12/31") True >>> betweenDates("2011/12/31", "2011/01/01", "2011/12/31") False
parseEarthquakeData
that takes two dates in the format YYYY/MM/DD,
accesses the earthquake data from the above USGS URL and returns a list of lists of four numbers representing latitude, longitude, magnitude and depth. The outer list should contain one of these four-number lists for each earthquake
between the given dates.
NOTE: If you have the first-edition textbook, it has two errors that were corrected in the second-edition. First, the first-edition textbook has a typo: use urllib.request
instead of urllib
. Second, remember that you need to decode anything you read from the web as ASCII. For example, if you read a string into a variable myString
, you can decode it by:
decodedString = myString.decode('ascii')
Some lines do not have any information on the magnitude. You can skip these lines by checking if there are only spaces in the place of magnitude.
You may use the strip
procedure to make this task easier. The strip
acts on a string and
it takes away all the white spaces from the left and the right until it finds a non-whitespace.
Your should use list comprehension at least once in your parseEarthquakeData
function.
>>> aString = " hello " >>> aString.strip() "hello" >>> bString = " " >>> bString == "" False >>> bString.strip() == "" True
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' |
cTurtle
library and familiarize yourself with them (if you have a second-edition textbook, your Appendix C describes the similar turtle
library instead. You will have fewer difficulties if you use cTurtle
as shown here).
myTurtle = cTurtle.Turtle()
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') |
# exitOnClick: -> void |
Wait for a mouse click inside the window, then close and exit | myTurtle.exitOnClick() |
# hideturtle: -> void |
Make the turtle invisible | myTurtle.hideturtle() |
plotEarthquakeData
that takes two dates and plots all the earthquake data from USGS between the given dates
with dots on the world map. You can use the procedure dot
from the cTurtle
library that takes the size and color.
You can use the product of 4 and the magnitude for the size of dots while using the depth for the right color.
The procedure bgpic
is useful to put the world map image in the background while the procedure 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.
>>> plotEarthquakeData("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).
You will be submitting your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit the following file:
You will earn one point for each of the following accomplishments:
betweenDates
.
betweenDates
includes the first date.
betweenDates
excludes the second date.
betweenDates
returns the correct boolean value based on the two dates.
parseEarthquakeData
.
colorCode
.
colorCode
returns the corresponding value.
plotEarthquakeData
.
plotEarthquakeData
puts the world map in the background.
plotEarthquakeData
sets the world coordinate system correctly.
plotEarthquakeData
plots the earthquake at the correct location.
plotEarthquakeData
plots the dot of the right size.
plotEarthquakeData
plots the dot of the right color.