{ "metadata": { "name": "", "signature": "sha256:5c55a4f7537053700661b6b946b8271c9abe1c00d780222278d12fea52fa3e93" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "http://earthquake.usgs.gov/earthquakes/feed/v1.0/" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!wget http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.quakeml" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "--2016-03-29 12:51:49-- http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.quakeml\r\n", "Resolving earthquake.usgs.gov (earthquake.usgs.gov)... 173.223.52.217, 173.223.52.216, 2607:f380:1:6001::cc66:7229, ...\r\n", "Connecting to earthquake.usgs.gov (earthquake.usgs.gov)|173.223.52.217|:80... connected.\r\n", "HTTP request sent, awaiting response... " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "200 OK\r\n", "Length: 9270 (9,1K) [application/xml]\r\n", "Saving to: \u2018all_hour.quakeml.2\u2019\r\n", "\r\n", "\r", "all_hour.quakeml.2 0%[ ] 0 --.-KB/s \r", "all_hour.quakeml.2 100%[=====================>] 9,05K --.-KB/s in 0s \r\n", "\r\n", "2016-03-29 12:51:49 (319 MB/s) - \u2018all_hour.quakeml.2\u2019 saved [9270/9270]\r\n", "\r\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "#XML (eXtensible Markup language)\n", "\n", "https://www.w3.org/XML/\n", "\n", "https://en.wikipedia.org/wiki/XML" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import xml.etree.ElementTree as ET\n", "tree = ET.parse('all_hour.quakeml')\n", "root = tree.getroot()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Good tutorial on ElementTree:\n", "http://getpython3.com/diveintopython3/xml.html#xml-parse\n", "\n", "Other options: \n", "\n", "* http://docs.python.org/2/library/markup.html\n", "* http://lxml.de/index.html\n", "* http://www.crummy.com/software/BeautifulSoup/" ] }, { "cell_type": "code", "collapsed": false, "input": [ "root" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://quake.ethz.ch/quakeml/Documents" ] }, { "cell_type": "code", "collapsed": false, "input": [ "root.tag" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "'{http://quakeml.org/xmlns/quakeml/1.2}quakeml'" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "root.attrib" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "{}" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "for child in root:\n", " print child.tag, \":\", child.attrib" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{http://quakeml.org/xmlns/bed/1.2}eventParameters : {'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.quakeml#requested=2014-03-25T20:27:27.000Z'}\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "root[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "eventParameters = root[0]\n", "eventParameters.getchildren()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "[,\n", " ,\n", " ,\n", " ,\n", " ,\n", " ,\n", " ,\n", " ]" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "for child in eventParameters:\n", " print child.tag, \":\", child.attrib" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '72188865', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72188865.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'nc', '{http://anss.org/xmlns/catalog/0.1}datasource': 'nc'}\n", "{http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '72188860', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72188860.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'nc', '{http://anss.org/xmlns/catalog/0.1}datasource': 'nc'}\n", "{http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '72188855', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72188855.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'nc', '{http://anss.org/xmlns/catalog/0.1}datasource': 'nc'}\n", "{http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '11198395', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ak11198395.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'ak', '{http://anss.org/xmlns/catalog/0.1}datasource': 'ak'}\n", "{http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': 'c000nq6f', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/usc000nq6f.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'us', '{http://anss.org/xmlns/catalog/0.1}datasource': 'us'}\n", "{http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '11198336', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ak11198336.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'ak', '{http://anss.org/xmlns/catalog/0.1}datasource': 'ak'}\n", "{http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '15480193', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ci15480193.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'ci', '{http://anss.org/xmlns/catalog/0.1}datasource': 'ci'}\n", "{http://quakeml.org/xmlns/bed/1.2}creationInfo : {}\n" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "eventParameters.findall(\"{http://quakeml.org/xmlns/bed/1.2}creationInfo\")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "[]" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "creationInfo = eventParameters.findall(\"{http://quakeml.org/xmlns/bed/1.2}creationInfo\")\n", "creationInfo[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "for child in creationInfo[0]:\n", " print child" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "creationTime = creationInfo[0].findall('{http://quakeml.org/xmlns/bed/1.2}creationTime')\n", "creationTime" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "[]" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "creationTime[0].text" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "'2014-03-25T20:27:27.000Z'" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "events = eventParameters.findall('{http://quakeml.org/xmlns/bed/1.2}event')\n", "events" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "[,\n", " ,\n", " ,\n", " ,\n", " ,\n", " ,\n", " ]" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "events" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "[,\n", " ,\n", " ,\n", " ,\n", " ,\n", " ,\n", " ]" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "for e in events[0]:\n", " print e" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "\n", "\n", "\n", "\n" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "------------------------------\n", "By: Andr\u00e9s Cabrera mantaraya36@gmail.com\n", "\n", "For Course MAT 240F at UCSB\n", "\n", "This ipython notebook is licensed under the CC-BY-NC-SA license: http://creativecommons.org/licenses/by-nc-sa/4.0/\n", "\n", "![http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png](http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png)" ] } ], "metadata": {} } ] }