tasks.py 4.96 KB
from __future__ import absolute_import

from celery.task import Task
from analyzer.tasks import analysis
from pyrosapp.models import *

from common import VISCamera as VIS
from common import NIRCamera as NIR
from common import Telescope

import time
import os

class execute_plan_vis(Task):
    '''
        Gives the orders to the instruments to retrieve the image(s) of a plan VIS.
        Send the images to the analyzer
    '''

    def run(self, plan_id, countdown):

        if countdown > 0:
            time.sleep(countdown)
        TaskId.objects.filter(task_id=self.request.id).delete()

        message = 'Start plan ' + str(plan_id) + ' execution'
        Log.objects.create(agent='Observation manager', message=message)
        print("execute_plan VIS : ", plan_id)

        plan = Plan.objects.get(id=plan_id)

        self.tel = Telescope.TelescopeObj()
        cam = VIS.VISCameraObj()

        self.set_camera(cam, plan)
        self.wait_camera_ready(cam)

        cam.do("START")

# TODO: décommenter quand on aura un simulateur
#         st = self.wait_camera_finished(cam)

        # TODO: récupérer les vraies images ? je fais quoi ?
        time.sleep(1)
        analysis.delay(plan_id)
        message = 'Finished plan ' + str(plan_id) + ' execution'
        Log.objects.create(agent='Observation manager', message=message)

    def set_camera(self, cam, plan):
        '''
            Set the camera configuration
        '''

        # TODO: mettre les vraies configurations en fct du plan
        cam.set("WINDOW", 0, 100, 10, 100)
        cam.set("READMODE", VIS.ReadmodeEnum.ramp)
        cam.set("FILENAME", plan.name)
        cam.set("HEADER", {})
        cam.set("READOUT_FREQUENCY", 20.0)
        cam.set("FILTER", VIS.FilterEnum.h)

        cam.set("EXPOSURE", 180)
        cam.set("BINNING", 300, 300)

    def wait_camera_ready(self, cam):
        '''
            Loop to wait for the configuration to be done
        '''
        st = 0
        while st == 0:
            st_tel = self.tel.get("STATUS")

            st_cam = cam.get("STATUS")

            st = 1

            # TODO: checker les statuts comme il faut, et repasser à 0 si on a des statuts pas bons
            if st_tel != "IDLE" or st_cam != "IDLE":
                st = 0


    def wait_camera_finished(self, cam):
        '''
            Loop to wait for the observation to be finished
        '''

        countdown = int(cam.get("TIMER"))
        while countdown > 5:
            time.sleep(5)
            countdown = int(cam.get("TIMER"))

        st = 0
        while st == 0:
            timer = int(cam.get("TIMER"))
            if timer == -1:
                st = 1
            else:
                time.sleep(1)


class execute_plan_nir(Task):
    '''
        Gives the orders to the instruments to retrieve the image(s) of a plan NIR.
        Send the images to the analyzer
    '''

    def run(self, plan_id, countdown):
        if countdown > 0:
            time.sleep(countdown)
        TaskId.objects.filter(task_id=self.request.id).delete()

        message = 'Start plan ' + str(plan_id) + ' execution'
        Log.objects.create(agent='Observation manager', message=message)
        print("execute_plan NIR : ", plan_id)

        plan = Plan.objects.get(pk=plan_id)

        self.tel = Telescope.TelescopeObj()
        cam = NIR.NIRCameraObj()

        self.set_camera(cam, plan)
        self.wait_camera_ready(cam)

        cam.do("START")

        st = self.wait_camera_finished(cam)

        time.sleep(1)

        analysis.delay(plan_id)
        message = 'Finished plan ' + str(plan_id) + ' execution'
        Log.objects.create(agent='Observation manager', message=message)

    def set_camera(self, cam, plan):

        cam.set("WINDOW", 0, 100, 10, 100)
        cam.set("READMODE", NIR.ReadmodeEnum.ramp)
        cam.set("FILENAME", plan.name)
        cam.set("HEADER", {})
        cam.set("READOUT_FREQUENCY", 20.0)
        cam.set("FILTER", NIR.FilterEnum.h)

        cam.set("NB_IMAGES", 28)

    def wait_camera_ready(self, cam):
        '''
            Loop to wait for the configuration to be done
        '''
        st = 0
        while st == 0:
            st_tel = self.tel.get("STATUS")

            st_cam = cam.get("STATUS")

            st = 1

            if st_tel != "IDLE" or st_cam != "IDLE":
                st = 0


    def wait_camera_finished(self, cam):
        '''
            Loop to wait for the observation to be finished
        '''

        countdown = int(cam.get("TIMER"))
        while countdown > 5:
            time.sleep(5)
            countdown = int(cam.get("TIMER"))

        st = 0
        while st == 0:
            timer = int(cam.get("TIMER"))
            if timer == -1:
                st = 1
            else:
                time.sleep(1)


class create_calibrations(Task):
     '''
         Directly make the right calls to the instruments to create the calibration files.
         When they are all finished, it creates the 'super' calibration files.
     '''
     def run(self):
         pass