{ "cells": [ { "cell_type": "markdown", "id": "4cab5295-3984-4db5-8383-df5e256d7475", "metadata": {}, "source": [ "# Puget Sound Plotting" ] }, { "cell_type": "code", "execution_count": 1, "id": "6364823b-1919-46da-99c0-7c83e361670a", "metadata": { "tags": [] }, "outputs": [], "source": [ "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", "\n", "import matplotlib\n", "from matplotlib import pyplot as plt" ] }, { "cell_type": "markdown", "id": "b15af479-2805-4917-a392-853279417d77", "metadata": {}, "source": [ "Downloading the Mooring Datasets" ] }, { "cell_type": "code", "execution_count": 2, "id": "5384fb19-c02f-42b5-a46f-5c00a25b385a", "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": "6d6d20c2-0c9e-4470-8c8d-216cb0af5eaf", "metadata": {}, "source": [ "Extracting Data for Plotting at Specific Depth for Two Mooring Sub-stations" ] }, { "cell_type": "markdown", "id": "ebf3ef1f-f6fd-4440-9d92-4cd378a30f5c", "metadata": {}, "source": [ "Visualising the Study Area" ] }, { "cell_type": "code", "execution_count": 3, "id": "9486499e-85d2-43e5-9871-ca122c6718d0", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "