{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Read a file from an remote OPeNDAP server and make a contour plot of the data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we demonstrate:\n", "1. How to read a netcdf file in Python using `xarray` from a remote OPeNDAP server\n", "2. How to make a contour plot of the data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data\n", "We will read data from the North American Multi-model Ensemble (NMME) database. Specifially, we will read the sea surface temperature (SST) data hindcast data for the COLA-RSMAS-CCSM4 model.\n", "\n", "The NMME public database is hosted on the International Research Institute for Climate and Society (IRI) data server http://iridl.ldeo.columbia.edu/SOURCES/.Models/.NMME/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python import statements\n", "You must first import the Python packages you wish to use. \n", "This is a common set of basic import statments you can start with." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import xarray as xr\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set the path and filename" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "url = 'http://iridl.ldeo.columbia.edu/SOURCES/.Models/.NMME/.COLA-RSMAS-CCSM4/.MONTHLY/.sst/dods'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read the data using `xarray` `open_dataset` http://xarray.pydata.org/en/stable/generated/xarray.open_dataset.html" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "ds =xr.open_dataset(url, decode_times=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you read in data using `xarray`, it creates an object called an `xarray.Dataset` which consists of your data and all its metadata. If we print out our `Dataset` which is called `ds`, its similar to doing a `ncdump -h` on a netcdf file. You can see all the dimensions, size, and attributes of the data in the file." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (L: 12, M: 10, S: 457, X: 360, Y: 181)\n", "Coordinates:\n", " * S (S) float32 264.0 265.0 266.0 267.0 ... 717.0 718.0 719.0 720.0\n", " * X (X) float32 0.0 1.0 2.0 3.0 4.0 ... 355.0 356.0 357.0 358.0 359.0\n", " * M (M) float32 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0\n", " * L (L) float32 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5\n", " * Y (Y) float32 -90.0 -89.0 -88.0 -87.0 -86.0 ... 87.0 88.0 89.0 90.0\n", "Data variables:\n", " sst (S, L, M, Y, X) float32 ...\n", "Attributes:\n", " Conventions: IRIDL" ], "text/plain": [ "
<xarray.DataArray 'sst' (S: 457, L: 12, M: 10, Y: 181, X: 360)>\n", "[3573374400 values with dtype=float32]\n", "Coordinates:\n", " * S (S) float32 264.0 265.0 266.0 267.0 ... 717.0 718.0 719.0 720.0\n", " * X (X) float32 0.0 1.0 2.0 3.0 4.0 ... 355.0 356.0 357.0 358.0 359.0\n", " * M (M) float32 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0\n", " * L (L) float32 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5\n", " * Y (Y) float32 -90.0 -89.0 -88.0 -87.0 -86.0 ... 87.0 88.0 89.0 90.0\n", "Attributes:\n", " defaultvalue: 720.0\n", " pointwidth: 0\n", " long_name: Sea Surface Temperature\n", " cell_methods: time: mean\n", " units: Celsius_scale\n", " spatial_op: Conservative remapping: 1st order: destarea: NCL: /homes/...\n", " lat: 89.5\n", " standard_name: sea_surface_temperature\n", " expires: 1580517720" ], "text/plain": [ "