forked from bolidozor/js9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js9onchange.html
executable file
·322 lines (322 loc) · 11.3 KB
/
js9onchange.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge;chrome=1" >
<link type="image/x-icon" rel="shortcut icon" href="./favicon.ico">
<link type="text/css" rel="stylesheet" href="js9support.css">
<link type="text/css" rel="stylesheet" href="js9.css">
<script type="text/javascript" src="js9support.min.js"></script>
<script type="text/javascript" src="js9.min.js"></script>
<script type="text/javascript" src="js9plugins.js"></script>
<style type="text/css">
#myAnalysisDiv {
background: #E9E9E9;
border-width: 0px;
width: 440px;
padding: 10px;
overflow: auto;
}
#myAnalysisResults {
height: 400px;
}
#myButtonsDiv {
background: lightgrey;
border-width: 0px;
padding-left: 10px;
padding-right: 10px;
overflow: auto;
}
</style>
<title>JS9 onchange</title>
</head>
<body onload="initMyAnalysis()">
<script type="text/javascript">
var aname, im;
var lastim, lastreg;
var ncall = 0;
// this is the callback for all region changes
JS9.Regions.opts.onchange = "runMyAnalysis";
// called when the function changes to redo the last display
function redo(){
if( lastim && lastreg ){
runMyAnalysis(lastim, lastreg);
}
}
// add event handlers to the analysis buttons to execute the specified task
// note that jQuery is available automatically
function initMyAnalysis(){
$("#counts").on("click", function(evt){
aname = "counts"; redo();
});
$("#enplot").on("click", function(evt){
aname = "enplot"; redo();
});
$("#modify").on("click", function(evt){
aname = "modify"; redo();
});
$("#disp").on("click", function(evt){
aname = "disp"; redo();
});
$("#dispAll").on("click", function(evt){
aname = "dispAll"; redo();
});
}
// display analysis arguments
function dispMyRegions(im, reg){
var i, xreg, oxeq, wcs;
var t="";
var rarr=[];
var rdiv = $("#myAnalysisResults")[0];
// if reg is an array (i.e. contains all regions for this image),
// use it as the array to display
if( Object.prototype.toString.call(reg) === '[object Array]' ) {
rarr = reg;
} else {
// for a single region, construct an array
rarr.push(reg);
}
t = sprintf("image: %s size=%s,%s bitpix=%s<br>",
im.file, im.raw.width, im.raw.height, im.raw.bitpix);
// process all regions in the array
for(i=0; i<rarr.length; i++){
xreg = rarr[i];
t += sprintf("region: mode=%s shape=%s id=%s",
xreg.mode, xreg.shape, xreg.id);
t += sprintf("\n tag(s)=%s color=%s ",
xreg.tags.filter(function(n){return n;}).join(","),
xreg.color);
t += sprintf("\n impos=%.2f,%.2f physpos=%.2f,%.2f",
xreg.x, xreg.y, xreg.lcs.x, xreg.lcs.y);
wcs = JS9.PixToWCS(xreg.x, xreg.y, {display: im});
if( wcs ){
t += sprintf("\n wcs=%s", wcs.str.trim());
}
switch(xreg.shape){
case "annulus":
t += sprintf("\n radii=%s", xreg.radii);
break;
case "box":
t += sprintf("\n size=%.2f,%.2f angle=%.2f",
xreg.width, xreg.height, xreg.angle);
break;
case "circle":
t += sprintf("\n radius=%.2f", xreg.radius);
break;
case "ellipse":
t += sprintf("\n eradius=%.2f,%.2f angle=%.2f",
xreg.r1, xreg.r2, xreg.angle);
break;
case "point":
break;
case "polygon":
t += sprintf("\n pts=");
for(j=0; j<xreg.pts.length; j++){
t += sprintf("%.2f,%.2f ", xreg.pts[j].x, xreg.pts[j].y);
}
break;
case "text":
t += sprintf("\n text=%s", xreg.text);
break;
}
t += "\n";
}
// display all regions
rdiv.innerHTML = "<pre>" + t + "<\/pre>";
}
//
function modifyMyRegion(im, xreg){
var swidth, oxeq, tag, angle, color;
var swidths=[2, 4, 6, 8];
var angles=[7, 15, 30, 60, 90, 120, 180, 270, 360];
var colors=["red", "orange", "yellow", "green", "blue"];
var tags=["include,source", "exclude,source", "include,background", "exclude,background", "defcolor"];
// change region to test JS9.Regions()
tag = tags[ncall%tags.length];
angle = angles[ncall%angles.length];
color = colors[ncall%colors.length];
// temporarily disable onchange callback, since we are going to change
// the region and we don't want an infinite loop!
switch(xreg.shape){
case "annulus":
JS9.ChangeRegions(xreg.id,
{x: xreg.x + 5, y: xreg.y + 5},
{display: im});
break;
case "box":
JS9.ChangeRegions(xreg.id,
{color: color, angle: angle},
{display: im});
break;
case "circle":
JS9.ChangeRegions(xreg.id,
{radius: xreg.radius + 2, tag: tag},
{display: im});
break;
case "ellipse":
JS9.ChangeRegions(xreg.id,
{r1: xreg.r1 + 4, r2: xreg.r2 + 2, angle: angle},
{display: im});
break;
case "polygon":
JS9.ChangeRegions(xreg.id,
{angle: angle},
{display: im});
break;
}
dispMyRegions(im, xreg);
ncall += 1;
}
// run analysis on current image, defining a function to display results
function runMyAnalysis(im, xreg){
var rarr = [];
lastim = im;
lastreg = xreg;
if( aname ){
switch(aname){
case "counts":
JS9.RunAnalysis("counts", null, dispMyAnalysis);
break;
case "enplot":
JS9.RunAnalysis("energyplot", null, dispMyAnalysis);
break;
case "modify":
modifyMyRegion(im, xreg);
break;
case "disp":
dispMyRegions(im, xreg);
break;
case "dispAll":
rarr = JS9.GetRegions();
dispMyRegions(im, rarr);
break;
default:
JS9.error("unknown analysis task: "+aname);
break;
}
}
}
function runMyAnalysis2(im, xreg){
runMyAnalysis(im, xreg);
}
// display function passed to JS9.RunAnalysis
// when the analysis task is complete, this callback displays results
function dispMyAnalysis(stdout, stderr, errcode, a){
var rdiv = $("#myAnalysisResults")[0];
if( !rdiv ){
alert("the analysis results area is missing on this web page");
}
else if( stderr ){
alert(stderr);
} else {
switch(a.rtype){
case "text":
// text can just get shoved into the div
rdiv.innerHTML = "<pre>" + stdout + "<\/pre>";
break;
case "plot":
// plot can make use of the already-loaded flot routines
// ( and JS9.plotOpts is defined in js9-version.js)
try{
// retrieve plot object (should contain a data object)
pobj = JSON.parse(stdout);
if( pobj && pobj.data ){
// erase explanatory text
rdiv.innerHTML = "";
// plot the data
$.plot(rdiv, [pobj], JS9.plotOpts);
} else {
alert("no analysis data returned");
}
}
catch(e){ alert("can't plot data: "+e+" "+stdout)};
break;
}
}
}
</script>
<center><font size="+1">
<b>JS9 Demo: running tasks in response to region changes</b>
</font></center>
<table cellspacing="20">
<tr valign="top">
<td>
PNG files (already converted from FITS):
<ul>
<li> <a href='javascript:JS9.Load("png/casa.png");'>CAS-A (Chandra)</a>
<li> <a href='javascript:JS9.Load("png/snr.png", {colormap:"heat"});'>CTB 109 (Einstein)</a>
<li> <a href='javascript:JS9.Load("png/m13.png", {scale:"linear", colormap:"sls"});'>m13 (via SkyView)</a>
<li> <a href='javascript:JS9.Load("png/i1000.png", {scale:"linear", colormap:"grey"});'>i1000 (v=x*1000+y)</a>
</ul>
</td>
<td>
FITS images and binary tables:
<ul>
<li> <a href='javascript:JS9.Load("fits/3c58.fits");'>3c58 (Chandra)</a>
<li> <a href='javascript:JS9.Load("fits/3c273.fits");'>3c273 (Chandra)</a>
<li> <a href='javascript:JS9.Load("fits/ngkper.fits");'>gkper (Chandra)</a>
<li> <a href='javascript:JS9.Load("fits/ngc1316.fits", {scale:"linear"});'>ngc1316 (AIPS)</a>
</ul>
</td>
</tr>
<tr valign="top">
<td>
<div class="JS9Menubar"></div>
<div class="JS9"></div>
</td>
<td>
<form autocomplete="off" action="">
<div id="myButtonsDiv">
<div id="myButtonsBox">
<input type="radio" name="onchange" id="disp">Display a Region
<input type="radio" name="onchange" id="dispAll">Display all Regions
<input type="radio" name="onchange" id="modify">Modify a Region
<br>
<input type="radio" name="onchange" id="enplot">Energy Spectrum
<input type="radio" name="onchange" id="counts">Counts in Regions
</div>
</div>
</form>
<div id="myAnalysisDiv">
<div id="myAnalysisResults">
JS9 can perform local and remote tasks in response to region changes:
<ul>
<li> choose a task by clicking one of the analysis buttons above
<ul>
<li> the region tasks are local (browser-based) tasks
<li> spectrum, counts are remote tasks (requiring the back-end helper to be running)
</ul>
<li> display an image by clicking on one of the data files
<li> create a region using the JS9 Region menu
<li> as you move/resize the region, the task will run on that region
<li> you can suspend task execution by unselecting xeqOnChange in the JS9 Region menu
</ul>
See the page source for implementation details.
</div>
</div>
</td>
</tr>
</table>
<p>
JS9 Demos:
<ul>
<li><a href='js9basics.html'>JS9 Demo: the basics</a>
<li><a href='js9bespoke.html'>JS9 Demo: web page control of JS9</a>
<li><a href='js9sizes.html'>JS9 Demo: setting the size of the JS9 display</a>
<li><a href='js9plugins.html'>JS9 Demo: adding plugins to JS9</a>
<li><a href='js9imexam.html'>JS9 Demo: the imexam plugin for JS9</a>
<li><a href='js9blend.html'>JS9 Demo: image blending</a>
<li><a href='js9cat.html'>JS9 Demo: overlaying catalogs</a>
<li><a href='js9panzoom.html'>JS9 Demo: pan and zoom</a>
<li><a href='js9rgb.html'>JS9 Demo: RGB composite images</a>
<li><a href='js9multi.html'>JS9 Demo: independent instances of JS9</a>
<li><a href='js9analysis.html'>JS9 Demo: remote data analysis</a>
<li><a href='js9onchange.html'>JS9 Demo: running tasks when a region changes</a>
<li><a href='js9create.html'>JS9 Demo: creating a JS9 instance dynamically</a>
<li><a href='js9preload.html'>JS9 Demo: preloading images into JS9</a>
<li><a href='js9bitpix.html'>JS9 Demo: displaying different FITS datatypes</a>
<li><a href='js9pngvsfits.html'>JS9 Demo: PNG representation files vs FITS files</a>
</ul>
</body>
</html>