{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "a6f1bbe4",
"metadata": {},
"source": [
"___We recommend working with this notebook on Google Colab___\n",
"[](https://colab.research.google.com/github/ridatadiscoverycenter/riddc-jbook/blob/main/riddc/notebooks/fox-kemper/noaa_coops_download.ipynb)"
]
},
{
"cell_type": "markdown",
"id": "4cf680d0",
"metadata": {},
"source": [
"# Downloading Tide Data from NOAA CO-OPS API"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c7e9278a",
"metadata": {},
"source": [
"Author of this document: Timothy Divoll [](https://github.com/tdivoll)"
]
},
{
"cell_type": "markdown",
"id": "3258b417",
"metadata": {},
"source": [
"The purpose of this notebook is to demonstrate how to download data from NOAA's CO-OPS Data API. In this example, data are parsed into a dataframe and also written to CSV files (as needed to use in the `Assessing Accuracy of the Tide Predictions of the Ocean State Ocean Model` notebook."
]
},
{
"cell_type": "markdown",
"id": "c9d2d0d8",
"metadata": {},
"source": [
"If needed, dataframes could be saved in other common formats and exported as needed."
]
},
{
"cell_type": "markdown",
"id": "0afff31d",
"metadata": {},
"source": [
"First, we need to install the noaa_coops Python wrapper (https://github.com/GClunies/noaa_coops)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "bf1ba2aa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mDEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mDEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621\u001b[0m\u001b[33m\n",
"\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.0\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49m/opt/homebrew/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip\u001b[0m\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install noaa_coops -q # this package sends requests to the NOAA CO-OPS Data API"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "9cc6c5ba",
"metadata": {},
"outputs": [],
"source": [
"# import dependencies and ignore warnings in code outputs\n",
"import noaa_coops\n",
"import pandas as pd\n",
"from datetime import datetime\n",
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "markdown",
"id": "1d8b2129",
"metadata": {},
"source": [
"### Direct data dowload - example\n",
"First make a list of all the stations to pull data for"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "0eca4516",
"metadata": {},
"outputs": [],
"source": [
"station_list = ['8461490', '8510560', '8447930', '8449130', '8452660', '8454049', '8447386', '8452944', '8454000']"
]
},
{
"cell_type": "markdown",
"id": "6cdc9483",
"metadata": {},
"source": [
"The next cell has code to extract the data directly from the NOAA CO-OPS API rather than downloading from the webpage. This block only shows one example station and the following code block loops through the station list to pull data for each station."
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "7d052486",
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"# send a request to the CO-OPS API for Data Retrieval\n",
"# https://api.tidesandcurrents.noaa.gov/api/prod/\n",
"# MLLW = mean lower low water\n",
"\n",
"# New London, CT example\n",
"new_london = noaa_coops.Station(station_list[0]) #use a different index for a different station\n",
"new_london_verified = new_london.get_data(\n",
" begin_date = \"20120601\",\n",
" end_date = \"20220616\",\n",
" product = \"water_level\",\n",
" datum = \"MLLW\",\n",
" units = \"metric\",\n",
" time_zone = \"gmt\",\n",
" interval = \"h\")\n",
"new_london_predicted = new_london.get_data(\n",
" begin_date = \"20120601\",\n",
" end_date = \"20220616\",\n",
" product = \"predictions\",\n",
" datum = \"MLLW\",\n",
" units = \"metric\",\n",
" time_zone = \"gmt\",\n",
" interval = \"h\")\n",
"\n",
"# merge verified and predicted, then rename columns to match `readcsv` function\n",
"new_london_df = pd.merge(new_london_verified, new_london_predicted, on=\"date_time\").drop(columns = [\"sigma\", \"flags\", \"QC\"]).rename(columns={\"water_level\": \"Verified (m)\", \"predicted_wl\": \"Predicted (m)\"}).reset_index()\n",
"new_london_df[\"Date\"] = new_london_df[\"date_time\"].dt.strftime(\"%m/%d/%Y\")\n",
"new_london_df[\"Time (GMT)\"] = new_london_df[\"date_time\"].dt.strftime(\"%H:%M\")\n",
"\n",
"# see the next example to save data to Google Drive or to a local folder"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "c6509306",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n", " | date_time | \n", "Verified (m) | \n", "Predicted (m) | \n", "Date | \n", "Time (GMT) | \n", "
---|---|---|---|---|---|
0 | \n", "2004-01-01 00:00:00 | \n", "0.353 | \n", "0.408 | \n", "01/01/2004 | \n", "00:00 | \n", "
1 | \n", "2004-01-01 01:00:00 | \n", "0.238 | \n", "0.311 | \n", "01/01/2004 | \n", "01:00 | \n", "
2 | \n", "2004-01-01 02:00:00 | \n", "0.119 | \n", "0.194 | \n", "01/01/2004 | \n", "02:00 | \n", "
3 | \n", "2004-01-01 03:00:00 | \n", "0.101 | \n", "0.104 | \n", "01/01/2004 | \n", "03:00 | \n", "
4 | \n", "2004-01-01 04:00:00 | \n", "0.118 | \n", "0.098 | \n", "01/01/2004 | \n", "04:00 | \n", "
\n", " | date_time | \n", "Verified (m) | \n", "Predicted (m) | \n", "Station ID | \n", "Date | \n", "Time (GMT) | \n", "
---|---|---|---|---|---|---|
0 | \n", "2021-01-01 00:00:00 | \n", "0.223 | \n", "0.241 | \n", "New London | \n", "01/01/2021 | \n", "00:00 | \n", "
1 | \n", "2021-01-01 01:00:00 | \n", "0.410 | \n", "0.431 | \n", "New London | \n", "01/01/2021 | \n", "01:00 | \n", "
2 | \n", "2021-01-01 02:00:00 | \n", "0.558 | \n", "0.564 | \n", "New London | \n", "01/01/2021 | \n", "02:00 | \n", "
3 | \n", "2021-01-01 03:00:00 | \n", "0.647 | \n", "0.637 | \n", "New London | \n", "01/01/2021 | \n", "03:00 | \n", "
4 | \n", "2021-01-01 04:00:00 | \n", "0.676 | \n", "0.639 | \n", "New London | \n", "01/01/2021 | \n", "04:00 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
13171 | \n", "2022-06-16 19:00:00 | \n", "0.084 | \n", "-0.006 | \n", "Providence | \n", "06/16/2022 | \n", "19:00 | \n", "
13172 | \n", "2022-06-16 20:00:00 | \n", "0.137 | \n", "0.004 | \n", "Providence | \n", "06/16/2022 | \n", "20:00 | \n", "
13173 | \n", "2022-06-16 21:00:00 | \n", "0.303 | \n", "0.182 | \n", "Providence | \n", "06/16/2022 | \n", "21:00 | \n", "
13174 | \n", "2022-06-16 22:00:00 | \n", "0.523 | \n", "0.373 | \n", "Providence | \n", "06/16/2022 | \n", "22:00 | \n", "
13175 | \n", "2022-06-16 23:00:00 | \n", "0.678 | \n", "0.588 | \n", "Providence | \n", "06/16/2022 | \n", "23:00 | \n", "
118584 rows × 6 columns
\n", "