MCS-177 Project 6: Plotting Earthquakes (Spring 2016)

Start: Tuesday 4/5; Due: Wednesday 4/13, by the beginning of class


Overview

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

Specific tasks

  1. Check out USGS for recent earthquakes. The website offers a comma-delimited list of earthquakes in the last three months. You can check out this data at:

    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:

  2. Write the contract, docstring, and implementation for a procedure 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
    
    

  3. Write the contract, docstring, and implementation for a procedure 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:

    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
    
    

  4. Write the contract, docstring, and implementation for a procedure colorCode that takes the depth of an earthquake and returns the corresponding color for the earthquake.

    RangeColor
    0-33'orange'
    34-70'yellow'
    71-150'green'
    151-300'blue'
    301-500'purple'
    501-900'red'

  5. Download the world map image by right-clicking on this link and put it in the same directory as the Python file you are working on right now. (This image is resized and converted from http://en.wikipedia.org/wiki/File:Equirectangular_projection_SW.jpg.)

  6. Review Appendix C of the textbook for various procedures from the 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 ContractDescriptionExample
    # 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()
  7. Write the contract, docstring, and implementation for a procedure 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).

Submitting your work

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:

Grading

You will earn one point for each of the following accomplishments:

  1. You wrote a correct contract and docstring for the procedure betweenDates.
  2. Your betweenDates includes the first date.
  3. Your betweenDates excludes the second date.
  4. Your betweenDates returns the correct boolean value based on the two dates.
  5. You wrote a correct contract and docstring for the procedure parseEarthquakeData.
  6. Your parseEarthquakeData connects to the remote server to retrieve the data.
  7. Your parseEarthquakeData parses the data correctly into latitude, longitude, magnitude and depth.
  8. Your parseEarthquakeData skips the header row and the data without any magnitudes.
  9. Your parseEarthquakeData returns the list of data between the given dates.
  10. Your parseEarthquakeData uses list comprehension.
  11. You wrote a correct contract and docstring for the procedure colorCode.
  12. Your colorCode returns the corresponding value.
  13. You wrote a correct contract and docstring for the procedure plotEarthquakeData.
  14. Your plotEarthquakeData puts the world map in the background.
  15. Your plotEarthquakeData sets the world coordinate system correctly.
  16. Your plotEarthquakeData plots the earthquake at the correct location.
  17. Your plotEarthquakeData plots the dot of the right size.
  18. Your plotEarthquakeData plots the dot of the right color.
  19. You use descriptive names for parameters and variables used in your procedures.
  20. Your procedures are not unnecessarily complex.