Skip to content

Commit

Permalink
Plotting functions for Earthquake data
Browse files Browse the repository at this point in the history
I have completed Issue UCL-COMP0233-24-25#10, adding functions to analyse the Earthquake data further and produce plots for the average annual earthquake magnitude and the annual frequency of Earthquakes
  • Loading branch information
dp0768 committed Oct 21, 2024
1 parent ec6fd21 commit bd39238
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
Binary file added Avg_mag_per_year.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
122 changes: 122 additions & 0 deletions Earthquake_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
from datetime import date
import numpy as np
import json
import os
import matplotlib.pyplot as plt


def get_data():
#See earthquakes.py to access data from online database
#Extra info specifying file path - ONLY FOR MY PERSONAL COMPUTER (I work in a VS code workspace, where the directory initialized is not the
# same as the directory where this file is hence I add \Week 4\earthwuakes\earthquakes_data.json)
file_loc = os.path.join(os.getcwd(),'Week 4','earthquakes','earthquakes_data.json')

#Access json file and load the json database as a Python dictionary
with open(file_loc) as json_data_in:
data = json.load(json_data_in)
return data


def get_year(data):
raw_t, t = [], []
for event in data['features']:
raw_t = event['properties']['time']
t.append(date.fromtimestamp(raw_t/1000).year)
return np.array(t)


def get_magnitude(data):
mag = []
for event in data['features']:
m = event['properties']['mag']
mag.append(m)
return np.array(mag)


#Determine only the frequency of earthquakes
def get_magnitudes_per_year(Event_year, Event_mag, Avg_flag=False, Fre_flag=False):
Total_years = np.arange(start=np.min(Event_year),stop=(np.max(Event_year)+1))


Event_frequency = np.zeros(len(Total_years))
Avg_mag = np.zeros(len(Total_years))

for k in range(len(Total_years)):
for j in range(len(Event_year)):
if Total_years[k] == Event_year[j]:
Event_frequency[k] += 1


Mag_year = 0
if Event_frequency[k] != 0:
for j in range(len(Event_year)):
if Total_years[k] == Event_year[j]:
Mag_year += Event_mag[j]

Avg_mag[k] = Mag_year/Event_frequency[k]

else:
continue
if Avg_flag:
return Total_years, Avg_mag, Total_years
elif Fre_flag:
return Total_years, Event_frequency, Total_years


def plot_average_magnitude_per_year(data):
Event_year = get_year(data)
Event_mag = get_magnitude(data)

Total_years, Avg_mag, Total_years = get_magnitudes_per_year(Event_year=Event_year,Event_mag=Event_mag,Avg_flag=True)

plt.figure(figsize=(24,16))
plt.rcParams.update({'font.size': 20})
fig = plt.gcf()
ax = fig.add_subplot(111)

ax.plot(Total_years,Avg_mag,label="Slow method", color='red',ls='-',marker='X')

ax.set_title('Average annual earthquake magnitude')
ax.set_xticks(Total_years)
#ax.set_ylim(0)
#ax.set_xlim(2000)
ax.set_ylabel('Magnitude (richter scale)')
ax.set_xlabel('Year')
plt.grid()


plt.savefig(os.path.join(os.getcwd(),'Week 4','earthquakes','Avg_mag_per_year.png'))


def plot_number_per_year(data):
Event_year = get_year(data)
Event_mag = get_magnitude(data)

Total_years, Event_frequency, Total_years = get_magnitudes_per_year(Event_year=Event_year,Event_mag=Event_mag,Fre_flag=True)

plt.figure(figsize=(24,16))
plt.rcParams.update({'font.size': 20})
fig = plt.gcf()
ax = fig.add_subplot(111)

ax.bar(Total_years,Event_frequency,label="Slow method", color='red')

ax.set_title('Annual Earthquake Frequency')
ax.set_xticks(Total_years)
ax.set_ylabel('Earthquake Frequency (Events per year)')
ax.set_xlabel('Year')
plt.grid()


plt.savefig(os.path.join(os.getcwd(),'Week 4','earthquakes','Freq_per_year.png'))



# Get the data we will work with
data = get_data()

plot_number_per_year(data)

plt.clf() # This clears the figure, so that we don't overlay the two plots

plot_average_magnitude_per_year(data)
Binary file added Freq_per_year.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion earthquakes_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -4812,7 +4812,7 @@
"metadata": {
"api": "1.14.1",
"count": 120,
"generated": 1729247656000,
"generated": 1729538330000,
"status": 200,
"title": "USGS Earthquakes",
"url": "https://earthquake.usgs.gov/fdsnws/event/1/query.geojson?format=geojson&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"
Expand Down

0 comments on commit bd39238

Please sign in to comment.