{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Griddap\n", "\n", "Erddapy can access gridded datasets,\n", "using the server-side subsetting of griddap or the OPeNDAP response,\n", "to download only the parts of a dataset that the user requires.\n", "\n", "In our example we will use a Region of Interest (ROI) to extract data within its bounds.\n", "First we need to read the ROI with `geopandas`.\n", "Let's use the South Atlantic Ocean basin from Natural Earth as our ROI." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-08-09T14:28:39.160112Z", "iopub.status.busy": "2023-08-09T14:28:39.159689Z", "iopub.status.idle": "2023-08-09T14:28:40.468399Z", "shell.execute_reply": "2023-08-09T14:28:40.467676Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Downloading data from 'https://naturalearth.s3.amazonaws.com/4.1.1/50m_physical/ne_50m_geography_marine_polys.zip' to file '/home/runner/.cache/pooch/f076ff648997d7b0fc77283e51b7d78d-ne_50m_geography_marine_polys.zip'.\n" ] } ], "source": [ "import geopandas\n", "import pooch\n", "\n", "\n", "url = \"https://naturalearth.s3.amazonaws.com/4.1.1/50m_physical/ne_50m_geography_marine_polys.zip\"\n", "fname = pooch.retrieve(\n", " url,\n", " known_hash=\"db6f59e5a747c016451caec2450db6deea25d702dc2fb9c39384c1b909fb7f72\",\n", ")\n", "\n", "oceans = geopandas.read_file(fname)\n", "\n", "name = \"South Atlantic Ocean\"\n", "SA = oceans.loc[oceans[\"name\"] == name]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When accessing gridded datasets we need to define the `protocol=\"griddap\"` in\n", "our class instantiation.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-08-09T14:28:40.471679Z", "iopub.status.busy": "2023-08-09T14:28:40.471252Z", "iopub.status.idle": "2023-08-09T14:28:40.733243Z", "shell.execute_reply": "2023-08-09T14:28:40.732442Z" }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "from erddapy import ERDDAP\n", "\n", "e = ERDDAP(\n", " server=\"https://pae-paha.pacioos.hawaii.edu/erddap\",\n", " protocol=\"griddap\",\n", ")\n", "\n", "e.dataset_id = \"etopo5_lon180\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "CAVEAT: Note that ERDDAP can serve gridded data with longitudes in the 0–360 format or -180–180.\n", "The user must inspect the dataset and modify your query accordingly.\n", "\n", "Information on the griddap dataset is fetched with `griddap_initialize`.\n", "This fills in the `variables` and `constraints` properties for that dataset." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-08-09T14:28:40.736949Z", "iopub.status.busy": "2023-08-09T14:28:40.736268Z", "iopub.status.idle": "2023-08-09T14:28:42.946522Z", "shell.execute_reply": "2023-08-09T14:28:42.945853Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "variables in this dataset:\n", "\n", "['ROSE']\n", "\n", "constraints of this dataset:\n", "\n", "{\n", " \"latitude>=\": -90.0,\n", " \"latitude<=\": 90.0,\n", " \"latitude_step\": 1,\n", " \"longitude>=\": -179.99833294744155,\n", " \"longitude<=\": 179.91833294744154,\n", " \"longitude_step\": 1\n", "}\n" ] } ], "source": [ "import json\n", "\n", "e.griddap_initialize()\n", "\n", "print(f\"variables in this dataset:\\n\\n{e.variables}\")\n", "print(\n", " f\"\\nconstraints of this dataset:\\n\\n{json.dumps(e.constraints, indent=1)}\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The default behaviour is to use erddap standard subsetting to return all variables at the most recent timestep and every point of the remaining dimensions.\n", "\n", "This can result in large data requests!\n", "However, the values of the constraints can be changed and variables dropped before data set is downloaded.\n", "Here we will download only one variable from that list." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2023-08-09T14:28:42.950486Z", "iopub.status.busy": "2023-08-09T14:28:42.950119Z", "iopub.status.idle": "2023-08-09T14:28:42.955157Z", "shell.execute_reply": "2023-08-09T14:28:42.954353Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloading ['ROSE'].\n" ] } ], "source": [ "e.variables = [e.variables[0]]\n", "\n", "print(f\"Downloading {e.variables}.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And we will reduce the dataset a bit further by requesting only the data that is inside the bounding box of the South Atlantic." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2023-08-09T14:28:42.957697Z", "iopub.status.busy": "2023-08-09T14:28:42.957476Z", "iopub.status.idle": "2023-08-09T14:28:42.971296Z", "shell.execute_reply": "2023-08-09T14:28:42.970689Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " | minx | \n", "miny | \n", "maxx | \n", "maxy | \n", "
---|---|---|---|---|
6 | \n", "-69.092082 | \n", "-60.533462 | \n", "19.634854 | \n", "0.032867 | \n", "