{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Read a netcdf 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 netcdf 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 CMIP5 data for surface air temperature (tas) from the RCP8.5 scenario produced by the NCAR/CCSM4 model. For this example, we will read teh first ensemble member.\n", "\n", "The data are located on the COLA severs in the following directory:\n", "```/shared/cmip5/data/rcp45/atmos/mon/Amon/tas/NCAR.CCSM4/r1i1p1/```\n", "\n", "The filename is:\n", "```tas_Amon_CCSM4_rcp45_r1i1p1_210101-229912.nc```" ] }, { "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/cmip5/data/rcp45/atmos/mon/Amon/tas/NCAR.CCSM4/r1i1p1/'\n", "fname='tas_Amon_CCSM4_rcp45_r1i1p1_200601-210012.nc'" ] }, { "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)" ] }, { "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: (bnds: 2, lat: 192, lon: 288, time: 1140)\n", "Coordinates:\n", " * time (time) object 2006-01-16 12:00:00 ... 2100-12-16 12:00:00\n", " * lat (lat) float64 -90.0 -89.06 -88.12 -87.17 ... 88.12 89.06 90.0\n", " * lon (lon) float64 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8\n", " height float64 ...\n", "Dimensions without coordinates: bnds\n", "Data variables:\n", " time_bnds (time, bnds) object ...\n", " lat_bnds (lat, bnds) float64 ...\n", " lon_bnds (lon, bnds) float64 ...\n", " tas (time, lat, lon) float32 ...\n", "Attributes:\n", " institution: NCAR (National Center for Atmospheric Resea...\n", " institute_id: NCAR\n", " experiment_id: rcp45\n", " source: CCSM4\n", " model_id: CCSM4\n", " forcing: Sl GHG Vl SS Ds SA BC MD OC Oz AA\n", " parent_experiment_id: historical\n", " parent_experiment_rip: r1i1p1\n", " branch_time: 2005.0\n", " contact: cesm_data@ucar.edu\n", " references: Gent P. R., et.al. 2011: The Community Clim...\n", " initialization_method: 1\n", " physics_version: 1\n", " tracking_id: 0bf35136-b266-44d2-9078-f3081b83b6ad\n", " acknowledgements: The CESM project is supported by the Nation...\n", " cesm_casename: b40.rcp4_5.1deg.001\n", " cesm_repotag: ccsm4_0_beta49\n", " cesm_compset: BRCP45CN\n", " resolution: f09_g16 (0.9x1.25_gx1v6)\n", " forcing_note: Additional information on the external forc...\n", " processed_by: strandwg on mirage0 at 20111021\n", " processing_code_information: Last Changed Rev: 428 Last Changed Date: 20...\n", " product: output\n", " experiment: RCP4.5\n", " frequency: mon\n", " creation_date: 2011-10-21T21:56:22Z\n", " history: 2011-10-21T21:56:22Z CMOR rewrote data to c...\n", " Conventions: CF-1.4\n", " project_id: CMIP5\n", " table_id: Table Amon (26 July 2011) 976b7fd1d9e1be31d...\n", " title: CCSM4 model output prepared for CMIP5 RCP4.5\n", " parent_experiment: historical\n", " modeling_realm: atmos\n", " realization: 1\n", " cmor_version: 2.7.1" ], "text/plain": [ "
<xarray.DataArray 'tas' (time: 1140, lat: 192, lon: 288)>\n", "[63037440 values with dtype=float32]\n", "Coordinates:\n", " * time (time) object 2006-01-16 12:00:00 ... 2100-12-16 12:00:00\n", " * lat (lat) float64 -90.0 -89.06 -88.12 -87.17 ... 87.17 88.12 89.06 90.0\n", " * lon (lon) float64 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8\n", " height float64 ...\n", "Attributes:\n", " standard_name: air_temperature\n", " long_name: Near-Surface Air Temperature\n", " units: K\n", " original_name: TREFHT\n", " comment: TREFHT no change\n", " cell_methods: time: mean (interval: 30 days)\n", " cell_measures: area: areacella\n", " history: 2011-10-21T21:56:22Z altered by CMOR: Treated scalar d...\n", " associated_files: baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation..." ], "text/plain": [ "