diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eba74f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ \ No newline at end of file diff --git a/earthquakes.ipynb b/earthquakes.ipynb new file mode 100644 index 0000000..f18d149 --- /dev/null +++ b/earthquakes.ipynb @@ -0,0 +1,215 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import requests # powerful library for communicating over the internet\n", + "import json\n", + "import datetime" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def get_data():\n", + " response = requests.get(\n", + " \"http://earthquake.usgs.gov/fdsnws/event/1/query.geojson\",\n", + " params={\n", + " 'starttime': \"2000-01-01\",\n", + " \"maxlatitude\": \"58.723\",\n", + " \"minlatitude\": \"50.008\",\n", + " \"maxlongitude\": \"1.67\",\n", + " \"minlongitude\": \"-9.756\",\n", + " \"minmagnitude\": \"1\",\n", + " \"endtime\": \"2018-10-11\",\n", + " \"orderby\": \"time-asc\"} \n", + " )\n", + "\n", + " # get json format\n", + " data = response.json()\n", + "\n", + " return data\n", + "\n", + "\n", + "\n", + "def count_earthquakes(data):\n", + " \"\"\"Get the total number of earthquakes in the response.\"\"\"\n", + " # Extract number from metadata\n", + " meta_count = data['metadata']['count']\n", + "\n", + " # Extractnumber from counting features\n", + " feature_count = len(data['features'])\n", + "\n", + " if meta_count == feature_count:\n", + " return meta_count\n", + " else:\n", + " print('Discrepency between metadata count vlue and number of features. Using number of features as the value.')\n", + " return feature_count\n", + " \n", + "\n", + "\n", + "\n", + "def get_magnitude(earthquake):\n", + " \"\"\"Retrive the magnitude of an earthquake item.\"\"\"\n", + " magnitude = earthquake['properties']['mag']\n", + " return magnitude\n", + "\n", + "\n", + "def get_location(earthquake):\n", + " \"\"\"Retrieve the latitude and longitude of an earthquake item.\"\"\"\n", + " # There are three coordinates, but we don't care about the third (altitude)\n", + " lat, long, _ = earthquake['geometry']['coordinates']\n", + " return [lat, long]\n", + "\n", + "def get_location_description(earthquake):\n", + " \"\"\"Retrieve the description of the location of an earthquake item.\"\"\"\n", + " description = earthquake['properties']['place']\n", + " return description\n", + "\n", + "def get_time(earthquake):\n", + " \"\"\"Retrieve the time of an eartquake. Convert from Unix Timestamp\"\"\"\n", + " unix_time_ms = earthquake['properties']['time']\n", + " time_sec = unix_time_ms / 1000 # convert from miliseconds to seconds\n", + " date_time = datetime.datetime.utcfromtimestamp(time_sec) # Convert to a datetime object in UTC\n", + " return date_time\n", + "\n", + "def get_earthquake_id(earthquake):\n", + " \"\"\"Retrieve id for earthqauke\"\"\"\n", + " id = earthquake['id']\n", + " return id\n", + "\n", + "def get_maximum(data):\n", + " \"\"\"Get the magnitude and location of the strongest earthquake in the data. \n", + " Checks for multiple quakes of same magnitude \"\"\"\n", + " max_mag = 0\n", + " earthquakes ={}\n", + "\n", + " for earthquake in data['features']:\n", + " mag = get_magnitude(earthquake)\n", + " if mag > max_mag:\n", + " # re-empty dict \n", + " earthquakes ={}\n", + " max_id = get_earthquake_id(earthquake)\n", + " max_mag = mag\n", + " max_loc = get_location(earthquake)\n", + " max_loc_description =get_location_description(earthquake)\n", + " max_time = get_time(earthquake)\n", + " earthquakes[max_id] ={}\n", + " earthquakes[max_id]['Magnitude'] = max_mag\n", + " earthquakes[max_id]['Location'] = max_loc\n", + " earthquakes[max_id]['Location_Description'] = max_loc_description\n", + " earthquakes[max_id]['Time'] = max_time\n", + " elif mag ==max_mag:\n", + " # Append to dict\n", + " max_id = get_earthquake_id(earthquake)\n", + " max_mag = mag\n", + " max_loc = get_location(earthquake)\n", + " max_loc_description =get_location_description(earthquake)\n", + " max_time = get_time(earthquake)\n", + " earthquakes[max_id] ={}\n", + " earthquakes[max_id]['Magnitude'] = max_mag\n", + " earthquakes[max_id]['Location'] = max_loc\n", + " earthquakes[max_id]['Location_Description'] = max_loc_description\n", + " earthquakes[max_id]['Time'] = max_time\n", + "\n", + " #return max_mag, max_loc, max_loc_description, max_time\n", + " return earthquakes\n", + "\n", + "\n", + "\n", + " \n", + " \n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Loaded 120\n", + "There are 2 earthquakes with the strongest magnitude in the range.\n", + "\n", + "The strongest earthquake(s) were at [-2.15, 52.52], 2 km ESE of Wombourn, United Kingdom, with magnitude 4.8, on 2002-09-22 23:53:14.600000\n", + "The strongest earthquake(s) were at [-0.332, 53.403], 1 km NNE of Market Rasen, United Kingdom, with magnitude 4.8, on 2008-02-27 00:56:47.800000\n" + ] + } + ], + "source": [ + "# Load the data\n", + "data = get_data()\n", + "print(f\"Loaded {count_earthquakes(data)}\")\n", + "\n", + "# Analyse the data\n", + "max_earthquakes= get_maximum(data)\n", + "\n", + "# Print summary of analysis\n", + "print(f'There are {len(max_earthquakes)} earthquakes with the strongest magnitude in the range.\\n')\n", + "for earthquake in max_earthquakes:\n", + " print(f\"The strongest earthquake(s) were at {max_earthquakes[earthquake]['Location']}, {max_earthquakes[earthquake]['Location_Description']}, with magnitude {max_earthquakes[earthquake]['Magnitude']}, on {max_earthquakes[earthquake]['Time']}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (3716333688.py, line 3)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[4], line 3\u001b[0;36m\u001b[0m\n\u001b[0;31m response.json? # automatically get json encoded response\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "dir(response) # get all modules and attributes\n", + "response.status_code # if status code == 200 means no server errors\n", + "response.json? # automatically get json encoded response\n", + "# json.loads(response.text) # alternative option - text is string, loads = load string file\n", + "type(response.json())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.9" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/earthquakes.py b/earthquakes.py index 16f433d..c88cef7 100644 --- a/earthquakes.py +++ b/earthquakes.py @@ -1,13 +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 numpy as np +import requests # powerful library for communicating over the internet +import json +import datetime 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={ @@ -21,41 +18,106 @@ def get_data(): "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 - # 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. - ... + # get json format + data = response.json() + + return data + - # 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 ... def count_earthquakes(data): """Get the total number of earthquakes in the response.""" - return ... + # Extract number from metadata + meta_count = data['metadata']['count'] + + # Extractnumber from counting features + feature_count = len(data['features']) + + if meta_count == feature_count: + return meta_count + else: + print('Discrepency between metadata count vlue and number of features. Using number of features as the value.') + return feature_count + + def get_magnitude(earthquake): """Retrive the magnitude of an earthquake item.""" - return ... + magnitude = earthquake['properties']['mag'] + return magnitude 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 ... + lat, long, _ = earthquake['geometry']['coordinates'] + return [lat, long] + +def get_location_description(earthquake): + """Retrieve the description of the location of an earthquake item.""" + description = earthquake['properties']['place'] + return description +def get_time(earthquake): + """Retrieve the time of an eartquake. Convert from Unix Timestamp""" + unix_time_ms = earthquake['properties']['time'] + time_sec = unix_time_ms / 1000 # convert from miliseconds to seconds + date_time = datetime.datetime.utcfromtimestamp(time_sec) # Convert to a datetime object in UTC + return date_time + +def get_earthquake_id(earthquake): + """Retrieve id for earthqauke""" + id = earthquake['id'] + return id def get_maximum(data): - """Get the magnitude and location of the strongest earthquake in the data.""" - ... + """Get the magnitude and location of the strongest earthquake in the data. + Checks for multiple quakes of same magnitude """ + max_mag = 0 + earthquakes ={} + + for earthquake in data['features']: + mag = get_magnitude(earthquake) + if mag > max_mag: + # re-empty dict + earthquakes ={} + max_id = get_earthquake_id(earthquake) + max_mag = mag + max_loc = get_location(earthquake) + max_loc_description =get_location_description(earthquake) + max_time = get_time(earthquake) + earthquakes[max_id] ={} + earthquakes[max_id]['Magnitude'] = max_mag + earthquakes[max_id]['Location'] = max_loc + earthquakes[max_id]['Location_Description'] = max_loc_description + earthquakes[max_id]['Time'] = max_time + elif mag ==max_mag: + # Append to dict + max_id = get_earthquake_id(earthquake) + max_mag = mag + max_loc = get_location(earthquake) + max_loc_description =get_location_description(earthquake) + max_time = get_time(earthquake) + earthquakes[max_id] ={} + earthquakes[max_id]['Magnitude'] = max_mag + earthquakes[max_id]['Location'] = max_loc + earthquakes[max_id]['Location_Description'] = max_loc_description + earthquakes[max_id]['Time'] = max_time + + #return max_mag, max_loc, max_loc_description, max_time + return earthquakes -# With all the above functions defined, we can now call them and get the result + +# Load the data 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}") \ No newline at end of file +print(f"Loaded {count_earthquakes(data)} \n") + +# Analyse the data +max_earthquakes= get_maximum(data) + +# Print summary of analysis +print(f'There are {len(max_earthquakes)} earthquakes with the strongest magnitude in the range.\n') +for earthquake in max_earthquakes: + print(f"The strongest earthquake(s) were at {max_earthquakes[earthquake]['Location']}, {max_earthquakes[earthquake]['Location_Description']}, with magnitude {max_earthquakes[earthquake]['Magnitude']}, on {max_earthquakes[earthquake]['Time']}")