Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions to find earthquake with max magnitude #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions earthquakes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

# The Python standard library includes some functionality for communicating
# over the Internet.
# However, we will use a more powerful and simpler library called requests.
# This is external library that you may need to install first.
import requests

import json

def get_data():
# With requests, we can ask the web service for the data.
Expand All @@ -24,38 +25,70 @@ def get_data():
# The response we get back is an object with several fields.
# The actual contents we care about are in its text field:
text = response.text
# could also do response.json instead
responseJson = json.loads(text)

with open("response.json", "w") as file:
json.dump(responseJson, file, indent=4)
# To understand the structure of this text, you may want to save it
# to a file and open it in VS Code or a browser.
# See the README file for more information.
...

# We need to interpret the text to get values that we can work with.
# What format is the text in? How can we load the values?
return ...
return responseJson


def count_earthquakes(data):
"""Get the total number of earthquakes in the response."""
return ...

return data["metadata"]["count"]

def get_magnitude(earthquake):
"""Retrive the magnitude of an earthquake item."""
return ...

return earthquake["properties"]["mag"]

def get_location(earthquake):
"""Retrieve the latitude and longitude of an earthquake item."""
# There are three coordinates, but we don't care about the third (altitude)
return ...

return earthquake['geometry']['coordinates'][:2]

def get_maximum(data):
"""Get the magnitude and location of the strongest earthquake in the data."""
...
# initiate maxMag and location
location = 0
maxMag = 0

# iterate through the earthquakes to find the max magintude
# should also have been able to handle earthquakes with the same mag
for earthquake in data["features"]:
if get_magnitude(earthquake) > maxMag:
# assign the values to the variables
maxMag = get_magnitude(earthquake)
location = get_location(earthquake)
return maxMag, location

# With all the above functions defined, we can now call them and get the result

# # With all the above functions defined, we can now call them and get the result
data = get_data()
print(f"Loaded {count_earthquakes(data)}")
max_magnitude, max_location = get_maximum(data)
print(f"The strongest earthquake was at {max_location} with magnitude {max_magnitude}")
print(f"The strongest earthquake was at {max_location} with magnitude {max_magnitude}")

# The strongest earthquake was at [-2.15, 52.52] with magnitude 4.8














# create a dictionary where the keys are the years
# for each year, look at every eathquake
# if the year is equal then append the magnitude to the year
127 changes: 127 additions & 0 deletions earthquakes_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from datetime import date

import matplotlib.pyplot as plt
import requests
import json
import numpy as np


def get_data():
# With requests, we can ask the web service for the data.
# Can you understand the parameters we are passing here?
response = requests.get(
"http://earthquake.usgs.gov/fdsnws/event/1/query.geojson",
params={
'starttime': "2000-01-01",
"maxlatitude": "58.723",
"minlatitude": "50.008",
"maxlongitude": "1.67",
"minlongitude": "-9.756",
"minmagnitude": "1",
"endtime": "2018-10-11",
"orderby": "time-asc"}
)

# The response we get back is an object with several fields.
# The actual contents we care about are in its text field:
text = response.text
# could also do response.json instead
responseJson = json.loads(text)

with open("response.json", "w") as file:
json.dump(responseJson, file, indent=4)
# To understand the structure of this text, you may want to save it
# to a file and open it in VS Code or a browser.
# See the README file for more information.

# We need to interpret the text to get values that we can work with.
# What format is the text in? How can we load the values?
return responseJson


def get_year(earthquake):
"""Extract the year in which an earthquake happened."""
timestamp = earthquake['properties']['time']
# The time is given in a strange-looking but commonly-used format.
# To understand it, we can look at the documentation of the source data:
# https://earthquake.usgs.gov/data/comcat/index.php#time
# Fortunately, Python provides a way of interpreting this timestamp:
# (Question for discussion: Why do we divide by 1000?)
year = date.fromtimestamp(timestamp/1000).year
return year


def get_magnitude(earthquake):
"""Retrive the magnitude of an earthquake item."""
return earthquake["properties"]["mag"]


# This is function you may want to create to break down the computations,
# although it is not necessary. You may also change it to something different.
def get_magnitudes_per_year(earthquakes):
"""Retrieve the magnitudes of all the earthquakes in a given year.

Returns a dictionary with years as keys, and lists of magnitudes as values.
"""
year_dict={}
for earth in earthquakes:
if get_year(earth) in year_dict:
year_dict[get_year(earth)].append(get_magnitude(earth))
else:
year_dict[get_year(earth)] = [get_magnitude(earth)]
return year_dict

def get_number_per_year(earthquakes):
year_dict={}
for quake in earthquakes:
if get_year(quake) in year_dict:
year_dict[get_year(quake)] += 1
else:
year_dict[get_year(quake)] = 1
return year_dict

def plot_average_magnitude_per_year(earthquakes):
dict = get_magnitudes_per_year(earthquakes)
year_list =[]
avg_list=[]
for item in dict:
year_list.append(item)

year_list.sort()

for year in year_list:
mag_list = dict[year]
value = 0
for mag in mag_list:
value += mag
avg_list.append(value/len(mag_list))

year_list=np.array(year_list)
avg_list = np.array(avg_list)
print(year_list,avg_list)
plt.title('Average magnitude per year')
plt.plot(year_list,avg_list, color='red')
plt.xlabel('Year')
plt.xticks(year_list,rotation=45)
plt.ylabel('Average magnitude')
# plt.show()

def plot_number_per_year(earthquakes):
year_list = list(get_number_per_year(earthquakes).keys())
num_list = list(get_number_per_year(earthquakes).values())
plt.title('Number of earthquakes per year')
plt.plot(year_list,num_list, color='blue')
# plt.show()


plot_average_magnitude_per_year(get_data()['features'])
plot_number_per_year(get_data()['features'])

# # Get the data we will work with
# quakes = get_data()['features']

# # Plot the results - this is not perfect since the x axis is shown as real
# # numbers rather than integers, which is what we would prefer!
# plot_number_per_year(quakes)
# plt.clf() # This clears the figure, so that we don't overlay the two plots
# plot_average_magnitude_per_year(quakes)