{ "cells": [ { "cell_type": "markdown", "id": "2676d3bb-e881-4989-907c-e604f1483ed7", "metadata": {}, "source": [ "# DFO Moorings" ] }, { "cell_type": "markdown", "id": "b109c82d-a858-4da6-9858-2d3f3385aedd", "metadata": {}, "source": [ "## Mooring CTD time series visulization - E01" ] }, { "cell_type": "code", "execution_count": 1, "id": "6c5aa766-2bc6-4b3c-a02d-28cd6214307a", "metadata": { "tags": [] }, "outputs": [], "source": [ "#import python packages needed\n", "import erddapy\n", "from erddapy import ERDDAP\n", "import numpy as np\n", "import pandas as pd\n", "import xarray\n", "import cf_xarray\n", "import datetime\n", "import netCDF4\n", "from netCDF4 import Dataset\n", "from matplotlib import pyplot as plt" ] }, { "cell_type": "markdown", "id": "1dc78db2-05cb-4278-86af-1e364118e52c", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ "### 1. develop a function to download mooring data from ERDDAP server" ] }, { "cell_type": "code", "execution_count": 2, "id": "c473c538-93af-411f-98e5-e5603726f955", "metadata": { "tags": [] }, "outputs": [], "source": [ "def get_erddap_data(erddap_url, dataset, data_protocol=\"griddap\", variables=None, constraints=None):\n", " \"\"\"\n", " Function: get_erddap_data\n", " This function uses the erddapy python library to access data from ERDDAP servers,\n", " and to return it to users in convenient formats for python users.\n", " Data can be pulled from \"tabledap\" or \"griddap\" formats, with different\n", " output types, depending on the dap type.\n", " \n", " Inputs:\n", " erddap_url - The url address of the erddap server to pull data from\n", " variables - The selected variables within the dataset.\n", " data_protocol - The erddap data protocol for the chosen dataset.\n", " Options include \"tabledap\" or \"griddap\"\n", " The default option is given as \"griddap\"\n", " dataset - The ID for the relevant dataset on the erddap server\n", " If no variables are given, it is assumed that all variables\n", " will be pulled.\n", " constraints - These are set by the user to help restrict the data pull\n", " to only the area and timeframe of interest.\n", " If no constraints are given, all data in a dataset is pulled.\n", " Constraints should be given as a dictionary, where\n", " each entry is a bound and/or selection of a specific axis variable\n", " Exs. {\"longitude<=\": \"min(longitude)+10\", \"longitude>=\": \"0\"}\n", " {\"longitude=\": \"140\", \"time>=\": \"max(time)-30\"}\n", " \n", " Outputs:\n", " erddap_data - This variable contains the pulled data from the erddap server.\n", " If the data_protocol is \"griddap\", then erddap_data is an xarray dataset\n", " If the data_protocol is \"tabledap\", then erddap_data is a pandas dataframe\n", " \"\"\"\n", " \n", " import erddapy\n", " from erddapy import ERDDAP\n", " import pandas as pd\n", " import xarray\n", " \n", " \n", " ############################################\n", " # Set-up the connection to the ERDDAP server\n", " ############################################\n", " \n", " # Connect to the erddap server\n", " e = ERDDAP(server=erddap_url, protocol=data_protocol, response='csv')\n", " \n", " # Identify the dataset of interest\n", " e.dataset_id = dataset\n", " \n", " \n", " #########################################\n", " # Pull the data, based upon protocol type\n", " #########################################\n", " \n", " # GRIDDAP Protocol\n", " if data_protocol == \"griddap\":\n", " \n", " # Initialize the connection\n", " e.griddap_initialize()\n", "\n", " # Update the constraints\n", " if constraints is not None:\n", " e.constraints.update(constraints)\n", " e.griddap_initialize()\n", " \n", " # Update the selection of the variables\n", " if variables is not None:\n", " e.variables = variables\n", "\n", " erddap_data = e.to_xarray()\n", " \n", " # TABLEDAP Protocol\n", " elif data_protocol == \"tabledap\":\n", "\n", " # Update the constraints\n", " if constraints is not None:\n", " e.constraints = constraints\n", " \n", " # Update the selection of the variables\n", " if variables is not None:\n", " e.variables = variables\n", " \n", " erddap_data = e.to_pandas()\n", " \n", " # Invalid protocol given\n", " else:\n", " print('Invalid ERDDAP protocol. Given protocol is: ' + data_protocol)\n", " print('Valid protocols include \"griddap\" or \"tabledap\". Please restart and try again with a valid protocol')\n", " erddap_data = None\n", " \n", " \n", " #############################\n", " return erddap_data" ] }, { "cell_type": "markdown", "id": "24f991c4-d33e-4969-b247-d69931c80132", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ "### 2. Set and map the data search polygon for E01" ] }, { "cell_type": "code", "execution_count": 3, "id": "378d1e85-c895-40f4-86f6-6b377df9ba26", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", " | time (UTC) | \n", "depth (m) | \n", "latitude (degrees_north) | \n", "longitude (degrees_east) | \n", "sea_water_pressure (dbar) | \n", "sea_water_temperature (degC) | \n", "sea_water_practical_salinity (PSS-78) | \n", "TEMPS901 (deg_C) | \n", "TEMPS902 (deg_C) | \n", "TEMPS601 (deg_C) | \n", "TEMPS602 (deg_C) | \n", "TEMPST01 (deg_C) | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "2008-04-28T20:00:01Z | \n", "73.502020 | \n", "49.288185 | \n", "-126.60239 | \n", "74.146 | \n", "NaN | \n", "32.9249 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "7.5953 | \n", "
1 | \n", "2008-04-28T20:00:21Z | \n", "73.502020 | \n", "49.288185 | \n", "-126.60239 | \n", "74.146 | \n", "NaN | \n", "32.9276 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "7.5958 | \n", "
2 | \n", "2008-04-28T20:00:41Z | \n", "73.448494 | \n", "49.288185 | \n", "-126.60239 | \n", "74.092 | \n", "NaN | \n", "32.9293 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "7.5964 | \n", "
3 | \n", "2008-04-28T20:01:01Z | \n", "73.484170 | \n", "49.288185 | \n", "-126.60239 | \n", "74.128 | \n", "NaN | \n", "32.9304 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "7.5981 | \n", "
4 | \n", "2008-04-28T20:01:21Z | \n", "73.466330 | \n", "49.288185 | \n", "-126.60239 | \n", "74.110 | \n", "NaN | \n", "32.9298 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "7.5972 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
1128615 | \n", "2022-07-14T23:00:07Z | \n", "97.252230 | \n", "49.288330 | \n", "-126.60290 | \n", "98.110 | \n", "7.1191 | \n", "33.7387 | \n", "7.1191 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1128616 | \n", "2022-07-14T23:30:07Z | \n", "97.026276 | \n", "49.288330 | \n", "-126.60290 | \n", "97.882 | \n", "7.1316 | \n", "33.7315 | \n", "7.1316 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1128617 | \n", "2022-07-15T00:00:07Z | \n", "96.897440 | \n", "49.288330 | \n", "-126.60290 | \n", "97.752 | \n", "7.1474 | \n", "33.7214 | \n", "7.1474 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1128618 | \n", "2022-07-15T00:30:07Z | \n", "96.605090 | \n", "49.288330 | \n", "-126.60290 | \n", "97.457 | \n", "7.1669 | \n", "33.7088 | \n", "7.1669 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1128619 | \n", "2022-07-15T01:00:07Z | \n", "96.442560 | \n", "49.288330 | \n", "-126.60290 | \n", "97.293 | \n", "7.1878 | \n", "33.6931 | \n", "7.1878 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1128620 rows × 12 columns
\n", "