.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/latlon_to_healpix.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_latlon_to_healpix.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_latlon_to_healpix.py:


HealPIX regridding
------------------

In this notebook, I demonstrate bilinear regridding onto healpy grids in O(10)
ms. this is a 3 order of magnitude speed-up compared to what Dale has reported.

Now, lets define a healpix grid with indexing in the XY convention. we convert
to NEST indexing in order to use the `healpy.pix2ang` to get the lat lon
coordinates. This operation is near instant.

.. GENERATED FROM PYTHON SOURCE LINES 28-55

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np
    import torch

    import earth2grid

    # level is the resolution
    level = 6
    hpx = earth2grid.healpix.Grid(level=level, pixel_order=earth2grid.healpix.XY())
    src = earth2grid.latlon.equiangular_lat_lon_grid(32, 64)
    regrid = earth2grid.get_regridder(src, hpx)


    z = np.cos(np.deg2rad(src.lat)) * np.cos(np.deg2rad(src.lon))


    z_torch = torch.as_tensor(z)
    z_hpx = regrid(z_torch)

    fig, (a, b) = plt.subplots(2, 1)
    a.pcolormesh(src.lon, src.lat, z)
    a.set_title("Lat Lon Grid")

    b.scatter(hpx.lon, hpx.lat, c=z_hpx, s=0.1)
    b.set_title("Healpix")




.. image-sg:: /auto_examples/images/sphx_glr_latlon_to_healpix_001.png
   :alt: Lat Lon Grid, Healpix
   :srcset: /auto_examples/images/sphx_glr_latlon_to_healpix_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    Text(0.5, 1.0, 'Healpix')



.. GENERATED FROM PYTHON SOURCE LINES 56-67

.. code-block:: Python

    nside = 2**level
    reshaped = z_hpx.reshape(12, nside, nside)
    lat_r = hpx.lat.reshape(12, nside, nside)
    lon_r = hpx.lon.reshape(12, nside, nside)

    tile = 11
    fig, axs = plt.subplots(3, 4, sharex=True, sharey=True)
    axs = axs.ravel()

    for tile in range(12):
        axs[tile].pcolormesh(lon_r[tile], lat_r[tile], reshaped[tile])



.. image-sg:: /auto_examples/images/sphx_glr_latlon_to_healpix_002.png
   :alt: latlon to healpix
   :srcset: /auto_examples/images/sphx_glr_latlon_to_healpix_002.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    /Users/nbrenowitz/workspace/nvidia/earth2grid/examples/sphinx_gallery/latlon_to_healpix.py:66: UserWarning: The input coordinates to pcolormesh are interpreted as cell centers, but are not monotonically increasing or decreasing. This may lead to incorrectly calculated cell edges, in which case, please supply explicit cell edges to pcolormesh.
      axs[tile].pcolormesh(lon_r[tile], lat_r[tile], reshaped[tile])





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.666 seconds)


.. _sphx_glr_download_auto_examples_latlon_to_healpix.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: latlon_to_healpix.ipynb <latlon_to_healpix.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: latlon_to_healpix.py <latlon_to_healpix.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_