
from shinsapp import SHINSApp
from util.shinsLogger import logger
from alignment.allineamento import Align_the_coronagraph as aly
from alignment.utils import read_image
from util import exception
from util import functions as ufun
from util import variables as uvars
import Basda
import numpy as np
import time

# Other templates
from SHARKNIR_gen_tec_CloseLoop import SHARKNIR_gen_tec_CloseLoop
from SHARKNIR_gen_base import SHARKNIR_gen_base
from SHARKNIR_gen_tec_PSFAlignment_lab import SHARKNIR_gen_tec_PSFAlignment_lab
from SHARKNIR_gen_tec_PSFAlignment_tiptilt_lab import SHARKNIR_gen_tec_PSFAlignment_tiptilt_lab

class SHARKNIR_cal_mon_Ghost(SHINSApp):

    @exception.handler_for_run
    def run(self, ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs):
        '''template that perform an automatic check for camera Ghosts for all coronagraphic masks:

        . End of Template
        
        '''
        
        logger.info(__name__)
        self.status = uvars.EXIT_SUCCESS
        obsController = self.obsController

        fitsDir = uvars.fitsDir + obsController.getCurrentDataDirectory() +'/'

        self.gen_base = SHARKNIR_gen_base();        gen_base = self.gen_base
        gen_tec_CloseLoop       = SHARKNIR_gen_tec_CloseLoop()
        gen_tec_PSFAlignment_lab = SHARKNIR_gen_tec_PSFAlignment_lab()
        gen_tec_PSFAlignment_tiptilt_lab = SHARKNIR_gen_tec_PSFAlignment_tiptilt_lab()

        #################
        # INITIAL SETUP #
        #################
        print("qui")
        self.status, _ , _ = gen_base.main(ins_s=ins_s, det_s=det_s, expose=False)

        

        # Acquisition setup params
        acq_ins_s = {"SCI_FILT_W2" : "HOLE"}
        
        acq_det_s = {
            "NDIT"        : "1",
            "READOUT"     : "Full_Image"
        }
        
        #########################
        # GAUSS CORO MASK GHOST #
        #########################

        this_mask = "GAUSS"
        
        if self.paragraph("{} CORO MASK GHOST".format(this_mask)):

            logger.info("Begin of PSF Alignment procedure for {} mask".format(this_mask))

            # PSF Alignment setup params
            psf_align_ins_s = {
                "SCI_FILT_W1" : "NB_H2",
                "SCI_FILT_W2" : "ND3",
                "DB_FILT_W"   : ins_s["DB_FILT_W"],

                "CORO_SLIT_W" : "GAUSS",
                "APODIZER_W"  : "HOLE",
                "LYOT_GRISM_W" : "GAUSS_LYOT_STOP"
            }
        
            psf_align_det_s = {
                "NDIT"        : "5",
                "READOUT"     : "128x128",
                "SAVE"        : det_s["SAVE"],
                "DIT"         : "0.14"
            }


            print(psf_align_ins_s)
            self.status,_,_ = gen_tec_PSFAlignment_lab.main(ins_s = psf_align_ins_s , det_s = psf_align_det_s)
            logger.info("End of PSF Alignment procedure for {} mask".format(this_mask))

            logger.info("Begin of Acquisition procedure for {} mask".format(this_mask))

            acq_det_s["DIT"] = "4"

            self.status, self.filenames, _ = gen_base.main(ins_s = acq_ins_s, det_s = acq_det_s)

            logger.info("End of Acquisition procedure for {} mask".format(this_mask))


        #######################
        # SP1 CORO MASK GHOST #
        #######################

        this_mask = "SP1"
        
        if self.paragraph("{} CORO MASK GHOST".format(this_mask)):

            logger.info("Begin of PSF Alignment procedure for {} mask".format(this_mask))

            # PSF Alignment setup params
            psf_align_ins_s = {
                "SCI_FILT_W1" : "NB_H2",
                "SCI_FILT_W2" : "ND3",
                "DB_FILT_W"   : ins_s["DB_FILT_W"],

                "CORO_SLIT_W" : "SP1_FPM",
                "APODIZER_W"  : "SP1_APO",
                "LYOT_GRISM_W" : "SP_LYOT_STOP"
            }
        
            psf_align_det_s = {
                "NDIT"        : "5",
                "READOUT"     : "128x128",
                "SAVE"        : det_s["SAVE"],
                "DIT"         : "0.4"
            }

            print(psf_align_ins_s)
            self.status,_,_ = gen_tec_PSFAlignment_lab.main(ins_s = psf_align_ins_s , det_s = psf_align_det_s)
            logger.info("End of PSF Alignment procedure for {} mask".format(this_mask))

            logger.info("Begin of Acquisition procedure for {} mask".format(this_mask))

            acq_det_s["DIT"] = "4"
            acq_det_s["READOUT"] = "coro_stripe"
            self.status, self.filenames, _ = gen_base.main(ins_s = acq_ins_s, det_s = acq_det_s)

            logger.info("End of Acquisition procedure for {} mask".format(this_mask))


        
        ########################
        # SP2a CORO MASK GHOST #
        ########################

        this_mask = "SP2a"
        
        if self.paragraph("{} CORO MASK GHOST".format(this_mask)):

            logger.info("Begin of PSF Alignment procedure for {} mask".format(this_mask))

            # PSF Alignment setup params
            psf_align_ins_s = {
                "SCI_FILT_W1" : "NB_H2",
                "SCI_FILT_W2" : "ND3",
                "DB_FILT_W"   : ins_s["DB_FILT_W"],

                "CORO_SLIT_W" : "SP2a_FPM",
                "APODIZER_W"  : "SP2a_APO",
                "LYOT_GRISM_W" : "SP_LYOT_STOP"
            }
        
            psf_align_det_s = {
                "NDIT"        : "5",
                "READOUT"     : "128x128",
                "SAVE"        : det_s["SAVE"],
                "DIT"         : "0.2"
            }
            
            print(psf_align_ins_s)
            self.status,_,_ = gen_tec_PSFAlignment_lab.main(ins_s = psf_align_ins_s , det_s = psf_align_det_s)
            logger.info("End of PSF Alignment procedure for {} mask".format(this_mask))

            logger.info("Begin of Acquisition procedure for {} mask".format(this_mask))

            acq_det_s["DIT"] = "0.5"
            acq_det_s["READOUT"] = "coro_stripe"
            self.status, self.filenames, _ = gen_base.main(ins_s = acq_ins_s, det_s = acq_det_s)

            logger.info("End of Acquisition procedure for {} mask".format(this_mask))

        
        ########################
        # SP2b CORO MASK GHOST #
        ########################

        this_mask = "SP2b"
        
        if self.paragraph("{} CORO MASK GHOST".format(this_mask)):

            logger.info("Begin of PSF Alignment procedure for {} mask".format(this_mask))

            # PSF Alignment setup params
            psf_align_ins_s = {
                "SCI_FILT_W1" : "NB_H2",
                "SCI_FILT_W2" : "ND3",
                "DB_FILT_W"   : ins_s["DB_FILT_W"],

                "CORO_SLIT_W" : "SP2b_FPM",
                "APODIZER_W"  : "SP2b_APO",
                "LYOT_GRISM_W" : "SP_LYOT_STOP"
            }
        
            psf_align_det_s = {
                "NDIT"        : "5",
                "READOUT"     : "128x128",
                "SAVE"        : det_s["SAVE"],
                "DIT"         : "0.2"
            }
            
            print(psf_align_ins_s)
            self.status,_,_ = gen_tec_PSFAlignment_lab.main(ins_s = psf_align_ins_s , det_s = psf_align_det_s)
            logger.info("End of PSF Alignment procedure for {} mask".format(this_mask))

            logger.info("Begin of Acquisition procedure for {} mask".format(this_mask))

            acq_det_s["DIT"] = "0.5"
            acq_det_s["READOUT"] = "coro_stripe"

            self.status, self.filenames, _ = gen_base.main(ins_s = acq_ins_s, det_s = acq_det_s)

            logger.info("End of Acquisition procedure for {} mask".format(this_mask))

         
        
        ########################
        # FQPM CORO MASK GHOST #
        ########################

        this_mask = "FQPM"
        
        if self.paragraph("{} CORO MASK GHOST".format(this_mask)):

            logger.info("Begin of PSF Alignment procedure for {} mask".format(this_mask))

            
            # PSF Alignment setup params
            psf_align_ins_s = {
                "SCI_FILT_W1" : "NB_H2",
                "SCI_FILT_W2" : "ND3",
                "DB_FILT_W"   : ins_s["DB_FILT_W"],

                "CORO_SLIT_W" : "FQPM",
                "APODIZER_W"  : "HOLE",
                "LYOT_GRISM_W" : "FQPM_LYOT_STOP"
            }
        
            psf_align_det_s = {
                "NDIT"        : "5",
                "READOUT"     : "128x128",
                "SAVE"        : det_s["SAVE"],
                "DIT"         : "0.14"
            }

            print(psf_align_ins_s)
            self.status = obsController.RTCTTOpenLoop()
            logger.debug("Internal Loop is OPEN")
            
            self.status,_,_ = gen_tec_PSFAlignment_tiptilt_lab.main(ins_s = psf_align_ins_s , det_s = psf_align_det_s)
            logger.info("End of PSF Alignment procedure for {} mask".format(this_mask))

            logger.info("Begin of Acquisition procedure for {} mask".format(this_mask))

            acq_det_s["DIT"] = "4"
            acq_det_s["READOUT"] = "Full_Image"
            self.status = obsController.RTCTTModeUpload(modes=[0, 0, 0.1, 0.1], absolute=False)
            logger.debug("Manual tip-tilt applied to DM")
            self.status, self.filenames, _ = gen_base.main(ins_s = acq_ins_s, det_s = acq_det_s)

            logger.info("End of Acquisition procedure for {} mask".format(this_mask))


        logger.info("End of automatic Ghost monitoring template")
        
