# Birthquakes
**by [Matt Hall](https://github.com/kwinkunks)**
We are going to look at earthquakes, on your birthdate. Birthquakes!
We will also be implementing the haversine formula for determining the distance between two ponts on the earth's surface.
This challenge is a bit different from the previous ones. You can use any old string for your key, as usual, but if you use a date, you'll get data for that date. For example:
url = 'https://kata.geosci.ai/challenge/birthquakes'
params = {'key': '1980-06-30'} # <-- The key can be a date.
r = requests.get(url, params)
Your challenge input is now `r.text`. There is a header row containing the names of the columns, plus a number of data rows or 'records'. Each row has 13 columns, and represents the data for a single earthquake.
You need to answer the following questions:
1. How many records (i.e. earthquakes) are there?
2. What is the depth **in metres** of the earthquake with the largest **Magnitude**? (If there's more than one, give the deepest.)
3. What is the great circle distance **to the nearest km**, as given by the haversine formula, between the epicentres of the two **largest** earthquakes, as measured by magnitude? (Again, if two earthquakes are equal in magnitude, choose the deepest first.)
4. Consider all pairs of events. How many pairs are within 100 km of each other? (The events must be less than 100 km from each other. A pair that is exactly 100 km apart would **not** be included.)
Note that because we're asking about epicentres, you don't need to worry about depth when calculating great circle distances.
For Question 4, only count unique pairs. For example, in the diagram below there are 15 pairs of points altogether, of which there are 7 pairs with a mutual distance of < 100 km here — 1 pair on the left and 6 on the right:
x x
x
x x x
==========
100 km
## Haversine formula
There are several formulas for computing [great circle distance](https://en.wikipedia.org/wiki/Great-circle_distance) on a sphere. The simplest accurate one is the haversine formula, which is described here.
Given two points with (_latitude_, _longitude_), we'll denote point 1 with $(\varphi_1, \lambda_1)$ and point 2 with $(\varphi_2, \lambda_2)$. Then distance _d_ is related to radius _r_ by:
$$ d = 2r \arcsin\left(\sqrt{\sin^2\left(\frac{\varphi_2 - \varphi_1}{2}\right) + \cos(\varphi_1) \cos(\varphi_2)\sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right)}\right)$$
Some hints about implementing this in Python:
- Use $r = 6371\ \mathrm{km}$ for the radius of the earth.
- $\sin^2(x)$ means $\sin(x) \times \sin(x)$.
- Both the `math` module and NumPy have the functions `sin()`, `cos()`; these functions expect radians, so an angle in degrees must be converted to radians with `radians()` before giving it to the function.
- The arcsine function in `math` is called `asin()`; in NumPy it's `arcsin()`.
- The function should return distances **to the nearest km**.
- You should get the following results from your function:
- The distance from (0, 0) to (0, 1) is 111 km.
- The distance from (0, 2.35) to (90, 2.35) is 10008 km. [(Why?)](https://en.wikipedia.org/wiki/History_of_the_metre)
- The distance from (44.65, -63.58) to (53.73, -1.86) is 4448 km.
## A quick reminder how this works
This document is formatted in [Markdown](https://daringfireball.net/projects/markdown/).
You can retrieve your data, which is always a string, by choosing a **``** (also a string). This ensures that you have different data from other people, so be creative.
```
url = 'https://kata.geosci.ai/challenge/birthquakes'
params = {
'key': # Replace with your own string.
}
r = requests.get(url, params)
r.text
```
To answer question 1, change the `params`:
```
params = {
'key': , # Use the same key you used to get your input.
'question': 1,
'answer': 1234 # Your answer; can be a float, int, list or array;
# the challenge description will tell you which.
}
```
To get a hint for a question, provide the question number but no answer:
```
params = {
'question': 1,
}
```
[Complete instructions at kata.geosci.ai](https://kata.geosci.ai/challenge)
[An example notebook to get you started](https://gist.github.com/kwinkunks/50f11dac6ab7ff8c3e6c7b34536501a2)
----
© 2023 [Scienxlab](https://scienxlab.org/) — Code: openly licensed under [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0) — Text: openly licensed under [CC BY](https://creativecommons.org/licenses/by/4.0/).