source: trunk/FACT++/www/viewer/index.js@ 17839

Last change on this file since 17839 was 17838, checked in by tbretz, 11 years ago
No fading for tooltip - hopefully this solves the 'maximum stack exceeded error'; fixed a problem with Disp... width and length need to be scaled in x correctly.
File size: 43.3 KB
Line 
1'use strict';
2
3// ==========================================================================
4
5function onRightMouseClick(event)
6{
7 if (event.button!=2)
8 return;
9
10 var strData = event.target.toDataURL("image/png");
11
12 var img = document.createElement("img");
13 img.src = strData;
14
15 $(img).css({ "z-index": "9999", "position": "absolute" });
16 $(img).insertBefore($(event.target));
17
18 setTimeout(function () { $(img).remove(); }, 100);
19}
20
21(function ($)
22 {
23
24 $.plot.plugins.push({
25 init: function(plot, classes)
26 {
27 plot.hooks.bindEvents.push(function(plot, eventHolder) { eventHolder.mousedown(onRightMouseClick); });
28 plot.hooks.shutdown.push(function(plot, eventHolder) { eventHolder.unbind("mousedown", onRightMouseClick); });
29 },
30 name: 'saveAsImage',
31 version: '1.0'
32 });
33
34 })(jQuery);
35
36// ==========================================================================
37
38var editor1;
39var editor2;
40var plot;
41
42function debug(txt)
43{
44 var dbg = document.getElementById("debug");
45 dbg.appendChild(document.createTextNode(txt));
46 dbg.appendChild(document.createElement("br"));
47}
48
49function setupAccordion(accordion, container, inactive)
50{
51 function onAccordion(event, ui)
52 {
53 if (ui.oldHeader.length==0)
54 $(container).slideDown(400);
55 else
56 $(container).slideUp(400);
57 }
58
59 var config = { collapsible: true };
60
61 if (inactive)
62 {
63 config.active = false;
64 $(container).hide();
65 }
66
67 var acc = $(accordion);
68
69 acc.accordion(config);
70 acc.on("accordionbeforeactivate", onAccordion);
71}
72
73function onResize(event, ui)
74{
75 if (!ui.size)
76 return;
77
78 $(event.target.id).css({marginRight:'2px'});
79
80 var editor = event.target.id=="textcontainer1" ? editor1 : editor2;
81
82 editor.setSize("100%", ui.size.height);
83 editor.refresh();
84}
85
86function setSize(id, w, h)
87{
88 w = parseInt(w);
89 h = parseInt(h);
90
91 $("#"+id).width(w);
92 $("#"+id).height(h);
93
94 document.getElementById(id).width=w;
95 document.getElementById(id).height=h;
96}
97
98function onResizeGrid(id)
99{
100 var w = document.getElementById(id+"container").clientWidth/4;
101
102 var offy = 0;
103 var offx = 5;
104
105 var cont = document.getElementById("center"+id).childNodes[0];
106
107 var nn;
108 if (cont)
109 {
110 nn = parseInt(cont.id[cont.id.length-1]);
111 setSize(id+nn, w*2, w*2);
112 }
113
114 if (nn!=1)
115 setSize(id+'1', w-offx, w-offy);
116 if (nn!=2)
117 setSize(id+'2', w-offx, w-offy);
118 if (nn!=3)
119 setSize(id+'3', w-offx, w-offy);
120 if (nn!=4)
121 setSize(id+'4', w-offx, w-offy);
122
123 document.getElementById("center"+id).width=parseInt(w*2);
124
125 setSize('cont'+id+'1', w, w);
126 setSize('cont'+id+'2', w, w);
127 setSize('cont'+id+'3', w, w);
128 setSize('cont'+id+'4', w, w);
129}
130
131function onResizeCameras(event, ui)
132{
133 onResizeGrid('camera');
134
135 drawFullCam("camera1");
136 drawFullCam("camera2");
137 drawFullCam("camera3");
138 drawFullCam("camera4");
139}
140
141function onResizeHistograms(event, ui)
142{
143 onResizeGrid('hist');
144}
145
146function createEditor(textarea)
147{
148 var editor;
149
150 var config =
151 {
152 //value: "function myScript(){return 100;}\n",
153 mode: { name: "text/typescript", globalVars: true },
154 indentUnit: 4,
155 styleActiveLine: true,
156 matchBrackets: true,
157 lineNumbers: true,
158 foldGutter: true,
159 lint: true,
160 highlightSelectionMatches: {showToken: /\w/},
161 gutters: ["CodeMirror-lint-markers", "CodeMirror-linenumbers", "CodeMirror-foldgutter"],
162 extraKeys: {
163 //"Ctrl-D": "duplicateLine",
164 //"Alt--": "goToBracket",
165 //"Ctrl-H": "findPrev",
166 "Ctrl-Down": "autocomplete",
167 "Tab": "indentAuto",
168 "Ctrl-Y": "deleteLine",
169 "Ctrl-.": function(cm) {
170 cm.foldCode(cm.getCursor());
171 },
172 "F11": function(cm) {
173 editor.setOption("fullScreen", !editor.getOption("fullScreen"));
174 },
175 "Ctrl-R": function(cm) {
176 editor.execCommand("replace");
177 },
178 "Esc": function(cm) {
179 if (editor.getOption("fullScreen")) editor.setOption("fullScreen", false);
180 },
181 "Enter": function(cm) {
182 editor.execCommand("indentAuto");
183 editor.execCommand("newlineAndIndent");
184 },
185 }
186 };
187
188 editor = CodeMirror.fromTextArea(document.getElementById(textarea), config);
189 editor.setOption("theme", "fact");
190
191 return editor;
192}
193
194function colorizeHTML(textarea)
195{
196 var config =
197 {
198 //value: "function myScript(){return 100;}\n",
199 mode: { name: "text/typescript", globalVars: true },
200 readOnly: true,
201 };
202
203 CodeMirror.fromTextArea(document.getElementById(textarea), config);
204}
205
206function setFileChecks()
207{
208 var list = document.getElementById("file").data;
209 if (!list)
210 return;
211
212 var file = document.getElementById("file").value;
213 var drs = document.getElementById("drsfile");
214
215 var hasDrs = list[file]&2;
216 var hasCal = list[file]&4;
217 var isMC = list[file]&8;
218
219 if (!hasDrs || isMC)
220 $('#drsfile').prop('checked', false);
221 if (!hasCal || isMC)
222 $('#calibrated').prop('checked', false);
223
224 $('#calibrated').prop('disabled', (!hasCal && !drs.checked) || isMC);
225 $('#drsfile').prop('disabled', !hasDrs || isMC);
226
227 $('#txtcalibrated').css('color', (!hasCal && !drs.checked) || isMC ? 'darkgrey' : 'black');
228 $('#txtdrsfile').css( 'color', !hasDrs || isMC ? 'darkgrey' : 'black');
229 $('#txtmontecarlo').css('color', !isMC ? 'darkgrey' : 'black');
230
231 $('#montecarlo').prop('checked', isMC);
232}
233
234function disableControls(disabled)
235{
236 $('#submit').prop('disabled', disabled);
237 $('#getcamera').prop('disabled', disabled);
238 $('#getwaveforms').prop('disabled', disabled);
239 $('#event').prop('disabled', disabled);
240 $('#pixel').prop('disabled', disabled);
241 $('#cbpx').prop('disabled', disabled);
242 $('#cbpx-c').prop('disabled', disabled);
243 $('#cbpx-b').prop('disabled', disabled);
244 $('#cbpx-p').prop('disabled', disabled);
245 $('#cbpx-x').prop('disabled', disabled);
246 $('#file').prop('disabled', disabled);
247
248 if (disabled)
249 {
250 $('#calibrated').prop('disabled', true);
251 $('#drsfile').prop('disabled', true);
252 $('#montecarlo').prop('disabled', true);
253 }
254 else
255 setFileChecks();
256}
257
258function onGetCameras()
259{
260 var arr =
261 [
262 document.getElementById("camera1").dataAbs,
263 document.getElementById("camera2").dataAbs,
264 document.getElementById("camera3").dataAbs,
265 document.getElementById("camera4").dataAbs,
266 ];
267
268 $('#controls > input[name=data]').val(JSON.stringify(arr));
269 $('#controls').attr('action','index.php');
270 $('#controls').submit();
271}
272
273function onGetWaveforms()
274{
275 var arr = document.getElementById("waveform").data;
276
277 $('#controls > input[name=data]').val(JSON.stringify(arr));
278 $('#controls').attr('action','index.php');
279 $('#controls').submit();
280}
281
282function onReady()
283{
284 CodeMirror.colorize(null, 'javascript');
285
286 //$('input,select').keypress(function(event) { return event.keyCode != 13; });
287
288 //colorizeHTML("code0");
289 //colorizeHTML("code1");
290
291 $("#accordion").accordion({collapsible:true,active:false,heightStyle:'content'});
292 $("#accordion").find('h3').filter(':contains(Runtime)').hide();
293 if (location.href.search('debug')==-1)
294 $("#accordion").find('h3').filter(':contains(Debug)').hide();
295
296 $("#textcontainer1").resizable({handles:"s",autoHide:true,});
297 $("#textcontainer1").on("resize", onResize);
298
299 $("#textcontainer2").resizable({handles:"s",autoHide:true,});
300 $("#textcontainer2").on("resize", onResize);
301
302 $("#cameracontainer").on("resize", onResizeCameras);
303 onResizeCameras();
304
305 $("#histcontainer").on("resize", onResizeHistograms);
306 onResizeHistograms();
307
308 $("#contcamera1").click(onClickContCamera);
309 $("#contcamera2").click(onClickContCamera);
310 $("#contcamera3").click(onClickContCamera);
311 $("#contcamera4").click(onClickContCamera);
312
313 $("#conthist1").click(onClickContHist);
314 $("#conthist2").click(onClickContHist);
315 $("#conthist3").click(onClickContHist);
316 $("#conthist4").click(onClickContHist);
317
318 $("#camera1").click(onClick);
319 $("#camera2").click(onClick);
320 $("#camera3").click(onClick);
321 $("#camera4").click(onClick);
322
323 $('#camera1').mousedown(onRightMouseClick);
324 $('#camera2').mousedown(onRightMouseClick);
325 $('#camera3').mousedown(onRightMouseClick);
326 $('#camera4').mousedown(onRightMouseClick);
327
328 editor1 = createEditor("editor1");
329 editor2 = createEditor("editor2");
330
331 setupAccordion('#accordion5', '#editorcontainer1', true);
332 setupAccordion('#accordion1', '#editorcontainer2');
333
334 $('#accordion5').on("accordionactivate", function() { $('#editorcontainer1fake').hide(); editor1.refresh(); });
335 $('#accordion1').on("accordionactivate", function() { $('#editorcontainer2fake').hide(); editor2.refresh(); });
336
337 setupAccordion('#accordion2', '#cameracontainer');
338 setupAccordion('#accordion7', '#histcontainer', true);
339 setupAccordion('#accordion3', '#waveformcontainer');
340 setupAccordion('#accordion4', '#helpcontainer', true);
341 setupAccordion('#accordion6', '#ctrlcontainer', true);
342
343 $("#selectfile1").on('change', onFile);
344 $("#selectfile2").on('change', onFile);
345
346 $(document).ajaxStart(function() { disableControls(true) }).ajaxStop(function() { disableControls(false); });
347
348
349 $.ajax({
350 type: "POST",
351 cache: false,
352 url: "index.php",
353 success: onFilelistReceived,
354 error: function(xhr) { if (xhr.status==0) alert("ERROR[0] - Request failed!"); else alert("ERROR[0] - "+xhr.statusText+" ["+xhr.status+"]"); }
355 });
356}
357
358function onFileSelect(event, ui)
359{
360 setFileChecks();
361 document.getElementById("event").value = 0;
362 onSubmit(ui.item.value);
363}
364
365function onFilelistReceived(result)
366{
367 //var dbg = document.getElementById("debug");
368
369 //var pre = document.createElement("pre");
370 //pre.appendChild(document.createTextNode(rc));
371 //dbg.appendChild(pre);
372
373 var rc;
374 try
375 {
376 rc = JSON.parse(result);
377 }
378 catch (e)
379 {
380 alert("ERROR[0] - Decoding answer:\n"+e);
381 debug(result);
382 return;
383 }
384
385 document.getElementById("file").data = rc;
386
387 var list = [ ];
388 for (var file in rc)
389 list.push(file);
390
391 var opts =
392 {
393 source: list,
394 select: onFileSelect,
395 position: { my: "right top", at: "right bottom", collision: "flipfit" },
396 };
397
398 $("#file").autocomplete(opts);
399 document.getElementById("file").value = "2014/04/17-181";
400
401 onSubmit("2014/04/17-181");
402}
403
404function setZoom(xfrom, xto, yfrom, yto)
405{
406 var xaxis = plot.getXAxes()[0];
407 var yaxis = plot.getYAxes()[0];
408
409 if (xfrom!==undefined)
410 xaxis.options.min = xfrom;
411 if (xto!==undefined)
412 xaxis.options.max = xto;
413
414 if (yfrom!==undefined)
415 yaxis.options.min = yfrom;
416 if (yto!==undefined)
417 yaxis.options.max = yto;
418
419 plot.setupGrid();
420 plot.draw();
421 plot.clearSelection();
422}
423
424function onPlotHover(event, pos, item)
425{
426 if (!item)
427 {
428 $("#tooltip").hide();//fadeOut(100);
429 return;
430 }
431
432 var x = item.datapoint[0].toFixed(2);
433 var y = item.datapoint[1].toFixed(2);
434
435 var tooltip = $("#tooltip");
436 tooltip.html(parseInt(x) + " / " + y);
437 tooltip.css({top: item.pageY-20, left: item.pageX+5});
438 tooltip.show();//fadeIn(200);
439}
440
441function drawHist(n)
442{
443 var canv = document.getElementById("camera"+n);
444 var hist = document.getElementById("hist"+n);
445
446 var xmin = parseFloat(document.getElementById("histmin"+n).value);
447 var xmax = parseFloat(document.getElementById("histmax"+n).value);
448 var nbins = 100;//parseInt(xmax-xmin);
449 var step = (xmax-xmin)/nbins;
450 if (step<1)
451 {
452 step = 1;
453 nbins = parseInt(xmax-xmin)+1;
454 }
455
456 var bins = new Array(nbins);
457 for (var i=0; i<nbins; i++)
458 bins[i] = [ xmin+i*step, 0 ];
459
460 var data = canv.dataAbs;
461 for (var i=0; i<1440; i++)
462 if (data[i]!==undefined/* && data[i]!==null*/)
463 {
464 var ix = parseInt((data[i]-xmin)/step);
465 if (ix>=0 && ix<nbins)
466 bins[ix][1] ++;
467 }
468
469 var opts =
470 {
471 grid: {
472 hoverable: true,
473 }
474 };
475
476 var hist = $.plot("#hist"+n, [ { data:bins, bars: {show:true} } ], opts);
477 $('#hist'+n).bind("plothover", onPlotHover);
478}
479
480function processCameraData(n, data)
481{
482 var canv = document.getElementById("camera"+n);
483 canv.dataRel = null;
484
485 if (!Array.isArray(data))
486 return;
487
488 canv.dataAbs = new Array(1440);
489 for (var i=0; i<1440; i++)
490 {
491 var val = data[map[i]];
492 if (!isNaN(val) && val!==null)
493 canv.dataAbs[i] = val;
494 }
495
496 canv.min = Math.min.apply(Math, canv.dataAbs.filter(function(e){return e!==undefined;}));
497 canv.max = Math.max.apply(Math, canv.dataAbs.filter(function(e){return e!==undefined;}));
498
499 canv.dataRel = new Array(1440);
500 for (var i=0; i<1440; i++)
501 {
502 var val = canv.dataAbs[i];
503 if (val!==undefined)
504 canv.dataRel[i] = (val-canv.min)/canv.max;
505 }
506
507 if (document.getElementById("cameraminon"+n).checked)
508 document.getElementById("cameramin"+n).value = canv.min;
509 if (document.getElementById("cameramaxon"+n).checked)
510 document.getElementById("cameramax"+n).value = canv.max;
511
512 // ---------------------------
513
514 var hist = document.getElementById("hist"+n);
515
516 hist.min = canv.min;
517 hist.max = canv.max;
518
519 if (document.getElementById("histminon"+n).checked)
520 document.getElementById("histmin"+n).value = canv.min;
521 if (document.getElementById("histmaxon"+n).checked)
522 document.getElementById("histmax"+n).value = canv.max;
523
524 drawHist(n);
525}
526
527function onDataReceived(result)
528{
529 var err = document.getElementById("error");
530 var dbg = document.getElementById("debug");
531 var con = document.getElementById("console");
532
533 //var pre = document.createElement("pre");
534 //pre.appendChild(document.createTextNode(rc));
535 //dbg.appendChild(pre);
536
537 var rc;
538 try
539 {
540 rc = JSON.parse(result);
541 if (!rc)
542 return;
543 }
544 catch (e)
545 {
546 alert("ERROR[1] - Decoding answer:\n"+e);
547 debug(result);
548 return;
549 }
550
551 var evt = rc.event;
552 var file = rc.file;
553
554 document.getElementById("event").max = file.numEvents-1;
555 var el = document.getElementById("numevents");
556 if (el.firstChild)
557 el.removeChild(el.firstChild);
558 el.appendChild(document.createTextNode(file.numEvents));
559
560 var infotxt = "<pre>";
561 infotxt += "\nStart time: "+new Date(file.runStart*24*3600*1000).toUTCString();
562 infotxt += "\nEnd time: "+new Date(file.runEnd*24*3600*1000).toUTCString();
563 infotxt += "\nRun type: "+file.runType;
564 if (file.drsFile>=0)
565 infotxt += " [drs-step "+file.drsFile+"]";
566
567 $("#runinfo").html(infotxt);
568 $("#eventinfo").html("Trigger: "+evt.trigger.join(' | ')+" [0x"+evt.triggerType.toString(16)+"]");
569
570 if (rc.ret)
571 {
572 while (con.lastChild)
573 con.removeChild(con.lastChild);
574 }
575
576 if (rc.err)
577 {
578 while (err.lastChild)
579 err.removeChild(err.lastChild);
580
581 err.appendChild(document.createTextNode("Javascript runtime exception: "+rc.err.file+":"+rc.err.lineNumber));
582 err.appendChild(document.createTextNode("\n"));
583 err.appendChild(document.createTextNode(rc.err.sourceLine));
584 err.appendChild(document.createTextNode("\n"));
585 err.appendChild(document.createTextNode(rc.err.trace));
586
587 var editor = rc.err.file=="main" ? editor2 : editor1;
588 editor.setCursor(rc.err.lineNumber-1, 1);
589
590 $("#accordion").find('h3').filter(':contains(Runtime)').show();
591 $("#accordion").accordion("option", "active", 0);
592 }
593
594 if (rc.debug!==undefined)
595 {
596 con.appendChild(document.createTextNode(rc.debug));
597
598 debug("PHP execution:");
599 debug("Time Javascripts = "+(rc.timeJs[0]*1000).toFixed(2)+","+(rc.timeJs[1]*1000).toFixed(2)+","+(rc.timeJs[2]*1000).toFixed(2)+ " [ms]");
600 }
601
602 if (rc.ret!==undefined && Array.isArray(rc.ret))
603 {
604 var now = new Date();
605
606 if (rc.ret[0] instanceof Object)
607 processCameraData(1, rc.ret[0]);
608 else
609 processCameraData(1, rc.ret);
610
611 if (rc.ret.length>1)
612 processCameraData(2, rc.ret[1]);
613
614 if (rc.ret.length>2)
615 processCameraData(3, rc.ret[2]);
616
617 if (rc.ret.length>3)
618 processCameraData(4, rc.ret[3]);
619
620 debug("Calc Time = "+(new Date()-now)+" ms");
621 }
622
623 // We have to redraw all of them to display the changed pixel value
624 onCameraMinMax(1);
625 onCameraMinMax(2);
626 onCameraMinMax(3);
627 onCameraMinMax(4);
628
629 debug("Total time = "+(rc.timePhp*1000).toFixed(1)+" ms");
630 debug("Peak memory = "+rc.memory+" MiB");
631
632 if (Array.isArray(rc.waveform))
633 {
634 var waveform = document.getElementById("waveform");
635 waveform.data = [ ];
636
637 var data = [
638 { label: "[0] ", data: new Array(evt.numRoi) },
639 { label: "[1] ", data: new Array(evt.numRoi) },
640 { label: "[2] ", data: new Array(evt.numRoi) },
641 { label: "[3] ", data: new Array(evt.numRoi) },
642 ];
643
644 var min = [];
645 var max = [];
646 if (Array.isArray(rc.waveform) && rc.waveform.length==evt.numRoi)
647 {
648 min.push(Math.min.apply(Math, rc.waveform));
649 max.push(Math.max.apply(Math, rc.waveform));
650
651 var d = data[0].data;
652 for (var i=0; i<evt.numRoi; i++)
653 d[i] = [ i, rc.waveform[i] ];
654
655 waveform.data[0] = rc.waveform;
656 }
657
658 for (var j=0; j<4; j++)
659 {
660 var ref = rc.waveform[j];
661
662 if (Array.isArray(ref) && ref.length==evt.numRoi)
663 {
664 min.push(Math.min.apply(Math, ref));
665 max.push(Math.max.apply(Math, ref));
666
667 var d = data[j].data;
668 for (var i=0; i<evt.numRoi; i++)
669 d[i] = [ i, ref[i] ];
670
671 waveform.data[j] = ref;
672 }
673 }
674
675 waveform.ymin = Math.min.apply(Math, min);
676 waveform.ymax = Math.max.apply(Math, max);
677 waveform.xmin = 0;
678 waveform.xmax = evt.numRoi;
679
680 if (document.getElementById("waveformxminon").checked)
681 document.getElementById("waveformxmin").value = waveform.xmin;
682 if (document.getElementById("waveformxmaxon").checked)
683 document.getElementById("waveformxmax").value = waveform.xmax;
684
685 if (document.getElementById("waveformminon").checked)
686 document.getElementById("waveformmin").value = waveform.ymin;
687 if (document.getElementById("waveformmaxon").checked)
688 document.getElementById("waveformmax").value = waveform.ymax;
689
690 var xmin = document.getElementById("waveformxminon").checked ? waveform.xmin : parseInt(document.getElementById("waveformxmin").value);
691 var xmax = document.getElementById("waveformxmaxon").checked ? waveform.xmax : parseInt(document.getElementById("waveformxmax").value);
692
693 var ymin = document.getElementById("waveformminon").checked ? waveform.ymin : parseInt(document.getElementById("waveformmin").value);
694 var ymax = document.getElementById("waveformmaxon").checked ? waveform.ymax : parseInt(document.getElementById("waveformmax").value);
695
696 var opts =
697 {
698 xaxis: {
699 min: xmin-1,
700 max: xmax+1,
701 },
702 yaxis: {
703 min: ymin-5,
704 max: ymax+5,
705 },
706 series: {
707 lines: {
708 show: true
709 },
710 points: {
711 show: true,
712 symbol: 'cross',
713 }
714 },
715 selection: {
716 mode: "xy"
717 },
718 grid: {
719 hoverable: true,
720 }
721 };
722
723 plot = $.plot("#waveform", data, opts);
724
725 waveform = $('#waveform');
726 waveform.bind("plotselected", function (event, ranges)
727 {
728 setZoom(ranges.xaxis.from, ranges.xaxis.to,
729 ranges.yaxis.from, ranges.yaxis.to);
730 });
731
732 waveform.dblclick(function ()
733 {
734 var waveform = document.getElementById("waveform");
735 setZoom(waveform.xmin-1, waveform.xmax+1, waveform.ymin-5, waveform.ymax+5);
736 });
737 waveform.bind("plothover", onPlotHover);
738 }
739}
740
741function onSubmit(file, pixelOnly)
742{
743 if (!file)
744 file = document.getElementById("file").value;
745
746 var dbg = document.getElementById("debug");
747 while (dbg.lastChild)
748 dbg.removeChild(dbg.lastChild);
749
750 var active = $("#accordion").accordion("option", "active");
751 if (active==0)
752 {
753 $("#accordion").accordion("option", "active", false);
754 $("#accordion").find('h3').filter(':contains(Runtime)').hide();
755 }
756
757 var calibrated = document.getElementById("calibrated");
758 var drsfile = document.getElementById("drsfile");
759 var montecarlo = document.getElementById("montecarlo");
760 var ismc = montecarlo.checked;
761 var calib = !calibrated.disabled && calibrated.checked;
762 var drs = !drsfile.disabled && drsfile.checked;
763 var event = document.getElementById("event").value;
764 var pixel = document.getElementById("pixel").value;
765 var source1 = editor1.getValue();
766 var source2 = editor2.getValue();
767
768 var uri = "file="+file+"&event="+event+"&pixel="+map[pixel];
769 uri += "&source1="+encodeURIComponent(source1);
770 if (!pixelOnly)
771 uri += "&source2="+encodeURIComponent(source2);
772 if (calib)
773 uri += "&calibrated=1";
774 if (drs)
775 uri += "&drsfile=1";
776 if (ismc)
777 uri += "&montecarlo=1";
778
779 $.ajax({
780 type: "POST",
781 cache: false,
782 url: "index.php",
783 data: uri,
784 success: onDataReceived,
785 error: function(xhr) { if (xhr.status==0) alert("ERROR[1] - Request failed!"); else alert("ERROR[1] - "+xhr.statusText+" ["+xhr.status+"]"); }
786 });
787}
788
789function onFile(event, ui)
790{
791 var f = event.target.files[0];
792 if (!f)
793 return;
794
795 if (!f.type.match('text/plain') && !f.type.match('application/javascript'))
796 {
797 alert("ERROR - Unknown file type.");
798 return;
799 }
800
801 var id = event.target.id;
802 var editor = id[id.length-1]=='1' ? editor1 : editor2;
803
804 var reader = new FileReader();
805
806 // Closure to capture the file information.
807 reader.onload = (function(theFile) { return function(e) { editor.setValue(e.target.result); }; })(f);
808 // onloadstart
809 // onloadend
810 // onprogress
811
812 // Read in the text file
813 reader.readAsText(f);
814}
815
816function refreshCameras()
817{
818 drawFullCam("camera1");
819 drawFullCam("camera2");
820 drawFullCam("camera3");
821 drawFullCam("camera4");
822}
823
824function onEvent()
825{
826 onSubmit();
827}
828
829function checkPixel()
830{
831 var pix = parseInt(document.getElementById("pixel").value);
832 var c = parseInt(document.getElementById("cbpx-c").value);
833 var b = parseInt(document.getElementById("cbpx-b").value);
834 var p = parseInt(document.getElementById("cbpx-p").value);
835 var x = parseInt(document.getElementById("cbpx-x").value);
836 var cbpx = parseInt(document.getElementById("cbpx").value);;
837
838 if (pix >=0 && pix <1440 &&
839 c >=0 && c < 4 &&
840 b >=0 && b < 10 &&
841 p >=0 && p < 4 &&
842 x >=0 && x < 9 &&
843 cbpx>=0 && cbpx<1440)
844 return;
845
846 document.getElementById("pixel").value = 0;
847 document.getElementById("cbpx-c").value = 1;
848 document.getElementById("cbpx-b").value = 0;
849 document.getElementById("cbpx-p").value = 3;
850 document.getElementById("cbpx-x").value = 6;
851 document.getElementById("cbpx").value = 393;
852}
853
854
855function onPixel()
856{
857 checkPixel();
858
859 var p = parseInt(document.getElementById("pixel").value);
860
861 var cbpx = map[p];
862
863 document.getElementById("cbpx-c").value = parseInt((cbpx/360));
864 document.getElementById("cbpx-b").value = parseInt((cbpx/36)%10);
865 document.getElementById("cbpx-p").value = parseInt((cbpx/9)%4);
866 document.getElementById("cbpx-x").value = parseInt((cbpx%9));
867 document.getElementById("cbpx").value = parseInt(cbpx);
868
869 onSubmit("", true);
870}
871
872function onCBPX()
873{
874 checkPixel();
875
876 var c = parseInt(document.getElementById("cbpx-c").value);
877 var b = parseInt(document.getElementById("cbpx-b").value);
878 var p = parseInt(document.getElementById("cbpx-p").value);
879 var x = parseInt(document.getElementById("cbpx-x").value);
880
881 var cbpx = c*360 + b*36 + p*9 + x;
882
883 document.getElementById("cbpx").value = parseInt(cbpx);
884 document.getElementById("pixel").value = map.indexOf(cbpx);
885
886 onSubmit("", true);
887}
888
889function onHW()
890{
891 checkPixel();
892
893 var cbpx = parseInt(document.getElementById("cbpx").value);;
894
895 document.getElementById("cbpx-c").value = parseInt((cbpx/360));
896 document.getElementById("cbpx-b").value = parseInt((cbpx/36)%10);
897 document.getElementById("cbpx-p").value = parseInt((cbpx/9)%4);
898 document.getElementById("cbpx-x").value = parseInt((cbpx%9));
899
900 document.getElementById("pixel").value = map.indexOf(cbpx);
901
902 onSubmit("", true);
903}
904
905function isInside(x, y, mouse)
906{
907 var dist = Math.sqrt((mouse.x-x)*(mouse.x-x)+(mouse.y-y)*(mouse.y-y));
908 return dist<0.5;
909
910 /*
911 ctx.translate(x, y);
912 ctx.scale(1/2, 1/3);
913
914 ctx.beginPath();
915 ctx.moveTo( 1, 1);
916 ctx.lineTo( 0, 2);
917 ctx.lineTo(-1, 1);
918 ctx.lineTo(-1, -1);
919 ctx.lineTo( 0, -2);
920 ctx.lineTo( 1, -1);
921 ctx.fill();
922
923 ctx.restore();
924 */
925}
926
927var inprogress = { };
928function moveElement(id, n, target, callback)
929{
930 if (inprogress[id]==n || inprogress[id]<0)
931 return;
932
933 inprogress[id] = target ? -n : n;
934
935 var element = $("#"+id+n); //Allow passing in either a JQuery object or selector
936 var newParent = $(target ? target : "#cont"+id+n); //Allow passing in either a JQuery object or selector
937
938 var oldOffset = element.offset();
939
940 var newOffset = newParent.offset();
941
942 var w = newParent.width();
943 var h = newParent.height();
944
945 var temp = element.appendTo('body');
946 temp.css('position', 'absolute')
947 .css('left', oldOffset.left)
948 .css('top', oldOffset.top)
949 .css('zIndex', 999);
950
951 temp.animate( {'top': newOffset.top, 'left':newOffset.left, 'width':w, 'height': h},
952 'slow', function()
953 {
954 temp = temp.appendTo(newParent);
955 temp.css('position', 'relative');
956 temp.css('width', '');
957 temp.css('height', '');
958 temp.css('zIndex', '');
959 temp.css('left', '0');
960 temp.css('top', '0');
961
962 setSize(id+n, w, h);
963
964 if (callback)
965 callback(id+n);
966
967 inprogress[id] = 0;
968 });
969}
970
971function onClickCont(event, callback)
972{
973 var id = event.target.id;
974 if (!id)
975 id = event.target.parentNode.id;
976
977 var n = parseInt(id[id.length-1]);
978 var type = id.substr(0, id.length-1);
979
980 if (id.substr(0, 4)=="cont")
981 id = id.substr(4, id.length-4);
982 if (type.substr(0, 4)=="cont")
983 type = type.substr(4, type.length-4);
984
985 if (id.substr(0, type.length)==type)
986 {
987 var cont = document.getElementById("center"+type).childNodes[0];
988 if (cont)
989 {
990 var nn = parseInt(cont.id[cont.id.length-1]);
991 moveElement(type, nn, null, callback);
992 }
993 moveElement(type, n, "#center"+type, callback);
994
995 }
996 else
997 moveElement(type, n, null, callback);
998
999}
1000
1001function onClickContCamera(event)
1002{
1003 onClickCont(event, function(el) { drawFullCam(el); });
1004}
1005
1006function onClickContHist(event)
1007{
1008 onClickCont(event);
1009}
1010
1011function onClick(event)
1012{
1013 var cont = document.getElementById("centercamera").childNodes[0];
1014 if (!cont)
1015 return;
1016
1017 if (cont.id!=event.target.id)
1018 return;
1019
1020 // get click position relative to canvas
1021 var rect = event.target.getBoundingClientRect();
1022
1023 var x = event.clientX - rect.left;
1024 var y = event.clientY - rect.top;
1025
1026 var mouse = { x: x, y: y };
1027
1028 // convert click position to pixel index
1029 var index = getIndex(event.target.id, mouse);
1030 if (index<0)
1031 return;
1032
1033 document.getElementById("pixel").value = index;
1034
1035 onPixel();
1036}
1037
1038function getClickPosition(event)
1039{
1040 var rect = event.target.getBoundingClientRect();
1041
1042 var x = event.clientX - rect.left;
1043 var y = event.clientY - rect.top;
1044
1045 return { x: x, y: y };
1046}
1047
1048function onMinMax(id, n)
1049{
1050 var el = document.getElementById(id+n);
1051
1052 el.zmin = document.getElementById(id+"min"+n).value;
1053 el.zmax = document.getElementById(id+"max"+n).value;
1054}
1055
1056function onCameraMinMax(n)
1057{
1058 onMinMax("camera", n);
1059 drawFullCam("camera"+n);
1060}
1061
1062function onHistMinMax(n)
1063{
1064 onMinMax("hist", n);
1065 drawHist(n);
1066}
1067
1068function onMinMaxOn(id, n)
1069{
1070 var el = document.getElementById(id+n);
1071
1072 var redraw;
1073 if (document.getElementById(id+"minon"+n).checked)
1074 {
1075 document.getElementById(id+"min"+n).setAttribute("disabled", "true");
1076 document.getElementById(id+"min"+n).value = el.min;
1077 redraw = true;
1078 }
1079 else
1080 document.getElementById(id+"min"+n).removeAttribute("disabled");
1081
1082 if (document.getElementById(id+"maxon"+n).checked)
1083 {
1084 document.getElementById(id+"max"+n).setAttribute("disabled", "true");
1085 document.getElementById(id+"max"+n).value = el.max;
1086 redraw = true;
1087 }
1088 else
1089 document.getElementById(id+"max"+n).removeAttribute("disabled");
1090
1091 return redraw;
1092}
1093
1094function onCameraMinMaxOn(n)
1095{
1096 if (onMinMaxOn("camera", n))
1097 onCameraMinMax(n);
1098}
1099
1100function onHistMinMaxOn(n)
1101{
1102 if (onMinMaxOn("hist", n))
1103 onHistMinMax(n);
1104}
1105
1106function onWaveformMinMax()
1107{
1108 var wf = document.getElementById("waveform");
1109
1110 var xmin, xmax, ymin, ymax;
1111
1112 var redraw;
1113 if (!document.getElementById("waveformxminon").checked)
1114 xmin = document.getElementById("waveformxmin").value;
1115 if (!document.getElementById("waveformxmaxon").checked)
1116 xmax = document.getElementById("waveformxmax").value;
1117 if (!document.getElementById("waveformminon").checked)
1118 ymin = document.getElementById("waveformmin").value;
1119 if (!document.getElementById("waveformmaxon").checked)
1120 ymax = document.getElementById("waveformmax").value;
1121
1122 setZoom(xmin, xmax, ymin, ymax);
1123
1124}
1125
1126function onWaveformMinMaxOn()
1127{
1128 var wf = document.getElementById("waveform");
1129
1130 var xmin, xmax, ymin, ymax;
1131
1132 var redraw;
1133 if (document.getElementById("waveformxminon").checked)
1134 {
1135 document.getElementById("waveformxmin").setAttribute("disabled", "true");
1136 document.getElementById("waveformxmin").value = wf.xmin;
1137 xmin = wf.xmin-1;
1138 }
1139 else
1140 document.getElementById("waveformxmin").removeAttribute("disabled");
1141
1142 if (document.getElementById("waveformxmaxon").checked)
1143 {
1144 document.getElementById("waveformxmax").setAttribute("disabled", "true");
1145 document.getElementById("waveformxmax").value = wf.xmax;
1146 xmax = wf.xmax+1;
1147 }
1148 else
1149 document.getElementById("waveformxmax").removeAttribute("disabled");
1150
1151 if (document.getElementById("waveformminon").checked)
1152 {
1153 document.getElementById("waveformmin").setAttribute("disabled", "true");
1154 document.getElementById("waveformmin").value = wf.ymin;
1155 ymin = wf.ymin-5;
1156 }
1157 else
1158 document.getElementById("waveformmin").removeAttribute("disabled");
1159
1160 if (document.getElementById("waveformmaxon").checked)
1161 {
1162 document.getElementById("waveformmax").setAttribute("disabled", "true");
1163 document.getElementById("waveformmax").value = wf.ymax;
1164 ymax = wf.ymax+5;
1165 }
1166 else
1167 document.getElementById("waveformmax").removeAttribute("disabled");
1168
1169 setZoom(xmin, xmax, ymin, ymax);
1170}
1171
1172//document.addEventListener("click", getClickPosition, false);
1173
1174$(document).ready(onReady);
1175
1176// ================================== Pixel mapping =================================================
1177
1178var map = new Array(1440);
1179
1180function initPixelMap()
1181{
1182 var codedMap = "966676:6:A;68656364626Y?\\?;A=A<AGADAN4K4i5g5h5o506W?Z?]?_?>A@A?AJAIAFACAM4J4H4f5d5e5l5m5n516X?[?^?N?P?AA1ABAVAUAKAHAEAO4L4I4G4E4c5a5b5M6j5k5V6Y6\\6_6G?J?O?Q?S?2A4A3AYAXAWAbA_AnAkAhA3404F4D4B4`5^5_5J6K6L6S6T6W6Z6]6E?H?K?M?R?T?V?5A7A6A\\A[AZAeAdAaA^AmAjAgA24o3m3C4A4?4]5[5\\5G6H6I6P6Q6R6U6X6[6^6F?I?L?<?>?U?;@=@8Ah@9AMALA]ABCACfAcA`AoAlAiA4414n3l3<4@4>425Z5X5Y5D6E6F698N6O608E8H8K8H>K>N>?>B>=???A?<@>@@@i@k@j@PAOANAECDCCC<C9CZCWCTC=3:3734313=4;4943515o4W5U5V5A6B6C6687888m7n7C8F8I8F>I>L>=>@>C>E>@?B?D??@A@C@l@n@m@SARAQAHCGCFC?C>C;C8CYCVCSC<393633303n2:4846405n4l4T5R5S5>6?6@6384858j7k7l7o7D8G8J8G>J>M>>>A>D>4>6>C?3?5?B@2@4@o@_@0ALBKBTA0CoBIC8D7D@C=C:C[CXCUC>3;3835323o2m2k27454j3m4k4i4Q5O5P5C7<6=6g71828o8h7i7S9<8?8B8Q>T>W>@=C=F=e<h<5>7>9>4?6?8?3@5@7@`@b@a@OBNBMB3C2C1C;D:D9D_D\\DQCNCKCF3C3@3>2;282Z1W1l2j2h2k3i3g3j4h4f4N5L5M5@7A7B7d7e7f7l8m8n8P9Q9:8=8@8O>R>U>>=A=D=c<f<i<k<8>:><>7?9?;?6@8@:@c@e@d@RBQBPB6C5C4C>D=D<DbDaD^D[DPCMCJCE3B3?3=2:272Y1V1T1i2g2e2h3f3d3g4e4c4K5I5J5=7>7?7a7b7c7i8j8k8M9N9O9R9;8>8A8P>S>V>?=B=E=d<g<j<Z<\\<;>k=m=:?j>l>9@i?k?f@V@g@CBBBSBgBfB7CoCnC?DSDRDcD`D]DRCOCLCG3D3A3?2<292[1X1U1S1Q1f2d2b2e3c3a3d4b4`4H5F5G5:7;7<7^7_7`7f8g8h8J9K9L97:::=:@:C:F:I:I=L=O=7=:===n<1=[<]<_<l=n=0>k>m>o>j?l?n?W@Y@X@FBEBDBjBiBhB2D1D0DVDUDTDCE@EOELEIEXEUERE5222o1l1i1f1c1`1R1P1N1c2a2_2b3`3^3a4_4]4<5:5;5778797[7\\7]7c8d8e8G9H9I94:5:8:;:>:A:D:G:G=J=M=5=8=;=l<o<2=4=^<`<b<o=1>3>n>0?2?m?o?1@Z@\\@[@IBHBGBmBlBkB5D4D3DYDXDWDFEEEBE?ENEKEHEWETEQE4212n1k1h1e1b1_1]1O1M1K1`2^2\\2_3]3[3^4\\4Z4957585475767X7Y7Z7`8a8b8D9E9F91:2:3:6:9:<:?:B:E:H:H=K=N=6=9=<=m<0=3=d;f;a<H<J<2>b=d=1?a>c>0@`?b?]@D@^@:B9BJB^B]BnBfCeC6DJDIDZDnDmDGEDEAEPEMEJEYEVESE623202m1j1g1d1a1^1\\1[0L1J1?1]2[2Y2\\3Z3X3[4Y4W4654555172737U7V7W7]8^8_8A9B9C9e9o90:n9g:j:m:L:O:R:U:X:[:];`;c;T;W;Z;7<9<e;g;i;I<K<M<c=e=g=b>d>f>a?c?e?E@G@F@=B<B;BaB`B_BiChCgCMDLDKD1E0EoD9E7E<F9F6FaE^E[EjEgEdER0O0L0I0F0C0n0l0\\0Z0X0@1>1<1Z2X2V2Y3W3U3X4V4T4E5C5D5n6o607R7S7T7Z8[8\\8>9?9@9b9c9d9l9m9e:h:k:J:M:P:S:V:Y:[;^;a;R;U;X;6<8<:<;<h;j;l;L<N<P<f=h=j=e>g>i>d?f?h?N@Q@H@@B?B>BdBcBbBlCkCjCPDODND4E3E2E;E:E8E6E;F8F5F`E]EZEiEfEcEQ0N0K0H0E0B0m0k0j0Y0W0U0=1;191W2U2S2V3T3R3U4S4Q4B5@5A5k6l6m6O7P7Q7W8X8Y8;9<9=9_9`9a9j9k9\\:_:f:i:l:K:N:Q:T:W:Z:\\;_;b;S;V;Y;C<E<G<<<m;k;1<2<O<Q<S<i=[=P=h>X>Z>g?M@O@R@U@S@J@I@AB1B0BeBTB\\CmCAD@DQDiDhD5EdD<E4F2F0F=F:F7FbE_E\\EkEhEeES0P0M0J0G0D011o0h0g0e0V0T0`0:181Q2T2R2H2S3Q3P3R4P4K3?5=5>5c6i6j6h6M7N7L7U8V8T899:9W9]9^9\\9g9h9i9]:`:c:5;2;o:>;;;8;P;M;J;G;D;A;?<A<D<F<=<><n;o;3<5<R<T<Y=Z=Q=R=Y>[>\\>^>P@T@L@K@5B4B3B2BVBUB^C]CCDBDkDjDfDeD>E=E3F1FnElE@FCFFFIFLFOF;0>0A0205080513101i0f0d0c0b0_0I1H1D1P2O2G2F2D2O3N3M3J3H3a6b6e6f6g6I7J7K7R8S8397989V9Y9Z9[9f9^:a:d:4;1;n:=;:;7;O;L;I;F;C;@;@<B<0<4<U<V<X<`=]=\\=S=U=W=]>_>`>8B7B6BZBXBWB_CaC`CFDEDDDlDgDoEmE>FAFDFGFJFMF90<0?0003060714121a0^0]0G1C1B1N2L2E2C2B2@2L3I3`6d6E7F7G7H7O8Q8192969T9U9X96;3;0;?;<;9;Q;N;K;H;E;B;W<Y<^=_=a=T=V=X=\\B[BYBdCcCbCHDGD?FBFEFHFKFNF:0=0@0104070F1E1A1M2K2J2I2A2D7L8M8N8P809495961b:";
1183 // first: decode the pixel mapping!
1184 var sum = 1036080;
1185 for (var i=0; i<1440; i++)
1186 {
1187 var d0 = codedMap.charCodeAt(i*2) -48;
1188 var d1 = codedMap.charCodeAt(i*2+1)-48;
1189
1190 map[i] = d0 | (d1<<6);
1191 sum -= map[i];
1192 }
1193 if (sum!=0)
1194 alert("Pixel mapping table corrupted!");
1195}
1196
1197initPixelMap();
1198
1199// ================================== Camera Display ================================================
1200
1201var coord = new Array(1440);
1202function initCameraCoordinates()
1203{
1204 coord[0] = [0, 0];
1205 var cnt = 1;
1206 for (var ring=1; ring<24; ring++)
1207 {
1208 for (var s=0; s<6; s++)
1209 {
1210 for (var i=1; i<=ring; i++)
1211 {
1212 var pos = new Position(s, ring, i);
1213 if (pos.d() - pos.x > 395.75)
1214 continue;
1215
1216 coord[cnt++] = [ pos.x, pos.y];
1217 }
1218 }
1219 }
1220
1221 coord[1438] = [7, -22];
1222 coord[1439] = [7, 22];
1223}
1224
1225initCameraCoordinates();
1226
1227function getIndex(id, mouse)
1228{
1229 var canv = document.getElementById(id);
1230
1231 var scale = 83;
1232
1233 var w = Math.min(canv.width/scale, canv.height/scale);
1234
1235 //ctx.translate(canv.width/2, canv.height/2);
1236 //ctx.scale(w*2, w*2);
1237 //ctx.scale(1, Math.sqrt(3)/2);
1238 //ctx.translate(-0.5, 0);
1239
1240 mouse.x -= canv.width/2;
1241 mouse.y -= canv.height/2;
1242 mouse.x /= w*2;
1243 mouse.y /= w*2;
1244 mouse.y /= Math.sqrt(3)/2;
1245 mouse.x -= -0.5;
1246
1247 for (var i=0; i<1440; i++)
1248 if (isInside(coord[i][0], coord[i][1], mouse))
1249 return i;
1250
1251 return -1;
1252}
1253
1254
1255function hueToRGB(hue)
1256{
1257 hue /= 3;
1258 hue %= 6;
1259
1260 if (hue<1) return parseInt(255*hue, 10);
1261 if (hue<3) return parseInt(255, 10);
1262 if (hue<4) return parseInt(255*(4-hue), 10);
1263
1264 return 0.
1265}
1266
1267function hueToHex(flt)
1268{
1269 var s = hueToRGB(flt).toString(16);
1270 return s.length==2 ? s : "0"+s;
1271}
1272
1273function HLStoRGB(hue)
1274{
1275 if (isNaN(hue))
1276 return "fff";
1277
1278 if (hue<0)
1279 return "eef"; // 555
1280
1281 if (hue>1)
1282 return "700"; // 666
1283
1284 hue *= 14;
1285
1286 var sr = hueToHex(20-hue);
1287 var sg = hueToHex(14-hue);
1288 var sb = hueToHex(26-hue);
1289
1290 return sr+sg+sb;
1291}
1292
1293function outlineHex(ctx)
1294{
1295 ctx.scale(1/2, 1/3);
1296
1297 ctx.beginPath();
1298 ctx.moveTo( 1, 1);
1299 ctx.lineTo( 0, 2);
1300 ctx.lineTo(-1, 1);
1301 ctx.lineTo(-1, -1);
1302 ctx.lineTo( 0, -2);
1303 ctx.lineTo( 1, -1);
1304 ctx.lineTo( 1, 1);
1305}
1306
1307function fillHex(ctx, i, col, min, max)
1308{
1309 if (col===undefined/* || col===null*/)
1310 return false;
1311
1312 var lvl = max==min ? 0.5 : (col-min)/(max-min);
1313
1314 ctx.fillStyle = "#"+HLStoRGB(lvl);
1315
1316 ctx.save();
1317 ctx.translate(coord[i][0], coord[i][1]);
1318 outlineHex(ctx);
1319 ctx.fill();
1320 ctx.restore();
1321
1322 return true;
1323}
1324
1325function drawHex(ctx, i)
1326{
1327 ctx.save();
1328 ctx.translate(coord[i][0], coord[i][1]);
1329 outlineHex(ctx);
1330 ctx.stroke();
1331 ctx.restore();
1332}
1333
1334
1335function Position(s, ring, i)
1336{
1337 switch (s)
1338 {
1339 case 1: this.x = ring - i*0.5; this.y = + i; break;
1340 case 2: this.x = ring*0.5 - i; this.y = ring ; break;
1341 case 3: this.x = -ring*0.5 - i*0.5; this.y = ring - i; break;
1342 case 4: this.x = -ring + i*0.5; this.y = - i; break;
1343 case 5: this.x = -ring*0.5 + i; this.y = -ring ; break;
1344 case 0: this.x = ring*0.5 + i*0.5; this.y = -ring + i; break;
1345 }
1346 this.d = (function () { return this.x*this.x + this.y*this.y*3/4; });
1347}
1348
1349function drawFullCam(id)
1350{
1351 var canv = document.getElementById(id);
1352 if (!canv)
1353 return;
1354
1355 var ctx = canv.getContext("2d");
1356
1357 ctx.clearRect(0, 0, canv.width, canv.height);
1358
1359 // ======================= Draw Graphics ======================
1360
1361 var data = canv.dataRel;
1362 if (!data)
1363 return;
1364
1365 var pixel = document.getElementById('pixel').value;
1366
1367 var min = (canv.zmin-canv.min)/canv.max;
1368 var max = (canv.zmax-canv.min)/canv.max;
1369
1370 var scale = 83;
1371
1372 var w = Math.min(canv.width/scale, canv.height/scale);
1373
1374 ctx.save();
1375 ctx.translate(canv.width/2, canv.height/2);
1376 ctx.scale(w*2, w*2);
1377 // ctx.rotate(Math.PI/3);
1378
1379 ctx.scale(1, Math.sqrt(3)/2);
1380 ctx.translate(-0.5, 0);
1381
1382 if (document.getElementById('grid').checked)
1383 {
1384 ctx.lineWidth = 0.02;
1385 ctx.strokeStyle = "#000";
1386 for (var i=0; i<1440; i++)
1387 drawHex(ctx, i);
1388 }
1389
1390 var hasData = false;
1391 if (max>=min)
1392 for (var i=0; i<1440; i++)
1393 hasData |= fillHex(ctx, i, data[i], min, max);
1394
1395 // =================== Draw Pixel marker ====================
1396
1397 if (document.getElementById('marker').checked)
1398 {
1399 // Draw marker
1400 ctx.lineWidth = 0.25;
1401 ctx.strokeStyle = "#000";
1402 drawHex(ctx, pixel);
1403 }
1404
1405 // ======================= Draw Ellipse ======================
1406
1407 if (document.getElementById('image').checked)
1408 {
1409 var h = Hillas(canv.dataAbs, canv.zmin);
1410 if (h)
1411 {
1412 ctx.save();
1413
1414 ctx.translate(h.mean[0], h.mean[1]);
1415 ctx.rotate(h.phi);
1416
1417 ctx.save();
1418 ctx.scale(h.axis[0], h.axis[1]);
1419 ctx.beginPath();
1420 ctx.arc(0, 0, 1, 0, 2*Math.PI);
1421 ctx.restore();
1422
1423 ctx.lineWidth = 0.15;
1424 ctx.strokeStyle = "#555";
1425 ctx.stroke();
1426
1427 ctx.strokeStyle = "#888";
1428 ctx.beginPath();
1429 ctx.moveTo(0, -h.disp);
1430 ctx.lineTo(0, h.disp);
1431 ctx.stroke();
1432
1433 ctx.restore();
1434 }
1435 }
1436
1437 ctx.restore();
1438
1439 if (!hasData)
1440 return;
1441
1442 // ======================= Draw Legend ======================
1443
1444 var v0 = parseFloat(canv.zmin);
1445 var v1 = parseFloat(canv.zmax);
1446
1447 var diff = v1-v0;
1448
1449 var cw = canv.width;
1450 //var ch = canv.height;
1451
1452 ctx.font = "8pt Arial";
1453 ctx.textAlign = "right";
1454 ctx.textBaseline = "top";
1455
1456 for (var i=0; i<11; i++)
1457 {
1458 ctx.strokeStyle = "#"+HLStoRGB(i/10);
1459 ctx.strokeText((v0+diff*i/10).toPrecision(3), cw-5, 125-i*12);
1460 }
1461
1462 var pval = parseFloat(canv.dataAbs[pixel]).toFixed(1);
1463 var lmin = parseFloat(canv.min).toFixed(1);
1464 var lmax = parseFloat(canv.max).toFixed(1);
1465
1466 if (isNaN(pval))
1467 pval = "";
1468
1469 var mw = Math.max(ctx.measureText(lmin).width,
1470 ctx.measureText(pval).width,
1471 ctx.measureText(lmax).width);
1472
1473 ctx.textBaseline = "top";
1474 ctx.strokeStyle = "#000";
1475
1476 ctx.strokeText(lmax, 5+mw, 5+24);
1477 ctx.strokeText(pval, 5+mw, 5+12);
1478 ctx.strokeText(lmin, 5+mw, 5);
1479}
1480
1481// ===================================================================
1482
1483function Hillas(data, min)
1484{
1485 var mx = 0;
1486 var my = 0;
1487 var sz = 0;
1488
1489 var mx2 = 0;
1490 var my2 = 0;
1491 var mxy = 0;
1492
1493 var cnt = 0;
1494 for (var i=0; i<1440; i++)
1495 {
1496 if (data[i]===undefined || data[i]<min)
1497 continue;
1498
1499 sz += data[i];
1500 mx += data[i] * coord[i][0];
1501 my += data[i] * coord[i][1];
1502
1503 mx2 += data[i] * coord[i][0]*coord[i][0];
1504 my2 += data[i] * coord[i][1]*coord[i][1];
1505 mxy += data[i] * coord[i][0]*coord[i][1];
1506
1507 cnt++;
1508 }
1509
1510 if (sz==0 || cnt<3)
1511 return;
1512
1513 var xx = mx2 - mx*mx/sz;
1514 var yy = my2 - my*my/sz;
1515 var xy = mxy - mx*my/sz;
1516
1517 var d0 = yy - xx;
1518 var d1 = xy*2;
1519 var d2 = Math.sqrt(d0*d0 + d1*d1) + d0;
1520
1521 var phi = 0;
1522 var cos = 0;
1523 var sin = 1;
1524
1525 var axis1 = yy;
1526 var axis2 = xx;
1527
1528 // Correction for scale in x
1529 var f = Math.sqrt(3)/2;
1530 var ratio = xx/yy/f;
1531
1532 if (d1!=0 || d2==0)
1533 {
1534 var tand = d2==0 ? 0 : d2 / d1;
1535 var tand2 = tand*tand;
1536
1537 var s2 = tand2+1;
1538 var s = Math.sqrt(s2);
1539
1540 phi = Math.atan(tand)-Math.PI/2;
1541 cos = 1.0 /s;
1542 sin = tand/s;
1543
1544 axis1 = (tand2*yy + d2 + xx);
1545 axis2 = (tand2*xx - d2 + yy);
1546
1547 ratio = axis2/axis1;
1548
1549 axis1 /= s2;
1550 axis2 /= s2;
1551
1552 ratio *= (f + tand*tand)/(tand*tand*f + 1);
1553 }
1554
1555 var length = axis1<0 ? 0 : Math.sqrt(axis1/sz);
1556 var width = axis2<0 ? 0 : Math.sqrt(axis2/sz);
1557
1558 //var r = (cos*cos*f + sin*sin)/(sin*sin*f + cos*cos);
1559 return {
1560 "mean": [ mx/sz, my/sz ],
1561 "axis": [ width, length ],
1562 "phi": phi,
1563 "delta": [ cos, sin ],
1564 "sumw": sz,
1565 "count": cnt,
1566 "disp": 1.42/0.1111*(1-Math.sqrt(ratio)),
1567 };
1568}
Note: See TracBrowser for help on using the repository browser.