1 | <?php
|
---|
2 |
|
---|
3 | if (!extension_loaded('v8js'))
|
---|
4 | die("V8Js missing");
|
---|
5 |
|
---|
6 | $path = array(
|
---|
7 | "cal" => "/daq/caltest/",
|
---|
8 | "raw" => "/daq/raw/"
|
---|
9 | );
|
---|
10 |
|
---|
11 | if (isset($_POST['editor1']) || isset($_POST['editor2']))
|
---|
12 | {
|
---|
13 | $isOne = isset($_POST['editor1']);
|
---|
14 |
|
---|
15 | $source = $isOne ? $_POST['editor1'] : $_POST['editor2'];
|
---|
16 | if (!isset($_POST['files']))
|
---|
17 | $name = $isOne ? "proc.js" : "main.js";
|
---|
18 | else
|
---|
19 | $name = $_POST['files'][0];
|
---|
20 |
|
---|
21 | header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
|
---|
22 | header('Cache-Control: public'); // needed for i.e.
|
---|
23 | header('Content-Type: text/plain');
|
---|
24 | header('Content-Transfer-Encoding: Text');
|
---|
25 | header('Content-Disposition: attachment; filename="'.$name.'"');
|
---|
26 | print($source);
|
---|
27 | return;
|
---|
28 | }
|
---|
29 |
|
---|
30 | // This is a pretty weird hack because it does first convert
|
---|
31 | // all data to ascii (json) to print it...
|
---|
32 | if (isset($_POST['data']))
|
---|
33 | {
|
---|
34 | header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
|
---|
35 | header('Cache-Control: public'); // needed for i.e.
|
---|
36 | header('Content-Type: text/plain');
|
---|
37 | header('Content-Transfer-Encoding: Text');
|
---|
38 | header('Content-Disposition: attachment; filename="data.txt"');
|
---|
39 |
|
---|
40 | $json = json_decode($_POST['data']);
|
---|
41 |
|
---|
42 | $n = count($json[0]);
|
---|
43 | $cnt = count($json);
|
---|
44 |
|
---|
45 | for ($i=0; $i<$n; $i++)
|
---|
46 | {
|
---|
47 | print($i." ".$json[0][$i]);
|
---|
48 |
|
---|
49 | if ($cnt>1)
|
---|
50 | print(" ".$json[1][$i]);
|
---|
51 | if ($cnt>2)
|
---|
52 | print(" ".$json[2][$i]);
|
---|
53 | if ($cnt>3)
|
---|
54 | print(" ".$json[3][$i]);
|
---|
55 | print("\n");
|
---|
56 | }
|
---|
57 | return;
|
---|
58 | }
|
---|
59 |
|
---|
60 | if (!isset($_POST['file']) || !isset($_POST['event']) || !isset($_POST['pixel']))
|
---|
61 | {
|
---|
62 | /*
|
---|
63 | function getList($path)
|
---|
64 | {
|
---|
65 | $hasdir = false;
|
---|
66 |
|
---|
67 | $list = array();
|
---|
68 | foreach (new DirectoryIterator($path) as $file)
|
---|
69 | {
|
---|
70 | if ($file->isDot())
|
---|
71 | continue;
|
---|
72 |
|
---|
73 | $name = $file->getFilename();
|
---|
74 |
|
---|
75 |
|
---|
76 | if ($file->isDir())
|
---|
77 | {
|
---|
78 | $list[$name] = getList($path."/".$name);
|
---|
79 | $hasdir = true;
|
---|
80 | }
|
---|
81 |
|
---|
82 | if ($file->isFile() && $file->isReadable())
|
---|
83 | {
|
---|
84 | if (substr($name, 12)!=".fits.fz")
|
---|
85 | continue;
|
---|
86 |
|
---|
87 | array_push($list, substr($name, 9, 3));
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (!$hasdir)
|
---|
92 | sort($list);
|
---|
93 | return $list;
|
---|
94 | }
|
---|
95 | */
|
---|
96 |
|
---|
97 | function getList($path, $sub, $check)
|
---|
98 | {
|
---|
99 | $hasdir = false;
|
---|
100 |
|
---|
101 | $list = array();
|
---|
102 | foreach (new DirectoryIterator($path."/".$sub) as $file)
|
---|
103 | {
|
---|
104 | if ($file->isDot())
|
---|
105 | continue;
|
---|
106 |
|
---|
107 | $name = $file->getFilename();
|
---|
108 |
|
---|
109 | if ($file->isDir())
|
---|
110 | $list = array_merge($list, getList($path, $sub."/".$name, $check));
|
---|
111 |
|
---|
112 | if ($file->isFile() && $file->isReadable())
|
---|
113 | {
|
---|
114 | if (substr($name, 12)!=".fits.fz")
|
---|
115 | continue;
|
---|
116 |
|
---|
117 | $rc = substr($name, 0, 4)."/".substr($name, 4, 2)."/".substr($name, 6, 2)."-".substr($name, 9, 3);
|
---|
118 |
|
---|
119 | $list[$rc] = array_key_exists($rc, $check);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | return $list;
|
---|
124 | }
|
---|
125 |
|
---|
126 | try
|
---|
127 | {
|
---|
128 | $cal = getList($path['cal'], "", array());
|
---|
129 | $raw = getList($path['raw'], "", $cal);
|
---|
130 | ksort($raw);
|
---|
131 | }
|
---|
132 | catch (Exception $e)
|
---|
133 | {
|
---|
134 | return header('HTTP/1.0 400 '.$e->getMessage());
|
---|
135 | }
|
---|
136 |
|
---|
137 | print(json_encode($raw));
|
---|
138 | return;
|
---|
139 | }
|
---|
140 |
|
---|
141 | $event = intval($_POST['event']);
|
---|
142 | $pixel = intval($_POST['pixel']);
|
---|
143 |
|
---|
144 | function get($handle, $format, $count = 1)
|
---|
145 | {
|
---|
146 | $size = 0;
|
---|
147 | switch ($format)
|
---|
148 | {
|
---|
149 | case 'd': $size = 8; break;
|
---|
150 | case 'L': $size = 4; break;
|
---|
151 | case 'S': $size = 2; break;
|
---|
152 | case 's': $size = 2; break;
|
---|
153 | }
|
---|
154 |
|
---|
155 | if ($size==0)
|
---|
156 | return;
|
---|
157 |
|
---|
158 | $binary = fread($handle, $size*$count);
|
---|
159 | $data = unpack($format.$count, $binary);
|
---|
160 |
|
---|
161 | return $count==1 ? $data[1] : $data;
|
---|
162 | }
|
---|
163 |
|
---|
164 | //ini_set("memory_limit", "64M");
|
---|
165 |
|
---|
166 | //define('E_FATAL', E_ERROR | E_USER_ERROR | E_PARSE | E_CORE_ERROR |
|
---|
167 | // E_COMPILE_ERROR | E_RECOVERABLE_ERROR);
|
---|
168 |
|
---|
169 | $rc = array();
|
---|
170 | $rc['startPhp'] = microtime(true);
|
---|
171 |
|
---|
172 | // ============================ Read data from file ==========================
|
---|
173 | $file = $_POST['file'];
|
---|
174 |
|
---|
175 | $y = substr($file, 0, 4);
|
---|
176 | $m = substr($file, 5, 2);
|
---|
177 | $d = substr($file, 8, 2);
|
---|
178 | $r = substr($file, 11, 3);
|
---|
179 |
|
---|
180 | $rootpath = isset($_POST['calibrated']) ? $path['cal'] : $path['raw'];
|
---|
181 |
|
---|
182 | $filename = $rootpath.$y."/".$m."/".$d."/".$y.$m.$d."_".$r.".fits.fz";
|
---|
183 |
|
---|
184 | if (!file_exists($filename))
|
---|
185 | return header("HTTP/1.0 400 File '".$file."' not found.");
|
---|
186 |
|
---|
187 | $file = popen("/home/fact/FACT++/getevent ".$filename." ".$event." 2> /dev/null", "r");
|
---|
188 | if (!$file)
|
---|
189 | return header('HTTP/1.0 400 Could not open pipe.');
|
---|
190 |
|
---|
191 | $evt = array();
|
---|
192 | $fil = array();
|
---|
193 |
|
---|
194 | $fil['runType'] = trim(fread($file, 80));
|
---|
195 | if (feof($file))
|
---|
196 | return header('HTTP/1.0 400 Data not available.');
|
---|
197 |
|
---|
198 | $fil['runStart'] = get($file, "d");
|
---|
199 | $fil['runEnd'] = get($file, "d");
|
---|
200 | $fil['drsFile'] = get($file, "s");
|
---|
201 | $fil['numEvents'] = get($file, "L");
|
---|
202 | $fil['scale'] = get($file, "s");
|
---|
203 | $evt['numRoi'] = get($file, "L");
|
---|
204 | $evt['numPix'] = get($file, "L");
|
---|
205 | $evt['eventNumber'] = get($file, "L");
|
---|
206 | //$evt['triggerNumber'] = get($file, "L");
|
---|
207 | $evt['triggerType'] = get($file, "S");
|
---|
208 | $evt['unixTime'] = get($file, "L", 2);
|
---|
209 |
|
---|
210 | if (isset($_POST['source1']))
|
---|
211 | {
|
---|
212 | // Read the data and copy it from an associative array to an Array
|
---|
213 | // (this is just nicer and seems more logical)
|
---|
214 | $binary = array();
|
---|
215 | for ($i=0; $i<$evt['numPix']; $i++)
|
---|
216 | $binary[$i] = fread($file, 2*$evt['numRoi']);
|
---|
217 | /*
|
---|
218 | $data = array();
|
---|
219 | for ($i=0; $i<$evt['numPix']; $i++)
|
---|
220 | {
|
---|
221 | $var = get($file, "s", $evt['numRoi']);
|
---|
222 | $data[$i] = array();
|
---|
223 | for ($j=0; $j<$evt['numRoi']; $j++)
|
---|
224 | $data[$i][$j] = $var[$j+1]*0.48828125; // dac -> mV
|
---|
225 | }*/
|
---|
226 | }
|
---|
227 |
|
---|
228 | if (feof($file))
|
---|
229 | return header('HTTP/1.0 400 Data from file incomplete.');
|
---|
230 |
|
---|
231 | pclose($file);
|
---|
232 |
|
---|
233 | if ($fil['numEvents']==0)
|
---|
234 | return header('HTTP/1.0 400 Could not read event.');
|
---|
235 |
|
---|
236 | // =========================== Decode trigger type ===========================
|
---|
237 |
|
---|
238 | $typ = $evt['triggerType'];
|
---|
239 | $evt['trigger'] = array();
|
---|
240 | if ($typ!=0 && ($typ & 0x8703)==0)
|
---|
241 | array_push($evt['trigger'], "PHYS");
|
---|
242 | if (($typ&0x0100)!=0)
|
---|
243 | array_push($evt['trigger'], "LPext");
|
---|
244 | if (($typ&0x0200)!=0)
|
---|
245 | array_push($evt['trigger'], "LPint");
|
---|
246 | if (($typ&0x0400)!=0)
|
---|
247 | array_push($evt['trigger'], "PED");
|
---|
248 | if (($typ&0x8000)!=0)
|
---|
249 | array_push($evt['trigger'], "TIM");
|
---|
250 | if (($typ&0x0001)!=0)
|
---|
251 | array_push($evt['trigger'], "EXT1");
|
---|
252 | if (($typ&0x0002)!=0)
|
---|
253 | array_push($evt['trigger'], "EXT2");
|
---|
254 |
|
---|
255 | // =============================== Some data =================================
|
---|
256 |
|
---|
257 | //require_once("neighbors.php");
|
---|
258 |
|
---|
259 | //$rc['neighbors'] = $neighbors;
|
---|
260 | $rc['event'] = $evt;
|
---|
261 | $rc['file'] = $fil;
|
---|
262 | $rc['event']['index'] = $event;
|
---|
263 | $rc['event']['pixel'] = $pixel;
|
---|
264 | //$rc['waveform'] = $data[$pixel];
|
---|
265 |
|
---|
266 | // Get execution times
|
---|
267 | $now = microtime(true);
|
---|
268 |
|
---|
269 | if (isset($_POST['source1']))
|
---|
270 | {
|
---|
271 | // =============================== Run Javascript ============================
|
---|
272 |
|
---|
273 | /*
|
---|
274 | $JS = <<< EOT
|
---|
275 | require("path/to/module1");
|
---|
276 | EOT;
|
---|
277 |
|
---|
278 | $v8 = new V8Js();
|
---|
279 | $v8->setModuleLoader(function($module) {
|
---|
280 | switch ($module) {
|
---|
281 | case 'path/to/module1':
|
---|
282 | return 'print(' . json_encode($module . PHP_EOL) . ');require("./module2");';
|
---|
283 |
|
---|
284 | case 'path/to/module2':
|
---|
285 | return 'print(' . json_encode($module . PHP_EOL) . ');require("../../module3");';
|
---|
286 |
|
---|
287 | default:
|
---|
288 | return 'print(' . json_encode($module . PHP_EOL) . ');';
|
---|
289 | }
|
---|
290 | });
|
---|
291 | */
|
---|
292 |
|
---|
293 | // V8Js global methods: exit, print, sleep, var_dump, require
|
---|
294 | //$v8 = new V8Js("$", array("data"=>"data"), extensions, flags, millisecond, bytes);
|
---|
295 | $v8 = new V8Js("$"/*, array("data"=>"data")*/);
|
---|
296 |
|
---|
297 | //$v8 = new V8Js("$", array("data"=>"data"));
|
---|
298 |
|
---|
299 | //V8Js::registerExtension("exit", "1");
|
---|
300 | //$v8 = new V8Js("$", array(), array("exit"));
|
---|
301 |
|
---|
302 | //$v8->func = function($a) { echo "Closure with param $a\n"; };
|
---|
303 | //$v8->greeting = "From PHP with love!";
|
---|
304 |
|
---|
305 | // This is much faster than the variables option in the constructor
|
---|
306 |
|
---|
307 | $roi = $evt['numRoi'];
|
---|
308 |
|
---|
309 | //$v8->data = $data;
|
---|
310 | //$v8->test = array();;
|
---|
311 | //$v8->data = array();
|
---|
312 | $v8->nroi = $evt['numRoi'];
|
---|
313 | $v8->npix = $evt['numPix'];
|
---|
314 | $v8->trigger = $evt['trigger'];
|
---|
315 | $v8->event = $evt;
|
---|
316 | $v8->file = $fil;
|
---|
317 | // $v8->neighbors = $neighbors;
|
---|
318 | $v8->clone = function($data) { return $data; };
|
---|
319 | $v8->scale = $fil['scale']==0 ? 2000./4096. : 1./$fil['scale'];
|
---|
320 | $v8->unpack = function($i)
|
---|
321 | {
|
---|
322 | global $binary, $roi, $v8;
|
---|
323 | $u = unpack("s".$roi, $binary[$i]);
|
---|
324 | $arr = array();
|
---|
325 | for ($i=0; $i<$roi; $i++)
|
---|
326 | $arr[$i] = $u[$i+1]*$v8->scale;
|
---|
327 | return $arr;
|
---|
328 | };
|
---|
329 |
|
---|
330 | // 10, 445, 91.75 MiB
|
---|
331 | // 376, 720, 91.75 MiB
|
---|
332 |
|
---|
333 | $internal = file_get_contents("internal.js");
|
---|
334 |
|
---|
335 | // Buffer output from javascript
|
---|
336 | ob_start();
|
---|
337 |
|
---|
338 | $rc['timeJS'] = array();
|
---|
339 | $rc['startJs'] = microtime(true);
|
---|
340 |
|
---|
341 | try
|
---|
342 | {
|
---|
343 | $v8->executeString($internal, 'internal.js');
|
---|
344 |
|
---|
345 | // We unpack the data pixel by pixel and copy the array directly to the
|
---|
346 | // Javasscript array. This significantly decreases memory usage because
|
---|
347 | // we need only one of the super memory hungry php arrays of size ROI
|
---|
348 | // instead of 1440.
|
---|
349 | $JS = "$.data = new Array($.event.numPix); for (var i=0; i<$.event.numPix; i++) $.data[i] = $.unpack(i);";
|
---|
350 | $v8->executeString($JS, 'internal');
|
---|
351 |
|
---|
352 | $rc['timeJs'][0] = (microtime(true) - $rc['startJs']);
|
---|
353 |
|
---|
354 | $JS = "'use strict'; function proc(pixel){\n".$_POST['source1']."\n};proc(".$pixel.");";
|
---|
355 | $rc['waveform'] = $v8->executeString($JS, 'proc');
|
---|
356 |
|
---|
357 | $rc['timeJs'][1] = (microtime(true) - $rc['startJs']);
|
---|
358 |
|
---|
359 | if (isset($_POST['source2']))
|
---|
360 | {
|
---|
361 | $JS = "'use strict'; (function main(){\n".$_POST['source2']."\n})();";
|
---|
362 | $rc['ret'] = $v8->executeString($JS, 'main');
|
---|
363 | }
|
---|
364 |
|
---|
365 | // This is supposed to work, but it seems it does not...
|
---|
366 | //$v8->proc = $rc['ret']->proc;
|
---|
367 | //$rc['ret'] = $v8->executeString('PHP.proc();', 'proc');
|
---|
368 | }
|
---|
369 | catch (V8JsException $e)
|
---|
370 | {
|
---|
371 | $rc['err'] = array();
|
---|
372 | $rc['err']['message'] = $e->getMessage();
|
---|
373 | $rc['err']['file'] = $e->getJsFileName();
|
---|
374 | $rc['err']['sourceLine'] = $e->getJsSourceLine();
|
---|
375 | $rc['err']['lineNumber'] = $e->getJsLineNumber()-1;
|
---|
376 | $rc['err']['trace'] = $e->getJsTrace();
|
---|
377 | }
|
---|
378 |
|
---|
379 | $rc['timeJs'][2] = (microtime(true) - $rc['startJs']);
|
---|
380 |
|
---|
381 | // Copy output buffer and clean it
|
---|
382 | $rc['debug'] = ob_get_contents();
|
---|
383 | ob_end_clean();
|
---|
384 | }
|
---|
385 |
|
---|
386 | $rc['memory'] = memory_get_peak_usage(true)/1024/1024;
|
---|
387 | $rc['timePhp'] = (microtime(true) - $rc['startPhp']);
|
---|
388 |
|
---|
389 | //unset($rc['neighbors']);
|
---|
390 |
|
---|
391 | // Output result as JSON object
|
---|
392 | print(json_encode($rc));
|
---|
393 | ?>
|
---|