{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Download and read raw data\n",
    "## Download"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before you download data, make sure that you specific the pass to the raw data directory (`rawdir`) in `ninolearn.pathes`. \n",
    "\n",
    "In this tutorial, we download the monthly Oceaninc Nino Index, the Warm Water Volume (WWV), the dipole mode index (DMI) of the Indian ocean dipole (IOD) and sea surface temparatures from the ERSSTv5 data set and\n",
    "sea surface heights from the ORAS4 data set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import the necessary methods and classes\n",
    "from ninolearn.download import download, sources"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NOTE: If the data was already downloaded, it won't be downloaded again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sst.mnmean.nc already downloaded\n",
      "oni.txt already downloaded\n",
      "iod.txt already downloaded\n",
      "wwv.dat already downloaded\n",
      "Download air.mon.mean.nc\n"
     ]
    }
   ],
   "source": [
    "download(sources.SST_ERSSTv5)\n",
    "download(sources.ONI)\n",
    "download(sources.IOD)\n",
    "download(sources.WWV)\n",
    "download(sources.SAT_monthly_NCEP)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The sources all are dictionaries which have keywords that specify the download:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'downloadType': 'ftp', 'filename': 'sst.mnmean.nc', 'host': 'ftp.cdc.noaa.gov', 'location': '/Datasets/noaa.ersst.v5/'}\n"
     ]
    }
   ],
   "source": [
    "print(sources.SST_ERSSTv5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'downloadType': 'http', 'url': 'https://www.cpc.ncep.noaa.gov/data/indices/oni.ascii.txt', 'filename': 'oni.txt'}\n"
     ]
    }
   ],
   "source": [
    "print(sources.ONI)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can see that that the two sources above have  entries different downloadTypes. The SST is downloaded from an ftp-server, whereas the ONI is downloaded via http."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read raw data\n",
    "\n",
    "Routines are available in the `ninolearn.IO.read_raw` module with which it is directly possible to read the raw data as it is."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ninolearn.IO.read_raw import oni, sst_ERSSTv5, sat, wwv_anom, iod\n",
    "\n",
    "ONI = oni()\n",
    "SST = sst_ERSSTv5()\n",
    "SAT = sat(mean='monthly')\n",
    "WWV = wwv_anom()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's have a look how the raw data looks like!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "
\n",
       "\n",
       "
\n",
       "  \n",
       "    \n",
       "      | \n",
       " | SEAS\n",
       " | YR\n",
       " | TOTAL\n",
       " | ANOM\n",
       " | 
\n",
       "  \n",
       "  \n",
       "    \n",
       "      | 0\n",
       " | DJF\n",
       " | 1950\n",
       " | 24.72\n",
       " | -1.53\n",
       " | 
\n",
       "    \n",
       "      | 1\n",
       " | JFM\n",
       " | 1950\n",
       " | 25.17\n",
       " | -1.34\n",
       " | 
\n",
       "    \n",
       "      | 2\n",
       " | FMA\n",
       " | 1950\n",
       " | 25.75\n",
       " | -1.16\n",
       " | 
\n",
       "    \n",
       "      | 3\n",
       " | MAM\n",
       " | 1950\n",
       " | 26.12\n",
       " | -1.18\n",
       " | 
\n",
       "    \n",
       "      | 4\n",
       " | AMJ\n",
       " | 1950\n",
       " | 26.32\n",
       " | -1.07\n",
       " | 
\n",
       "  \n",
       "
\n",
       "
\n",
       "\n",
       "
\n",
       "  \n",
       "    \n",
       "      | \n",
       " | date\n",
       " | Volume\n",
       " | Anomaly\n",
       " | 
\n",
       "  \n",
       "  \n",
       "    \n",
       "      | 0\n",
       " | 198001\n",
       " | 2.605404e+15\n",
       " | 7.657363e+13\n",
       " | 
\n",
       "    \n",
       "      | 1\n",
       " | 198002\n",
       " | 2.564434e+15\n",
       " | 7.004931e+13\n",
       " | 
\n",
       "    \n",
       "      | 2\n",
       " | 198003\n",
       " | 2.514065e+15\n",
       " | 5.240853e+13\n",
       " | 
\n",
       "    \n",
       "      | 3\n",
       " | 198004\n",
       " | 2.468250e+15\n",
       " | 4.008869e+13\n",
       " | 
\n",
       "    \n",
       "      | 4\n",
       " | 198005\n",
       " | 2.439852e+15\n",
       " | 4.020975e+13\n",
       " | 
\n",
       "  \n",
       "
\n",
       "