|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "cell_marker": "\"\"\"" |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "MetPy Declarative - 300 hPa\n", |
| 10 | + "===========================\n", |
| 11 | + "\n", |
| 12 | + "By: Kevin Goebbert\n", |
| 13 | + "\n", |
| 14 | + "This example uses the declarative syntax available through the MetPy\n", |
| 15 | + "package to allow a more convenient method for creating simple maps of\n", |
| 16 | + "atmospheric data. The key thing the declarative language does is to\n", |
| 17 | + "reduce the number of packages that users will need to know in detail and\n", |
| 18 | + "instead allow them to set key parameters to get the map they desire. One\n", |
| 19 | + "key element is the use of xarray as the data object, which allows\n", |
| 20 | + "coordinate information to be associated with atmospheric variables." |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "from datetime import datetime\n", |
| 30 | + "\n", |
| 31 | + "import metpy.calc as mpcalc\n", |
| 32 | + "from metpy.plots.declarative import *\n", |
| 33 | + "from metpy.units import units\n", |
| 34 | + "import xarray as xr" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "markdown", |
| 39 | + "metadata": { |
| 40 | + "cell_marker": "######################################################################" |
| 41 | + }, |
| 42 | + "source": [ |
| 43 | + "Open dataset using xarray module and subset global GFS to be over the\n", |
| 44 | + "CONUS.\n" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "ds = xr.open_dataset('https://thredds.ucar.edu/thredds/dodsC/casestudies'\n", |
| 54 | + " '/python-gallery/GFS_20101026_1200.nc').sel(\n", |
| 55 | + " lon=slice(360-150, 360-50, 2), lat=slice(65, 20, 2))" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "markdown", |
| 60 | + "metadata": { |
| 61 | + "cell_marker": "######################################################################" |
| 62 | + }, |
| 63 | + "source": [ |
| 64 | + "Calculate Variable and Add to Dataset\n", |
| 65 | + "-------------------------------------\n", |
| 66 | + "\n", |
| 67 | + "Here it is demonstrated how you can calculate a new variable and add it\n", |
| 68 | + "to the xarray dataset (ds) so that it can be plotted with the\n", |
| 69 | + "declarative syntax. The key to adding a variable to an xarray dataset\n", |
| 70 | + "for use in the declarative syntax is the need to add a ``grid_mapping``\n", |
| 71 | + "and ``units`` attribute.\n" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "execution_count": null, |
| 77 | + "metadata": {}, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "# Calculate New Variables and place into Xarray Dataset\n", |
| 81 | + "uwnd = ds['u-component_of_wind_isobaric']\n", |
| 82 | + "vwnd = ds['v-component_of_wind_isobaric']\n", |
| 83 | + "\n", |
| 84 | + "# Compute wind speed using MetPy\n", |
| 85 | + "wspd = mpcalc.wind_speed(uwnd, vwnd)\n", |
| 86 | + "\n", |
| 87 | + "# Place wind speed (wspd) into xarray dataset and attach needed attributes\n", |
| 88 | + "ds['wind_speed'] = wspd" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": { |
| 94 | + "cell_marker": "######################################################################" |
| 95 | + }, |
| 96 | + "source": [ |
| 97 | + "Declarative Plot\n", |
| 98 | + "----------------\n", |
| 99 | + "\n", |
| 100 | + "The following settings create a single panel map plot of 300 hPa\n", |
| 101 | + "geopotential heights, wind speed, and wind barbs.\n" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": null, |
| 107 | + "metadata": {}, |
| 108 | + "outputs": [], |
| 109 | + "source": [ |
| 110 | + "# Countour Plot of Geopotential Heights\n", |
| 111 | + "contour = ContourPlot()\n", |
| 112 | + "contour.data = ds\n", |
| 113 | + "contour.time = datetime(2010, 10, 31, 12)\n", |
| 114 | + "contour.field = 'Geopotential_height_isobaric'\n", |
| 115 | + "contour.level = 300 * units.hPa\n", |
| 116 | + "contour.linecolor = 'black'\n", |
| 117 | + "contour.linestyle = '-'\n", |
| 118 | + "contour.linewidth = 2\n", |
| 119 | + "contour.clabels = True\n", |
| 120 | + "contour.contours = list(range(0, 20000, 120))\n", |
| 121 | + "\n", |
| 122 | + "# Colorfilled Plot of Wind Speed\n", |
| 123 | + "cfill = FilledContourPlot()\n", |
| 124 | + "cfill.data = ds\n", |
| 125 | + "cfill.field = 'wind_speed'\n", |
| 126 | + "cfill.level = 300 * units.hPa\n", |
| 127 | + "cfill.colormap = 'BuPu'\n", |
| 128 | + "cfill.contours = list(range(50, 171, 20))\n", |
| 129 | + "cfill.colorbar = 'vertical'\n", |
| 130 | + "cfill.plot_units = 'kt'\n", |
| 131 | + "\n", |
| 132 | + "# Plot wind barbs\n", |
| 133 | + "barb = BarbPlot()\n", |
| 134 | + "barb.data = ds\n", |
| 135 | + "barb.level = 300 * units.hPa\n", |
| 136 | + "barb.field = ['u-component_of_wind_isobaric', 'v-component_of_wind_isobaric']\n", |
| 137 | + "barb.skip = (3, 3)\n", |
| 138 | + "barb.color = 'black'\n", |
| 139 | + "barb.barblength = 6.5\n", |
| 140 | + "barb.earth_relative = False\n", |
| 141 | + "barb.plot_units = 'kt'\n", |
| 142 | + "\n", |
| 143 | + "# Panel for plot with Map features\n", |
| 144 | + "panel = MapPanel()\n", |
| 145 | + "panel.layout = (1, 1, 1)\n", |
| 146 | + "panel.area = (-124, -72, 20, 53)\n", |
| 147 | + "panel.projection = 'lcc'\n", |
| 148 | + "panel.layers = ['coastline', 'borders', 'states', 'land']\n", |
| 149 | + "panel.plots = [cfill, contour, barb]\n", |
| 150 | + "\n", |
| 151 | + "# Bringing it all together\n", |
| 152 | + "pc = PanelContainer()\n", |
| 153 | + "pc.size = (15, 9)\n", |
| 154 | + "pc.panels = [panel]\n", |
| 155 | + "\n", |
| 156 | + "pc.show()" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": null, |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [], |
| 164 | + "source": [] |
| 165 | + } |
| 166 | + ], |
| 167 | + "metadata": { |
| 168 | + "jupytext": { |
| 169 | + "cell_metadata_filter": "-all", |
| 170 | + "main_language": "python", |
| 171 | + "notebook_metadata_filter": "-all" |
| 172 | + }, |
| 173 | + "kernelspec": { |
| 174 | + "display_name": "Python 3 (ipykernel)", |
| 175 | + "language": "python", |
| 176 | + "name": "python3" |
| 177 | + }, |
| 178 | + "language_info": { |
| 179 | + "codemirror_mode": { |
| 180 | + "name": "ipython", |
| 181 | + "version": 3 |
| 182 | + }, |
| 183 | + "file_extension": ".py", |
| 184 | + "mimetype": "text/x-python", |
| 185 | + "name": "python", |
| 186 | + "nbconvert_exporter": "python", |
| 187 | + "pygments_lexer": "ipython3", |
| 188 | + "version": "3.11.4" |
| 189 | + } |
| 190 | + }, |
| 191 | + "nbformat": 4, |
| 192 | + "nbformat_minor": 4 |
| 193 | +} |
0 commit comments