{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Read a grib file and make a contour plot of the data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we demonstrate:\n", "1. How to read a grib file in Python using `xarray` \n", "2. How to make a contour plot of the data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data\n", "We will read the ERA-Interim for geopotential height at 500hPa for Jan 6, 2010\n", "\n", "The data are located on the COLA severs in the following directory:\n", "```/shared/working/rean/era-interim/daily/data/2010/```\n", "\n", "The filename is:\n", "```ei.oper.an.pl.regn128cm.2010010600```" ] }, { "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": 1, "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": 2, "metadata": {}, "outputs": [], "source": [ "path='/shared/working/rean/era-interim/daily/data/2010/'\n", "fname='ei.oper.an.pl.regn128cm.2010010600'" ] }, { "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": 3, "metadata": {}, "outputs": [], "source": [ "ds=xr.open_dataset(path+fname,engine='cfgrib',backend_kwargs={'indexpath': ''})" ] }, { "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": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (isobaricInhPa: 37, latitude: 256, longitude: 512)\n", "Coordinates:\n", " number int64 ...\n", " time datetime64[ns] ...\n", " step timedelta64[ns] ...\n", " * isobaricInhPa (isobaricInhPa) int64 1000 975 950 925 900 875 ... 7 5 3 2 1\n", " * latitude (latitude) float64 89.46 88.77 88.07 ... -88.07 -88.77 -89.46\n", " * longitude (longitude) float64 0.0 0.7031 1.406 ... 357.9 358.6 359.3\n", " valid_time datetime64[ns] ...\n", "Data variables:\n", " pv (isobaricInhPa, latitude, longitude) float32 ...\n", " z (isobaricInhPa, latitude, longitude) float32 ...\n", " t (isobaricInhPa, latitude, longitude) float32 ...\n", " q (isobaricInhPa, latitude, longitude) float32 ...\n", " w (isobaricInhPa, latitude, longitude) float32 ...\n", " vo (isobaricInhPa, latitude, longitude) float32 ...\n", " d (isobaricInhPa, latitude, longitude) float32 ...\n", " r (isobaricInhPa, latitude, longitude) float32 ...\n", " o3 (isobaricInhPa, latitude, longitude) float32 ...\n", " clwc (isobaricInhPa, latitude, longitude) float32 ...\n", " ciwc (isobaricInhPa, latitude, longitude) float32 ...\n", " cc (isobaricInhPa, latitude, longitude) float32 ...\n", " u (isobaricInhPa, latitude, longitude) float32 ...\n", " v (isobaricInhPa, latitude, longitude) float32 ...\n", "Attributes:\n", " GRIB_edition: 1\n", " GRIB_centre: ecmf\n", " GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts\n", " GRIB_subCentre: 0\n", " Conventions: CF-1.7\n", " institution: European Centre for Medium-Range Weather Forecasts\n", " history: 2020-01-21T22:19:06 GRIB to CDM+CF via cfgrib-0...." ], "text/plain": [ "
<xarray.DataArray 'z' (isobaricInhPa: 37, latitude: 256, longitude: 512)>\n", "[4849664 values with dtype=float32]\n", "Coordinates:\n", " number int64 ...\n", " time datetime64[ns] ...\n", " step timedelta64[ns] ...\n", " * isobaricInhPa (isobaricInhPa) int64 1000 975 950 925 900 875 ... 7 5 3 2 1\n", " * latitude (latitude) float64 89.46 88.77 88.07 ... -88.07 -88.77 -89.46\n", " * longitude (longitude) float64 0.0 0.7031 1.406 ... 357.9 358.6 359.3\n", " valid_time datetime64[ns] ...\n", "Attributes:\n", " GRIB_paramId: 129\n", " GRIB_shortName: z\n", " GRIB_units: m**2 s**-2\n", " GRIB_name: Geopotential\n", " GRIB_cfName: geopotential\n", " GRIB_cfVarName: z\n", " GRIB_dataType: an\n", " GRIB_missingValue: 9999\n", " GRIB_numberOfPoints: 131072\n", " GRIB_totalNumber: 0\n", " GRIB_typeOfLevel: isobaricInhPa\n", " GRIB_NV: 0\n", " GRIB_stepUnits: 1\n", " GRIB_stepType: instant\n", " GRIB_gridType: regular_gg\n", " GRIB_gridDefinitionDescription: Gaussian Latitude/Longitude Grid\n", " GRIB_Nx: 512\n", " GRIB_iDirectionIncrementInDegrees: 0.703\n", " GRIB_iScansNegatively: 0\n", " GRIB_longitudeOfFirstGridPointInDegrees: 0.0\n", " GRIB_longitudeOfLastGridPointInDegrees: 359.297\n", " GRIB_N: 128\n", " GRIB_Ny: 256\n", " long_name: Geopotential\n", " units: m**2 s**-2\n", " standard_name: geopotential" ], "text/plain": [ "
<xarray.Dataset>\n", "Dimensions: (latitude: 256, longitude: 512)\n", "Coordinates:\n", " number int64 ...\n", " time datetime64[ns] ...\n", " step timedelta64[ns] ...\n", " isobaricInhPa int64 500\n", " * latitude (latitude) float64 89.46 88.77 88.07 ... -88.07 -88.77 -89.46\n", " * longitude (longitude) float64 0.0 0.7031 1.406 ... 357.9 358.6 359.3\n", " valid_time datetime64[ns] ...\n", "Data variables:\n", " pv (latitude, longitude) float32 ...\n", " z (latitude, longitude) float32 ...\n", " t (latitude, longitude) float32 ...\n", " q (latitude, longitude) float32 ...\n", " w (latitude, longitude) float32 ...\n", " vo (latitude, longitude) float32 ...\n", " d (latitude, longitude) float32 ...\n", " r (latitude, longitude) float32 ...\n", " o3 (latitude, longitude) float32 ...\n", " clwc (latitude, longitude) float32 ...\n", " ciwc (latitude, longitude) float32 ...\n", " cc (latitude, longitude) float32 ...\n", " u (latitude, longitude) float32 ...\n", " v (latitude, longitude) float32 ...\n", "Attributes:\n", " GRIB_edition: 1\n", " GRIB_centre: ecmf\n", " GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts\n", " GRIB_subCentre: 0\n", " Conventions: CF-1.7\n", " institution: European Centre for Medium-Range Weather Forecasts\n", " history: 2020-01-21T22:19:06 GRIB to CDM+CF via cfgrib-0...." ], "text/plain": [ "