.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/gridding/Point_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_Point_Interpolation.py: =================== Point Interpolation =================== Compares different point interpolation approaches. .. GENERATED FROM PYTHON SOURCE LINES 11-24 .. code-block:: Python import cartopy.crs as ccrs import cartopy.feature as cfeature from matplotlib.colors import BoundaryNorm import matplotlib.pyplot as plt import numpy as np from metpy.cbook import get_test_data from metpy.interpolate import (interpolate_to_grid, remove_nan_observations, remove_repeat_coordinates) from metpy.plots import add_metpy_logo .. GENERATED FROM PYTHON SOURCE LINES 25-76 .. code-block:: Python def basic_map(proj, title): """Make our basic default map for plotting""" fig = plt.figure(figsize=(15, 10)) add_metpy_logo(fig, 0, 80, size='large') view = fig.add_axes([0, 0, 1, 1], projection=proj) view.set_title(title) 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) view.add_feature(cfeature.BORDERS, linestyle=':') return fig, view def station_test_data(variable_names, proj_from=None, proj_to=None): with get_test_data('station_data.txt') as f: all_data = np.loadtxt(f, skiprows=1, delimiter=',', usecols=(1, 2, 3, 4, 5, 6, 7, 17, 18, 19), dtype=np.dtype([('stid', '3S'), ('lat', 'f'), ('lon', 'f'), ('slp', 'f'), ('air_temperature', 'f'), ('cloud_fraction', 'f'), ('dewpoint', 'f'), ('weather', '16S'), ('wind_dir', 'f'), ('wind_speed', 'f')])) all_stids = [s.decode('ascii') for s in all_data['stid']] data = np.concatenate([all_data[all_stids.index(site)].reshape(1, ) for site in all_stids]) value = data[variable_names] lon = data['lon'] lat = data['lat'] if proj_from is not None and proj_to is not None: proj_points = proj_to.transform_points(proj_from, lon, lat) return proj_points[:, 0], proj_points[:, 1], value return lon, lat, value from_proj = ccrs.Geodetic() to_proj = ccrs.AlbersEqualArea(central_longitude=-97.0000, central_latitude=38.0000) levels = list(range(-20, 20, 1)) cmap = plt.get_cmap('magma') norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) x, y, temp = station_test_data('air_temperature', from_proj, to_proj) x, y, temp = remove_nan_observations(x, y, temp) x, y, temp = remove_repeat_coordinates(x, y, temp) .. GENERATED FROM PYTHON SOURCE LINES 77-79 Scipy.interpolate linear ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 79-85 .. code-block:: Python gx, gy, img = interpolate_to_grid(x, y, temp, interp_type='linear', hres=75000) img = np.ma.masked_where(np.isnan(img), img) fig, view = basic_map(to_proj, 'Linear') mmb = view.pcolormesh(gx, gy, img, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0, boundaries=levels) .. image-sg:: /examples/gridding/images/sphx_glr_Point_Interpolation_001.png :alt: Linear :srcset: /examples/gridding/images/sphx_glr_Point_Interpolation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 86-89 Natural neighbor interpolation (MetPy implementation) ----------------------------------------------------- `Reference `_ .. GENERATED FROM PYTHON SOURCE LINES 89-95 .. code-block:: Python gx, gy, img = interpolate_to_grid(x, y, temp, interp_type='natural_neighbor', hres=75000) img = np.ma.masked_where(np.isnan(img), img) fig, view = basic_map(to_proj, 'Natural Neighbor') mmb = view.pcolormesh(gx, gy, img, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0, boundaries=levels) .. image-sg:: /examples/gridding/images/sphx_glr_Point_Interpolation_002.png :alt: Natural Neighbor :srcset: /examples/gridding/images/sphx_glr_Point_Interpolation_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 96-103 Cressman interpolation ---------------------- search_radius = 100 km grid resolution = 75 km min_neighbors = 1 .. GENERATED FROM PYTHON SOURCE LINES 103-110 .. code-block:: Python gx, gy, img = interpolate_to_grid(x, y, temp, interp_type='cressman', minimum_neighbors=1, hres=75000, search_radius=100000) img = np.ma.masked_where(np.isnan(img), img) fig, view = basic_map(to_proj, 'Cressman') mmb = view.pcolormesh(gx, gy, img, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0, boundaries=levels) .. image-sg:: /examples/gridding/images/sphx_glr_Point_Interpolation_003.png :alt: Cressman :srcset: /examples/gridding/images/sphx_glr_Point_Interpolation_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 111-116 Barnes Interpolation -------------------- search_radius = 100km min_neighbors = 3 .. GENERATED FROM PYTHON SOURCE LINES 116-123 .. code-block:: Python gx, gy, img1 = interpolate_to_grid(x, y, temp, interp_type='barnes', hres=75000, search_radius=100000) img1 = np.ma.masked_where(np.isnan(img1), img1) fig, view = basic_map(to_proj, 'Barnes') mmb = view.pcolormesh(gx, gy, img1, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0, boundaries=levels) .. image-sg:: /examples/gridding/images/sphx_glr_Point_Interpolation_004.png :alt: Barnes :srcset: /examples/gridding/images/sphx_glr_Point_Interpolation_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 124-127 Radial basis function interpolation ------------------------------------ linear .. GENERATED FROM PYTHON SOURCE LINES 127-135 .. code-block:: Python gx, gy, img = interpolate_to_grid(x, y, temp, interp_type='rbf', hres=75000, rbf_func='linear', rbf_smooth=0) img = np.ma.masked_where(np.isnan(img), img) fig, view = basic_map(to_proj, 'Radial Basis Function') mmb = view.pcolormesh(gx, gy, img, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0, boundaries=levels) plt.show() .. image-sg:: /examples/gridding/images/sphx_glr_Point_Interpolation_005.png :alt: Radial Basis Function :srcset: /examples/gridding/images/sphx_glr_Point_Interpolation_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.412 seconds) .. _sphx_glr_download_examples_gridding_Point_Interpolation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: Point_Interpolation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: Point_Interpolation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: Point_Interpolation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_