{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import cmocean\n",
    "import matplotlib.pylab as plt\n",
    "from scipy.interpolate import griddata\n",
    "from scipy import interpolate\n",
    "from datetime import datetime\n",
    "import pdb\n",
    "import os\n",
    "import csv\n",
    "\n",
    "from datetime import datetime, timedelta\n",
    "import calendar\n",
    "\n",
    "import matplotlib\n",
    "matplotlib.font_manager._rebuild()\n",
    "\n",
    "#used for map projections\n",
    "from cartopy import config\n",
    "import cartopy.crs as ccrs\n",
    "import matplotlib.patches as mpatches\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "#sets plot styles\n",
    "import seaborn as sns\n",
    "from matplotlib import rc\n",
    "from matplotlib import rcParams\n",
    "import matplotlib.ticker as mtick\n",
    "rc('text', usetex=False)\n",
    "rcStyle = {\"font.size\": 10,\n",
    "           \"axes.titlesize\": 20,\n",
    "           \"axes.labelsize\": 20,\n",
    "           'xtick.labelsize': 16,\n",
    "           'ytick.labelsize': 16}\n",
    "sns.set_context(\"paper\", rc=rcStyle)\n",
    "sns.set_style(\"whitegrid\", {'axes.grid' : False})\n",
    "myColors = [\"windows blue\", \"amber\", \"dusty rose\", \"prussian blue\", \"faded green\", \"dusty purple\", \"gold\", \"dark pink\", \"green\", \"red\", \"brown\"]\n",
    "colorsBW = [\"black\", \"grey\"]\n",
    "sns.set_palette(sns.xkcd_palette(myColors))\n",
    "\n",
    "curDir = os.getcwd()\n",
    "dataDir = os.path.join(curDir, 'data')\n",
    "\n",
    "if not os.path.exists(dataDir):\n",
    "    os.mkdir(dataDir)\n",
    "    \n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Get a BGC profile\n",
    "\n",
    "If you know that a profile contains BGC parameters, the standard profile api contains the bgc measurements under the field bgcMeas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_profile(profile_number):\n",
    "    url = 'https://argovis.colorado.edu/catalog/profiles/{}'.format(profile_number)\n",
    "    resp = requests.get(url)\n",
    "    # Consider any status other than 2xx an error\n",
    "    if not resp.status_code // 100 == 2:\n",
    "        return \"Error: Unexpected response {}\".format(resp)\n",
    "    profile = resp.json()\n",
    "    return profile\n",
    "\n",
    "def json2dataframe(profiles, measKey='measurements'):\n",
    "    \"\"\" convert json data to Pandas DataFrame \"\"\"\n",
    "    # Make sure we deal with a list\n",
    "    if isinstance(profiles, list):\n",
    "        data = profiles\n",
    "    else:\n",
    "        data = [profiles]\n",
    "    # Transform\n",
    "    rows = []\n",
    "    for profile in data:\n",
    "        keys = [x for x in profile.keys() if x not in ['measurements', 'bgcMeas']]\n",
    "        meta_row = dict((key, profile[key]) for key in keys)\n",
    "        for row in profile[measKey]:\n",
    "            row.update(meta_row)\n",
    "            rows.append(row)\n",
    "    df = pd.DataFrame(rows)\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> <ipython-input-44-da136a77d156>(20)json2dataframe()\n",
      "-> for profile in data:\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "(Pdb)  c\n"
     ]
    }
   ],
   "source": [
    "profileId = \"5901069_270\"\n",
    "profile = get_profile(profileId)\n",
    "df = json2dataframe([profile], 'bgcMeas')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pres</th>\n",
       "      <th>pres_qc</th>\n",
       "      <th>psal</th>\n",
       "      <th>psal_qc</th>\n",
       "      <th>temp</th>\n",
       "      <th>temp_qc</th>\n",
       "      <th>doxy_qc</th>\n",
       "      <th>bgcMeasKeys</th>\n",
       "      <th>station_parameters</th>\n",
       "      <th>station_parameters_in_nc</th>\n",
       "      <th>...</th>\n",
       "      <th>jcommopsPlatform</th>\n",
       "      <th>euroargoPlatform</th>\n",
       "      <th>formatted_station_parameters</th>\n",
       "      <th>roundLat</th>\n",
       "      <th>roundLon</th>\n",
       "      <th>strLat</th>\n",
       "      <th>strLon</th>\n",
       "      <th>date_formatted</th>\n",
       "      <th>id</th>\n",
       "      <th>doxy</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.400000</td>\n",
       "      <td>1</td>\n",
       "      <td>35.058998</td>\n",
       "      <td>1</td>\n",
       "      <td>19.774000</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>[pres, psal, temp, doxy]</td>\n",
       "      <td>[pres, psal, temp]</td>\n",
       "      <td>[PRES, PSAL, TEMP, DOXY]</td>\n",
       "      <td>...</td>\n",
       "      <td>http://www.jcommops.org/board/wa/Platform?ref=...</td>\n",
       "      <td>https://fleetmonitoring.euro-argo.eu/float/590...</td>\n",
       "      <td>[ pres,  psal,  temp]</td>\n",
       "      <td>29.931</td>\n",
       "      <td>-173.291</td>\n",
       "      <td>29.931 N</td>\n",
       "      <td>173.291 W</td>\n",
       "      <td>2009-12-31</td>\n",
       "      <td>5901069_270</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9.300000</td>\n",
       "      <td>1</td>\n",
       "      <td>35.057999</td>\n",
       "      <td>1</td>\n",
       "      <td>19.778000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>[pres, psal, temp, doxy]</td>\n",
       "      <td>[pres, psal, temp]</td>\n",
       "      <td>[PRES, PSAL, TEMP, DOXY]</td>\n",
       "      <td>...</td>\n",
       "      <td>http://www.jcommops.org/board/wa/Platform?ref=...</td>\n",
       "      <td>https://fleetmonitoring.euro-argo.eu/float/590...</td>\n",
       "      <td>[ pres,  psal,  temp]</td>\n",
       "      <td>29.931</td>\n",
       "      <td>-173.291</td>\n",
       "      <td>29.931 N</td>\n",
       "      <td>173.291 W</td>\n",
       "      <td>2009-12-31</td>\n",
       "      <td>5901069_270</td>\n",
       "      <td>227.421417</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19.500000</td>\n",
       "      <td>1</td>\n",
       "      <td>35.057999</td>\n",
       "      <td>1</td>\n",
       "      <td>19.777000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>[pres, psal, temp, doxy]</td>\n",
       "      <td>[pres, psal, temp]</td>\n",
       "      <td>[PRES, PSAL, TEMP, DOXY]</td>\n",
       "      <td>...</td>\n",
       "      <td>http://www.jcommops.org/board/wa/Platform?ref=...</td>\n",
       "      <td>https://fleetmonitoring.euro-argo.eu/float/590...</td>\n",
       "      <td>[ pres,  psal,  temp]</td>\n",
       "      <td>29.931</td>\n",
       "      <td>-173.291</td>\n",
       "      <td>29.931 N</td>\n",
       "      <td>173.291 W</td>\n",
       "      <td>2009-12-31</td>\n",
       "      <td>5901069_270</td>\n",
       "      <td>222.475571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>29.700001</td>\n",
       "      <td>1</td>\n",
       "      <td>35.056000</td>\n",
       "      <td>1</td>\n",
       "      <td>19.768999</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>[pres, psal, temp, doxy]</td>\n",
       "      <td>[pres, psal, temp]</td>\n",
       "      <td>[PRES, PSAL, TEMP, DOXY]</td>\n",
       "      <td>...</td>\n",
       "      <td>http://www.jcommops.org/board/wa/Platform?ref=...</td>\n",
       "      <td>https://fleetmonitoring.euro-argo.eu/float/590...</td>\n",
       "      <td>[ pres,  psal,  temp]</td>\n",
       "      <td>29.931</td>\n",
       "      <td>-173.291</td>\n",
       "      <td>29.931 N</td>\n",
       "      <td>173.291 W</td>\n",
       "      <td>2009-12-31</td>\n",
       "      <td>5901069_270</td>\n",
       "      <td>219.328979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>39.400002</td>\n",
       "      <td>1</td>\n",
       "      <td>35.055000</td>\n",
       "      <td>1</td>\n",
       "      <td>19.768999</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>[pres, psal, temp, doxy]</td>\n",
       "      <td>[pres, psal, temp]</td>\n",
       "      <td>[PRES, PSAL, TEMP, DOXY]</td>\n",
       "      <td>...</td>\n",
       "      <td>http://www.jcommops.org/board/wa/Platform?ref=...</td>\n",
       "      <td>https://fleetmonitoring.euro-argo.eu/float/590...</td>\n",
       "      <td>[ pres,  psal,  temp]</td>\n",
       "      <td>29.931</td>\n",
       "      <td>-173.291</td>\n",
       "      <td>29.931 N</td>\n",
       "      <td>173.291 W</td>\n",
       "      <td>2009-12-31</td>\n",
       "      <td>5901069_270</td>\n",
       "      <td>216.657944</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        pres  pres_qc       psal  psal_qc       temp  temp_qc  doxy_qc  \\\n",
       "0   5.400000        1  35.058998        1  19.774000        1        4   \n",
       "1   9.300000        1  35.057999        1  19.778000        1        1   \n",
       "2  19.500000        1  35.057999        1  19.777000        1        1   \n",
       "3  29.700001        1  35.056000        1  19.768999        1        1   \n",
       "4  39.400002        1  35.055000        1  19.768999        1        1   \n",
       "\n",
       "                bgcMeasKeys  station_parameters  station_parameters_in_nc  \\\n",
       "0  [pres, psal, temp, doxy]  [pres, psal, temp]  [PRES, PSAL, TEMP, DOXY]   \n",
       "1  [pres, psal, temp, doxy]  [pres, psal, temp]  [PRES, PSAL, TEMP, DOXY]   \n",
       "2  [pres, psal, temp, doxy]  [pres, psal, temp]  [PRES, PSAL, TEMP, DOXY]   \n",
       "3  [pres, psal, temp, doxy]  [pres, psal, temp]  [PRES, PSAL, TEMP, DOXY]   \n",
       "4  [pres, psal, temp, doxy]  [pres, psal, temp]  [PRES, PSAL, TEMP, DOXY]   \n",
       "\n",
       "   ...                                   jcommopsPlatform  \\\n",
       "0  ...  http://www.jcommops.org/board/wa/Platform?ref=...   \n",
       "1  ...  http://www.jcommops.org/board/wa/Platform?ref=...   \n",
       "2  ...  http://www.jcommops.org/board/wa/Platform?ref=...   \n",
       "3  ...  http://www.jcommops.org/board/wa/Platform?ref=...   \n",
       "4  ...  http://www.jcommops.org/board/wa/Platform?ref=...   \n",
       "\n",
       "                                    euroargoPlatform  \\\n",
       "0  https://fleetmonitoring.euro-argo.eu/float/590...   \n",
       "1  https://fleetmonitoring.euro-argo.eu/float/590...   \n",
       "2  https://fleetmonitoring.euro-argo.eu/float/590...   \n",
       "3  https://fleetmonitoring.euro-argo.eu/float/590...   \n",
       "4  https://fleetmonitoring.euro-argo.eu/float/590...   \n",
       "\n",
       "  formatted_station_parameters roundLat  roundLon    strLat     strLon  \\\n",
       "0        [ pres,  psal,  temp]   29.931  -173.291  29.931 N  173.291 W   \n",
       "1        [ pres,  psal,  temp]   29.931  -173.291  29.931 N  173.291 W   \n",
       "2        [ pres,  psal,  temp]   29.931  -173.291  29.931 N  173.291 W   \n",
       "3        [ pres,  psal,  temp]   29.931  -173.291  29.931 N  173.291 W   \n",
       "4        [ pres,  psal,  temp]   29.931  -173.291  29.931 N  173.291 W   \n",
       "\n",
       "  date_formatted           id        doxy  \n",
       "0     2009-12-31  5901069_270         NaN  \n",
       "1     2009-12-31  5901069_270  227.421417  \n",
       "2     2009-12-31  5901069_270  222.475571  \n",
       "3     2009-12-31  5901069_270  219.328979  \n",
       "4     2009-12-31  5901069_270  216.657944  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Get a BGC Platform, two variables at a time\n",
    "Platform metadata is queried separatly from the BGC data. This is to keep the payload small enough for the server to operate efficiently.\n",
    "Platform BGC data is queried by two parameters at a time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_platform_profile_metadata(platform_number):\n",
    "    url = f'https://argovis.colorado.edu/catalog/platform_profile_metadata/{platform_number}'\n",
    "    print(url)\n",
    "    resp = requests.get(url)\n",
    "    # Consider any status other than 2xx an error\n",
    "    if not resp.status_code // 100 == 2:\n",
    "        return \"Error: Unexpected response {}\".format(resp)\n",
    "    platformMetadata = resp.json()\n",
    "    return platformMetadata\n",
    "\n",
    "def get_platform_profile_data(platform_number, xaxis='doxy', yaxis='pres'):\n",
    "    url = 'https://argovis.colorado.edu/catalog/bgc_platform_data/{0}/?xaxis={1}&yaxis={2}'.format(platform_number, xaxis, yaxis)\n",
    "    print(url)\n",
    "    resp = requests.get(url)\n",
    "    # Consider any status other than 2xx an error\n",
    "    if not resp.status_code // 100 == 2:\n",
    "        return \"Error: Unexpected response {}\".format(resp)\n",
    "    platformData = resp.json()\n",
    "    return platformData\n",
    "\n",
    "def join_platform_data(platformMetadata, platformData):\n",
    "    platforms = []\n",
    "    for idx, platform in enumerate(platformMetadata):\n",
    "        metadata_id = platform['_id']\n",
    "        data_id = platformData[idx]['_id']\n",
    "        if (metadata_id == data_id) and ('bgcMeas' in platformData[idx].keys()) and isinstance(platformData[idx]['bgcMeas'], list):\n",
    "            platform['bgcMeas'] = platformData[idx]['bgcMeas']\n",
    "            platforms.append(platform)\n",
    "    return platforms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We merge the metadata and data and convert it into a dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://argovis.colorado.edu/catalog/platform_profile_metadata/5901464\n",
      "https://argovis.colorado.edu/catalog/bgc_platform_data/5901464/?xaxis=doxy&yaxis=pres\n"
     ]
    }
   ],
   "source": [
    "platformMetadata = get_platform_profile_metadata(5901464)\n",
    "platformData = get_platform_profile_data(5901464, 'doxy', 'pres')\n",
    "platforms = join_platform_data(platformMetadata, platformData)\n",
    "df = json2dataframe(platforms, 'bgcMeas')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>doxy</th>\n",
       "      <th>doxy_qc</th>\n",
       "      <th>pres</th>\n",
       "      <th>pres_qc</th>\n",
       "      <th>_id</th>\n",
       "      <th>POSITIONING_SYSTEM</th>\n",
       "      <th>DATA_CENTRE</th>\n",
       "      <th>PI_NAME</th>\n",
       "      <th>WMO_INST_TYPE</th>\n",
       "      <th>DATA_MODE</th>\n",
       "      <th>...</th>\n",
       "      <th>cycle_number</th>\n",
       "      <th>dac</th>\n",
       "      <th>platform_number</th>\n",
       "      <th>station_parameters_in_nc</th>\n",
       "      <th>nc_url</th>\n",
       "      <th>PARAMETER_DATA_MODE</th>\n",
       "      <th>bgcMeasKeys</th>\n",
       "      <th>containsBGC</th>\n",
       "      <th>DIRECTION</th>\n",
       "      <th>BASIN</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>71.110497</td>\n",
       "      <td>1</td>\n",
       "      <td>1797.489990</td>\n",
       "      <td>1</td>\n",
       "      <td>5901464_115</td>\n",
       "      <td>GPS</td>\n",
       "      <td>AO</td>\n",
       "      <td>STEPHEN RISER,</td>\n",
       "      <td>846</td>\n",
       "      <td>D</td>\n",
       "      <td>...</td>\n",
       "      <td>115</td>\n",
       "      <td>aoml</td>\n",
       "      <td>5901464</td>\n",
       "      <td>[PRES, PSAL, TEMP, DOXY]</td>\n",
       "      <td>ftp://ftp.ifremer.fr/ifremer/argo/dac/aoml/590...</td>\n",
       "      <td>[[D, D, D, D]]</td>\n",
       "      <td>[pres, psal, temp, doxy]</td>\n",
       "      <td>True</td>\n",
       "      <td>A</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>75.924095</td>\n",
       "      <td>1</td>\n",
       "      <td>1848.199951</td>\n",
       "      <td>1</td>\n",
       "      <td>5901464_115</td>\n",
       "      <td>GPS</td>\n",
       "      <td>AO</td>\n",
       "      <td>STEPHEN RISER,</td>\n",
       "      <td>846</td>\n",
       "      <td>D</td>\n",
       "      <td>...</td>\n",
       "      <td>115</td>\n",
       "      <td>aoml</td>\n",
       "      <td>5901464</td>\n",
       "      <td>[PRES, PSAL, TEMP, DOXY]</td>\n",
       "      <td>ftp://ftp.ifremer.fr/ifremer/argo/dac/aoml/590...</td>\n",
       "      <td>[[D, D, D, D]]</td>\n",
       "      <td>[pres, psal, temp, doxy]</td>\n",
       "      <td>True</td>\n",
       "      <td>A</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>79.144302</td>\n",
       "      <td>1</td>\n",
       "      <td>1897.660034</td>\n",
       "      <td>1</td>\n",
       "      <td>5901464_115</td>\n",
       "      <td>GPS</td>\n",
       "      <td>AO</td>\n",
       "      <td>STEPHEN RISER,</td>\n",
       "      <td>846</td>\n",
       "      <td>D</td>\n",
       "      <td>...</td>\n",
       "      <td>115</td>\n",
       "      <td>aoml</td>\n",
       "      <td>5901464</td>\n",
       "      <td>[PRES, PSAL, TEMP, DOXY]</td>\n",
       "      <td>ftp://ftp.ifremer.fr/ifremer/argo/dac/aoml/590...</td>\n",
       "      <td>[[D, D, D, D]]</td>\n",
       "      <td>[pres, psal, temp, doxy]</td>\n",
       "      <td>True</td>\n",
       "      <td>A</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>81.345016</td>\n",
       "      <td>1</td>\n",
       "      <td>1947.329956</td>\n",
       "      <td>1</td>\n",
       "      <td>5901464_115</td>\n",
       "      <td>GPS</td>\n",
       "      <td>AO</td>\n",
       "      <td>STEPHEN RISER,</td>\n",
       "      <td>846</td>\n",
       "      <td>D</td>\n",
       "      <td>...</td>\n",
       "      <td>115</td>\n",
       "      <td>aoml</td>\n",
       "      <td>5901464</td>\n",
       "      <td>[PRES, PSAL, TEMP, DOXY]</td>\n",
       "      <td>ftp://ftp.ifremer.fr/ifremer/argo/dac/aoml/590...</td>\n",
       "      <td>[[D, D, D, D]]</td>\n",
       "      <td>[pres, psal, temp, doxy]</td>\n",
       "      <td>True</td>\n",
       "      <td>A</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>84.040329</td>\n",
       "      <td>1</td>\n",
       "      <td>1997.800049</td>\n",
       "      <td>1</td>\n",
       "      <td>5901464_115</td>\n",
       "      <td>GPS</td>\n",
       "      <td>AO</td>\n",
       "      <td>STEPHEN RISER,</td>\n",
       "      <td>846</td>\n",
       "      <td>D</td>\n",
       "      <td>...</td>\n",
       "      <td>115</td>\n",
       "      <td>aoml</td>\n",
       "      <td>5901464</td>\n",
       "      <td>[PRES, PSAL, TEMP, DOXY]</td>\n",
       "      <td>ftp://ftp.ifremer.fr/ifremer/argo/dac/aoml/590...</td>\n",
       "      <td>[[D, D, D, D]]</td>\n",
       "      <td>[pres, psal, temp, doxy]</td>\n",
       "      <td>True</td>\n",
       "      <td>A</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 34 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        doxy  doxy_qc         pres  pres_qc          _id POSITIONING_SYSTEM  \\\n",
       "0  71.110497        1  1797.489990        1  5901464_115                GPS   \n",
       "1  75.924095        1  1848.199951        1  5901464_115                GPS   \n",
       "2  79.144302        1  1897.660034        1  5901464_115                GPS   \n",
       "3  81.345016        1  1947.329956        1  5901464_115                GPS   \n",
       "4  84.040329        1  1997.800049        1  5901464_115                GPS   \n",
       "\n",
       "  DATA_CENTRE         PI_NAME WMO_INST_TYPE DATA_MODE  ... cycle_number   dac  \\\n",
       "0          AO  STEPHEN RISER,           846         D  ...          115  aoml   \n",
       "1          AO  STEPHEN RISER,           846         D  ...          115  aoml   \n",
       "2          AO  STEPHEN RISER,           846         D  ...          115  aoml   \n",
       "3          AO  STEPHEN RISER,           846         D  ...          115  aoml   \n",
       "4          AO  STEPHEN RISER,           846         D  ...          115  aoml   \n",
       "\n",
       "   platform_number  station_parameters_in_nc  \\\n",
       "0          5901464  [PRES, PSAL, TEMP, DOXY]   \n",
       "1          5901464  [PRES, PSAL, TEMP, DOXY]   \n",
       "2          5901464  [PRES, PSAL, TEMP, DOXY]   \n",
       "3          5901464  [PRES, PSAL, TEMP, DOXY]   \n",
       "4          5901464  [PRES, PSAL, TEMP, DOXY]   \n",
       "\n",
       "                                              nc_url  PARAMETER_DATA_MODE  \\\n",
       "0  ftp://ftp.ifremer.fr/ifremer/argo/dac/aoml/590...       [[D, D, D, D]]   \n",
       "1  ftp://ftp.ifremer.fr/ifremer/argo/dac/aoml/590...       [[D, D, D, D]]   \n",
       "2  ftp://ftp.ifremer.fr/ifremer/argo/dac/aoml/590...       [[D, D, D, D]]   \n",
       "3  ftp://ftp.ifremer.fr/ifremer/argo/dac/aoml/590...       [[D, D, D, D]]   \n",
       "4  ftp://ftp.ifremer.fr/ifremer/argo/dac/aoml/590...       [[D, D, D, D]]   \n",
       "\n",
       "                bgcMeasKeys containsBGC DIRECTION  BASIN  \n",
       "0  [pres, psal, temp, doxy]        True         A      2  \n",
       "1  [pres, psal, temp, doxy]        True         A      2  \n",
       "2  [pres, psal, temp, doxy]        True         A      2  \n",
       "3  [pres, psal, temp, doxy]        True         A      2  \n",
       "4  [pres, psal, temp, doxy]        True         A      2  \n",
       "\n",
       "[5 rows x 34 columns]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. Get a BGC selection\n",
    "\n",
    "https://argovis.colorado.edu/selection/bgc_data_selection?xaxis=temp&yaxis=pres&startDate=2020-03-01&endDate=2020-03-11&presRange=[0,50]&shape=[[[-155.929898,27.683528],[-156.984448,13.752725],[-149.468316,8.819693],[-142.15318,3.741443],[-134.922845,-1.396838],[-127.660888,-6.512815],[-120.250934,-11.523088],[-110.056944,-2.811371],[-107.069051,12.039321],[-118.141833,20.303418],[-125.314828,22.509761],[-132.702476,24.389053],[-140.290513,25.90038],[-148.048372,27.007913],[-155.929898,27.683528]]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_bgc_selection_profiles(startDate, endDate, shape, xaxis, yaxis, presRange=None, printUrl=True):\n",
    "    url = 'https://argovis.colorado.edu/selection/bgc_data_selection'\n",
    "    url += '?startDate={}'.format(startDate)\n",
    "    url += '&endDate={}'.format(endDate)\n",
    "    url += '&shape={}'.format(shape)\n",
    "    url += '&xaxis={}'.format(xaxis)\n",
    "    url += '&yaxis={}'.format(yaxis)\n",
    "    if presRange:\n",
    "        pressRangeQuery = '&presRange='.format(presRange)\n",
    "        url += pressRangeQuery\n",
    "    url = url.replace(' ', '')\n",
    "    if printUrl:\n",
    "        print(url)\n",
    "    resp = requests.get(url)\n",
    "    # Consider any status other than 2xx an error\n",
    "    if not resp.status_code // 100 == 2:\n",
    "        return \"Error: Unexpected response {}\".format(resp)\n",
    "    selectionProfiles = resp.json()\n",
    "    return selectionProfiles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://argovis.colorado.edu/selection/bgc_data_selection?startDate=2020-03-01&endDate=2020-03-11&shape=[[[-155.929898,27.683528],[-156.984448,13.752725],[-149.468316,8.819693],[-142.15318,3.741443],[-134.922845,-1.396838],[-127.660888,-6.512815],[-120.250934,-11.523088],[-110.056944,-2.811371],[-107.069051,12.039321],[-118.141833,20.303418],[-125.314828,22.509761],[-132.702476,24.389053],[-140.290513,25.90038],[-148.048372,27.007913],[-155.929898,27.683528]]]&xaxis=doxy&yaxis=pres&presRange=\n"
     ]
    }
   ],
   "source": [
    "startDate = '2020-03-01'\n",
    "endDate = '2020-03-11'\n",
    "presRange = [0, 50]\n",
    "shape = [[[-155.929898,27.683528],[-156.984448,13.752725],[-149.468316,8.819693],[-142.15318,3.741443],[-134.922845,-1.396838],\\\n",
    "          [-127.660888,-6.512815],[-120.250934,-11.523088],[-110.056944,-2.811371],[-107.069051,12.039321],[-118.141833,20.303418],\\\n",
    "          [-125.314828,22.509761],[-132.702476,24.389053],[-140.290513,25.90038],[-148.048372,27.007913],[-155.929898,27.683528]]]\n",
    "xaxis='doxy'\n",
    "yaxis='pres'\n",
    "profiles = get_bgc_selection_profiles(startDate, endDate, shape, xaxis, yaxis, presRange, printUrl=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = json2dataframe(profiles, 'bgcMeas')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>doxy</th>\n",
       "      <th>doxy_qc</th>\n",
       "      <th>pres</th>\n",
       "      <th>pres_qc</th>\n",
       "      <th>_id</th>\n",
       "      <th>POSITIONING_SYSTEM</th>\n",
       "      <th>DATA_MODE</th>\n",
       "      <th>date</th>\n",
       "      <th>lat</th>\n",
       "      <th>lon</th>\n",
       "      <th>cycle_number</th>\n",
       "      <th>bgcMeasKeys</th>\n",
       "      <th>core_data_mode</th>\n",
       "      <th>roundLat</th>\n",
       "      <th>roundLon</th>\n",
       "      <th>strLat</th>\n",
       "      <th>strLon</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>203.949081</td>\n",
       "      <td>1</td>\n",
       "      <td>7.460000</td>\n",
       "      <td>8</td>\n",
       "      <td>5906046_32</td>\n",
       "      <td>GPS</td>\n",
       "      <td>D</td>\n",
       "      <td>2020-03-10T09:12:22.000Z</td>\n",
       "      <td>-5.061</td>\n",
       "      <td>-118.6</td>\n",
       "      <td>32</td>\n",
       "      <td>[temp, nitrate, pres, psal, ph_in_situ_total, ...</td>\n",
       "      <td>D</td>\n",
       "      <td>-5.061</td>\n",
       "      <td>-118.600</td>\n",
       "      <td>5.061 S</td>\n",
       "      <td>118.600 W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>203.940735</td>\n",
       "      <td>1</td>\n",
       "      <td>11.550000</td>\n",
       "      <td>8</td>\n",
       "      <td>5906046_32</td>\n",
       "      <td>GPS</td>\n",
       "      <td>D</td>\n",
       "      <td>2020-03-10T09:12:22.000Z</td>\n",
       "      <td>-5.061</td>\n",
       "      <td>-118.6</td>\n",
       "      <td>32</td>\n",
       "      <td>[temp, nitrate, pres, psal, ph_in_situ_total, ...</td>\n",
       "      <td>D</td>\n",
       "      <td>-5.061</td>\n",
       "      <td>-118.600</td>\n",
       "      <td>5.061 S</td>\n",
       "      <td>118.600 W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>203.849197</td>\n",
       "      <td>1</td>\n",
       "      <td>16.520000</td>\n",
       "      <td>8</td>\n",
       "      <td>5906046_32</td>\n",
       "      <td>GPS</td>\n",
       "      <td>D</td>\n",
       "      <td>2020-03-10T09:12:22.000Z</td>\n",
       "      <td>-5.061</td>\n",
       "      <td>-118.6</td>\n",
       "      <td>32</td>\n",
       "      <td>[temp, nitrate, pres, psal, ph_in_situ_total, ...</td>\n",
       "      <td>D</td>\n",
       "      <td>-5.061</td>\n",
       "      <td>-118.600</td>\n",
       "      <td>5.061 S</td>\n",
       "      <td>118.600 W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>203.733582</td>\n",
       "      <td>1</td>\n",
       "      <td>21.540001</td>\n",
       "      <td>8</td>\n",
       "      <td>5906046_32</td>\n",
       "      <td>GPS</td>\n",
       "      <td>D</td>\n",
       "      <td>2020-03-10T09:12:22.000Z</td>\n",
       "      <td>-5.061</td>\n",
       "      <td>-118.6</td>\n",
       "      <td>32</td>\n",
       "      <td>[temp, nitrate, pres, psal, ph_in_situ_total, ...</td>\n",
       "      <td>D</td>\n",
       "      <td>-5.061</td>\n",
       "      <td>-118.600</td>\n",
       "      <td>5.061 S</td>\n",
       "      <td>118.600 W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>203.752197</td>\n",
       "      <td>1</td>\n",
       "      <td>26.670000</td>\n",
       "      <td>8</td>\n",
       "      <td>5906046_32</td>\n",
       "      <td>GPS</td>\n",
       "      <td>D</td>\n",
       "      <td>2020-03-10T09:12:22.000Z</td>\n",
       "      <td>-5.061</td>\n",
       "      <td>-118.6</td>\n",
       "      <td>32</td>\n",
       "      <td>[temp, nitrate, pres, psal, ph_in_situ_total, ...</td>\n",
       "      <td>D</td>\n",
       "      <td>-5.061</td>\n",
       "      <td>-118.600</td>\n",
       "      <td>5.061 S</td>\n",
       "      <td>118.600 W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         doxy  doxy_qc       pres  pres_qc         _id POSITIONING_SYSTEM  \\\n",
       "0  203.949081        1   7.460000        8  5906046_32                GPS   \n",
       "1  203.940735        1  11.550000        8  5906046_32                GPS   \n",
       "2  203.849197        1  16.520000        8  5906046_32                GPS   \n",
       "3  203.733582        1  21.540001        8  5906046_32                GPS   \n",
       "4  203.752197        1  26.670000        8  5906046_32                GPS   \n",
       "\n",
       "  DATA_MODE                      date    lat    lon  cycle_number  \\\n",
       "0         D  2020-03-10T09:12:22.000Z -5.061 -118.6            32   \n",
       "1         D  2020-03-10T09:12:22.000Z -5.061 -118.6            32   \n",
       "2         D  2020-03-10T09:12:22.000Z -5.061 -118.6            32   \n",
       "3         D  2020-03-10T09:12:22.000Z -5.061 -118.6            32   \n",
       "4         D  2020-03-10T09:12:22.000Z -5.061 -118.6            32   \n",
       "\n",
       "                                         bgcMeasKeys core_data_mode roundLat  \\\n",
       "0  [temp, nitrate, pres, psal, ph_in_situ_total, ...              D   -5.061   \n",
       "1  [temp, nitrate, pres, psal, ph_in_situ_total, ...              D   -5.061   \n",
       "2  [temp, nitrate, pres, psal, ph_in_situ_total, ...              D   -5.061   \n",
       "3  [temp, nitrate, pres, psal, ph_in_situ_total, ...              D   -5.061   \n",
       "4  [temp, nitrate, pres, psal, ph_in_situ_total, ...              D   -5.061   \n",
       "\n",
       "   roundLon   strLat     strLon  \n",
       "0  -118.600  5.061 S  118.600 W  \n",
       "1  -118.600  5.061 S  118.600 W  \n",
       "2  -118.600  5.061 S  118.600 W  \n",
       "3  -118.600  5.061 S  118.600 W  \n",
       "4  -118.600  5.061 S  118.600 W  "
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "av_py_env",
   "language": "python",
   "name": "av_py_env"
  },
  "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.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}