Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotting Mean Sea Ice Concentration\n",
"\n",
"[Video walkthrough](https://mediaspace.itap.purdue.edu/media/Dask%20Demo%201%20-%20Plotting%20Mean%20Sea%20Ice%20Concentration/1_7yr1cf21)\n",
"\n",
"This is a simple demo that involves reading multiple netCDF files, calculating means, and creating a simple plot using `matplotlib`.\n",
"\n",
"The demo notebook uses the following Python dependencies:\n",
"\n",
"```\n",
"dask==2021.6.0\n",
"dask-core==2021.6.0\n",
"distributed==2021.6.0\n",
"ipywidgets\n",
"matplotlib\n",
"netcdf4\n",
"xarray\n",
"```\n",
"\n",
"**If you're running this notebook on Binder, no setup is needed.** \n",
"\n",
"If you're running in another notebook environment, you need to install some dependencies and download data files. Please follow the [setup guide](https://pages.github.itap.purdue.edu/xiao253/dask-demo/#user-guides)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dataset\n",
"\n",
"This demo notebook uses the NOAA OISST (v2.1) dataset.\n",
"\n",
"Links:\n",
"- Learn more about the dataset at: https://www.ncdc.noaa.gov/oisst/optimum-interpolation-sea-surface-temperature-oisst-v20\n",
"- Browse all available data at: https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/v2.1/access/avhrr/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Start a Local Dask Cluster\n",
"\n",
"The \"local\" Dask cluster runs on the notebook server."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Suppress warnings, which are generally safe to ignore\n",
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from dask.distributed import Client, LocalCluster, progress\n",
"import xarray as xr\n",
"\n",
"# This will automatically determine the number of workers based on available CPU cores\n",
"cluster = LocalCluster()\n",
"# Or, manually specify the desired number of workers\n",
"# cluster = LocalCluster(n_workers=12)\n",
"\n",
"client = Client()\n",
"client"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Preview source data\n",
"\n",
"Here we load one NetCDF file that contains data from Feb. 1, 2020, and create a plot of sea ice concentration.\n",
"\n",
"Note that Dask is not involved in this section."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Read netCDF file and load sea ice concentration variable "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# read netcdf file as xarray Dataset\n",
"ds = xr.open_dataset(\"data/oisst-avhrr-v02r01.20200201.nc\")\n",
"ds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load sea ice concentration variable into xarray DataArray\n",
"da = ds['ice']\n",
"da"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot sea ice concentration with `matplotlib`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"\n",
"da.load().plot(figsize=(9, 5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dask / Xarray with Multiple netCDF Files\n",
"\n",
"Here we load all NetCDF files that contains data from Feb 2020 (29 files total), and create a plot of the monthly mean sea ice concentration.\n",
"\n",
"Note: The following steps use Dask."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Open All netCDF Files and load sea ice concentration variable "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import glob\n",
"\n",
"# build file list\n",
"netcdf_list = glob.glob('data/oisst-avhrr-v02r01.*.nc')\n",
"netcdf_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import xarray as xr\n",
"\n",
"# read files into xarray Dataset\n",
"ds = xr.open_mfdataset(paths=netcdf_list)\n",
"ds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load sea ice concentration variable data into xarray DataArray\n",
"da = ds['ice']\n",
"da"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Calculate mean monthly sea ice concentration "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da_mean = da.groupby('time.month').mean()\n",
"da_mean = da_mean.persist() # persist mean DataArray in memory\n",
"progress(da_mean)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da_mean"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot mean monthly sea ice concentration with `matplotlib`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"da_mean.plot(figsize=(9, 5))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "myconda",
"language": "python",
"name": "myconda"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}