forked from stat157/recent-quakes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecent Earthquakes.py
67 lines (41 loc) · 2.05 KB
/
Recent Earthquakes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <markdowncell>
# Use [urllib](http://docs.python.org/2/library/urllib.html) to open arbitrary resources by URL and pass that data to the [read_csv](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html) function of pandas. Then print out the first few rows of data using pandas [Indexing and Selecting Data](http://pandas.pydata.org/pandas-docs/dev/indexing.html).
# <codecell>
import urllib
from pandas import read_csv
url = 'http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M1.txt'
data = read_csv(urllib.urlopen(url))
data[0:10]
# <markdowncell>
# **UH OH!** Note that our data is a bit *dirty* and contains a notice that this data feed has been deprecated:
# <codecell>
print data[0:1]['Src'].values[0]
# <markdowncell>
# We can filter out the dirty data using [dropna](http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.dropna.html) to drop any rows that contain **NaN**.
# <codecell>
clean_data = data.dropna(axis=0, how='any')
clean_data[0:3]
# <markdowncell>
# In the code above note that I saved the results of `data.dropna()` into a different variable `clean_data` rather than over-writing the old variable `data`. **Why?** Why not just re-use old variable names? And if we did re-use old variable names what extra danger do we have to keep in mind while using the IPython Notebook?
# <markdowncell>
# Now let's just focus on earthquakes in Alaska (my home state! :)
# <codecell>
alaska = clean_data[clean_data.Src == 'ak']
alaska[0:10]
# <codecell>
from mpl_toolkits.basemap import Basemap
def plot_quakes(quakes):
m = Basemap(llcrnrlon=-180,llcrnrlat=50.,
urcrnrlon=-120.,urcrnrlat=72,
resolution='l',area_thresh=1000.,projection='merc',
lat_0=62.9540,lon_0=-149.2697)
m.drawcoastlines()
m.drawcountries()
m.fillcontinents(color='coral',lake_color='blue')
m.drawmapboundary(fill_color='aqua')
x, y = m(quakes.Lon, quakes.Lat)
m.plot(x, y, 'k.')
return m
plot_quakes(alaska)