.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/project_grid.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_project_grid.py: Projection of gridded data ========================== Sometimes gridded data products need to be projected before they can be used. For example, you might want to project the topography of Antarctica from geographic latitude and longitude to a planar polar stereographic projection before doing your analysis. When projecting, the data points will likely not fall on a regular grid anymore and must be interpolated (re-sampled) onto a new grid. The :func:`verde.project_grid` function automates this process using the interpolation methods available in Verde. An input grid (:class:`xarray.DataArray`) is interpolated onto a new grid in the given `pyproj `__ projection. The function takes care of choosing a default grid spacing and region, running a blocked mean to avoid spatial aliasing (using :class:`~verde.BlockReduce`), and masking the points in the new grid that aren't constrained by the original data (using :func:`~verde.convexhull_mask`). In this example, we'll generate a synthetic geographic grid with a checkerboard pattern around the South pole. We'll project the grid to South Polar Stereographic, revealing the checkered pattern of the data. .. note:: The interpolation methods are limited to what is available in Verde and there is only support for single 2D grids. For more sophisticated use cases, you might want to try `pyresample `__ instead. .. GENERATED FROM PYTHON SOURCE LINES 39-84 .. image-sg:: /gallery/images/sphx_glr_project_grid_001.png :alt: Geographic Grid, Polar Stereographic Grid :srcset: /gallery/images/sphx_glr_project_grid_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /usr/share/miniconda3/envs/test/lib/python3.9/site-packages/verde/base/base_classes.py:463: FutureWarning: The 'spacing', 'shape' and 'region' arguments will be removed in Verde v2.0.0. Please use the 'verde.grid_coordinates' function to define grid coordinates and pass them as the 'coordinates' argument. warnings.warn( Geographic grid: Dimensions: (latitude: 121, longitude: 1441) Coordinates: * longitude (longitude) float64 0.0 0.25 0.5 0.75 ... 359.5 359.8 360.0 * latitude (latitude) float64 -90.0 -89.75 -89.5 ... -60.5 -60.25 -60.0 Data variables: checkerboard (latitude, longitude) float64 0.0 0.0 0.0 ... 90.55 45.51 -0.0 Attributes: metadata: Generated by CheckerBoard(region=(-3333134.027630276, 3333134.... /usr/share/miniconda3/envs/test/lib/python3.9/site-packages/verde/base/base_classes.py:463: FutureWarning: The 'spacing', 'shape' and 'region' arguments will be removed in Verde v2.0.0. Please use the 'verde.grid_coordinates' function to define grid coordinates and pass them as the 'coordinates' argument. warnings.warn( Projected grid: array([[nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan], ..., [nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan], [nan, nan, nan, ..., nan, nan, nan]]) Coordinates: * x (x) float64 -3.333e+06 -3.283e+06 ... 3.283e+06 3.333e+06 * y (y) float64 -3.333e+06 -3.283e+06 ... 3.283e+06 3.333e+06 Attributes: metadata: Generated by Chain(steps=[('mean',\n BlockReduce(... | .. code-block:: default import matplotlib.pyplot as plt import pyproj import verde as vd # We'll use synthetic data near the South pole to highlight the effects of the # projection. EPSG 3031 is a South Polar Stereographic projection. projection = pyproj.Proj("epsg:3031") # Create a synthetic geographic grid using a checkerboard pattern region = (0, 360, -90, -60) spacing = 0.25 wavelength = 10 * 1e5 # The size of the cells in the checkerboard checkerboard = vd.synthetic.CheckerBoard( region=vd.project_region(region, projection), w_east=wavelength, w_north=wavelength ) data = checkerboard.grid( region=region, spacing=spacing, projection=projection, data_names="checkerboard", dims=("latitude", "longitude"), ) print("Geographic grid:") print(data) # Do the projection while setting the output grid spacing (in projected # meters). Set the coordinates names to x and y since they aren't really # "northing" or "easting". polar_data = vd.project_grid( data.checkerboard, projection, spacing=0.5 * 1e5, dims=("y", "x") ) print("\nProjected grid:") print(polar_data) # Plot the original and projected grids fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6)) data.checkerboard.plot( ax=ax1, cbar_kwargs=dict(orientation="horizontal", aspect=50, pad=0.1) ) ax1.set_title("Geographic Grid") polar_data.plot(ax=ax2, cbar_kwargs=dict(orientation="horizontal", aspect=50, pad=0.1)) ax2.set_title("Polar Stereographic Grid") plt.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.820 seconds) .. _sphx_glr_download_gallery_project_grid.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: project_grid.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: project_grid.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_