  ## Python Homework Help on Earthquakes

• 29th Apr, 2022
• 15:34 PM
```import turtle, datetime

# copied from my lab 5 solution:
def teleport(t, x, y):
""" Move the turtle to (x, y), ensuring that nothing is drawn along the
way. Postcondition: the turtle's orientation and up/down state is the
same as before.
"""
# save the current pen state
pen_was_down = t.isdown()

# pick up pen, move to coordinates
t.up()
t.goto(x, y)

# restore pen state
if pen_was_down:
t.down()

# copied from A4, with a couple slight modifications:
def turtle_setup(canv_width, canv_height):
""" Set up the canvas and a turtle; return a turtle object in hidden
state. The canvas has size canv_width by canv_height, with a
coordinate system where (0,0) is in the center, and automatic
re-drawing of the canvas is disabled. Set the background image to
earth.png.
"""
# create a turtle to color pixels with
t = turtle.Turtle()

# set the screen size, coordinate system, and color mode:
screen = t.getscreen()
screen.setup(canv_width, canv_height)
turtle.colormode(255) # specify how colors are set: we'll use 0-255

t.hideturtle() # hide the turtle triangle
screen.tracer(0, 0) # turn off redrawing after each movement

turtle.bgpic('earth.png') # set the background image
turtle.update()
return t

def parse_row(line):
""" Parse a line of the csv file, returning a dict with keys
for latitude, longitude, timestamp, and magnitude.
Pre: line is an unprocessed string representing a line of the file.
Post: the returned dict has the following keys with values according
to the data in the given line of the file:
"latitude" -> (float)
"longitude" -> (float)
"timestamp" -> (str)
"magnitude" -> (float)
"""
# the line has all the data separated by commas, so
# split the line into its constituent numbers
# to make it easy to access the latitude, longitude, timestamp, and magnitude values
data = line.split(",")
# create a dictionary
quake_dictionary = {}
#Populate the dictionary with the  keys and values for latitude, longitude, timestamp, magnitude
quake_dictionary["latitude"] = float(data)
quake_dictionary["longitude"] = float(data)
quake_dictionary["timestamp"] = data
quake_dictionary["magnitude"] = float(data)
# return the resulting dictionary
return quake_dictionary

def main():
""" Main function: plot a circle on a map for each earthquake """
# we'll scale coordinates and canvas to be 720x360, double
# the size of the range of lat/lon coordinates
scale = 2.0

# call turtle_setup to set up the canvas and get a turtle
t = turtle_setup(scale * 360, scale * 180)

eq_file = open('earthquakes.csv', 'r')

# make a list to store the earthquate dictionaries
dicts = []
# parse each line of the file using parse_row and add each returned
# dictionary into the list (skip the headers on the first line!)
dicts.append(parse_row(line))
# for each earthquake dictionary in the list:
for dict in dicts:
# if the magnitude is larger than 1.0:
magnitude = dict['magnitude']
if magnitude > 1.0:
# draw a circle with radius equal to the magnitude
# at (longitude * scale, latitude * scale).
teleport(t, dict['longitude'] * scale, dict['latitude'] * scale)

# (optional) color the circle by magnitude
r = int((magnitude - 1) * 255 / 8)
g = int((9 - magnitude) * 255 / 8)
b = int((9 - magnitude) * 255 / 8)

# (challenge) by date.
# Looking at the data, you may notice that the
# earthquakes in our dataset all happened in 2016.
"""
dateStart = datetime.datetime(2016, 7, 1)
date = datetime.datetime.strptime(dict['timestamp'], '%Y-%m-%d %H:%M:%S')
delta = date - dateStart
days = delta.days
totalDays = 62
r = int(days * 255 / totalDays)
g = int((totalDays - days) * 255 / totalDays)
b = int((totalDays - days) * 255 / totalDays)
"""

t.pen(pencolor=(r, g, b), pensize=1)
t.circle(magnitude)

# update the screen so all the circles are drawn on the canvas
turtle.update()
input('Press a key')  # There must be a better way

if __name__ == "__main__":
main()```