# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: LSST
#     language: python
#     name: lsst
# ---

import os
import glob
import lsst.eotest.sensor as sensorTest
import lsst.eotest.image_utils as imutils
import bot_eo_analyses as bot_eo
from camera_components import camera_info
from lsst.eotest.sensor.flatPairTask import find_flat2    # for use with TS8 data in the flat_pairs and ptc tasks

# +
# Specify a raft and a run to analyze.
root_dir = '/gpfs/slac/lsst/fs1/g/data/jobHarness/jh_archive/LCA-11021_RTM'
raft = 'LCA-11021_RTM-014'
run = '9537'

# Pick a CCD by slot.
slot = 'S00'
det_name = '{}_{}'.format(raft, slot)

# +
# File to gather the eotest results.  A file with this name is updated by the various eotasks.
eotest_results_file = '_'.join((det_name, run, 'eotest_results.fits'))

# A function to help gather the data.
def glob_files(*args):
    return sorted(glob.glob(os.path.join(root_dir, raft, run, *args)))

# For the gain/psf and read noise, we use the Fe55 acquisition.
job = 'fe55_raft_acq'
bias_files = glob_files('fe55_raft_acq', 'v0', '*', slot, '*fe55_bias*.fits')
fe55_files = glob_files('fe55_raft_acq', 'v0', '*', slot, '*fe55_fe55*.fits')

# For full raft noise correlation matrix, one needs to crate a dictionary of bias files, keyed by slot.
bias_file_dict = {slot: glob_files('fe55_raft_acq', 'v0', '*', slot, '*fe55_bias*.fits')[0]
                  for slot in camera_info.get_slot_names()}

# For bright defects and dark current tasks.
dark_files = glob_files('dark_raft_acq', 'v0', '*', slot, '*dark_dark*.fits')

# For dark defects and CTE tasks.
sflat_files = glob_files('sflat_raft_acq', 'v0', '*', slot, '*sflat_500_flat_H*.fits')

# For linearity and ptc tasks.
flat1_files = glob_files('flat_pair_raft_acq', 'v0', '*', slot, '*flat1*.fits')

# Set default gain values in case the Fe55 task is skipped.
gains = {_: 0.8 for _ in range(1, 17)}

# +
# Run the Fe55 single sensor task.
# %time bot_eo.fe55_task(run, det_name, fe55_files, bias_files)

# Retrieve the calculated gains from the eotest results file. 
#eotest_results = sensorTest.EOTestResults(eotest_results_file)
#gains = {amp: gain for amp, gain in zip(eotest_results['AMP'], eotest_results['GAIN'])}
# -

# Run the read noise task.
# %time bot_eo.read_noise_task(run, det_name, bias_files, gains)

# Run the raft-level overscan correlation calculation.
# %time bot_eo.raft_noise_correlations(run, raft, bias_file_dict)

# Run the bright defects task.
mask_files = sorted(glob.glob('_'.join((det_name, run, '*mask.fits'))))
# %time bot_eo.bright_defects_task(run, det_name, dark_files, gains=gains, mask_files=mask_files)

# Run the dark defects task.
mask_files = sorted(glob.glob('_'.join((det_name, run, '*mask.fits'))))
# %time bot_eo.dark_defects_task(run, det_name, sflat_files, mask_files=mask_files)

# Run the dark current task.
mask_files = sorted(glob.glob('_'.join((det_name, run, '*mask.fits'))))
# %time dark_curr_pixels, dark95s = bot_eo.dark_current_task(run, det_name, dark_files, gains, mask_files=mask_files)
bot_eo.plot_ccd_total_noise(run, det_name, dark_curr_pixels, dark95s, eotest_results_file)

# Run the CTE task.
mask_files = sorted(glob.glob('_'.join((det_name, run, '*mask.fits'))))
# %time superflat_file = bot_eo.cte_task(run, det_name, sflat_files, gains, mask_files=mask_files)
bot_eo.plot_cte_results(run, det_name, superflat_file, eotest_results_file, mask_files=mask_files);

# Run the linearity task
mask_files = sorted(glob.glob('_'.join((det_name, run, '*mask.fits'))))
flat1_files_subset = flat1_files[::len(flat1_files)//4]   # downselect so that this demo runs more quickly
# %time bot_eo.flat_pairs_task(run, det_name, flat1_files_subset, gains, mask_files=mask_files, flat2_finder=find_flat2)

# Run the PTC task
mask_files = sorted(glob.glob('_'.join((det_name, run, '*mask.fits'))))
# %time bot_eo.ptc_task(run, det_name, flat1_files[::2], gains, mask_files=mask_files, flat2_finder=find_flat2)

# Run the tearing task
# %time bot_eo.tearing_task(run, det_name, flat1_files)

StarcoderdataJupyterScriptsDedupFiltered