{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Animating plots\nHow to make animated plots.\n\nIf multiple timesteps are loaded, then it is possible to make an animation with\nany of the plotting methods associated with a `Variable` object.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, load the required modules.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from psipy.data import sample_data\nfrom psipy.model import MASOutput"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, load a set of MAS output files. Here we just download two succesive\nCarrington rotations, but you can change this line to point to any directory\nthat has data files spanning multiple timesteps of a simulation.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mas_path = sample_data.mas_helio_timesteps()\nmodel = MASOutput(mas_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get a variable from the model output. We can also inspect the variable to see\nhow many timesteps it has.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "vr = model[\"vr\"]\nprint(f\"Number of timesteps: {vr.n_timesteps}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can now create an animation. In this example we plot a radial cut at the\nouter boundary of the model (i.e. a radial index of ``-1``).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "animation = vr.plot_radial_cut(r_idx=-1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Note that ``animation`` is a `matplotlib.animation.Animation` object. To save\nthe animation to disk you can use the\n:meth:`~matplotlib.animation.Animation.save` method, e.g.:\n``animation.save('mymovie.mp4')``\n\n"
      ]
    }
  ],
  "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.10.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}