1 | 'use strict';
|
---|
2 |
|
---|
3 | var Func = function() { };
|
---|
4 | Func.sum = function(a, b) { return a+b; }
|
---|
5 | Func.sq = function(a, b) { return Math.sqrt(a*a + b*b); }
|
---|
6 | Func.min = function(a, b) { return Math.min(a, b); }
|
---|
7 | Func.max = function(a, b) { return Math.max(a, b); }
|
---|
8 | Func.avg = function(arr) { return arr.reduce(Func.Sum, 0)/arr.length; }
|
---|
9 | Func.stat = function(arr, func)
|
---|
10 | {
|
---|
11 | if (arr.length==0)
|
---|
12 | return undefined;
|
---|
13 |
|
---|
14 | var sum = 0;
|
---|
15 | var sq = 0;
|
---|
16 | var cnt = 0;
|
---|
17 | var min = arr[0];
|
---|
18 | var max = arr[0];
|
---|
19 | arr.forEach(function(val, idx) { sum+=val; sq+=val*val; if (val>max) max=val; if (val<min) min=val; if (func && func(val, idx)) cnt++ });
|
---|
20 | sum /= arr.length;
|
---|
21 | sq /= arr.length;
|
---|
22 |
|
---|
23 | return { avg:sum, rms:Math.sqrt(sq-sum*sum), min:min, max:max, count:cnt };
|
---|
24 | }
|
---|
25 |
|
---|
26 | // ===================================================================
|
---|
27 |
|
---|
28 | console.out(("\n%78s".$("")).replace(/ /g, "="));
|
---|
29 |
|
---|
30 | if (dim.state("FTM_CONTROL").name=="TriggerOn")
|
---|
31 | {
|
---|
32 | dim.send("FTM_CONTROL/STOP_TRIGGER");
|
---|
33 | dim.wait("FTM_CONTROL", "Valid");
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | include('scripts/CheckStates.js');
|
---|
38 |
|
---|
39 | var table =
|
---|
40 | [
|
---|
41 | [ "MCP", [ "Idle" ] ],
|
---|
42 | [ "AGILENT_CONTROL", [ "VoltageOn" ] ],
|
---|
43 | [ "FTM_CONTROL", [ "Valid" ] ],
|
---|
44 | [ "FAD_CONTROL", [ "Connected", "RunInProgress" ] ],
|
---|
45 | [ "BIAS_CONTROL", [ "Disconnected", "VoltageOff" ] ],
|
---|
46 | [ "DATA_LOGGER", [ "WaitForRun", "NightlyFileOpen", "Logging" ] ],
|
---|
47 | ];
|
---|
48 |
|
---|
49 | console.out("Checking states.");
|
---|
50 | if (!checkStates(table))
|
---|
51 | {
|
---|
52 | throw new Error("Something unexpected has happened. One of the servers",
|
---|
53 | "is in a state in which it should not be. Please,",
|
---|
54 | "try to find out what happened...");
|
---|
55 | }
|
---|
56 |
|
---|
57 | // ===================================================================
|
---|
58 |
|
---|
59 | include('scripts/Hist1D.js');
|
---|
60 | include('scripts/Hist2D.js');
|
---|
61 |
|
---|
62 | console.out("Checking power on time");
|
---|
63 |
|
---|
64 | var service_drs = new Subscription("FAD_CONTROL/DRS_RUNS");
|
---|
65 |
|
---|
66 | var runs = service_drs.get(5000, false);
|
---|
67 | //if (!runs)
|
---|
68 | // throw new Error("Could not connect to FAD_CONTROL/DRS_RUNS");
|
---|
69 |
|
---|
70 | var power = dim.state("AGILENT_CONTROL").time;
|
---|
71 | var now = new Date();
|
---|
72 |
|
---|
73 | var diff = (now-runs.time)/3600000;
|
---|
74 |
|
---|
75 | console.out(" * Now: "+now);
|
---|
76 | console.out(" * Last power cycle: "+power);
|
---|
77 | console.out(" * Last DRS calib set: "+(runs.data?runs.time:"none"));
|
---|
78 |
|
---|
79 |
|
---|
80 | if (1)//diff>8 && now.getHours()>16 || runs.time<power)
|
---|
81 | {
|
---|
82 | console.out("Checking send.");
|
---|
83 | checkSend(["FAD_CONTROL", "MCP", "RATE_CONTROL"]);
|
---|
84 | console.out("Checking send: done");
|
---|
85 |
|
---|
86 | //console.out("Most probablay the camera has not been checked for underflows yet.");
|
---|
87 |
|
---|
88 | var service_event = new Subscription("FAD_CONTROL/EVENT_DATA");
|
---|
89 |
|
---|
90 | dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
|
---|
91 | dim.send("FAD_CONTROL/SET_FILE_FORMAT", 0);
|
---|
92 |
|
---|
93 | var sub_runs = new Subscription("FAD_CONTROL/RUNS");
|
---|
94 | var sruns = sub_runs.get(5000, false);
|
---|
95 |
|
---|
96 | if (dim.state("FAD_CONTROL").name=="RunInProgress" || sruns.qos==1)
|
---|
97 | {
|
---|
98 | dim.send("FAD_CONTROL/CLOSE_OPEN_FILES");
|
---|
99 | dim.wait("FAD_CONTROL", "Connected", 3000);
|
---|
100 |
|
---|
101 | console.out("Waiting for open files to be closed...");
|
---|
102 | v8.timeout(60000, function() { if (sub_runs.get(0, false).qos==0) return true; });
|
---|
103 |
|
---|
104 | // Although the file should be closed now, the processing might still be on-going
|
---|
105 | // and delayed events might be received. The only fix for that issue is to
|
---|
106 | // add the run number to the data we are waiting for
|
---|
107 | v8.sleep(5000);
|
---|
108 | }
|
---|
109 |
|
---|
110 | sub_runs.close();
|
---|
111 |
|
---|
112 | console.out("Starting drs-gain... waiting for new event");
|
---|
113 |
|
---|
114 | var sub_startrun = new Subscription("FAD_CONTROL/START_RUN");
|
---|
115 | var sub_incomplete = new Subscription("FAD_CONTROL/INCOMPLETE");
|
---|
116 | var sub_connections = new Subscription("FAD_CONTROL/CONNECTIONS");
|
---|
117 | sub_connections.get(5000);
|
---|
118 | sub_startrun.get(5000);
|
---|
119 |
|
---|
120 | include('scripts/takeRun.js');
|
---|
121 |
|
---|
122 | while (1)
|
---|
123 | {
|
---|
124 | var event_counter = service_event.get(10000, false).counter;
|
---|
125 |
|
---|
126 | var stop = function ()
|
---|
127 | {
|
---|
128 | while (1)
|
---|
129 | {
|
---|
130 | if (dim.state("MCP").name=="TakingData" && service_event.get(0, false).counter>event_counter)
|
---|
131 | {
|
---|
132 | //dim.send("FAD_CONTROL/CLOSE_OPEN_FILES");
|
---|
133 | dim.send("MCP/STOP");
|
---|
134 | return;
|
---|
135 | }
|
---|
136 | v8.sleep(100);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | var thread = new Thread(250, stop);
|
---|
141 |
|
---|
142 | var rc = takeRun("drs-gain");
|
---|
143 |
|
---|
144 | thread.kill();
|
---|
145 |
|
---|
146 | if (rc)
|
---|
147 | break;
|
---|
148 | }
|
---|
149 |
|
---|
150 | console.out("Event received.");
|
---|
151 |
|
---|
152 | sub_incomplete.close();
|
---|
153 | sub_connections.close();
|
---|
154 | sub_startrun.close();
|
---|
155 |
|
---|
156 |
|
---|
157 | // FIXME: Restore DRS calibration in case of failure!!
|
---|
158 | // FAD Re-connect in case of failure?
|
---|
159 | // MCP/RESET in case of failure?
|
---|
160 | // Proper error reporting!
|
---|
161 |
|
---|
162 | var event = service_event.get(3000);//, false);
|
---|
163 | service_event.close();
|
---|
164 |
|
---|
165 | console.out("Run stopped.");
|
---|
166 |
|
---|
167 | dim.send("RATE_CONTROL/STOP"); // GlobalThresholdSet -> Connected
|
---|
168 | dim.wait("MCP", "Idle", 3000);
|
---|
169 |
|
---|
170 | var nn = runs.data && runs.data.length>0 && runs.obj['roi']>0 ? runs.obj['run'].reduce(Func.max) : -1;
|
---|
171 | if (nn>0)
|
---|
172 | {
|
---|
173 | var night = runs.obj['night'];
|
---|
174 |
|
---|
175 | var yy = night/10000;
|
---|
176 | var mm = (night/100)%100;
|
---|
177 | var dd = night%100;
|
---|
178 |
|
---|
179 | var filefmt = "/loc_data/raw/%d/%02d/%02d/%8d_%03d.drs.fits";
|
---|
180 |
|
---|
181 | dim.log("Trying to restore last DRS calibration #"+nn+" ["+runs.time+"; "+night+"]");
|
---|
182 |
|
---|
183 | // FIXME: Timeout
|
---|
184 | var drs_counter = service_drs.get(0, false).counter;
|
---|
185 | dim.send("FAD_CONTROL/LOAD_DRS_CALIBRATION", filefmt.$(yy, mm, dd, night, nn));
|
---|
186 |
|
---|
187 | var now = new Date();
|
---|
188 | v8.timeout(3000, function() { if (service_drs.get(0, false).counter>drs_counter) return true; });
|
---|
189 |
|
---|
190 | dim.log("Last DRS calibration restored ["+(new Date()-now)/1000+"s]");
|
---|
191 | }
|
---|
192 |
|
---|
193 | var hist = Hist2D(16, -2048.5, 2048.5, 11, -10, 100);
|
---|
194 |
|
---|
195 | var data = event.obj;
|
---|
196 |
|
---|
197 | for (var i=0; i<1440; i++)
|
---|
198 | hist.fill(data.avg[i], isNaN(data.rms[i])?-1:data.rms[i]);
|
---|
199 |
|
---|
200 | hist.print();
|
---|
201 |
|
---|
202 | var stat0 = Func.stat(data.avg, function(val, idx) { if (val<600) console.out(" PIX[hw="+idx+"]="+val); return val<600; });
|
---|
203 | var stat1 = Func.stat(data.rms);
|
---|
204 |
|
---|
205 | console.out("Avg[min]=%.1f".$(stat0.min));
|
---|
206 | console.out("Avg[avg]=%.1f +- %.1f".$(stat0.avg, stat0.rms));
|
---|
207 | console.out("Avg[max]=%.1f".$(+stat0.max));
|
---|
208 | console.out("Avg[cnt]="+stat0.count);
|
---|
209 | console.out("");
|
---|
210 | console.out("Rms[min]=%.1f".$(stat1.min));
|
---|
211 | console.out("Rms[avg]=%.1f +- %.1f".$(stat1.avg, stat1.rms));
|
---|
212 | console.out("Rms[max]=%.1f".$(stat1.max));
|
---|
213 | console.out(("%78s\n".$("")).replace(/ /g, "="));
|
---|
214 |
|
---|
215 | // OK UNDERFLOW
|
---|
216 | // ------------------------------------------------------
|
---|
217 | // Avg[min]=722.0 Avg[min]=-380.0
|
---|
218 | // Avg[avg]=815.9 +- 45.9 Avg[avg]= 808.0 +- 102.0
|
---|
219 | // Avg[max]=930.5 Avg[max]= 931.1
|
---|
220 | // Avg[cnt]=0 Avg[cnt]= 9
|
---|
221 |
|
---|
222 | // Rms[min]=14.0 Rms[min]=13.9
|
---|
223 | // Rms[avg]=16.5 +- 1.6 Rms[avg]=18.8 +- 26.8
|
---|
224 | // Rms[max]=44.0 Rms[max]=382.1
|
---|
225 |
|
---|
226 | if (stat0.count>0)
|
---|
227 | {
|
---|
228 | if (stat0.count>8)
|
---|
229 | throw new Error("Underflow condition detected in about "+parseInt(stat0.count/9+.5)+" DRS.");
|
---|
230 |
|
---|
231 | console.warn("There is probably an underflow condition in one DRS... please check manually.");
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | service_drs.close();
|
---|