# $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/scripts/templates/tpl/testOB_tiptilt_apply_shape.py 3724 2021-08-30 14:26:48Z depa $

from util.shinsLogger import logger
from shinsapp import SHINSApp
from util import variables as uvars
from util import functions as ufun
from util import exception

class testOB_tiptilt_apply_shape(SHINSApp):
    
    @exception.handler_for_run
    def run(self, ins_s=None, det_s=None, tel_s=None, rtc_s=None, **kwargs):
        '''Check that BCU has been initialized, then send specific command
        to CRED2 camera
        '''

        logger.info(__name__)
        obsController = self.obsController
        self.status = uvars.EXIT_SUCCESS
        
        ##################
        # SHOW RTC SETUP #
        ##################

        logger.debug("Enumerating detector setup")
        for element, value in rtc_s.items():
            logger.debug("Element : % 15s  -  Value % 15s    " % (element, value))

        ##############
        # SAVE MODES #
        ##############

        modes = [ float(rtc_s["VECTOR_{}".format(i)]) for i in range(1, 39) ]

        logger.warning("Modes: {}".format(modes))

        #############################
        # APPLY THE DM DISPLACEMENT #
        #############################

        if "ABSOLUTE" in rtc_s:
            logger.warning("ABSOLUTE keyword found")                
            if (rtc_s["ABSOLUTE"] == "true") : # or rtc_s["ABSOLUTE"]=="0"):
                is_absolute = True       
                logger.warning("Absolute movement")
            else:
                is_absolute = False                
        else:
            is_absolute = False

        logger.info("Upload a mode vector to internal DM")
        self.status = obsController.RTCTTModeUpload(modes, absolute=is_absolute)
        logger.info("Waiting setup to apply displacement... DONE")


