generated from ProjectPythia/cookbook-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
3,652 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+132 KB
_images/a8193169e72e8c4254ded28daa11b0c426f09c9b5a7713d0fe737ed0892eabb6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Animation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"collapsed": false, | ||
"jupyter": { | ||
"outputs_hidden": false | ||
} | ||
}, | ||
"source": [ | ||
"time stamp at 1:19" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"NCL_animate_1\n", | ||
"\n", | ||
"Please note:\n", | ||
" - Executing this script will not display a gif, but you have the option to uncomment a line at the bottom that will save a gif in the same directory as this script.\n", | ||
"\n", | ||
"[GeoCAT-examples](https://geocat-examples.readthedocs.io/en/latest/gallery/Animations/NCL_animate_1.html)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import packages:\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false, | ||
"jupyter": { | ||
"outputs_hidden": false | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import cartopy.crs as ccrs\n", | ||
"import matplotlib.animation as animation\n", | ||
"import numpy as np\n", | ||
"import xarray as xr\n", | ||
"from matplotlib import pyplot as plt\n", | ||
"\n", | ||
"import geocat.datafiles as gdf\n", | ||
"import geocat.viz as gv" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Read in data:\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false, | ||
"jupyter": { | ||
"outputs_hidden": false | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Open a netCDF data file using xarray default engine and load the data into xarrays\n", | ||
"# Disable time decoding due to missing necessary metadata\n", | ||
"ds = xr.open_dataset(gdf.get(\"netcdf_files/meccatemp.cdf\"), decode_times=False)\n", | ||
"\n", | ||
"tas = ds.t\n", | ||
"tas" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Create animation:\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false, | ||
"jupyter": { | ||
"outputs_hidden": false | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Set up Axes with Cartopy Projection\n", | ||
"fig = plt.figure(figsize=(10, 8))\n", | ||
"ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=150))\n", | ||
"ax.coastlines(linewidths=0.5)\n", | ||
"ax.set_extent([-180, 180, -90, 90], ccrs.PlateCarree())\n", | ||
"\n", | ||
"# Use geocat.viz.util convenience function to set axes limits & tick values\n", | ||
"gv.set_axes_limits_and_ticks(ax,\n", | ||
" xlim=(-180, 180),\n", | ||
" ylim=(-90, 90),\n", | ||
" xticks=np.linspace(-180, 180, 13),\n", | ||
" yticks=np.linspace(-90, 90, 7))\n", | ||
"\n", | ||
"# Use geocat.viz.util convenience function to add minor and major tick lines\n", | ||
"gv.add_major_minor_ticks(ax, labelsize=10)\n", | ||
"\n", | ||
"# Use geocat.viz.util convenience function to make latitude, longitude tick labels\n", | ||
"gv.add_lat_lon_ticklabels(ax)\n", | ||
"\n", | ||
"# create initial plot that establishes a colorbar\n", | ||
"tas[0, :, :].plot.contourf(ax=ax,\n", | ||
" transform=ccrs.PlateCarree(),\n", | ||
" vmin=195,\n", | ||
" vmax=328,\n", | ||
" levels=53,\n", | ||
" cmap=\"inferno\",\n", | ||
" cbar_kwargs={\n", | ||
" \"extendrect\": True,\n", | ||
" \"orientation\": \"horizontal\",\n", | ||
" \"ticks\": np.arange(195, 332, 9),\n", | ||
" \"label\": \"\",\n", | ||
" \"shrink\": 0.90\n", | ||
" })\n", | ||
"\n", | ||
"\n", | ||
"# animate function for matplotlib FuncAnimation\n", | ||
"def animate(i):\n", | ||
" tas[i, :, :].plot.contourf(\n", | ||
" ax=ax,\n", | ||
" transform=ccrs.PlateCarree(),\n", | ||
" vmin=195,\n", | ||
" vmax=328,\n", | ||
" levels=53,\n", | ||
" cmap=\"inferno\",\n", | ||
" add_colorbar=False,\n", | ||
" )\n", | ||
"\n", | ||
" gv.set_titles_and_labels(\n", | ||
" ax,\n", | ||
" maintitle=\"January Global Surface Temperature (K) - Day \" +\n", | ||
" str(tas.coords['time'].values[i])[:13],\n", | ||
" xlabel=\"\",\n", | ||
" ylabel=\"\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"jupyter": { | ||
"outputs_hidden": false | ||
} | ||
}, | ||
"source": [ | ||
"### Run:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false, | ||
"jupyter": { | ||
"outputs_hidden": false | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# runs the animation initiated with the frame from init and progressed with the animate function\n", | ||
"anim = animation.FuncAnimation(fig, animate, frames=30, interval=200)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"jupyter": { | ||
"outputs_hidden": false | ||
} | ||
}, | ||
"source": [ | ||
"### Save:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false, | ||
"jupyter": { | ||
"outputs_hidden": false | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Uncomment this line to save the created animation\n", | ||
"anim.save('images/animate_1.gif', writer='pillow', fps=5);" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"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.10.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.