<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Interactively rendering weather maps from the KNMI database\n",
    "In the process of analysing any kind of meteorological data, it is important to comprehend the synoptic situation, *the bigger picture*. Therefore, I often visit the KNMI database to view weathermaps, but to get to the desired map requires an unpleasant and time consuming amount of mouse clicks. Therefore, I developed a small script that is able to automatically find the correct weather map given a date and time, and to visualize it immediately in the Jupyter notebook.\n",
    "\n",
    "##### Note! \n",
    "*Unfortunately, the widgets are not visible when the notebook is viewed outside ipython. As far as I know, this is currently not possible. If anyone knows how to do it, please let me know.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing and displaying the weather maps\n",
    "Every weather map is stored in the KNMI database with a standard format, e.g.:\n",
    "http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_2012052812.gif.\n",
    "There are several ways to load such a figure in the notebook, the most simple solution is\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_2012052812.gif\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Required package\n",
    "from IPython.display import Image\n",
    "\n",
    "weblink = 'http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_2012052812.gif'\n",
    "Image(url=weblink)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's a good start. The next step is to automatically generate the correct url, given any date and time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The nearest operational analysis time is 2016-03-19 12:00:00\n",
      "As a string, this looks like 2016031912\n",
      "The corresponding map: http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_2016031912.gif\n"
     ]
    }
   ],
   "source": [
    "# Required package\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "def find_weathermap(timestamp):\n",
    "    ''' Given a datetime object, find the url corresponding to the closest operation analysis map on the KNMI database'''\n",
    "    # First, find the time corresponding to the nearest operational analysis\n",
    "    nearest_6hour = round((timestamp.hour+timestamp.minute/60.)/6)*6 # yields either 0, 6, 12, 18 or 24(!)\n",
    "    nearest_analysis = datetime(timestamp.year,timestamp.month,timestamp.day) + timedelta(hours=nearest_6hour)\n",
    "    print 'The nearest operational analysis time is',nearest_analysis\n",
    "    # Then, convert the datetime object to a string\n",
    "    timestring = nearest_analysis.strftime('%Y%m%d%H')\n",
    "    print 'As a string, this looks like', timestring\n",
    "    # Finally, embed the string in the url\n",
    "    url = 'http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_'+timestring+'.gif'  \n",
    "    print 'The corresponding map:',url\n",
    "    return url\n",
    "\n",
    "# Demonstration\n",
    "x = datetime.now()\n",
    "weblink = find_weathermap(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This result can be plotted, but only if the current 'nearest weather map' is already there."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding interactivity\n",
    "This demonstrates how weathermaps can be viewed interactively with the help of widgets. First, I will let the user choose some dates. Then, I will use a slider to visualize weather maps in a 'loop'."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_2010010700.gif\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Required package\n",
    "from IPython.display import Image, display\n",
    "from IPython.html.widgets import interact\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# We use the function as above, but without the print statements\n",
    "def find_weathermap(timestamp):\n",
    "    ''' Given a datetime object, find the url corresponding to the closest operation analysis map on the KNMI database'''\n",
    "    # First, find the time corresponding to the nearest operational analysis\n",<commit_msg>Small fix<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Interactively rendering weather maps from the KNMI database\n",
    "*<NAME>, March 2016*\n",
    "\n",
    "In the process of analysing any kind of meteorological data, it is important to comprehend the synoptic situation, *the bigger picture*. Therefore, I often visit the KNMI database to view weathermaps, but to get to the desired map requires an unpleasant and time consuming amount of mouse clicks. Therefore, I developed a small script that is able to automatically find the correct weather map given a date and time, and to visualize it immediately in the Jupyter notebook.\n",
    "\n",
    "##### Note! \n",
    "*Unfortunately, the widgets are not visible when the notebook is viewed outside ipython. As far as I know, this is currently not possible. If anyone knows how to do it, please let me know.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Accessing and displaying the weather maps\n",
    "Every weather map is stored in the KNMI database with a standard format, e.g.:\n",
    "http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_2012052812.gif.\n",
    "There are several ways to load such a figure in the notebook, the most simple solution is\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_2012052812.gif\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Required package\n",
    "from IPython.display import Image\n",
    "\n",
    "weblink = 'http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_2012052812.gif'\n",
    "Image(url=weblink)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That's a good start. The next step is to automatically generate the correct url, given any date and time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The nearest operational analysis time is 2016-03-19 18:00:00\n",
      "As a string, this looks like 2016031918\n",
      "The corresponding map: http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_2016031918.gif\n"
     ]
    }
   ],
   "source": [
    "# Required package\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "def find_weathermap(timestamp):\n",
    "    ''' Given a datetime object, find the url corresponding to the closest operation analysis map on the KNMI database'''\n",
    "    # First, find the time corresponding to the nearest operational analysis\n",
    "    nearest_6hour = round((timestamp.hour+timestamp.minute/60.)/6)*6 # yields either 0, 6, 12, 18 or 24(!)\n",
    "    nearest_analysis = datetime(timestamp.year,timestamp.month,timestamp.day) + timedelta(hours=nearest_6hour)\n",
    "    print 'The nearest operational analysis time is',nearest_analysis\n",
    "    # Then, convert the datetime object to a string\n",
    "    timestring = nearest_analysis.strftime('%Y%m%d%H')\n",
    "    print 'As a string, this looks like', timestring\n",
    "    # Finally, embed the string in the url\n",
    "    url = 'http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_'+timestring+'.gif'  \n",
    "    print 'The corresponding map:',url\n",
    "    return url\n",
    "\n",
    "# Demonstration\n",
    "x = datetime.now()\n",
    "weblink = find_weathermap(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This result can be plotted, but only if the current 'nearest weather map' is already there."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding interactivity\n",
    "This demonstrates how weathermaps can be viewed interactively with the help of widgets. First, I will let the user choose some dates. Then, I will use a slider to visualize weather maps in a 'loop'."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"http://cdn.knmi.nl/knmi/map/page/klimatologie/daggegevens/weerkaarten/analyse_2010010100.gif\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<function __main__.view_map>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Required package\n",
    "from IPython.display import Image, display\n",
    "from IPython.html.widgets import interact,fixed\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# We use the function as above, but without the print statements\n",
    "def find_weathermap(timestamp):\n",
    "    ''' Given a datetime object, find the url corresponding to the closest operation analysis map on the KNMI database'''\n",
    "    # First, find the time corresponding to the nearest operational analysis\n",
{'fraction_non_alphanumeric': 0.1448338643604916, 'fraction_numerical': 0.017569412835685024, 'mean_word_length': 2.892983699503898, 'pattern_counts': {'":': 101, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6690745', 'n_tokens_mistral': 3776, 'n_tokens_neox': 3564, 'n_words': 1007}