Loading...

Sunday, August 24, 2014

Earthquakes and USGS


Hello Readers,

Early Sunday morning on the 24th, there was an 6.0 magnitude earthquake which hit around 3:20 AM near the San Francisco Bay area by Napa and American Canyon. From the U.S. Geological Survey, we can find the earthquake information.

Here we will use Python (2.7.5) to retrieve and plot the magnitudes of recent recorded earthquakes by the USGS.


Using Python to Access USGS

Opening the command prompt, start iPython with matplotlib module. It should look something like this:


Figure 1. iPython

Then we need to import urllib, and pylab for URL access and plotting, respectively. Using the ".urlopen()" method in urllib we will open the desired URL- the significant earthquakes in the past day, updated every 5 minutes as a CSV file (here).


Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
In [1]: import urllib

In [2]: url = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_week.csv'

In [3]: quakes = urllib.urlopen(url)

In [4]: for record in quakes:
   ...:     print(record)
   ...:

time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net,id,updated,place,type

2014-08-24T23:21:45.980Z,-14.5864,-73.5777,101,6.9,mww,,23,4.095,1.32,us,usb000s5x1,2014-08-25T00:50
:04.000Z,"43km ENE of Tambo, Peru",earthquake

2014-08-24T10:20:44.000Z,38.2202,-122.3128,11.3,6,Mw,146,28.8,0.03593261,0.16,nc,nc72282711,2014-08-
25T01:13:29.811Z,"6km NW of American Canyon, California",earthquake

2014-08-23T22:32:23.630Z,-32.7049,-71.3967,35,6.4,mww,,30,0.419,0.9,us,usb000s5rc,2014-08-24T11:48:3
9.000Z,"18km WNW of Hacienda La Calera, Chile",earthquake

2014-08-19T12:41:35.220Z,35.8186,-97.4718,3.46,4.2,mb,,29,0.025,0.69,us,usb000s45z,2014-08-24T06:27:
52.792Z,"7km SSW of Guthrie, Oklahoma",earthquake

We iterate through each line with "for" and print the all the records. As you can see, the first line is the header, containing the names of the columns. In the second set of earthquake information (3rd row), we spot the early morning earthquake in California.



Organizing into DataFrame

Since the rows are quite difficult to read, we can transform "quakes" into a DataFrame, using the ".from_csv()" method in DataFrame. It generates a tidy table from which we can view the data in the output below.

Code:
 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
In [5]: from pandas import DataFrame

In [6]: import pandas as pd

In [7]: quakes = urllib.urlopen(url)

In [8]: frame = DataFrame.from_csv(quakes)

In [9]: frame
Out[9]:
                            latitude  longitude   depth  mag magType  nst  \
time
2014-08-24 23:21:45.980000  -14.5864   -73.5777  101.00  6.9     mww  NaN
2014-08-24 10:20:44          38.2202  -122.3128   11.30  6.0      Mw  146
2014-08-23 22:32:23.630000  -32.7049   -71.3967   35.00  6.4     mww  NaN
2014-08-19 12:41:35.220000   35.8186   -97.4718    3.46  4.2      mb  NaN

                             gap      dmin   rms net          id  \
time
2014-08-24 23:21:45.980000  23.0  4.095000  1.32  us  usb000s5x1
2014-08-24 10:20:44         28.8  0.035933  0.16  nc  nc72282711
2014-08-23 22:32:23.630000  30.0  0.419000  0.90  us  usb000s5rc
2014-08-19 12:41:35.220000  29.0  0.025000  0.69  us  usb000s45z

                                             updated  \
time
2014-08-24 23:21:45.980000  2014-08-25T00:50:04.000Z
2014-08-24 10:20:44         2014-08-25T01:28:14.766Z
2014-08-23 22:32:23.630000  2014-08-24T11:48:39.000Z
2014-08-19 12:41:35.220000  2014-08-24T06:27:52.792Z

                                                            place        type
time
2014-08-24 23:21:45.980000                43km ENE of Tambo, Peru  earthquake
2014-08-24 10:20:44         6km NW of American Canyon, California  earthquake
2014-08-23 22:32:23.630000  18km WNW of Hacienda La Calera, Chile  earthquake
2014-08-19 12:41:35.220000           7km SSW of Guthrie, Oklahoma  earthquake

The 3:20 AM earthquake occured 6 kilometers North West of American Canyon in California  with a magnitude of 6.0 from the 'place' variable. More recently, there was another earthquake in Peru with a magnitude of 6.9.



Plotting Magnitude

We can compare the magnitudes by using the ".plot()" function. We specify the x-axis to be the 'place' and the y-axis to be the 'mag', or magnitude, and we want a horizontal bar plot through 'kind='barh''.

Code:
1
2
In [10]: frame.plot(x='place',y='mag',kind='barh')
Out[10]: <matplotlib.axes.AxesSubplot at 0x87682b0>
Figure 2. Plot of Earthquake Magnitudes


Great! That concludes this short post of using Python to retrieve CSV files from the USGS and basic plotting. Stay tuned for more posts!



Thanks for reading,

Wayne
@beyondvalence
LinkedIn

1 comment:

  1. Deposit Menggunakan GO Pay di Agen Judi Online terpopuler di Indonesia !

    Minimal Deposit 50ribu saja, Tersedia di situs www.bolavita.fun Anda dapat menikmati berbagai jenis judi online yang tersedia ...

    Info selengkapnya silahkan hubungi :
    WA : +62812-2222-995
    BBM : BOLAVITA

    Keluaran Togel Singapura Hari Ini yang terbaru

    ReplyDelete