DataAnalysis2022/ObsPy/02_UTCDateTime.ipynb

1 line
5.7 KiB
Plaintext
Raw Normal View History

{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["<div style='background-image: url(\"../share/images/header.svg\") ; padding: 0px ; background-size: cover ; border-radius: 5px ; height: 250px'>\n", " <div style=\"float: right ; margin: 50px ; padding: 20px ; background: rgba(255 , 255 , 255 , 0.7) ; width: 50% ; height: 150px\">\n", " <div style=\"position: relative ; top: 50% ; transform: translatey(-50%)\">\n", " <div style=\"font-size: xx-large ; font-weight: 900 ; color: rgba(0 , 0 , 0 , 0.8) ; line-height: 100%\">ObsPy Tutorial</div>\n", " <div style=\"font-size: large ; padding-top: 20px ; color: rgba(0 , 0 , 0 , 0.5)\">Handling Time</div>\n", " </div>\n", " </div>\n", "</div>"]}, {"cell_type": "markdown", "metadata": {}, "source": ["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 double precision POSIX 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": {}, "outputs": [], "source": ["%matplotlib inline\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(\"2011-03-11T05:46:23.2\")) # mostly time strings defined by ISO standard\n", "print(UTCDateTime(\"2011-03-11T14:46:23.2+09:00\")) # non-UTC timezone input\n", "print(UTCDateTime(2011, 3, 11, 5, 46, 23, 2))\n", "print(UTCDateTime(1299822383.2))"]}, {"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(\"2011-03-11T05:46:23.200000Z\")\n", "print(time.year)\n", "print(time.julday)\n", "print(time.timestamp)\n", "print(time.weekday)\n", "# try time.<Tab>"]}, {"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(\"2011-03-11T05:46:23.200000Z\")\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": ["time2 = UTCDateTime(2012, 1, 1)\n", "print(time2 - time)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Exercises\n", "\n", "Calculate the number of days passed since the Tohoku main shock (the timestamp used above)."]}, {"cell_type": "code", "execution_count": null, "metadata": {"lines_to_next_cell": 2, "tags": ["exercise"]}, "outputs": [], "source": []}, {"cell_type": "code", "execution_count": null, "metadata": {"tags": ["solution"]}, "outputs": [], "source": []}, {"cell_type": "markdown", "metadata": {}, "source": ["Make a list of