-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgls.html
344 lines (301 loc) · 11.6 KB
/
gls.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>GLS</title>
<style type="text/css">
label {
display: inline-block;
width: 4em;
text-align: right;
}
.e_grey td:nth-child(4) { color:grey }
td { padding: 0px 3px; }
.ftable tr:nth-child(n+2) {
counter-increment: rowNumber;
}
.ftable tr:nth-child(n+2) td:first-child::before {
content: counter(rowNumber);
float: right;
vertical-align: super;
font-size: smaller;
color: grey;
}
.input {
font-size: 1.2em;
font-family: Tahoma;
padding: 0.1em;
border: 1px solid #ccc;
box-sizing: border-box;
width:140px;
}
</style>
</head>
<body style="font-family: verdana; font-size: 70%">
<script type="text/javascript">
function parse() {
delim = RegExp(document.getElementById('delim').value);
// parse data file into arrays and create table preview
var n=0, txt="Parsing";
parsed = '<table class="ftable" style="width:50%; font-size: 100%">';
parsed += "<tr>";
parsed += "<td></td>";
parsed += "<td><i title='time'>t</i></td>";
parsed += "<td><i title='data value'>y</i></td>";
parsed += "<td><i title='uncertainty'>e<sub>y</i></sub></td>";
parsed += "</tr>";
t = [];
y = [];
e = [];
has_e = lines.some(l => l.split(delim)[2])
for (line of lines) {
if (line != "") {
line = line.split(delim);
t.push(parseFloat(line[0]));
y.push(parseFloat(line[1]));
has_e && e.push(parseFloat(line[2]));
parsed += "<tr><td></td><td>"+t[n]+"</td><td>" +y[n]+"</td><td>" + (has_e? e[n]: '')+ "</td></tr>";
++n;
};
};
document.getElementById("weighted").disabled = e.some(x => isNaN(x))
parsed += '</table>';
document.getElementById("uploadinfo").innerHTML = "file info<br>number of lines: " + n;
document.getElementById("output").innerHTML = parsed;
document.getElementById("demo").innerHTML = txt;
}
function loaddata() {
parse();
runGLS();
plotGLS(gls.f, gls.p);
download();
plotdata(t,y,e);
}
function runGLS() {
gls = GLS(t, y, {e_y: !document.getElementById('weighted').disabled && document.getElementById('weighted').checked && e, ofac:document.getElementById('ofac').value, fbeg:document.getElementById('fbeg').value, fend:document.getElementById('fend').value, ls:document.getElementById('ls').checked});
document.getElementById('demo').innerHTML = '<label title="best frequency"><i>f</i><sub>best</sub>:</label> '+gls.f[gls.k]+'<br><label title="best period"><i>P</i><sub>best</sub>:</label> '+1/gls.f[gls.k]+'<br><label title="maximum power"><i>p</i><sub>max</sub>:</label> '+gls.p[gls.k]+'<br><label title="number of frequencies"><i>n<sub>f</sub></i>:</label> '+gls.f.length;
}
function download() {
var k, out="";
for (k=0; k<gls.f.length; k++) {out += gls.f[k]+" "+gls.p[k]+"\n";}
document.getElementById("glsfile").href = "data:text/plain," + encodeURI(out);
}
</script>
<h3>Data</h3>
<div id="datafield" style="display: flex;">
<div id="datafield_left" style="flex: 1; min-width: 1px; display:flex; flex-direction: column;"
ondragover="dragover(event)"
ondrop="drop(event)">
<fieldset style="display:flex; flex-direction: column; flex:1"><legend>Data loading</legend>
<div>
<label>file:</label><label for="csvFileInput" class="input" style="display: inline-block; text-align: left">Choose file</label><input id="csvFileInput" type="file" style="display:none" onchange="this.previousElementSibling.textContent = this.previousElementSibling.title = this.files[0].name; handleFiles(this.files)"><button onclick="this.previousElementSibling.click()">Browse...</button><br>
<label>url:</label><input type="url" id="url" class="input" value="http://cdsarc.unistra.fr/ftp/J/A+A/552/A78/harps/hr3259_h.dat" onkeyup="if (event.key === 'Enter') {handleURL(this.value)}"><input type="submit" value="Load" onclick="handleURL(document.getElementById('url').value); "><br>
<label>delimiter:</label>
<select id="delim" onChange="parse()" title="regex for delimiter">
<option value=" +" title="multiple space" selected=true>" +"</option>
<option value=" " title="space">" "</option>
<option value="," title="comma">","</option>
<option value=";" title="semicolon">";"</option>
</select><br>
<div id="uploadinfo">
file info
</div>
</div>
<div style="border:1px solid #ccc; flex:1; overflow:auto">
<div id="output" style="max-height: 20px;">
no data
</div>
</div>
</fieldset>
</div>
<div id="dataplot" style="height:300px; width:calc(100% - 400px); resize: both; overflow: auto; direction: rtl;"></div>
</div>
<h3>Periodogram</h3>
<div style="display:flex;">
<div id="GLSinput" style="flex: 1; min-width: 1px">
<form action="javascript:
loaddata()">
<fieldset><legend>Parameters</legend>
<span title="start frequency"><label><i>f</i><sub>beg</sub>:</label><input type="float" id="fbeg" title="start frequency" placeholder="auto"></span><br>
<span title="end frequency"><label><i>f</i><sub>end</sub>:</label><input type="float" id="fend" placeholder="auto"></span><br>
<span title="osampling factor for frequency steps"><label>ofac:</label><input type="float" id="ofac" placeholder="20"></span><br>
<label>mode:</label><span title="Generalisied Lomb-Scargle"><input type="radio" id="gls" name="gls" checked>GLS</span>
<span title="Lomb-Scargle (no floating offset)"><input type="radio" id="ls" name="gls">LS</span><br>
<span title="weight"><label></label><input type="checkbox" title="weight" id="weighted" value="weights" checked onclick="errorbar_visibility()">use errors</span><br>
<button style="float: right;">Calculate</button>
</fieldset>
</form>
<fieldset><legend>Results</legend>
<p id="demo"></p>
<label>file:</label> <a download='gls.dat' href="gls.dat" id="glsfile">gls.dat</a> (Periodogram in ascii format)
</fieldset>
</div>
<div id="GLSplot" style="height: 300px; width:calc(100% - 400px); resize: both; overflow: auto; direction: rtl;">
</div>
</div>
<script>
// Optional. Show the copy icon when dragging over.
function dragover(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
};
// Get file data on drop
function drop(e) {
e.stopPropagation();
e.preventDefault();
document.getElementById("csvFileInput").previousSibling.textContent = e.dataTransfer.files[0].name
handleFiles(e.dataTransfer.files);
};
function setactive(csv) {
document.getElementById("csvFileInput").previousSibling.style.color = csv? "" : "grey"
document.getElementById("url").style.color = csv? "grey" : ""
}
var fmtaxis = {tickformat: 'g',
linecolor: 'black', mirror: true,
ticks:"inside", "mirror" : "allticks", zeroline: false}
function plotdata(x,y, e=[]) {
var trace1 = {
x: x,
y: y,
name: 'data',
error_y: {
type: 'data',
array: e,
thickness: 1.,
width: 1.5,
color: x,
},
hovertemplate: '%{text}',
text: x.map((xi,i) => i + ": " + xi + ", " + y[i] + (e.length ? " ± "+e[i] : "")),
type: 'scatter',
showlegend: false,
mode: 'markers', marker:{color: x, line:{width: 0.5}, colorscale: 'Portland'},
};
P = 1 / gls.f[gls.k];
xmin = Math.min(...x)
xmax = Math.max(...x)
N = 100 * Math.ceil((xmax-xmin)/P)
amp = Math.sqrt(gls.Ak**2 + gls.Bk**2)
phk = Math.atan2(gls.Ak, gls.Bk) / (2.*Math.PI)
T0k = xmin - phk*P
tt = [...Array(N).keys()].map(i => xmin + i*P/100)
yy = tt.map(ti => Math.sin(2*Math.PI*(ti-T0k)/P)*amp+gls.Ck)
var sinmod = {
x: tt,
y: yy,
xaxis: 'x',
yaxis: 'y',
hoverinfo: 'skip',
line: {color: 'black', width:1},
showlegend: false,
}
xx = [...Array(100).keys()].map(i => i*P/100)
var sinphase = {
x: xx,
y: xx.map(xi => Math.sin(2*Math.PI*xi/P)*amp+gls.Ck),
xaxis: 'x2',
yaxis: 'y',
hoverinfo: 'skip',
line: {color: 'black', width:1},
showlegend: false,
}
var trace2 = {
x: x.map(x => ((x-T0k)%P+P)%P),
y: y,
name: 'data',
error_y: {
type: 'data',
array: e,
thickness: 1.,
width: 1.5,
color: x
},
hovertemplate: '%{text}',
text: x.map((xi,i) => i + ": " + xi + ", " + y[i] + (e.length ? " ± "+e[i] : "")),
type: 'scatter',
mode: 'markers', marker:{color: x, line:{width: 0.5}, colorscale: 'Portland'},
xaxis: 'x2',
yaxis: 'y',
showlegend: false
}
var traces = [sinmod, trace1, trace2, sinphase]
var xc = 0.65
var layout = {
xaxis: {
domain: [0, xc],
anchor: 'y1',
title: 'Time <i>t</i>',
...fmtaxis
},
yaxis: {
domain: [0, 1],
anchor: 'x1',
...fmtaxis
},
xaxis2: {
title: 'Phase',
range: [0,P],
domain: [0.7, 1],
anchor: 'y2',
...fmtaxis
},
margin: {l: 50, r: 10, b: 40, t: 10},
hovermode: 'closest',
};
Plotly.newPlot('dataplot', traces, layout, {responsive: true});
errorbar_visibility();
};
let observer = new MutationObserver(function(mutations) {
window.dispatchEvent(new Event('resize'));
});
let childdata = document.getElementById('dataplot');
let childGLS = document.getElementById('GLSplot');
//console.log(document.getElementById('dataplot'), document.getElementById('GLSplot'))
observer.observe(childdata, {attributes: true})
observer.observe(childGLS, {attributes: true})
function errorbar_visibility() {
visible = !document.getElementById('weighted').disabled && document.getElementById('weighted').checked
var all = document.getElementsByClassName("errorbars");
for (x of all) {x.style["visibility"] = visible? "visible" : "hidden"};
document.getElementsByClassName("ftable")[0].className = "ftable" + (visible? "" : " e_grey")
}
function plotGLS(x,y) {
Plotly.newPlot('GLSplot',
[{x:x, y:y, line: {color: "#0000FFAA", width: 1},
text: x.map(fk => 'P: '+1/fk),
name: ls.checked ? 'LS' : 'GLS'},
{x:[gls.f[gls.k]], y:[gls.p[gls.k]],
name: "best",
hovertemplate: "<i>p</i><sub>max</sub>: %{y:.6f}<br>"+
"<i>f</i><sub>best</sub>: %{x}<br>" + "<i>P</i><sub>best</sub>: " + (1/gls.f[gls.k])}],
{xaxis: {title: 'Frequency <i>f</i>', ...fmtaxis},
yaxis: {title: 'Power <i>p</i>', hoverformat: '.6f',
rangemode: 'tozero', autorange: true,
...fmtaxis},
margin: {l: 50, r: 10, b: 40, t: 10},
showlegend: false,
hovermode: 'closest',
},
{responsive: true}
)
}
</script>
<script src="read-csv.js"></script>
<script src="gls.js"></script>
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<script>
// allow to pre-fill fields via url, e.g.
// http://raw.githack.com/mzechmeister/GLS/master/javascript/gls.html?fend=1&url=http://cdsarc.unistra.fr/ftp/J/A%2BA/552/A78/harps/hr3259_h.dat&run
// "run" is special keyword here and it should be the last keyword
(new URL(window.location)).searchParams.forEach((x, y) => {
console.log(x, y);
if (y=="run") {document.getElementById('url').nextSibling.click()}
else {document.getElementById(y).value = x;}}
);
</script>
</body>
<footer>
view code on <a href="https://github.com/mzechmeister/GLS/tree/master/javascript">github</a>, by M. Zechmeister 2022-10-29
</footer>
</html>