.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/gridding/Wind_SLP_Interpolation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_gridding_Wind_SLP_Interpolation.py: ========================================= Wind and Sea Level Pressure Interpolation ========================================= Interpolate sea level pressure, as well as wind component data, to make a consistent looking analysis, featuring contours of pressure and wind barbs. .. GENERATED FROM PYTHON SOURCE LINES 12-27 .. code-block:: default import cartopy.crs as ccrs import cartopy.feature as cfeature from matplotlib.colors import BoundaryNorm import matplotlib.pyplot as plt import numpy as np import pandas as pd from metpy.calc import wind_components from metpy.cbook import get_test_data from metpy.interpolate import interpolate_to_grid, remove_nan_observations from metpy.plots import add_metpy_logo from metpy.units import units to_proj = ccrs.AlbersEqualArea(central_longitude=-97., central_latitude=38.) .. GENERATED FROM PYTHON SOURCE LINES 28-29 Read in data .. GENERATED FROM PYTHON SOURCE LINES 29-35 .. code-block:: default with get_test_data('station_data.txt') as f: data = pd.read_csv(f, header=0, usecols=(2, 3, 4, 5, 18, 19), names=['latitude', 'longitude', 'slp', 'temperature', 'wind_dir', 'wind_speed'], na_values=-99999) .. GENERATED FROM PYTHON SOURCE LINES 36-37 Project the lon/lat locations to our final projection .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: default lon = data['longitude'].values lat = data['latitude'].values xp, yp, _ = to_proj.transform_points(ccrs.Geodetic(), lon, lat).T .. GENERATED FROM PYTHON SOURCE LINES 42-43 Remove all missing data from pressure .. GENERATED FROM PYTHON SOURCE LINES 43-45 .. code-block:: default x_masked, y_masked, pressure = remove_nan_observations(xp, yp, data['slp'].values) .. GENERATED FROM PYTHON SOURCE LINES 46-47 Interpolate pressure using Cressman interpolation .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: default slpgridx, slpgridy, slp = interpolate_to_grid(x_masked, y_masked, pressure, interp_type='cressman', minimum_neighbors=1, search_radius=400000, hres=100000) .. GENERATED FROM PYTHON SOURCE LINES 52-53 Get wind information and mask where either speed or direction is unavailable .. GENERATED FROM PYTHON SOURCE LINES 53-63 .. code-block:: default wind_speed = (data['wind_speed'].values * units('m/s')).to('knots') wind_dir = data['wind_dir'].values * units.degree good_indices = np.where((~np.isnan(wind_dir)) & (~np.isnan(wind_speed))) x_masked = xp[good_indices] y_masked = yp[good_indices] wind_speed = wind_speed[good_indices] wind_dir = wind_dir[good_indices] .. GENERATED FROM PYTHON SOURCE LINES 64-67 Calculate u and v components of wind and then interpolate both. Both will have the same underlying grid so throw away grid returned from v interpolation. .. GENERATED FROM PYTHON SOURCE LINES 67-76 .. code-block:: default u, v = wind_components(wind_speed, wind_dir) windgridx, windgridy, uwind = interpolate_to_grid(x_masked, y_masked, np.array(u), interp_type='cressman', search_radius=400000, hres=100000) _, _, vwind = interpolate_to_grid(x_masked, y_masked, np.array(v), interp_type='cressman', search_radius=400000, hres=100000) .. GENERATED FROM PYTHON SOURCE LINES 77-78 Get temperature information .. GENERATED FROM PYTHON SOURCE LINES 78-84 .. code-block:: default x_masked, y_masked, t = remove_nan_observations(xp, yp, data['temperature'].values) tempx, tempy, temp = interpolate_to_grid(x_masked, y_masked, t, interp_type='cressman', minimum_neighbors=3, search_radius=400000, hres=35000) temp = np.ma.masked_where(np.isnan(temp), temp) .. GENERATED FROM PYTHON SOURCE LINES 85-86 Set up the map and plot the interpolated grids appropriately. .. GENERATED FROM PYTHON SOURCE LINES 86-111 .. code-block:: default levels = list(range(-20, 20, 1)) cmap = plt.get_cmap('viridis') norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) fig = plt.figure(figsize=(20, 10)) add_metpy_logo(fig, 360, 120, size='large') view = fig.add_subplot(1, 1, 1, projection=to_proj) view.set_extent([-120, -70, 20, 50]) view.add_feature(cfeature.STATES.with_scale('50m')) view.add_feature(cfeature.OCEAN) view.add_feature(cfeature.COASTLINE.with_scale('50m')) view.add_feature(cfeature.BORDERS, linestyle=':') cs = view.contour(slpgridx, slpgridy, slp, colors='k', levels=list(range(990, 1034, 4))) view.clabel(cs, inline=1, fontsize=12, fmt='%i') mmb = view.pcolormesh(tempx, tempy, temp, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0.02, boundaries=levels) view.barbs(windgridx, windgridy, uwind, vwind, alpha=.4, length=5) view.set_title('Surface Temperature (shaded), SLP, and Wind.') plt.show() .. image-sg:: /examples/gridding/images/sphx_glr_Wind_SLP_Interpolation_001.png :alt: Surface Temperature (shaded), SLP, and Wind. :srcset: /examples/gridding/images/sphx_glr_Wind_SLP_Interpolation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 7.485 seconds) .. _sphx_glr_download_examples_gridding_Wind_SLP_Interpolation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: Wind_SLP_Interpolation.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: Wind_SLP_Interpolation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_