Animation
+time stamp at 1:19
+NCL_animate_1
+Please note:
+-
+
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.
+
Import packages:
+import cartopy.crs as ccrs
+import matplotlib.animation as animation
+import numpy as np
+import xarray as xr
+from matplotlib import pyplot as plt
+
+import geocat.datafiles as gdf
+import geocat.viz as gv
+
Downloading file 'registry.txt' from 'https://github.com/NCAR/GeoCAT-datafiles/raw/main/registry.txt' to '/home/runner/.cache/geocat'.
+
Read in data:
+# Open a netCDF data file using xarray default engine and load the data into xarrays
+# Disable time decoding due to missing necessary metadata
+ds = xr.open_dataset(gdf.get("netcdf_files/meccatemp.cdf"), decode_times=False)
+
+tas = ds.t
+tas
+
Downloading file 'netcdf_files/meccatemp.cdf' from 'https://github.com/NCAR/GeoCAT-datafiles/raw/main/netcdf_files/meccatemp.cdf' to '/home/runner/.cache/geocat'.
+
<xarray.DataArray 't' (time: 31, lat: 40, lon: 49)> +[60760 values with dtype=float32] +Coordinates: + * lat (lat) int32 -86 -82 -77 -73 -68 -64 -59 ... 59 64 68 73 77 82 86 + * lon (lon) int32 -180 -172 -165 -157 -150 -142 ... 150 157 165 172 180 + * time (time) int32 1 2 3 4 5 6 7 8 9 10 ... 22 23 24 25 26 27 28 29 30 31 +Attributes: + units: degrees
Create animation:
+# Set up Axes with Cartopy Projection
+fig = plt.figure(figsize=(10, 8))
+ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=150))
+ax.coastlines(linewidths=0.5)
+ax.set_extent([-180, 180, -90, 90], ccrs.PlateCarree())
+
+# Use geocat.viz.util convenience function to set axes limits & tick values
+gv.set_axes_limits_and_ticks(ax,
+ xlim=(-180, 180),
+ ylim=(-90, 90),
+ xticks=np.linspace(-180, 180, 13),
+ yticks=np.linspace(-90, 90, 7))
+
+# Use geocat.viz.util convenience function to add minor and major tick lines
+gv.add_major_minor_ticks(ax, labelsize=10)
+
+# Use geocat.viz.util convenience function to make latitude, longitude tick labels
+gv.add_lat_lon_ticklabels(ax)
+
+# create initial plot that establishes a colorbar
+tas[0, :, :].plot.contourf(ax=ax,
+ transform=ccrs.PlateCarree(),
+ vmin=195,
+ vmax=328,
+ levels=53,
+ cmap="inferno",
+ cbar_kwargs={
+ "extendrect": True,
+ "orientation": "horizontal",
+ "ticks": np.arange(195, 332, 9),
+ "label": "",
+ "shrink": 0.90
+ })
+
+
+# animate function for matplotlib FuncAnimation
+def animate(i):
+ tas[i, :, :].plot.contourf(
+ ax=ax,
+ transform=ccrs.PlateCarree(),
+ vmin=195,
+ vmax=328,
+ levels=53,
+ cmap="inferno",
+ add_colorbar=False,
+ )
+
+ gv.set_titles_and_labels(
+ ax,
+ maintitle="January Global Surface Temperature (K) - Day " +
+ str(tas.coords['time'].values[i])[:13],
+ xlabel="",
+ ylabel="")
+
/usr/share/miniconda3/envs/cookbook-dev/lib/python3.10/site-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/110m_physical/ne_110m_coastline.zip
+ warnings.warn(f'Downloading: {url}', DownloadWarning)
+