.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ex_4d.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. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_ex_4d.py: **************** 4D interpolation **************** Interpolation of a four-dimensional regular grid. Quadrivariate ============= The :py:func:`quadrivariate ` interpolation allows obtaining values at arbitrary points in a 4D space of a function defined on a grid. This method performs a bilinear interpolation in 2D space by considering the axes of longitude and latitude of the grid, then performs a linear interpolation in the third and fourth dimensions. Its interface is similar to the :py:func:`trivariate ` class except for a fourth axis, which is handled by this object. .. GENERATED FROM PYTHON SOURCE LINES 22-31 .. code-block:: Python import cartopy.crs import matplotlib import matplotlib.pyplot import numpy import pyinterp import pyinterp.backends.xarray import pyinterp.tests .. GENERATED FROM PYTHON SOURCE LINES 32-34 The first step is to load the data into memory and create the interpolator object: .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python ds = pyinterp.tests.load_grid4d() interpolator = pyinterp.backends.xarray.Grid4D(ds.pressure) .. GENERATED FROM PYTHON SOURCE LINES 38-46 We will build a new grid that will be used to build a new interpolated grid. .. warning :: When using a time axis, care must be taken to use the same unit of dates, between the axis defined and the dates supplied during interpolation. The function :py:meth:`pyinterp.TemporalAxis.safe_cast` automates this task and will warn you if there is an inconsistency during the date conversion. .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: Python mx, my, mz, mu = numpy.meshgrid(numpy.arange(-125, -70, 0.5), numpy.arange(25, 50, 0.5), numpy.datetime64('2000-01-01T12:00'), 0.5, indexing='ij') .. GENERATED FROM PYTHON SOURCE LINES 53-55 We interpolate our grid using a :py:meth:`classical `: .. GENERATED FROM PYTHON SOURCE LINES 55-62 .. code-block:: Python quadrivariate = interpolator.quadrivariate({ 'longitude': mx.ravel(), 'latitude': my.ravel(), 'time': mz.ravel(), 'level': mu.ravel() }).reshape(mx.shape) .. GENERATED FROM PYTHON SOURCE LINES 63-70 Bicubic on 4D grid ================== Used grid organizes the latitudes in descending order. We ask our constructor to flip this axis in order to correctly evaluate the bicubic interpolation from this 4D cube (only necessary to perform a bicubic interpolation). .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python interpolator = pyinterp.backends.xarray.Grid4D(ds.pressure, increasing_axes=True) .. GENERATED FROM PYTHON SOURCE LINES 74-77 We interpolate our grid using a :py:meth:`bicubic ` interpolation in space followed by a linear interpolation in the temporal axis: .. GENERATED FROM PYTHON SOURCE LINES 77-87 .. code-block:: Python bicubic = interpolator.bicubic( { 'longitude': mx.ravel(), 'latitude': my.ravel(), 'time': mz.ravel(), 'level': mu.ravel() }, nx=2, ny=2).reshape(mx.shape) .. GENERATED FROM PYTHON SOURCE LINES 88-89 We transform our result cubes into a matrix. .. GENERATED FROM PYTHON SOURCE LINES 89-94 .. code-block:: Python quadrivariate = quadrivariate.squeeze(axis=(2, 3)) bicubic = bicubic.squeeze(axis=(2, 3)) lons = mx[:, 0].squeeze() lats = my[0, :].squeeze() .. GENERATED FROM PYTHON SOURCE LINES 95-103 Let's visualize our results. .. note:: The resolution of the grid example is very low (one pixel everyone degree) therefore the calculation window cannot find the required pixels at the edges to calculate the interpolation correctly. See Chapter :doc:`ex_fill_undef` to see how to address this issue. .. GENERATED FROM PYTHON SOURCE LINES 103-127 .. code-block:: Python fig = matplotlib.pyplot.figure(figsize=(5, 4)) ax1 = fig.add_subplot( 211, projection=cartopy.crs.PlateCarree(central_longitude=180)) pcm = ax1.pcolormesh(lons, lats, quadrivariate.T, cmap='jet', shading='auto', transform=cartopy.crs.PlateCarree()) ax1.coastlines() ax1.set_title('Trilinear') ax2 = fig.add_subplot( 212, projection=cartopy.crs.PlateCarree(central_longitude=180)) pcm = ax2.pcolormesh(lons, lats, bicubic.T, cmap='jet', shading='auto', transform=cartopy.crs.PlateCarree()) ax2.coastlines() ax2.set_title('Spline & Linear in time') fig.colorbar(pcm, ax=[ax1, ax2], shrink=0.8) fig.show() .. image-sg:: /auto_examples/images/sphx_glr_ex_4d_001.png :alt: Trilinear, Spline & Linear in time :srcset: /auto_examples/images/sphx_glr_ex_4d_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.210 seconds) .. _sphx_glr_download_auto_examples_ex_4d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/CNES/pangeo-pyinterp/master?urlpath=lab/tree/notebooks/auto_examples/ex_4d.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ex_4d.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ex_4d.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ex_4d.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_