{ "metadata": { "kernelspec": { "codemirror_mode": { "name": "python", "version": 3 }, "display_name": "Python 3", "language": "python", "name": "python3" }, "name": "", "signature": "sha256:a21152aa59b7f81d8288957344f3f7883a5d0841e77769a6d74f8c6265912367" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from IPython.core.display import HTML\n", "\n", "with open('creative_commons.txt', 'r') as f:\n", " html = f.read()\n", " \n", "name = '2014-08-11-supermoon'\n", "\n", "html = '''\n", "\n", "
This post was written as an IPython notebook.\n", " It is available for download\n", " or as a static html.
\n", "\n", "%s''' % (name, name, html)\n", "\n", "%matplotlib inline\n", "from matplotlib import style\n", "style.use('ggplot')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Yesterday we had the super \"moon event\". Unfortunately Salvador was cloudy and\n", "the show was not as impressive as we expected. All the same, watching the sunset\n", "while the moon was rising by the\n", "[Barra lighthouse](http://pt.wikipedia.org/wiki/Farol_da_Barra_%28Bahia%29)\n", "was quite impressive. The super moon event inspired me to go back there next\n", "full moon to take a fell pictures. Sure I could just Google for the date and\n", "time. But let's do this the hard way." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next two functions are part of a previous post, so I will not\n", "get into the details. Just to remember: `get_ip` returns the\n", "local machine ip address and `get_location` gets, among other things,\n", "the geographical position." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import re\n", "import urllib\n", "from pygeoip import GeoIP\n", "\n", "def get_ip(url='http://checkip.dyndns.org'):\n", " request = urllib.request.urlopen(url).read().decode('utf-8')\n", " return re.findall(r\"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}.\\d{1,3}\", request)[0]\n", "\n", "def get_location(ip, fname):\n", " \"\"\"Database can be downloaded at:\n", " http://dev.maxmind.com/geoip/legacy/geolite/\"\"\"\n", " gi = GeoIP(fname)\n", " location = gi.record_by_addr(ip)\n", " return location" ], "language": "python", "metadata": { "internals": {}, "slideshow": { "slide_type": "-" } }, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We get a nice dictionary back with all the information we need." ] }, { "cell_type": "code", "collapsed": false, "input": [ "ip = get_ip()\n", "fname = './data/GeoLiteCity.dat'\n", "location = get_location(ip, fname)\n", "location" ], "language": "python", "metadata": { "internals": {}, "slideshow": { "slide_type": "-" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "{'country_name': 'Brazil',\n", " 'continent': 'SA',\n", " 'longitude': -38.516699999999986,\n", " 'country_code3': 'BRA',\n", " 'country_code': 'BR',\n", " 'city': 'Salvador',\n", " 'time_zone': 'America/Bahia',\n", " 'area_code': 0,\n", " 'region_code': '05',\n", " 'latitude': -12.983300000000014,\n", " 'metro_code': None,\n", " 'dma_code': 0,\n", " 'postal_code': None}" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can create a [pyephem](http://rhodesmill.org/pyephem/) `moon` object," ] }, { "cell_type": "code", "collapsed": false, "input": [ "import ephem\n", "import numpy as np\n", "from datetime import date\n", "\n", "date = date(2014, 8, 10)\n", "moon = ephem.Moon()\n", "\n", "obs = ephem.Observer()\n", "obs.date = date\n", "obs.lon = np.deg2rad(location['longitude'])\n", "obs.lat = np.deg2rad(location['latitude'])\n", "obs" ], "language": "python", "metadata": { "internals": {}, "slideshow": { "slide_type": "-" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "This post was written as an IPython notebook.\n", " It is available for download\n", " or as a static html.
\n", "\n", "