{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "075bbcc4",
   "metadata": {},
   "source": [
    "# Using the remove_lightleak function to analyze XRT composite images"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c749c1e",
   "metadata": {},
   "source": [
    "[xrt.cfa.harvard.edu]:https://xrt.cfa.harvard.edu/data_products/index.php\n",
    "\n",
    "The `remove_lightleak` function is built to subtract light leak (visible stray light) image from XRT synoptic composite images. Level two synoptic composite image data is available at [xrt.cfa.harvard.edu]. This template will guide you through the method of using `remove_lightleak`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20381613",
   "metadata": {},
   "source": [
    "Begin by importing `remove_lightleak` function from XRTpy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ac058f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from xrtpy.image_correction.remove_lightleak import remove_lightleak"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74536d57",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pkg_resources\n",
    "import sunpy.map\n",
    "\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1da8e603",
   "metadata": {},
   "source": [
    "This is example will be using XRT synoptic data from the first day of summer of 2015. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab5987e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "directory = pkg_resources.resource_filename(\n",
    "    \"xrtpy\", \"image_correction/data/example_data\"\n",
    ")\n",
    "data_file = Path(directory) / \"comp_XRT20150621_055911.7.fits\"\n",
    "\n",
    "print(\"File used:\\n\", data_file.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ffad190",
   "metadata": {},
   "source": [
    "[Sunpy maps]:https://docs.sunpy.org/en/stable/code_ref/map.html\n",
    "\n",
    "Take on [Sunpy maps] to run the composite data file. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4e771b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "in_map = sunpy.map.Map(data_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c91805a8",
   "metadata": {},
   "source": [
    "[Sunpy maps]:https://docs.sunpy.org/en/stable/code_ref/map.html\n",
    "\n",
    "The `remove_lightleak` function takes a `sunpy.map.Map` as input and returns the composite image and metadata as a `Map`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45746692",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_map = remove_lightleak(in_map)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81cb82fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from matplotlib.colors import LogNorm\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\", norm=LogNorm(1.0, None))\n",
    "ax2 = fig.add_subplot(1, 2, 2, projection=out_map)\n",
    "out_map.plot(axes=ax2, title=\"Light Leak Subtracted Image\", norm=LogNorm(1.0, None))\n",
    "fig.subplots_adjust(wspace=0.5)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eda9e234",
   "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
}
