-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
455 lines (424 loc) · 13.4 KB
/
index.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/fetch/0.10.1/fetch.min.js"></script>
<link rel="icon" href="http://spiddal.marine.ie/favicon.ico">
<title>Anispectrogram: D3.js Animated Spectrogram Example</title>
<style>
body {
position: relative;
}
svg,
canvas {
position: absolute;
display: inline;
}
.axis text {
font: 10px sans-serif;
stroke: #FFF;
shape-rendering: crispEdges;
}
.axis path,
.axis line {
fill: none;
stroke: #FFF;
shape-rendering: crispEdges;
}
.axis path {
display: none;
}
</style>
<script>
// see http://bl.ocks.org/mbostock/3074470
var rgb_cache = {};
var width = 1000,
height = 491;
var width_rows = 1000;
var drps = 100;
var updated_time = 0;
function spectrogram(){
if(data_rows.length < width_rows) return;
var dy = data_rows[0].data.length-2;
var dx = width_rows;
var first_seq_no = data_rows[0].seq_no;
var offset = start_seq_no - first_seq_no;
if(offset >= data_rows.length){
return;
}
var now = new Date().getTime();
var shift_lines = Math.min(Math.ceil((now - updated_time)*drps/1000),Math.ceil(dx/5));
if(offset < 0 || start_seq_no < 0){
offset = 0;
start_seq_no = first_seq_no;
}else{
start_seq_no = start_seq_no+shift_lines;
}
var limit = offset + shift_lines + shift_lines;
if(limit >= data_rows.length){
return;
}
var x = d3.scale.linear()
.domain([0, dx])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, dy])
.range([height, 0]);
var color = d3.scale.linear()
.domain([10,20,30,40,50])
.range(["#0000ff","#33cc33","#ffff00","#ff9933", "#cc3300"]);
var yAxis = d3.svg.axis()
.ticks(20)
.scale(y)
.orient("right")
.tickFormat(function(d, i){
var hz = parseInt(data_rows[0].headers[d]);
return isNaN(hz)?"":hz>1000?(""+(hz/1000)+"kHz"):(""+hz+" Hz");
return ""+data_rows[0].headers[d]+" Hz";
});
var xAxis = d3.svg.axis()
.scale(x)
.orient("top")
.tickFormat(function(d, i){
var k = limit - dx + d;
return k >= 0?data_rows[k].data[1]:"";
});
var hk = Math.max(limit-dx,0);
document.location.hash = data_rows[hk].hash;
/*
d3.select("#anispectrogram")
.attr("width", dx)
.attr("height", dy)
.style("width", width + "px")
.style("height", height + "px");
//.call(drawImage);
*/
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
d3.select("#xaxis")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.call(removeZero);
d3.select("#yaxis")
.attr("class", "y axis")
.call(yAxis)
.call(removeZero);
drawImage(acontext);
updated_time = now;
for(var i=0; i<offset && data_rows.length>max_buffer;i++){
data_rows.shift();
}
function drawImage(context) {
var image = null, orig = null;
var shiftx = shift_lines;
var orig = null;
if(shiftx<dx){
orig = context.getImageData(shiftx,0,dx-shiftx,dy);
}
var image = context.getImageData(dx-shiftx,0,shiftx,dy);
var c = null;
var xstart = shiftx > 0?limit-shiftx:offset;
for (var y = dy-1, p = -1; y > 0 ; y--) {
for (var x = xstart; x < limit; x++) {
var k = data_rows[x].data[y+2];
if(k in rgb_cache){
c = rgb_cache[k];
}else{
c = d3.rgb(color(k));
rgb_cache[k] = c;
}
image.data[++p] = c.r;
image.data[++p] = c.g;
image.data[++p] = c.b;
image.data[++p] = 255;
}
}
if(orig != null){
context.putImageData(orig, 0, 0);
}
context.putImageData(image, dx-shiftx, 0);
}
// Compute the pixel colors; scaled by CSS.
function drawImage_orig(canvas) {
var context = canvas.node().getContext("2d");
var image = context.getImageData(0,0,dx,dy);
var newImageData = false;
if(image == null || image.length != dx*dy){
image = context.createImageData(dx, dy);
newImageData = true;
}
var c = null;
for (var y = 0, p = -1; y < dy; ++y) {
for (var x = 0; x < dx; ++x) {
var k = data[x][y+2];
if(k in rgb_cache){
c = rgb_cache[k];
}else{
c = d3.rgb(color(k));
rgb_cache[k] = c;
}
image.data[++p] = c.r;
image.data[++p] = c.g;
image.data[++p] = c.b;
image.data[++p] = 255;
}
}
if(newImageData){
context.putImageData(image, 0, 0);
}
}
function removeZero(axis) {
axis.selectAll("g").filter(function(d) { return !d; }).remove();
}
}
</script>
<script>
var fft_seq_no = 0;
function getFFTData(fetch_obj,t){
var text = t.split('\n'), l = "";
var start_date = "";
while(text.length > 0 ){
l = text.shift();
if(l.match("^Start Date")){
start_date = l.split(/\s+/)[2];
}
if( l.match("^Data:.*$")){
break;
}
}
var headers = null;
for(var i=0;i<text.length;i++){
var a = text[i].split(/\t/);
if(i>0 && a.length < headers.length){
break;
}
var time = a[0];
a = a.slice(6);
for(var j=0; j<a.length;j++){
a[j] = parseInt(a[j]);
}
a.unshift(time);
a.unshift(start_date);
if(i == 0){
headers = a;
}else{
data_rows.push({ seq_no: fft_seq_no++, row: i, headers: headers, data: a, url: fetch_obj.url, hash: fetch_obj.hash});
}
}
}
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response
} else {
var error = new Error(response.statusText)
error.response = response
throw error
}
}
var data_rows = [];
var start_seq_no = -1;
var urls = [];
var getSelectedText = function(elementId){
var e = document.getElementById(elementId);
if (e.selectedIndex == -1) return null;
return e.options[e.selectedIndex].text;
}
var getSelectedValue = function(elementId){
var e = document.getElementById(elementId);
if (e.selectedIndex == -1) return null;
return e.options[e.selectedIndex].value;
}
var set_time_urls = function(){
pause();
data_rows = [];
start_seq_no = -1;
var e = document.getElementById("time");
if (e.selectedIndex == -1) return;
var hash_day = '#'+getSelectedText('year')
+ getSelectedText('month')
+ getSelectedText('day');
document.location.hash = hash_day + getSelectedText('time');
var time = e.options[e.selectedIndex].text;
var nurls = [];
for(var i=e.selectedIndex; i<e.options.length;i++){
nurls.push({hash: hash_day+e.options[i].text, url:e.options[i].value});
}
urls = nurls;
resume();
}
var max_buffer = 100000;
var update_freq = 100;
var paused = false;
var resume = function(){
paused = false;
document.getElementById('pause_resume').value = "pause";
}
var pause = function(){
paused = true;
document.getElementById('pause_resume').value = "resume";
}
var pause_resume = function(){
paused?resume():pause();
}
var data_url = "http://spiddal.marine.ie/data/hydrophones/SBF1622/";
var update_spectrogram = function(){
try{
if(!paused){
spectrogram();
}
}finally{
setTimeout(update_spectrogram,update_freq);
}
};
var fetch_hydrophone_data_next = function(){
if(data_rows.length >= max_buffer){
setTimeout(fetch_hydrophone_data_next,1000);
return;
}
var fetch_obj = urls.shift();
if(fetch_obj){
fetch_hydrophone_data(fetch_obj);
}else{
setTimeout(fetch_hydrophone_data_next,1000);
}
};
var fetch_hydrophone_data = function(fetch_obj){
fetch(fetch_obj.url)
.then(checkStatus)
.then(function(response) {
return response.text();
}).then(function(t) {
getFFTData(fetch_obj,t);
setTimeout(fetch_hydrophone_data_next,500);
}).catch(function(error) {
console.log('request failed', error);
fetch_hydrophone_data_next();
})
};
var get_links=function(url,pattern,callback){
fetch(url)
.then(checkStatus)
.then(function(response){
return response.text();
}).then(function(text){
var d = document.createElement("div");
d.innerHTML = text;
wanted = [];
var a = d.querySelectorAll('a');
for(var i=0;i<a.length;i++){
var href = a[i].getAttribute("href");
if(href.match(pattern)){
wanted.push(href);
}
}
callback(wanted);
}).catch(function(error) {
console.log('request failed', error);
});
};
var a_to_options = function(base_url,a,hash_pattern,default_selected_index,transform){
var options = [];
var selected = default_selected_index;
if(document.location.hash.match(hash_pattern)){
var wanted = document.location.hash.match(hash_pattern)[1];
for(var i=0;i<a.length;i++){
var text = transform(a[i]);
if(text == wanted){
selected = i;
break;
}
if(text > wanted){
selected = i>0?i-1:0;
break;
}
}
}
for(var i=0;i<a.length;i++){
options.push("<option value='"+base_url+a[i]+"' ");
var text = transform(a[i]);
if(i == selected){
options.push("selected='selected'");
}
options.push(">"+text+"</option>");
}
return options.join("");
}
var set_year_options = function(callback){
get_links(data_url,/^\d{4}\//, function(a){
var pattern = /#(20[0-9][0-9]\/)[01][0-9]\/[0-3][0-9]\/[0-2][0-9]:[0-5][0-9]/;
var options = a_to_options(data_url,a,pattern,a.length-1,function(t){return t;});
document.getElementById("year").innerHTML = options;
set_month_options(callback);
});
}
var set_month_options = function(callback){
var e = document.getElementById("year");
var yurl = e.options[e.selectedIndex].value;
get_links(yurl,/^\d{2}\//, function(a){
var pattern = /#20[0-9][0-9]\/([01][0-9]\/)[0-3][0-9]\/[0-2][0-9]:[0-5][0-9]/;
var options = a_to_options(yurl,a,pattern,a.length-1, function(t){return t;});
document.getElementById("month").innerHTML = options;
set_day_options(callback);
});
}
var set_day_options = function(callback){
var e = document.getElementById("month");
var murl = e.options[e.selectedIndex].value;
get_links(murl,/^\d{2}\//, function(a){
var pattern = /#20[0-9][0-9]\/[01][0-9]\/([0-3][0-9]\/)[0-2][0-9]:[0-5][0-9]/;
var options = a_to_options(murl,a,pattern,a.length-1, function(t){return t;});
document.getElementById("day").innerHTML = options;
set_time_options(callback);
});
}
var set_time_options = function(callback){
var e = document.getElementById("day");
var durl = e.options[e.selectedIndex].value;
get_links(durl,/_\d{8}_\d{6}.txt/, function(a){
var regex = /.*_(\d\d)(\d\d)[^_]*$/;
var pattern = /#20[0-9][0-9]\/[01][0-9]\/[0-3][0-9]\/([0-2][0-9]:[0-5][0-9])/;
var options = a_to_options(durl,a,pattern,0,function(t){
return t.replace(regex,"$1:$2");
});
document.getElementById("time").innerHTML = options;
if(callback){
callback();
}
});
}
</script>
</head>
<body>
<h1>D3.js Animated Spectrogram Example</h1>
<p>Animated spectrogram visualisation of <a href="http://oceansonics.com/iclisten-smart-hydrophones/">hydrophone</a>
data from the <a href="http://spiddal.marine.ie/data.html">Irish Marine Institute</a> implemented in <a href="http://d3js.org">d3.js</a>.</p>
<p>This is a preliminary implementation, for which the <a href="https://github.com/fullergalway/anispectrogram">full source</a>
is available on github, where merge requests are welcome.</p>
<p>Anispectrogram is based on <a href="http://bl.ocks.org/mbostock/3074470">mbostock's heatmap example</a>
and is released under the <a href="http://opensource.org/licenses/BSD-3-Clause">BSD license</a>.
Copyright 2015 <a href="https://ie.linkedin.com/in/robertfuller">Rob Fuller</a> and <a href="http://www.marine.ie/">Irish Marine Institute</a></p>
<p>
<select id="year" onchange="set_month_options()" name="year"></select>
<select id="month" onchange="set_day_options()" name="month"></select>
<select id="day" onchange="set_time_options()" name="day"></select>
<select id="time" name="time"></select>
<input type="button" onclick="set_time_urls()" name="go" value="go">
<input type="button" onclick="pause_resume()" name="pause_resume" id="pause_resume" value="pause">
slow<input type="range" name="speed" onchange="drps=this.value*this.value;" value="10" min="1" max="100">fast
</p>
<canvas width=1000 height=491 id="anispectrogram" style="width: 950px; height: 591px"></canvas>
<svg><g id="xaxis"></g><g id="yaxis"></g></svg>
<script>
var canvas = document.getElementById("anispectrogram");
var acontext = canvas.getContext("2d");
acontext.createImageData(width,height);
set_year_options(set_time_urls);
fetch_hydrophone_data_next();
setTimeout(update_spectrogram,100);
</script>
<a href="https://github.com/fullergalway/anispectrogram"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/e7bbb0521b397edbd5fe43e7f760759336b5e05f/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677265656e5f3030373230302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png"></a>
</body>
</html>