{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "1598b0e8",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "0166efba",
            "cell_type": "markdown",
            "source": "Suppose that we want to use splines to interpolate a bivariate function on a\nsphere. The value of the function is known on a grid of longitudes and\ncolatitudes.\n",
            "metadata": {}
        },
        {
            "id": "0546d98c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.interpolate import RectSphereBivariateSpline\ndef f(theta, phi):\n    return np.sin(theta) * np.cos(phi)",
            "outputs": []
        },
        {
            "id": "89a118ce",
            "cell_type": "markdown",
            "source": "We evaluate the function on the grid. Note that the default indexing=\"xy\"\nof meshgrid would result in an unexpected (transposed) result after\ninterpolation.\n",
            "metadata": {}
        },
        {
            "id": "43564a9d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "thetaarr = np.linspace(0, np.pi, 22)[1:-1]\nphiarr = np.linspace(0, 2 * np.pi, 21)[:-1]\nthetagrid, phigrid = np.meshgrid(thetaarr, phiarr, indexing=\"ij\")\nzdata = f(thetagrid, phigrid)",
            "outputs": []
        },
        {
            "id": "54e3c4c4",
            "cell_type": "markdown",
            "source": "We next set up the interpolator and use it to evaluate the function\nat points not on the original grid.\n",
            "metadata": {}
        },
        {
            "id": "9a2c3adb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "rsbs = RectSphereBivariateSpline(thetaarr, phiarr, zdata)\nthetainterp = np.linspace(thetaarr[0], thetaarr[-1], 200)\nphiinterp = np.linspace(phiarr[0], phiarr[-1], 200)\nzinterp = rsbs.ev(thetainterp, phiinterp)",
            "outputs": []
        },
        {
            "id": "08187ffa",
            "cell_type": "markdown",
            "source": "Finally we plot the original data for a diagonal slice through the\ninitial grid, and the spline approximation along the same slice.\n",
            "metadata": {}
        },
        {
            "id": "212481a7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nfig = plt.figure()\nax1 = fig.add_subplot(1, 1, 1)\nax1.plot(np.sin(thetaarr) * np.sin(phiarr), np.diag(zdata), \"or\")\nax1.plot(np.sin(thetainterp) * np.sin(phiinterp), zinterp, \"-b\")\nplt.show()",
            "outputs": []
        }
    ]
}