{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Accessing SHARP metadata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import dates\n",
    "import drms\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting the metadata\n",
    "\n",
    "In this example, we access metadata from the [Space-weather Active Region Patch](http://jsoc.stanford.edu/doc/data/hmi/sharp/sharp.htm) (SHARP) series. We query some of the available indices for the active region of SHARP 4315, and create a simple plot from this data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "series = 'hmi.sharp_720s'\n",
    "sharpnum = 4315\n",
    "kwlist = ['T_REC', 'LON_FWT', 'AREA_ACR', 'MEANGAM', 'USFLUX', 'ERRVF', 'MEANJZH', 'ERRMIH']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create DRMS client and query metadata:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "c = drms.Client()\n",
    "k = c.query('%s[%d]' % (series, sharpnum), key=kwlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use T_REC timestamps as DataFrame index:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "k.index = drms.to_datetime(k.T_REC)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find timestamp of the record that is closest to the central meridian:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "t_cm = k.LON_FWT.abs().argmin()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting the metadata\n",
    "\n",
    "We change some matplotlib options to make plot commands less verbose:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "plt.rc('axes', titlesize='medium')\n",
    "plt.rc('axes.formatter', use_mathtext=True)\n",
    "plt.rc('mathtext', default='regular')\n",
    "plt.rc('legend', fontsize='medium')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following creates the plots and saves the figure as PDF and PNG."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(2, 2, sharex=True, figsize=(10, 6))\n",
    "\n",
    "axi = ax[0,0]\n",
    "axi.plot(k.index, k.AREA_ACR/1e3, '.', ms=2, label='AREA_ACR')\n",
    "axi.set_title('LoS area of active pixels')\n",
    "axi.set_ylabel(r'$\\mu$Hem $\\times 1000$')\n",
    "\n",
    "axi = ax[0, 1]\n",
    "axi.plot(k.index, k.MEANGAM, '.', ms=2, label='MEANGAM')\n",
    "axi.set_title('Mean inclination angle')\n",
    "axi.set_ylabel('degree')\n",
    "\n",
    "axi = ax[1, 0]\n",
    "axi.errorbar(k.index, k.USFLUX/1e22, yerr=k.ERRVF/1e22, fmt='.', ms=2,\n",
    "             capsize=0, label='USFLUX')\n",
    "axi.set_title('Total unsigned flux')\n",
    "axi.set_ylabel(r'Mx $\\times 10^{\\minus 22}$')\n",
    "axi.set_xlabel('Date')\n",
    "\n",
    "axi = ax[1, 1]\n",
    "axi.errorbar(k.index, k.MEANJZH*1e3, yerr=k.ERRMIH*1e3, fmt='.', ms=2,\n",
    "             capsize=0, label='MEANJZH')\n",
    "axi.set_title('Mean current helicity')\n",
    "axi.set_ylabel(r'G$^2$ m$^{\\minus 1}$ $\\times 1000$')\n",
    "axi.set_xlabel('Date')\n",
    "\n",
    "axi.xaxis.set_major_locator(dates.AutoDateLocator())\n",
    "axi.xaxis.set_major_formatter(dates.DateFormatter('%b\\n%d'))\n",
    "\n",
    "for axi in ax.flatten():\n",
    "    axi.axvline(t_cm, ls='--', color='r')\n",
    "    axi.legend(loc='best', numpoints=1)\n",
    "\n",
    "fig.tight_layout(pad=1.2, w_pad=2)\n",
    "plt.draw()\n",
    "\n",
    "fig.savefig('sharp_metadata.pdf')\n",
    "fig.savefig('sharp_metadata.png', dpi=200)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}