{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Astrometric calibration\n",
    "\n",
    "In this tutorial you will astrometrically calibrate the ReducedScienceFrames (i.e., de-biased and flatfielded science frames) from the previous tutorial.\n",
    "\n",
    "### Find ReducedScienceFrames to run astrometry on\n",
    "\n",
    "In a previous exercise, you reduced some RawScienceFrames, resulting in the creation of ReducedScienceFrames. Construct a list of these frames.\n",
    "\n",
    "*Instruction:* Use information in the [Python database querying HOW-TO](http://doc.astro-wise.org/man_howto_queries.html) to construct a query that contains your and only your ReducedScienceFrames.\n",
    "\n",
    "Answer (1):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "context.set_project('TUTORIAL')\n",
    "context.set_privileges(1)\n",
    "print(context.get_current_project())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = '2012-06-01T08:51:44'\n",
    "q = ReducedScienceFrame.template.start == dateutil.parser.parse(t)\n",
    "frames = q.project_only().user_only()\n",
    "len(frames)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Answer (2):\n",
    "Alternatively, you can look for ReducedScienceFrames that you made less than, say, 60 minutes ago:\n",
    "\n",
    "Use current UTC minus datetime.timedelta(days, seconds, microseconds)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "date = datetime.datetime.utcnow() - datetime.timedelta(0, 60*60, 0)\n",
    "frames = (ReducedScienceFrame.creation_date > date).project_only().user_only()\n",
    "len(frames)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Derive astrometric calibration\n",
    "\n",
    "Derive astrometry by creating AstrometricParameters objects for the ReducedScienceFrames.\n",
    "\n",
    "*Instruction:* Use information in the [Astrometry HOW-TO](http://doc.astro-wise.org/man_howto_astrom.html) to re-derive the AstrometricParameters for your ReducedScienceFrames.\n",
    "\n",
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filenames = [f.filename for f in frames]\n",
    "dpu.run('Astrometry', instrument='OMEGACAM', red_filenames=filenames, C=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check the progress of the job:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dpu.get_status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "or \n",
    "\n",
    "(note that this command selects the most recent, valid ReducedScienceFrames for the specified template and chip, which may be frames made by someone other than yourself)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dpu.run('Astrometry', instrument='OMEGACAM', template='2012-06-01T08:51:44',\n",
    "        chip='ESO_CCD_#65', C=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Examine the attributes of an AstrometricParameters object.\n",
    "Instruction: Use the ``dir()`` method to see the attributes of interest (usually in all capital letters).\n",
    "\n",
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ap = (AstrometricParameters.reduced == frames[0]).max('creation_date')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir(ap)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ap.NREF, ap.RMS, ap.SEEING"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Visually inspect astrometry\n",
    "\n",
    "Display the quality control plot for the AstrometricParameters object.\n",
    "\n",
    "*Instruction:* Follow the instructions in the [Astrometry Quality Control HOW-TO](http://doc.astro-wise.org/man_howto_qcastrom.html) to inspect the astrometric solution for one of your ReducedScienceFrames.\n",
    "\n",
    "Answer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ap.inspect(verbose=2)"
   ]
  }
 ],
 "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.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}