Blame view

src/observation_manager/tasks.py 5.18 KB
5b5566ab   haribo   added celery
1
2
3
from __future__ import absolute_import

from celery.task import Task
77816f10   haribo   Workflow implemen...
4
from analyzer.tasks import analysis
bb45cd4a   haribo   Date: 25/05/2016
5
from pyrosapp.models import *
5b5566ab   haribo   added celery
6

ad85da7c   haribo   Date: 28/06/2016
7
8
from common import VISCamera as VIS
from common import NIRCamera as NIR
ac26ad2b   haribo   Date: 22/07/2016
9
from common import Telescope
ad85da7c   haribo   Date: 28/06/2016
10

5b5566ab   haribo   added celery
11
import time
77816f10   haribo   Workflow implemen...
12
13
14
import os

IMAGES_FOLDER = 'simulation_images'
5b5566ab   haribo   added celery
15

9774228b   haribo   Date: 22/06/2016
16
class execute_plan_vis(Task):
9b5bad52   haribo   Commented all the...
17
18
    '''
        Gives the orders to the instruments to retrieve the image(s) of a plan VIS.
164eebbd   haribo   UML and comments
19
        Send the images to the analyzer
9b5bad52   haribo   Commented all the...
20
    '''
5b5566ab   haribo   added celery
21

ad85da7c   haribo   Date: 28/06/2016
22
    def run(self, plan_id, countdown):
00fa2456   haribo   Debug adaption code
23

9774228b   haribo   Date: 22/06/2016
24
25
        if countdown > 0:
            time.sleep(countdown)
bb45cd4a   haribo   Date: 25/05/2016
26
        TaskId.objects.filter(task_id=self.request.id).delete()
ec372f36   haribo   Date: 28/06/2016
27

ad85da7c   haribo   Date: 28/06/2016
28
        message = 'Start plan ' + str(plan_id) + ' execution'
77816f10   haribo   Workflow implemen...
29
        Log.objects.create(agent='Observation manager', message=message)
ad85da7c   haribo   Date: 28/06/2016
30
31
32
33
        print("execute_plan VIS : ", plan_id)

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

ac26ad2b   haribo   Date: 22/07/2016
34
        self.tel = Telescope.TelescopeObj()
ad85da7c   haribo   Date: 28/06/2016
35
36
        cam = VIS.VISCameraObj()

00fa2456   haribo   Debug adaption code
37
        self.set_camera(cam, plan)
ad85da7c   haribo   Date: 28/06/2016
38
39
        self.wait_camera_ready(cam)

00fa2456   haribo   Debug adaption code
40
        cam.do("START")
ad85da7c   haribo   Date: 28/06/2016
41
42

# TODO: décommenter quand on aura un simulateur
ad85da7c   haribo   Date: 28/06/2016
43
44
#         st = self.wait_camera_finished(cam)

5d61cbfe   haribo   Just adding / rem...
45
        # TODO: récupérer les vraies images ? je fais quoi ?
9774228b   haribo   Date: 22/06/2016
46
        time.sleep(1)
ad85da7c   haribo   Date: 28/06/2016
47
        with open(os.path.join(IMAGES_FOLDER, str(plan_id)), 'w'):
9774228b   haribo   Date: 22/06/2016
48
            pass
ad85da7c   haribo   Date: 28/06/2016
49
50
        analysis.delay(plan_id)
        message = 'Finished plan ' + str(plan_id) + ' execution'
9774228b   haribo   Date: 22/06/2016
51
52
        Log.objects.create(agent='Observation manager', message=message)

ad85da7c   haribo   Date: 28/06/2016
53
    def set_camera(self, cam, plan):
9b5bad52   haribo   Commented all the...
54
55
56
        '''
            Set the camera configuration
        '''
ad85da7c   haribo   Date: 28/06/2016
57

00fa2456   haribo   Debug adaption code
58
        # TODO: mettre les vraies configurations en fct du plan
ad85da7c   haribo   Date: 28/06/2016
59
60
61
62
63
        cam.set("WINDOW", 0, 100, 10, 100)
        cam.set("READMODE", VIS.ReadmodeEnum.ramp)
        cam.set("FILENAME", "toto_a_la_campagne")
        cam.set("HEADER", {})
        cam.set("READOUT_FREQUENCY", 20.0)
00fa2456   haribo   Debug adaption code
64
        cam.set("FILTER", VIS.FilterEnum.h)
ad85da7c   haribo   Date: 28/06/2016
65
66
67
68
69

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

    def wait_camera_ready(self, cam):
9b5bad52   haribo   Commented all the...
70
71
72
        '''
            Loop to wait for the configuration to be done
        '''
ad85da7c   haribo   Date: 28/06/2016
73
74
        st = 0
        while st == 0:
ac26ad2b   haribo   Date: 22/07/2016
75
76
            st_tel = self.tel.get("STATUS")

00fa2456   haribo   Debug adaption code
77
            st_cam = cam.get("STATUS")
ac26ad2b   haribo   Date: 22/07/2016
78

ad85da7c   haribo   Date: 28/06/2016
79
            st = 1
ac26ad2b   haribo   Date: 22/07/2016
80

ad85da7c   haribo   Date: 28/06/2016
81
            # TODO: checker les statuts comme il faut, et repasser à 0 si on a des statuts pas bons
00fa2456   haribo   Debug adaption code
82
            if st_tel != "IDLE" or st_cam != "IDLE":
ac26ad2b   haribo   Date: 22/07/2016
83
                st = 0
ad85da7c   haribo   Date: 28/06/2016
84
85
86


    def wait_camera_finished(self, cam):
9b5bad52   haribo   Commented all the...
87
88
89
        '''
            Loop to wait for the observation to be finished
        '''
00fa2456   haribo   Debug adaption code
90
91
92
93
94

        countdown = int(cam.get("TIMER"))
        while countdown > 5:
            time.sleep(5)
            countdown = int(cam.get("TIMER"))
5d61cbfe   haribo   Just adding / rem...
95

ad85da7c   haribo   Date: 28/06/2016
96
97
        st = 0
        while st == 0:
00fa2456   haribo   Debug adaption code
98
            timer = int(cam.get("TIMER"))
ad85da7c   haribo   Date: 28/06/2016
99
100
            if timer == -1:
                st = 1
00fa2456   haribo   Debug adaption code
101
102
            else:
                time.sleep(1)
ad85da7c   haribo   Date: 28/06/2016
103

9774228b   haribo   Date: 22/06/2016
104
105

class execute_plan_nir(Task):
9b5bad52   haribo   Commented all the...
106
107
108
109
    '''
        Gives the orders to the instruments to retrieve the image(s) of a plan NIR.
        Send the images to the analyzer
    '''
9774228b   haribo   Date: 22/06/2016
110

ad85da7c   haribo   Date: 28/06/2016
111
    def run(self, plan_id, countdown):
9774228b   haribo   Date: 22/06/2016
112
113
114
        if countdown > 0:
            time.sleep(countdown)
        TaskId.objects.filter(task_id=self.request.id).delete()
ad85da7c   haribo   Date: 28/06/2016
115
116

        message = 'Start plan ' + str(plan_id) + ' execution'
9774228b   haribo   Date: 22/06/2016
117
        Log.objects.create(agent='Observation manager', message=message)
ad85da7c   haribo   Date: 28/06/2016
118
119
120
121
        print("execute_plan NIR : ", plan_id)

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

00fa2456   haribo   Debug adaption code
122
        self.tel = Telescope.TelescopeObj()
ad85da7c   haribo   Date: 28/06/2016
123
124
125
126
127
128
129
        cam = NIR.NIRCameraObj()

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

        cam.do("START")

00fa2456   haribo   Debug adaption code
130
        st = self.wait_camera_finished(cam)
ad85da7c   haribo   Date: 28/06/2016
131

77816f10   haribo   Workflow implemen...
132
        time.sleep(1)
ad85da7c   haribo   Date: 28/06/2016
133
        with open(os.path.join(IMAGES_FOLDER, str(plan_id)), 'w'):
77816f10   haribo   Workflow implemen...
134
            pass
ad85da7c   haribo   Date: 28/06/2016
135
136
        analysis.delay(plan_id)
        message = 'Finished plan ' + str(plan_id) + ' execution'
77816f10   haribo   Workflow implemen...
137
        Log.objects.create(agent='Observation manager', message=message)
ad85da7c   haribo   Date: 28/06/2016
138
139
140

    def set_camera(self, cam, plan):

ad85da7c   haribo   Date: 28/06/2016
141
142
143
144
145
        cam.set("WINDOW", 0, 100, 10, 100)
        cam.set("READMODE", NIR.ReadmodeEnum.ramp)
        cam.set("FILENAME", "toto_a_la_campagne")
        cam.set("HEADER", {})
        cam.set("READOUT_FREQUENCY", 20.0)
00fa2456   haribo   Debug adaption code
146
        cam.set("FILTER", NIR.FilterEnum.h)
ad85da7c   haribo   Date: 28/06/2016
147
148
149
150

        cam.set("NB_IMAGES", 28)

    def wait_camera_ready(self, cam):
00fa2456   haribo   Debug adaption code
151
152
153
        '''
            Loop to wait for the configuration to be done
        '''
ad85da7c   haribo   Date: 28/06/2016
154
155
        st = 0
        while st == 0:
00fa2456   haribo   Debug adaption code
156
157
            st_tel = self.tel.get("STATUS")

ad85da7c   haribo   Date: 28/06/2016
158
            st_cam = cam.get("STATUS")
00fa2456   haribo   Debug adaption code
159

ad85da7c   haribo   Date: 28/06/2016
160
            st = 1
00fa2456   haribo   Debug adaption code
161
162
163

            if st_tel != "IDLE" or st_cam != "IDLE":
                st = 0
ad85da7c   haribo   Date: 28/06/2016
164
165
166


    def wait_camera_finished(self, cam):
00fa2456   haribo   Debug adaption code
167
168
169
170
171
172
173
174
        '''
            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"))
ad85da7c   haribo   Date: 28/06/2016
175

ad85da7c   haribo   Date: 28/06/2016
176
177
        st = 0
        while st == 0:
00fa2456   haribo   Debug adaption code
178
            timer = int(cam.get("TIMER"))
ad85da7c   haribo   Date: 28/06/2016
179
180
            if timer == -1:
                st = 1
00fa2456   haribo   Debug adaption code
181
182
            else:
                time.sleep(1)
ad85da7c   haribo   Date: 28/06/2016
183

c5a3b4a0   haribo   Date: 05/07/2016
184

164eebbd   haribo   UML and comments
185
class create_calibrations(Task):
c5a3b4a0   haribo   Date: 05/07/2016
186
187
188
189
190
     '''
         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):
9b5bad52   haribo   Commented all the...
191
         pass