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