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", [ "Idle" ] ],
|
---|
37 | [ "FAD_CONTROL", [ "Connected", "WritingData" ] ],
|
---|
38 | [ "BIAS_CONTROL", [ "Disconnected", "VoltageOff" ] ],
|
---|
39 | [ "DATA_LOGGER", [ "WaitForRun", "NightlyFileOpen", "Logging" ] ],
|
---|
40 | ];
|
---|
41 |
|
---|
42 | console.out("Checking states.");
|
---|
43 | if (!checkStates(table))
|
---|
44 | {
|
---|
45 | console.out("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 | exit();
|
---|
49 | }
|
---|
50 |
|
---|
51 | // ===================================================================
|
---|
52 |
|
---|
53 | include('scripts/Hist1D.js');
|
---|
54 | include('scripts/Hist2D.js');
|
---|
55 |
|
---|
56 | console.out("Checking power on time");
|
---|
57 |
|
---|
58 | var service_drs = new Subscription("FAD_CONTROL/DRS_RUNS");
|
---|
59 |
|
---|
60 | var runs = service_drs.get(5000, false);
|
---|
61 | if (!runs)
|
---|
62 | throw new Error("Could not connect to FAD_CONTROL/DRS_RUNS");
|
---|
63 | /*
|
---|
64 | runs = {
|
---|
65 | "name" : "FAD_CONTROL/DRS_RUNS",
|
---|
66 | "format" : "I:1;I:3",
|
---|
67 | "qos" : 0,
|
---|
68 | "size" : 16,
|
---|
69 | "counter" : 0,
|
---|
70 | "time" : new Date("2012-11-20T05:23:52.804Z"),
|
---|
71 | "data" : [300,[148,149,154]]
|
---|
72 | };
|
---|
73 | */
|
---|
74 |
|
---|
75 | // Wait for agilent to be online with state information
|
---|
76 | // FIXME: timeout
|
---|
77 | while (!dim.state("AGILENT_CONTROL"))
|
---|
78 | v8.sleep();
|
---|
79 |
|
---|
80 | var power = dim.state("AGILENT_CONTROL").time;
|
---|
81 | var now = new Date();
|
---|
82 |
|
---|
83 | var diff = (now-runs.time)/3600000;
|
---|
84 |
|
---|
85 | console.out(" * Now: "+now);
|
---|
86 | console.out(" * Last power cycle: "+power);
|
---|
87 | console.out(" * Last DRS calib set: "+runs.time);
|
---|
88 | //console.out(JSON.stringify(dim.state("AGILENT_CONTROL")));
|
---|
89 |
|
---|
90 |
|
---|
91 | if (1)//diff>8 && now.getHours()>16 || runs.time<power)
|
---|
92 | {
|
---|
93 | //console.out("Most probablay the camera has not been checked for underflows yet.");
|
---|
94 |
|
---|
95 | // Wait for dim.send to be ready
|
---|
96 | // FIXME: timeout
|
---|
97 | while (!dim.send("FAD_CONTROL"))
|
---|
98 | v8.sleep();
|
---|
99 |
|
---|
100 | // FAD_CONTROL/RAW_DATA:
|
---|
101 | // roi,roi_tm,num_fad,num_ftm,type,num_boards,error,
|
---|
102 | // dummy,time,time_board,start_pix,start_tm,adc
|
---|
103 |
|
---|
104 | // FAD_CONTROL/EVENT_DATA
|
---|
105 | // avg,rms,max,pos
|
---|
106 |
|
---|
107 | var service_event = new Subscription("FAD_CONTROL/EVENT_DATA");
|
---|
108 | var event = service_event.get(5000, false);
|
---|
109 | //if (!event)
|
---|
110 | // throw new Error("Could not connect to FAD_CONTROL/EVENT_DATA");
|
---|
111 |
|
---|
112 | var event_counter = event.counter;
|
---|
113 |
|
---|
114 | dim.send("FAD_CONTROL/START_DRS_CALIBRATION");
|
---|
115 | dim.send("FAD_CONTROL/SET_FILE_FORMAT", 0);
|
---|
116 |
|
---|
117 | console.out("Starting drs-gain");
|
---|
118 | dim.send("MCP/START", -1, 3, "drs-gain");
|
---|
119 |
|
---|
120 | // FIXME: add a timeout here (10s)
|
---|
121 | console.out("Waiting for new event");
|
---|
122 | var d = new Date();
|
---|
123 | while (event_counter == service_event.get(0, false).counter)
|
---|
124 | v8.sleep();
|
---|
125 |
|
---|
126 | console.out("Event received: "+(new Date()-d)+"ms");
|
---|
127 |
|
---|
128 | console.out("Stop run");
|
---|
129 | dim.send("MCP/STOP");
|
---|
130 | while (!dim.send("RATE_CONTROL"))
|
---|
131 | {
|
---|
132 | v8.sleep(800);
|
---|
133 | console.out("waiting for RATE_CONTROL to send the necessary format information");
|
---|
134 | }
|
---|
135 | dim.send("RATE_CONTROL/STOP"); // GlobalThresholdSet -> Connected
|
---|
136 | dim.wait("MCP", "Idle", 3000);
|
---|
137 |
|
---|
138 | var nn = runs.data[0]>0 ? runs.data[1].reduce(Func.max) : -1;
|
---|
139 | if (nn>0)
|
---|
140 | {
|
---|
141 | console.out("Trying to restore last DRS calibration #"+nn+" ["+runs.time+"]");
|
---|
142 |
|
---|
143 | var night = new Date(runs.time-1000*60*60*12);
|
---|
144 | var yy = night.getUTCFullYear();
|
---|
145 | var mm = night.getUTCMonth()+1;
|
---|
146 | var dd = night.getUTCDate();
|
---|
147 |
|
---|
148 | var filefmt = "/loc_data/raw/%d/%02d/%02d/%4d%02d%02d_%03d.drs.fits";
|
---|
149 |
|
---|
150 | // FIXME: Timeout
|
---|
151 | var drs_counter = service_drs.get(0, false);
|
---|
152 | dim.send("FAD_CONTROL/LOAD_DRS_CALIBRATION", filefmt.$(yy, mm, dd, yy, mm, dd, nn));
|
---|
153 | while (drs_counter == service_drs.get(0, false).counter)
|
---|
154 | v8.sleep();
|
---|
155 | }
|
---|
156 |
|
---|
157 | //console.out(JSON.stringify(service_drs.get(0, false)));
|
---|
158 |
|
---|
159 | event = service_event.get(0, false);
|
---|
160 |
|
---|
161 | service_event.close();
|
---|
162 |
|
---|
163 | var hist = Hist2D(16, -2048.5, 2048.5, 11, -10, 100);
|
---|
164 |
|
---|
165 | for (var i=0; i<1440; i++)
|
---|
166 | hist.fill(event.data[0][i], isNaN(event.data[1][i])?-1:event.data[1][i]);
|
---|
167 |
|
---|
168 | //console.out(("%78s".$("")).replace(/ /g, "-"));
|
---|
169 | hist.print();
|
---|
170 | //console.out(("%78s".$("")).replace(/ /g, "-"));
|
---|
171 |
|
---|
172 | // Could also be <512
|
---|
173 | var stat0 = Func.stat(event.data[0], function(val) { if (val<0) console.out(" VAL="+val); return val<0; });
|
---|
174 | var stat1 = Func.stat(event.data[1]);
|
---|
175 |
|
---|
176 | console.out("Avg[min]=%.1f".$(stat0.min));
|
---|
177 | console.out("Avg[avg]=%.1f +- %.1f".$(stat0.avg, stat0.rms));
|
---|
178 | console.out("Avg[max]=%.1f".$(+stat0.max));
|
---|
179 | console.out("Avg[cnt]="+stat0.count);
|
---|
180 | console.out("");
|
---|
181 | console.out("Rms[min]=%.1f".$(stat1.min));
|
---|
182 | console.out("Rms[avg]=%.1f +- %.1f".$(stat1.avg, stat1.rms));
|
---|
183 | console.out("Rms[max]=%.1f".$(stat1.max));
|
---|
184 | console.out(("%78s\n".$("")).replace(/ /g, "="));
|
---|
185 | }
|
---|
186 |
|
---|
187 | service_drs.close();
|
---|