import time
import numpy as np
from shinsapp import SHINSApp
from util.shinsLogger import logger
from util import variables as uvars
from util import exception
from util import functions as ufun
from astropy.time import Time, TimeDelta

from SHARKNIR_gen_base import SHARKNIR_gen_base

class SHARKNIR_lss_obs_Speckles(SHINSApp):
    """calibration
    """

    @exception.handler_for_run
    def run(self, ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs):

        logger.info(__name__)
        obsController = self.obsController
        self.status = uvars.EXIT_SUCCESS

        gen_base = SHARKNIR_gen_base()

        drotMode    = ins_s["DROT_Mode"]
        offsetAng   = 10
        
        
        ##############################
        # INSTRUMENT AND SASHA SETUP #
        ##############################
        if self.paragraph("INSTRUMENT AND SASHA SETUP + IMAGES FOR SPECKLES ACQUISITION"):
        
            # GET CURRENT DROT #
            minDrotPos, maxDrotPos = [1, 180] #[74.6, 181.6]
            logger.debug("Retriving Drot Position")
            inDict = {"DROT":-9999.9}
            unit = "DEGREE"
            self.status, drotDict = self.obsController.getPosition(unit, inDict)
            drotPos = drotDict["DROT"]
            logger.info("DROT pos : {} deg".format(drotPos))

            # Update new Drot angular position
            newDrotPos = drotPos+offsetAng if drotPos+offsetAng < maxDrotPos else drotPos-offsetAng
            ins_s["DROT_PosAng"] = str(newDrotPos)
        
            logger.info("Begin Instrument and SASHA setup")
            # print("INS_S : {}".format(ins_s))
            # print("DET_S : {}".format(det_s))
            gen_base.main(ins_s=ins_s, det_s=det_s)
            logger.info("End of Instrument and SASHA setup")
            logger.info("End of speckles subtraction lss template")
            
            
