{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# SPICE orbit plotting\n\nHow to plot orbits from SPICE kernels.\n\nIn this example we download the Parker Solar Probe SPICE kernel, and plot\nits orbit for the first year.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import heliopy.data.spice as spicedata\nimport heliopy.spice as spice\nfrom datetime import datetime, timedelta\nimport astropy.units as u\nimport numpy as np"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load the solar orbiter spice kernel. heliopy will automatically fetch and\nload the latest kernel\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "spicedata.get_kernel('psp')\nspicedata.get_kernel('psp_pred')\npsp = spice.Trajectory('SPP')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate a time for every day between starttime and endtime\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "starttime = datetime(2018, 8, 14)\nendtime = starttime + timedelta(days=365)\ntimes = []\nwhile starttime < endtime:\n    times.append(starttime)\n    starttime += timedelta(hours=6)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate positions\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "psp.generate_positions(times, 'Sun', 'ECLIPJ2000')\npsp.change_units(u.au)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the orbit. The orbit is plotted in 3D\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom astropy.visualization import quantity_support\nquantity_support()\n\n# Generate a set of timestamps to color the orbits by\ntimes_float = (psp.times - psp.times[0]).value\nfig = plt.figure()\nax = fig.add_subplot(111, projection='3d')\nkwargs = {'s': 3, 'c': times_float}\nax.scatter(psp.x, psp.y, psp.z, **kwargs)\nax.set_xlim(-1, 1)\nax.set_ylim(-1, 1)\nax.set_zlim(-1, 1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot radial distance and elevation as a function of time\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plot_times = psp.times.to_datetime()\nelevation = np.rad2deg(np.arcsin(psp.z / psp.r))\n\nfig, axs = plt.subplots(3, 1, sharex=True)\naxs[0].plot(plot_times, psp.r)\naxs[0].set_ylim(0, 1.1)\naxs[0].set_ylabel('r (AU)')\n\naxs[1].plot(plot_times, elevation)\naxs[1].set_ylabel('Elevation (deg)')\n\naxs[2].plot(plot_times, psp.speed)\naxs[2].set_ylabel('Speed (km/s)')\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}