{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Dependencies | Version\n", "--- | ---\n", "SatPy | 0.9.0\n", "\n", "# Cartopy Plot\n", "\n", "This example loads multiple VIIRS SDR granules of band I04 data, resamples them to a known `AreaDefinition`, and uses matplotlib to plot the data on a map. Although cartopy is not imported directly, we use pyresample's `to_cartopy_crs()` method to create a cartopy CRS object and use that in the plotting.\n", "\n", "This example should act as a starting point for plotting data processed with SatPy. Additional information can be found on [XArray's plotting documentation](http://xarray.pydata.org/en/stable/plotting.html#maps) as well as [Cartopy's documentation](https://scitools.org.uk/cartopy/docs/latest/gallery/geostationary.html#sphx-glr-gallery-geostationary-py) for help building more complex plots." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load data\n", "\n", "Using the `glob` module makes it easy to get a series of data files. After creating the Scene object, we load the I04 band data." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from satpy import Scene\n", "from glob import glob\n", "import matplotlib.pyplot as plt\n", "\n", "filenames = glob('/data/data/viirs/conus_day/*t180*.h5')\n", "scn = Scene(reader='viirs_sdr', filenames=filenames)\n", "scn.load(['I04'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resample to a uniform area\n", "\n", "Next we put the VIIRS data on a high resolution uniform grid. We could have also used any other area definition. For polar-orbiting satellite data it is best to resample the data first before putting it on a plot due to the size of the lon/lat arrays that would need to be used. For geo-stationary satellite data, which is typically already gridded, the source data's area can be used directly in the plotting steps below." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/davidh/anaconda/envs/polar2grid_py36/lib/python3.6/site-packages/dask/local.py:271: RuntimeWarning: invalid value encountered in greater_equal\n", " return func(*args2)\n", "/Users/davidh/anaconda/envs/polar2grid_py36/lib/python3.6/site-packages/dask/local.py:271: RuntimeWarning: invalid value encountered in less_equal\n", " return func(*args2)\n" ] } ], "source": [ "my_area = scn['I04'].attrs['area'].compute_optimal_bb_area({'proj': 'lcc', 'lon_0': -95., 'lat_0': 25., 'lat_1': 25., 'lat_2': 25.})\n", "new_scn = scn.resample(my_area)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot data using matplotlib and cartopy\n", "\n", "To plot the data using cartopy we first convert the pyresample AreaDefinition to a cartopy CRS and then use that to create matplotlib axes." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/davidh/repos/git/pyresample/pyresample/_cartopy.py:36: UserWarning: 'cartopy' >= 0.17 required for better 'from_proj' functionality.\n", " warnings.warn(\"'cartopy' >= 0.17 required for better 'from_proj' \"\n" ] } ], "source": [ "crs = new_scn['I04'].attrs['area'].to_cartopy_crs()\n", "ax = plt.axes(projection=crs)\n", "\n", "ax.coastlines()\n", "ax.gridlines()\n", "ax.set_global()\n", "plt.imshow(new_scn['I04'], transform=crs, extent=crs.bounds, origin='upper')\n", "cbar = plt.colorbar()\n", "cbar.set_label(\"Kelvin\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![title](Cartopy Plot 1.png)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }