{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "651705ab",
   "metadata": {},
   "source": [
    "# Temperature Response Analysis for X-Ray Telescope (XRT)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa939b73",
   "metadata": {},
   "source": [
    "This notebook explores the temperature response of X-ray channels in the X-Ray Telescope (XRT). The temperature response provides valuable insights into how the XRT instrument detects and responds to different temperatures of X-ray emissions. By assuming a specific spectral emission model at a given date, we can investigate the behavior of the XRT channels."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf0b72e9",
   "metadata": {},
   "source": [
    "To begin the analysis, we will import the necessary packages that enable us to perform the temperature response calculations and generate visualizations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcde5557",
   "metadata": {},
   "outputs": [],
   "source": [
    "import xrtpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3adc142e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aeaf3280",
   "metadata": {},
   "source": [
    "Let's dive in and explore the fascinating temperature response characteristics of the XRT!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c15461c5",
   "metadata": {},
   "source": [
    "## Contents\n",
    "\n",
    "1. [Define a Filter Channel](#Define-a-Filter-Channel)\n",
    "2. [Define Date & Time](#Define-a-date-and-time) \n",
    "3. [Temperature-Response-Fundamental](#TemperatureResponseFundamental)\n",
    "4. [Temperature Response Function](#Temperature-Response-function)\n",
    "5. [Plot Temperature Response](#Plotting-the-Temperature-Response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14415906",
   "metadata": {},
   "source": [
    "## Define a Filter Channel"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14a820fe",
   "metadata": {},
   "source": [
    "[xrtpy- X-Ray Filter Channel]: https://xrtpy.readthedocs.io/en/latest/getting_started.html\n",
    "\n",
    "A filter channel is defined by its common abbreviation, which represents a specific type of filter used to modify the X-ray radiation passing through. In this example, we will explore the carbon-on-polyimide filter (abbreviated as \"C-poly\").\n",
    "\n",
    "For detailed information about various filter channels and their characteristics, you can refer to the [xrtpy- X-Ray Filter Channel] section.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "745b98ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "filter = \"C-poly\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1dba447",
   "metadata": {},
   "source": [
    "## Define a date and time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9434fb73",
   "metadata": {},
   "source": [
    "[sunpy-time]: https://docs.sunpy.org/en/stable/guide/time.html\n",
    "\n",
    "In order to analyze the temperature response, it is necessary to specify a date and time for the analysis. The date and time can be defined together using specific string formats. To explore the data captured a year after the launch date, we will define the date and time accordingly.\n",
    "\n",
    "For detailed examples and further information about date and time string formats, you can refer to the [sunpy-time] documentation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3268b4b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "date_time = \"2007-09-22T21:59:59\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a98f3d5",
   "metadata": {},
   "source": [
    "## TemperatureResponseFundamental"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b113a3ef",
   "metadata": {},
   "source": [
    "The `TemperatureResponseFundamental` object is a crucial component that provides all the necessary functions and properties for calculating the temperature response in our analysis. By referencing this object, we can access the required methods and attributes for further calculations.\n",
    "\n",
    "To create a `TemperatureResponseFundamental` object, you need to provide the defined filter channel (`Filter`) and the desired date and time (`date_time`). Additionally, you can specify the abundance model of interest, such as `Photospheric`, which influences the temperature response calculations.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8a24315",
   "metadata": {},
   "outputs": [],
   "source": [
    "Temperature_Response_Fundamental = xrtpy.response.TemperatureResponseFundamental(\n",
    "    filter, date_time, abundance_model=\"Photospheric\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "453dbe8c",
   "metadata": {},
   "source": [
    "## Temperature Response function"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f56c4b89",
   "metadata": {},
   "source": [
    "\n",
    "To calculate the temperature response, simply call the `temperature_response()` function on the `Temperature_Response_Fundamental` object. This function utilizes the specified filter, date, and abundance model to generate the temperature response as a result.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5dc098fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "temperature_response = Temperature_Response_Fundamental.temperature_response()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07a2b8ed",
   "metadata": {},
   "source": [
    "[astropy-quantity]: https://docs.astropy.org/en/stable/api/astropy.units.quantity.Quantity.html\n",
    "\n",
    "[astropy.units]: https://docs.astropy.org/en/stable/units/index.html\n",
    "\n",
    "The `temperature_response()` function returns the temperature response for the selected filter, date, and time. The returned value is an [astropy-quantity] object with associated [astropy.units]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0961f9e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Temperature Response:\\n\", temperature_response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b94ac326",
   "metadata": {},
   "source": [
    "## Plotting the Temperature-Response"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b23d6157",
   "metadata": {},
   "source": [
    "[chiantidatrbase.org]: https://www.chiantidatabase.org/\n",
    "\n",
    "In this section, we will visualize the temperature response by plotting the `temperature_response` function against the corresponding temperatures. It's important to note that the CHIANTI temperatures used in this plot are the temperatures of the solar plasma and are independent of the channel filter.\n",
    "\n",
    "The CHIANTI temperatures are stored in the `Temperature_Response_Fundamental` object and are provided in units of Kelvin (K). These temperatures serve as the independent variable for plotting the temperature response.\n",
    "\n",
    "By visualizing the temperature response, we can gain insights into how it varies with respect to temperature, providing a deeper understanding of the XRT channelcharacteristics.\n",
    "\n",
    "Additionally, if you wish to explore more details about the CHIANTI database, you can find further information at [chiantidatrbase.org].\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc2b0b98",
   "metadata": {},
   "outputs": [],
   "source": [
    "CHIANTI_temperature = Temperature_Response_Fundamental.CHIANTI_temperature"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e5be3e3",
   "metadata": {},
   "source": [
    "[numpy.log10]: https://numpy.org/doc/stable/reference/generated/numpy.log10.html\n",
    "We take the log of the `CHIANTI_temperature` for plotting, which compresses the scale and enhances the visibility of the variations for lower temperatures.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a247d01",
   "metadata": {},
   "outputs": [],
   "source": [
    "log_CHIANTI_temperature = np.log10(CHIANTI_temperature.value)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0d954e8",
   "metadata": {},
   "source": [
    "In addition, we will compare the data shortly after the spacecraft launch date with the current data. This allows us to identify any differences or variations in the temperature response over time.\n",
    "\n",
    "We define a new temperature response data for the launch date. The process for obtaining this data is the same as previously shown, where we specify the filter, launch date, and abundance model.\n",
    "\n",
    "By comparing the temperature response at the launch date with the current temperature response, we can gain insights into any changes that may have occurred over time. This comparison helps us understand the stability and evolution of the XRT."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01876a0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "launch_date_time = \"2006-09-22T23:59:59\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a31f268",
   "metadata": {},
   "outputs": [],
   "source": [
    "launch_date_temperature_response = xrtpy.response.TemperatureResponseFundamental(\n",
    "    filter, launch_date_time, abundance_model=\"Photospheric\"\n",
    ").temperature_response()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eeb2df4c",
   "metadata": {},
   "source": [
    "Create a plotting function that plots the `temperature_response` versus `log_CHIANTI_temperature` for the chosen filter, date, and time. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6868a101",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plotting_temperature_response():\n",
    "    plt.figure(figsize=(30, 12))\n",
    "\n",
    "    plt.plot(\n",
    "        log_CHIANTI_temperature,\n",
    "        np.log10(temperature_response.value),\n",
    "        linewidth=4,\n",
    "        label=\"{:} {:}\".format(filter, date_time),\n",
    "    )\n",
    "    plt.plot(\n",
    "        log_CHIANTI_temperature,\n",
    "        np.log10(launch_date_temperature_response.value),\n",
    "        linewidth=3,\n",
    "        label=\"{:} {:}\".format(filter, launch_date_time),\n",
    "        color=\"red\",\n",
    "    )\n",
    "\n",
    "    plt.title(\"XRT Temperature Response\", fontsize=30)\n",
    "    plt.xlabel(\"Log(T) ($K$)\", fontsize=27)\n",
    "    plt.ylabel(\"$DN$ $cm^5$ $ s^-1$ $pix^-1$\", fontsize=27)\n",
    "    plt.legend(fontsize=30)\n",
    "\n",
    "    plt.xticks(fontsize=25)\n",
    "    plt.yticks(fontsize=25)\n",
    "\n",
    "    plt.grid()\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "967f1713",
   "metadata": {},
   "source": [
    "Run `plotting_temperature_response` function to create the plot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f36b383",
   "metadata": {},
   "outputs": [],
   "source": [
    "plotting_temperature_response()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a116ead0",
   "metadata": {},
   "source": [
    "[Montana State University Solar Physics site]: http://solar.physics.montana.edu/HINODE/XRT/xrt_contam_db.html\n",
    "[Narukage et. al. (2011)]: https://doi.org/10.1007/s11207-010-9685-2 \n",
    "\n",
    "Plotting the temperature response at launch date and a year after highlights the differences. This is due to the contamination layer thickness on the CCD. Information about the XRT CCD contaminant layer thickness calculation can be found at [Montana State University Solar Physics site]. In addition, more information can be found referencing [Narukage et. al. (2011)]."
   ]
  }
 ],
 "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
}
