{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "f247859c",
            "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": "2442a747",
            "cell_type": "markdown",
            "source": "Suppose we have global data on a coarse grid (the input data does not\nhave to be on a grid):\n",
            "metadata": {}
        },
        {
            "id": "40fb2448",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.interpolate import LSQSphereBivariateSpline\nimport numpy as np\nimport matplotlib.pyplot as plt",
            "outputs": []
        },
        {
            "id": "fb51904d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "theta = np.linspace(0, np.pi, num=7)\nphi = np.linspace(0, 2*np.pi, num=9)\ndata = np.empty((theta.shape[0], phi.shape[0]))\ndata[:,0], data[0,:], data[-1,:] = 0., 0., 0.\ndata[1:-1,1], data[1:-1,-1] = 1., 1.\ndata[1,1:-1], data[-2,1:-1] = 1., 1.\ndata[2:-2,2], data[2:-2,-2] = 2., 2.\ndata[2,2:-2], data[-3,2:-2] = 2., 2.\ndata[3,3:-2] = 3.\ndata = np.roll(data, 4, 1)",
            "outputs": []
        },
        {
            "id": "6db210fb",
            "cell_type": "markdown",
            "source": "We need to set up the interpolator object. Here, we must also specify the\ncoordinates of the knots to use.\n",
            "metadata": {}
        },
        {
            "id": "8086a4dd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "lats, lons = np.meshgrid(theta, phi)\nknotst, knotsp = theta.copy(), phi.copy()\nknotst[0] += .0001\nknotst[-1] -= .0001\nknotsp[0] += .0001\nknotsp[-1] -= .0001\nlut = LSQSphereBivariateSpline(lats.ravel(), lons.ravel(),\n                               data.T.ravel(), knotst, knotsp)",
            "outputs": []
        },
        {
            "id": "d3d9174f",
            "cell_type": "markdown",
            "source": "As a first test, we'll see what the algorithm returns when run on the\ninput coordinates\n",
            "metadata": {}
        },
        {
            "id": "20cf069d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "data_orig = lut(theta, phi)",
            "outputs": []
        },
        {
            "id": "1c50bf08",
            "cell_type": "markdown",
            "source": "Finally we interpolate the data to a finer grid\n",
            "metadata": {}
        },
        {
            "id": "e2f97e9f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fine_lats = np.linspace(0., np.pi, 70)\nfine_lons = np.linspace(0., 2*np.pi, 90)\ndata_lsq = lut(fine_lats, fine_lons)",
            "outputs": []
        },
        {
            "id": "e2b795bb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig = plt.figure()\nax1 = fig.add_subplot(131)\nax1.imshow(data, interpolation='nearest')\nax2 = fig.add_subplot(132)\nax2.imshow(data_orig, interpolation='nearest')\nax3 = fig.add_subplot(133)\nax3.imshow(data_lsq, interpolation='nearest')\nplt.show()",
            "outputs": []
        }
    ]
}