-
Notifications
You must be signed in to change notification settings - Fork 0
/
wind.js
485 lines (407 loc) · 13.7 KB
/
wind.js
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* jshint undef: true, unused: true */
/* global d3, moment, $, location, console, window */
// Generic helper functions
var mean = function(a) {
var sum = 0;
for (var i = 0; i < a.length; i++){
sum += a[i];
}
return sum/a.length;
};
var rad2deg = function(angle) {
return angle * (180 / Math.PI);
};
var deg2rad = function(angle) {
return angle * (Math.PI / 180);
};
var circ_mean = function(a) {
var sins = a.map(function(d) { return Math.sin(deg2rad(d)); });
var coss = a.map(function(d) { return Math.cos(deg2rad(d)); });
var out = rad2deg(Math.atan2(mean(sins), mean(coss)));
return out > 0 ? out : out + 360;
};
var get_5_num_summary = function(a) {
var arr = a.slice(0);
arr.sort(d3.ascending);
var out = [arr[0],
d3.quantile(arr,0.75),
d3.median(arr),
d3.quantile(arr,0.25),
arr[arr.length-1],
];
return out;
};
var summary_data = [];
var win_size = 15,
win_num = 60*3/win_size;
var compute_summaries = function() {
var out = [];
var size = data[source].length/win_num;
var starts = d3.range(0, data[source].length, size);
var get_speed = function(d) { return d.wind_speed; };
var get_dir = function(d) { return (d.wind_direction + 180) % 360; };
var filt_by_dir = function(d,i) {
return spd[i] > 2;
};
for (var i = 0; i < starts.length; i++) {
var b = data[source].slice(starts[i],starts[i]+size);
var spd = b.map(get_speed);
var out_tmp = {};
var tmp = get_5_num_summary(spd);
out_tmp.speed = tmp.slice(0);
var dir = b.map(get_dir);
var filt_dir = dir.filter(filt_by_dir);
out_tmp.all_dirs = filt_dir;
out_tmp.dir_mean = circ_mean(filt_dir);
out_tmp.timepoint = i;
out.push(out_tmp);
}
summary_data = out;
};
// This project specific helper functions
var meters_per_sec2knots = function(d) {
return d * 1.94384;
};
var parse_response = function(d) {
var myascii = [];
var out = {};
for (var i = 0; i < d.data.length; i++) {
// FIXME This time code break with daylight savings...
//var time = moment(d.stamps[i]).subtract('hours',6).format('YYYY-MM-DD HH:mm:ss');
var time = moment(d.stamps[i]).subtract(5, 'hours').format('YYYY-MM-DD HH:mm:ss');
out = {stamp: format.parse(time)};
for (var j = 0; j < d.symbols.length; j++) {
out[d.symbols[j]] = +d.data[i][j];
// Fix the column names FIXME: Delete old columns
if (out['WIND_DIRECTION_2.0'] !== undefined) {
out['wind_direction'] = out['WIND_DIRECTION_2.0'];
out['wind_speed'] = out['WIND_SPEED_2.0'];
}
out.wind_speed = meters_per_sec2knots(out.wind_speed);
}
myascii.push(out);
}
return myascii;
};
// Global plot variables
var padding = 40,
width = 1100,
height = 450;
var margins = [80, 80, 10, 10],
mb = margins[0],
ml = margins[1],
mt = margins[2],
mr = margins[3],
w = width - (ml + mr),
h = height - (mb + mt);
var time_scale = d3.time.scale()
.range([0, w]);
var speed_scale = d3.scale.linear()
.domain([0,30])
.rangeRound([h, 0]);
var summary_x = d3.scale.linear()
.domain([0,win_num])
.range([0, w]);
var timeAxis = d3.svg.axis().scale(time_scale).orient("bottom").ticks(6);
var yAxisSpeed = d3.svg.axis().scale(speed_scale).orient("left");
var yAxisGrid = d3.svg.axis().scale(speed_scale).orient("left");
var format = d3.time.format("%Y-%m-%d %X");
var update_timescale = function () {
time_scale.domain(d3.extent(data[source].map(function(d) { return d.stamp;})));
console.log(time_scale.domain());
d3.select('.x.axis').transition().call(timeAxis);
};
// Summary plot specific code
var draw_summary = function() {
width = $('#container').width();
height = $(window).height()*0.7;
w = width - (ml + mr);
h = height - (mb + mt);
speed_scale.rangeRound([h,0]);
summary_x.range([0, w]);
time_scale.range([0, w]);
var windchart = d3.select("#wind-summary")
.append("svg:svg")
.attr('id','wind-summary')
.attr("width", width)
.attr("height", height+padding);
var plot = windchart.append('g')
.attr('id','plot')
.attr('transform','translate('+ml+','+mt+')');
plot.append('svg:rect')
.attr('x',0)
.attr('y',0)
.attr('width',w)
.attr('height',h)
.attr('class','plot-bg');
// Add y-axis grid lines
var y_grid_speed = plot.append("g")
.attr("class", "y grid");
y_grid_speed.call(yAxisGrid.tickSize(-w));
// Add the plot-group, which will be populated asynchronously when
// the data is fetched
plot.append('g')
.attr('class','plot-group');
// Add y-axis
var y_axis_speed = plot.append("g")
.attr("class", "y axis");
y_axis_speed.append('svg:text')
.text('Wind')
.attr('transform','translate(-50,'+speed_scale(17)+')');
y_axis_speed.append('svg:text')
.text('Speed')
.attr('transform','translate(-50,'+speed_scale(15)+')');
y_axis_speed.append('svg:text')
.text('(knots)')
.attr('transform','translate(-50,'+speed_scale(13)+')');
y_axis_speed.append('svg:text')
.text('Wind')
.attr('transform','translate(-50,'+(h+50)+')');
y_axis_speed.append('svg:text')
.text('Dir')
.attr('transform','translate(-50,'+(h+70)+')');
y_axis_speed.call(yAxisSpeed);
var time_axis = plot.append("g")
.attr("class", "x axis")
.attr('transform','translate(0,'+h+')');
time_axis.append('svg:text')
.text('Last 3 Hours')
.attr('transform','translate('+(w/2)+',100)');
time_axis.call(timeAxis);
};
var pad_x = function(d,i) {
if (i === 0) {
return summary_x(0);
} else if (i === (summary_data.length-1)) {
return summary_x(i+1);
} else {
return summary_x(i+0.5);
}
};
var speed_mean = d3.svg.line()
.interpolate('monotone')
.x(pad_x)
.y(function(d) { return speed_scale(d.speed[2]); });
var speed_extremes = d3.svg.area()
.interpolate('monotone')
.x(pad_x)
.y0(function(d) { return speed_scale(d.speed[0]); })
.y1(function(d) { return speed_scale(d.speed[4]); });
var speed_quartiles = d3.svg.area()
.interpolate('monotone')
.x(pad_x)
.y0(function(d) { return speed_scale(d.speed[1]); })
.y1(function(d) { return speed_scale(d.speed[3]); });
var add_summary_ribbons = function() {
var ribbon_group = d3.select('#wind-summary g.plot-group')
.append('svg:g')
.attr('class','ribbon-group');
ribbon_group.append('svg:path')
.style('fill','#deebf7')
.style('opacity','.6')
.attr('id','speed-extremes')
.attr('d', speed_extremes(summary_data));
ribbon_group.append('svg:path')
.style('fill','#9ecae1')
.style('opacity','.6')
.attr('id','speed-quartiles')
.attr('d', speed_quartiles(summary_data));
ribbon_group.append('svg:path')
.style('stroke','#3182bd')
.style('opacity','.6')
.attr('id','speed-mean')
.style('stroke-width','2px')
.attr('d', speed_mean(summary_data));
};
var add_overlapping_dir_arrows = function() {
var arrow_groups = d3.select('#wind-summary g.plot-group').selectAll('.arrow-group')
.data(summary_data, function(d) { return d.timepoint; })
.enter().append('svg:g')
.attr('class','arrow-group')
.attr('transform',function(d,i) {
return 'translate('+summary_x(i+0.5)+','+(h+54)+')';
});
arrow_groups.selectAll('line.arrows')
.data(function(d) {
return d.all_dirs;
})
.enter().append('svg:line')
.attr('class','arrows')
.attr('x1',0)
.attr('x2',function(d) { return 25*Math.sin(deg2rad(d)); })
.attr('y1',0)
.attr('y2',function(d) { return -25*Math.cos(deg2rad(d)); });
// Mean direction line
arrow_groups.append('svg:line')
.style('stroke','#31a354')
.style('stroke-width','3px')
.attr('class','dir-mean-line')
.attr('x1',0)
.attr('x2',function(d) { return 25*Math.sin(deg2rad(d.dir_mean)); })
.attr('y1',0)
.attr('y2',function(d) { return -25*Math.cos(deg2rad(d.dir_mean)); });
arrow_groups.append('svg:line')
.style('stroke','#31a354')
.style('stroke-width','3px')
.attr('class','dir-arrow-head1')
.attr('x1',function(d) { return 18*Math.sin(deg2rad(d.dir_mean-20)); })
.attr('x2',function(d) { return 25*Math.sin(deg2rad(d.dir_mean)); })
.attr('y1',function(d) { return -18*Math.cos(deg2rad(d.dir_mean-20)); })
.attr('y2',function(d) { return -25*Math.cos(deg2rad(d.dir_mean)); });
arrow_groups.append('svg:line')
.style('stroke','#31a354')
.style('stroke-width','3px')
.attr('class','dir-arrow-head2')
.attr('x1',function(d) { return 18*Math.sin(deg2rad(d.dir_mean+20)); })
.attr('x2',function(d) { return 25*Math.sin(deg2rad(d.dir_mean)); })
.attr('y1',function(d) { return -18*Math.cos(deg2rad(d.dir_mean+20)); })
.attr('y2',function(d) { return -25*Math.cos(deg2rad(d.dir_mean)); });
};
// Draw the initial plot frame
draw_summary();
var never_drawn = true;
// Initial callback on data pull
var draw_plots = function() {
compute_summaries();
if (never_drawn) {
update_timescale();
add_summary_ribbons();
add_overlapping_dir_arrows();
never_drawn = false;
} else {
update_summary_ribbons();
update_overlapping_dir_arrows();
}
$('.loading').hide();
};
var update_summary_ribbons = function() {
d3.select('#speed-extremes').transition()
.attr('d', speed_extremes(summary_data));
d3.select('#speed-quartiles').transition()
.attr('d', speed_quartiles(summary_data));
d3.select('#speed-mean').transition()
.attr('d', speed_mean(summary_data));
};
var update_overlapping_dir_arrows = function() {
var arrow_groups = d3.select('#wind-summary g.plot-group').selectAll('.arrow-group')
.data(summary_data, function(d) { return d.timepoint; });
arrow_groups.selectAll('line.arrows')
.data(function(d) {
return d.all_dirs;
})
.transition()
.attr('x2',function(d) { return 25*Math.sin(deg2rad(d)); })
.attr('y2',function(d) { return -25*Math.cos(deg2rad(d)); });
// Mean direction line
arrow_groups.select('.dir-mean-line')
.transition()
.attr('x2',function(d) { return 25*Math.sin(deg2rad(d.dir_mean)); })
.attr('y2',function(d) { return -25*Math.cos(deg2rad(d.dir_mean)); });
arrow_groups.select('.dir-arrow-head1')
.transition()
.attr('x1',function(d) { return 18*Math.sin(deg2rad(d.dir_mean-20)); })
.attr('x2',function(d) { return 25*Math.sin(deg2rad(d.dir_mean)); })
.attr('y1',function(d) { return -18*Math.cos(deg2rad(d.dir_mean-20)); })
.attr('y2',function(d) { return -25*Math.cos(deg2rad(d.dir_mean)); });
arrow_groups.select('.dir-arrow-head2')
.transition()
.attr('x1',function(d) { return 18*Math.sin(deg2rad(d.dir_mean+20)); })
.attr('x2',function(d) { return 25*Math.sin(deg2rad(d.dir_mean)); })
.attr('y1',function(d) { return -18*Math.cos(deg2rad(d.dir_mean+20)); })
.attr('y2',function(d) { return -25*Math.cos(deg2rad(d.dir_mean)); });
};
var pull_local = function() {
d3.csv('ascii.txt', function(d) {
return {
stamp: format.parse(d['YYYY-MM-DD'] + ' ' + d['hh:mm:ss']),
//air_temp: +d.air_temp,
wind_direction: +d.wind_direction,
wind_speed: +d.wind_speed,
//pressure: +d.pressure
};
}, function(error, rows) {
data[source] = rows;
draw_plots();
});
};
var begin_time, end_time;
var pull_last_3_hours = function() {
// Only get the last 3 hours once, reused this time range for data source changes
if (begin_time === undefined) {
begin_time = moment().utc().subtract(3, 'hours').format('YYYY-MM-DD%20HH:mm:ss');
end_time = moment().utc().format('YYYY-MM-DD%20HH:mm:ss');
}
pull_data(source,begin_time, end_time);
};
var data = {'mendota/buoy':[],'rig/tower':[]};
var source = 'mendota/buoy';
var failcount = 0;
// May also use: http://whateverorigin.org/
var pull_data = function(which,begin_time, end_time) {
$('.loading').show();
var url = 'http://metobs.ssec.wisc.edu/app/'+which+'/data/jsonp?';
var begin = 'begin='+begin_time;
var end = '&end='+end_time;
var symbols = '&symbols=dir:spd:&separator=,&interval=00:00:05';
var callback = '&callback=?';
var full_url = url+begin+end+symbols+callback;
$.getJSON(full_url)
.done(function(d) {
var ascii = parse_response(d);
data[which] = ascii.slice();
draw_plots();
})
.fail(function() {
failcount++;
if (failcount < 3) {
console.log('Data fetch failed, trying again');
pull_data();
} else {
// FIXME: Generate pop-up to warn user that multiple fetches failed
console.log('Data fetch failed, giving up');
}
});
};
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] === variable){return pair[1];}
}
return(null);
}
if (location.hash === "#debug") {
console.log('Using local data...');
pull_local();
} else {
var start = getQueryVariable('start');
var end = getQueryVariable('end');
if ((start !== null) && (end !== null)) {
console.log('Pull historical data...');
var begin_time = moment(start, 'YYYY-MM-DD%20HH:mm:ss');
var end_time = moment(end, 'YYYY-MM-DD%20HH:mm:ss');
begin_time = begin_time.utc().format('YYYY-MM-DD%20HH:mm:ss');
end_time = end_time.utc().format('YYYY-MM-DD%20HH:mm:ss');
pull_data('mendota/buoy',begin_time, end_time);
} else {
console.log('Pull last 3 hours...');
pull_last_3_hours();
}
}
$('#buoyLabel').on('click',function() {
source = 'mendota/buoy';
switch_data();
});
$('#towerLabel').on('click',function() {
source = 'rig/tower';
switch_data();
});
var switch_data = function() {
if (data[source].length === 0) {
pull_last_3_hours();
} else {
draw_plots();
}
};