{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "de9cdd42-f9bb-4dd6-80ba-d63d0dbc3ca2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import linecache\n",
    "\n",
    "from astropy.io import fits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "72780ead-06f0-4fbb-b12e-602b203286a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.04197595 0.17233 0.0350472\n",
      "-0.0372089 1.2312531999999994\n",
      "-0.0186623 0.07995692199999997\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<>:5: SyntaxWarning: invalid escape sequence '\\s'\n",
      "<>:5: SyntaxWarning: invalid escape sequence '\\s'\n",
      "/tmp/ipykernel_4414/3947829372.py:5: SyntaxWarning: invalid escape sequence '\\s'\n",
      "  df = pd.read_csv(fname, sep=\"\\s+\", skipinitialspace=True, skiprows=7, names=columns)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x151c2e3a4bf0>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14528 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fname = '/data101/bartlett/fsigma8/PV_data/Foundation_DR1/Foundation_DR1.FITRES.TEXT'\n",
    "\n",
    "# Get header\n",
    "columns = ['SN'] + linecache.getline(fname, 6).strip().split()[1:]\n",
    "df = pd.read_csv(fname, sep=\"\\s+\", skipinitialspace=True, skiprows=7, names=columns)\n",
    "\n",
    "zCMB = df['zCMB']\n",
    "m = df['mB']\n",
    "m_err = df['mBERR']\n",
    "\n",
    "x1 = df['x1']\n",
    "hyper_stretch_mu = np.median(x1)\n",
    "hyper_stretch_sigma = (np.percentile(x1, 84) - np.percentile(x1, 16)) / 2\n",
    "\n",
    "c = df['c']\n",
    "hyper_c_mu = np.median(c)\n",
    "hyper_c_sigma = (np.percentile(c, 84) - np.percentile(c, 16)) / 2\n",
    "\n",
    "sigma_m = np.median(df['mBERR'])\n",
    "sigma_stretch = np.median(df['x1ERR'])\n",
    "sigma_c = np.median(df['cERR'])\n",
    "print(sigma_m, sigma_stretch, sigma_c)\n",
    "\n",
    "print(hyper_stretch_mu, hyper_stretch_sigma)\n",
    "print(hyper_c_mu, hyper_c_sigma)\n",
    "    \n",
    "plt.figure()\n",
    "speed_of_light = 299792  # km/s\n",
    "mock_z = np.load('sn_z.npy')\n",
    "mock_z /= speed_of_light\n",
    "plt.hist(mock_z, bins=10, label='mock', histtype='step', density=True)\n",
    "plt.hist(zCMB, bins=10, label='true', histtype='step', density=True)\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8e538aec-c47e-43ef-95ad-84afc388fc8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Filename: /data101/bartlett/fsigma8/PV_data/Foundation_DR1/kcor_PS1_none.fits\n",
      "No.    Name      Ver    Type      Cards   Dimensions   Format\n",
      "  0  PRIMARY       1 PrimaryHDU      41   (0,)      \n",
      "  1  ZPoff         1 BinTableHDU     19   11R x 5C   [20A, 20A, 1E, 1E, 1E]   \n",
      "  2  SN SED        1 BinTableHDU     19   97626R x 1C   [1E]   \n",
      "  3  KCOR          1 BinTableHDU     15   0R x 3C   [1E, 1E, 1E]   \n",
      "  4  MAG+MWXTCOR    1 BinTableHDU     59   0R x 25C   [1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E]   \n",
      "  5  FilterTrans    1 BinTableHDU     33   921R x 12C   [1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E, 1E]   \n",
      "  6  PrimarySED    1 BinTableHDU     15   921R x 3C   [1E, 1E, 1E]   \n"
     ]
    }
   ],
   "source": [
    "# Open the FITS file\n",
    "fname = '/data101/bartlett/fsigma8/PV_data/Foundation_DR1/kcor_PS1_none.fits'\n",
    "with fits.open(fname) as hdul:\n",
    "    hdul.info()  # Show summary of FITS file contents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d012b67e-1600-4f5c-8724-6b926e4c32a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "borg_new",
   "language": "python",
   "name": "borg_new"
  },
  "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
