{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1aa7ee5e",
   "metadata": {},
   "source": [
    "# Using deconvolve to deconvolve images"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3ee1e4a",
   "metadata": {},
   "source": [
    "deconvolve can sharpen images using the point spread function derived for Hinode XRT "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11ff4317",
   "metadata": {},
   "outputs": [],
   "source": [
    "from xrtpy.image_correction.deconvolve import deconvolve"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d3ff603",
   "metadata": {},
   "source": [
    "Deconvolution is ordinarily used when wanting to remove the blurring around sharp objects or features caused by the telescope's point spread function. Here we show an example of its use for an XRT image taken during the transit of Venus in 2012. We download this data from the VSO using methods in SunPy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4442702b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sunpy.map import Map\n",
    "from sunpy.net import attrs as a\n",
    "from sunpy.net import Fido\n",
    "\n",
    "result = Fido.search(\n",
    "    a.Time(\"2012-06-05 21:58:39\", \"2012-06-05 21:59:00\"), a.Instrument(\"xrt\")\n",
    ")\n",
    "\n",
    "data_file = Fido.fetch(result[0], progress=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9414720e",
   "metadata": {},
   "source": [
    "deconvolve takes a SunPy map as input and returns the deconvolved image and metadata as a SunPy map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebd15dbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_map = Map(data_file)\n",
    "out_map = deconvolve(in_map)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c34a2a1",
   "metadata": {},
   "source": [
    "deconvolve uses the Richardson-Lucy deconvolution algorithm and takes a few optional input parameters including niter (no. of iterations to perform, 5 by default), pdf1keV (to use the point spread function defined at 1.0 keV rather than the default one defined at 560 eV) and verbose (False by default). Above we just used the default settings.\n",
    "\n",
    "To see the effects of the deconvolution we plot both the input and output images:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94a60a68",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig = plt.figure()\n",
    "ax1 = fig.add_subplot(1, 2, 1, projection=in_map)\n",
    "in_map.plot(axes=ax1, title=\"Original Image\")\n",
    "ax2 = fig.add_subplot(1, 2, 2, projection=out_map)\n",
    "out_map.plot(axes=ax2, title=\"Deconvolved Image\")\n",
    "fig.subplots_adjust(wspace=0.5)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "549d08ea",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
