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