{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "
\n", "
ObsPy Tutorial
\n", "
Handling Time
\n", "
\n", "
\n", "
\n", "image: User:Abbaszade656 / Wikimedia Commons / CC-BY-SA-4.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Workshop for the \"Training in Network Management Systems and Analytical Tools for Seismic\"\n", "### Baku, October 2018\n", "\n", "\n", "Seismo-Live: http://seismo-live.org\n", "\n", "##### Authors:\n", "* Lion Krischer ([@krischer](https://github.com/krischer))\n", "* Tobias Megies ([@megies](https://github.com/megies))\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/obspy_logo_full_524x179px.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a bit dry but not very difficult and important to know. It is used **everywhere** in ObsPy!\n", "\n", "\n", "* All absolute time values are consistently handled with this class.\n", "* Based on a nanosecond precision POSIX integer timestamp for accuracy.\n", "* Timezone can be specified at initialization (if necessary).\n", "* In Coordinated Universal Time (UTC) so no need to deal with timezones, daylight savings, ...\n", "\n", "---" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline\n", "from __future__ import print_function\n", "import matplotlib.pyplot as plt\n", "plt.style.use('ggplot')\n", "plt.rcParams['figure.figsize'] = 12, 8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "## Features of **`UTCDateTime`**\n", "\n", "#### Initialization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from obspy import UTCDateTime\n", "\n", "print(UTCDateTime(\"2014-08-24T10:20:44.0\")) # mostly time strings defined by ISO standard\n", "print(UTCDateTime(\"2014-08-24T01:20:44.0-09:00\")) # non-UTC timezone input\n", "print(UTCDateTime(2014, 8, 24, 10, 20, 44)) # year, month, day, hour, min, sec, musec\n", "print(UTCDateTime(1408875644.0)) # timestamp" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Current time can be initialized by leaving out any arguments\n", "print(UTCDateTime())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Attribute Access" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "time = UTCDateTime(\"2014-08-24T10:20:44.0\")\n", "print(time.year)\n", "print(time.julday)\n", "print(time.timestamp)\n", "print(time.weekday)\n", "# try time." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Handling time differences\n", "\n", "* \"**`+`**/**`-`**\" defined to add seconds to an **`UTCDateTime`** object\n", "* \"**`-`**\" defined to get time difference of two **`UTCDateTime`** objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "time = UTCDateTime(\"2014-08-24T10:20:44.0\")\n", "print(time)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# one hour later\n", "print(time + 3600)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Time differences\n", "time2 = UTCDateTime(2015, 1, 1)\n", "print(time2 - time)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises\n", "\n", "Calculate the number of days passed since the 2014 South Napa earthquake (the timestamp used above)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print((UTCDateTime() - UTCDateTime(\"2014-08-24T11:20:44.000000Z\")) / 86400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make a list of 10 UTCDateTime objects, starting today at 10:00 with a spacing of 90 minutes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below is a list of strings with origin times of magnitude 8+ earthquakes since 2000 (fetched from IRIS). Assemble a list of interevent times in days. Use matplotlib to display a histogram." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "times = [\"2000-11-16T04:54:56\",\n", " \"2001-06-23T20:33:09\",\n", " \"2003-09-25T19:50:07\",\n", " \"2004-12-23T14:59:00\",\n", " \"2004-12-26T00:58:52\",\n", " \"2005-03-28T16:09:35\",\n", " \"2006-05-03T15:26:39\",\n", " \"2006-06-01T18:57:02\",\n", " \"2006-06-05T00:50:31\",\n", " \"2006-11-15T11:14:14\",\n", " \"2007-01-13T04:23:23\",\n", " \"2007-04-01T20:39:56\",\n", " \"2007-08-15T23:40:58\",\n", " \"2007-09-12T11:10:26\",\n", " \"2009-09-29T17:48:11\",\n", " \"2010-02-27T06:34:13\",\n", " \"2011-03-11T05:46:23\",\n", " \"2012-04-11T08:38:37\",\n", " \"2012-04-11T10:43:10\",\n", " \"2013-05-24T05:44:49\",\n", " \"2014-04-01T23:46:47\",\n", " \"2015-09-16T22:54:32\",\n", " \"2017-09-08T04:49:21\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }