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