-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_retrieval.html
500 lines (453 loc) · 26.8 KB
/
data_retrieval.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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HiSPARC - jSparc - Data retrieval</title>
<link rel="shortcut icon" href="images/favicon.ico">
<link rel="stylesheet" href="styles/common.css">
<link rel="stylesheet" href="styles/data_retrieval.css">
<script src="scripts/jquery-3.7.1.min.js"></script>
<script src="scripts/jquery.flot.js"></script>
<script src="scripts/jquery.flot.time.js"></script>
<script src="scripts/jquery.flot.log.js"></script>
<script src="scripts/jquery.flot.canvas.js"></script>
<script src="scripts/jquery.flot.axislabels.js"></script>
<script src="scripts/regression.js"></script>
<script src="jquery.jsparc.js"></script>
<!-- Add MathJax to enhance the look of the fit formulas -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
messageStyle: "none",
tex2jax: {preview: "none"}});
</script>
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
"use strict";
var jsparc;
$(document).ready(function () {
// Animate HiSPARC logo during ajax activity
$(document).bind('ajaxStart', function() {
$('#pageHeader').addClass('animated');})
.bind('ajaxStop', function() {
$('#pageHeader').removeClass('animated');});
// Limit datetime fields and set initial values
var last_midnight = new Date();
last_midnight.setUTCHours(0, 0, 0, 0);
var max_choice = last_midnight.toISOString().split('.')[0];
var day_before_max = new Date(last_midnight - 86400 * 1000).toISOString().split('.')[0];
$('#startdate_choice').attr('max', max_choice);
$('#enddate_choice').attr('max', max_choice);
$('#startdate_choice').val(day_before_max);
$('#enddate_choice').val(max_choice);
// Load jSparc library
jsparc = $.jsparc();
var station_events_select = jsparc.make_station_select($('#station_events_choice'), 'events'),
station_weather_select = jsparc.make_station_select($('#station_weather_choice'), 'weather'),
station_singles_select = jsparc.make_station_select($('#station_singles_choice'), 'singles');
jsparc.set_dataset_list_controls($('#dataset_list'));
jsparc.set_flot_options([jsparc.flot_scatter()]);
// Show station list for specific data type
$('#type_choice input').change(function() {
if ($(this).val() == 'weather') {
$('#station_events_choice').hide();
$('#station_singles_choice').hide();
$('#station_weather_choice').show();}
else if ($(this).val() == 'singles') {
$('#station_singles_choice').show();
$('#station_events_choice').hide();
$('#station_weather_choice').hide();}
else if ($(this).val() == 'events') {
$('#station_events_choice').show();
$('#station_singles_choice').hide();
$('#station_weather_choice').hide();}
});
// Synchronize the two station choice lists
$('#station_choice').on('change', 'select', function() {
$('#station_choice select').val($(this).val());
});
// Recheck all radio buttons that were checked before loading new data
// This function does only work for the specific 'select data' radio buttons
function recheck_buttons(array) {
$.each(array, function(key, value){
$('#select_dataset table input:radio[name=' + key + '][value="' + value + '"]').prop("checked", true);
});
}
// Download button
$('#download_dataset').on('click', function() {
var data_type = $('#type_choice input:checked').val(),
input_array = {},
checked_input = $('#select_dataset table input:checked');
// Make sure check_input exists
if (typeof checked_input != "undefined") {
for (var i = 0; i < checked_input.length; i++) {
input_array[checked_input[i].name] = checked_input[i].value;
}
}
var download_return = jsparc.download_dataset($('#station_' + data_type + '_choice select').val(),
$('#startdate_choice').val(),
$('#enddate_choice').val(),
data_type);
// Check return type because if for example dataset is already available
// the object return is not a reader object
if (typeof download_return != "undefined") {
download_return.done(function(data) {
recheck_buttons(input_array);
$('#select_dataset').show();});}
});
// Load data button
$('#load_dataset').on('click', function() {
// Remember all checked input
// Create array to store name and value of checked input, because key should be unique we use name as key
var input_array = {};
var checked_input = $('#select_dataset table input:checked');
// Make sure check_input exists
if (typeof checked_input != "undefined") {
for (var i = 0; i < checked_input.length; i++) {
input_array[checked_input[i].name] = checked_input[i].value;
}
}
var reader_return = jsparc.load_dataset($('#file_choice')[0].files[0]);
// Check return type because if for example dataset is already available
// the object return is not a reader object
if (typeof reader_return != "undefined") {
reader_return.onloadend = function() {
recheck_buttons(input_array);
$('#select_dataset').show();};}
});
// Show plot button
$('#make_plot').on('click', function() {
var plot_type = $('#plot_type input:checked').val(),
x = $('#select_variables input[name=x-axis]:checked'),
y = $('#select_variables input[name=y-axis]:checked'),
xset = x.parents('table').attr('name'),
yset = y.parents('table').attr('name'),
xlog = $("#plot_x_axis :checked").val() !== 'none',
ylog = $("#plot_y_axis :checked").val() !== 'none',
x_min, x_max,
y_min, y_max,
data_to_plot,
i;
var x_data, y_data;
if ($('#use_x_limits').prop('checked')) {
x_min = parseFloat($("#plot_x_limits input[name=plot_x_min]").val());
x_max = parseFloat($("#plot_x_limits input[name=plot_x_max]").val());}
else {
x_min = null;
x_max = null;}
if ($('#use_y_limits').prop('checked')) {
y_min = parseFloat($("#plot_y_limits input[name=plot_y_min]").val());
y_max = parseFloat($("#plot_y_limits input[name=plot_y_max]").val());}
else {
y_min = null;
y_max = null;}
jsparc.add_flot_options(jsparc.flot_xlimits(x_min, x_max));
jsparc.add_flot_options(jsparc.flot_ylimits(y_min, y_max));
// Here the plot data is prepared, each section results in
// a multidimensional array `data_to_plot`
// [[[x, y], [x, y], ...], [set2], ...]
if (plot_type == 'histogram') {
jsparc.add_flot_options(jsparc.flot_x_axis_labels(x.data('label')));
jsparc.add_flot_options(jsparc.flot_y_axis_labels('Counts'));
x_data = jsparc.get_column(x.val(), xset);
// Filter error values before determining bins and making the histogram
if (x_data[0] instanceof Array) {
for (i = 0; i < x_data.length; i++) {
x_data[i] = jsparc.remove_error_values_1d(x_data[i]);
if (xlog) {
x_data[i] = jsparc.remove_invalid_log_values_1d(x_data[i]);}}}
else {
x_data = jsparc.remove_error_values_1d(x_data);
if (xlog) {
x_data = jsparc.remove_invalid_log_values_1d(x_data);}}
var hist = jsparc.histogram(x_data, parseInt($('#hist_bins input').val()));
if (hist[1][0] === undefined) {
// If there are no bins
data_to_plot = [[]];}
else {
// Add another bin at the end (can mess up a fit)
if (hist[0][0] instanceof Array) {
for (i = 0; i < hist[0].length; i++) {
hist[0][i].push(hist[0][i][hist[0][i].length - 1]);}}
else {
hist[0].push(hist[0][hist[0].length - 1]);}
var n_bins = hist[1].length,
bin_width = (hist[1][n_bins - 1] - hist[1][n_bins - 2]);
hist[1].push(hist[1][n_bins - 1] + bin_width);
// The data that needs to be plotted
data_to_plot = jsparc.zip_data(hist[1], hist[0]);
if (data_to_plot[0][0] instanceof Array === false) {
data_to_plot = [data_to_plot];}}
}
else if (plot_type == 'scatter') {
jsparc.add_flot_options(jsparc.flot_x_axis_labels(x.data('label')));
jsparc.add_flot_options(jsparc.flot_y_axis_labels(y.data('label')));
if (xset != yset) {
y_data = jsparc.linear_interpolation(jsparc.get_ext_timestamp(xset),
jsparc.get_ext_timestamp(yset),
jsparc.get_column(y.val(), yset));}
else {
y_data = jsparc.get_column(y.val(), yset);}
// The data that needs to be plotted
data_to_plot = jsparc.zip_data(jsparc.get_column(x.val(), xset), y_data);
if (data_to_plot[0][0] instanceof Array === false) {
data_to_plot = [data_to_plot];}
for (i = 0; i < data_to_plot.length; i++) {
if (ylog) {
data_to_plot[i] = jsparc.remove_invalid_log_values(data_to_plot[i], 1);}
if (xlog) {
data_to_plot[i] = jsparc.remove_invalid_log_values(data_to_plot[i], 0);}}
}
else if (plot_type == 'timeseries') {
jsparc.add_flot_options(jsparc.flot_y_axis_labels(y.data('label')));
// The data that needs to be plotted
y_data = jsparc.get_column(y.val(), yset);
x_data = jsparc.make_javascript_timestamp(jsparc.get_column('timestamp', yset));
data_to_plot = jsparc.zip_data(x_data, y_data);
if (data_to_plot[0][0] instanceof Array === false) {
data_to_plot = [data_to_plot];}
if (ylog) {
for (i = 0; i < data_to_plot.length; i++) {
data_to_plot[i] = jsparc.remove_invalid_log_values(data_to_plot[i], 1);}}
}
// Clean the data by removing all values of -1 and -999.
for (i = 0; i < data_to_plot.length; i++) {
data_to_plot[i] = jsparc.remove_error_values(data_to_plot[i]);}
if (jsparc.is_data_empty(data_to_plot)) {
alert('No data to plot. The selected data may contain only ' +
'error values or values invalid for logarithmic axes.');
return;}
// Get selected fit options
var fit_type = $("#fit_options").val(),
order = 0;
if (fit_type === 'polynomial') {
order = parseInt($('#fit [name="degree"]').val());}
else if (fit_type === 'sine') {
order = parseFloat($('#fit [name="period"]').val());
if (plot_type == 'timeseries') {
// Convert seconds to milliseconds.
order = order * 1000;}}
// If a fit type is selected use regression.js to get trend
// points and add them to data to plot
if (fit_type != 'none') {
var formulas = $('<div>').append($('<h3>').text('Fit formula with correlation coefficient'));
// Check for multidimensional array
// Loop over each single dataset
var length = data_to_plot.length;
for (i = 0; i < length; i++) {
// Check if dataset is not empty
if (!jsparc.is_data_empty(data_to_plot[i])) {
// And for each dataset calculate the fit
var summary = regression(fit_type, data_to_plot[i], order);
// Only show fit if NaN not present
if (summary.string.indexOf("NaN") != -1) {
summary.string = "Fit not possible";
summary.corrstring = "";}
// The calculated fit is returned as summary.points
data_to_plot.push(summary.points);
var formula = $('<p>');
// Parse the formula and correlation coefficient
// Color the formula as its line in the plot.
formula.append($('<span>').addClass('formula').addClass('ch' + (i % 8 + length + 1)).text(summary.string));
formula.append($('<span>').addClass('correlation').text(summary.corrstring));
formulas.append(formula);}}
$("#preview").html(formulas);
$("#results").show();
// Update MathJax to render new text
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "results"]);}
else {
$("#results").hide();}
// Create the plot
var plot = jsparc.make_plot($('#plot'), data_to_plot);
$('#plot').show();
$('#graph').show();
jsparc.show_id('plot');
});
// Show trace button
$('#preview').on('click', 'td.trace', function() {
$('#preview tr.selectedRow').removeClass('selectedRow');
$(this).parent().addClass('selectedRow');
var url = $(this).attr('data-url');
jsparc.get_json(url).done(function(data) {
// Set plot options
jsparc.set_flot_options([]);
jsparc.add_flot_options(jsparc.flot_x_axis_labels('Time [ns]'));
jsparc.add_flot_options(jsparc.flot_y_axis_labels('ADCcounts'));
var x_data = jsparc.range(0, 2.5 * data[0].length, 2.5);
var plot = jsparc.make_plot($('#plot'), jsparc.zip_data(x_data, data));
$('#plot').show();
$('#graph').show();
jsparc.show_id('plot');
jsparc.set_flot_options($('#plot_settings input:checked').map(function () {
return jsparc['flot_' + $(this).val()]();}));
});
});
// Dataset list buttons
$('#dataset_list').on('click', '.preview', function() {
$('#dataset_list tr.selectedRow').removeClass('selectedRow');
$(this).parent().addClass('selectedRow');
jsparc.create_dataset_table($(this).attr('name'),
$('#preview'), 29);
$('#results').show();
jsparc.show_id('preview');
});
$('#dataset_list').on('click', '.download', function() {
window.open($(this).attr('name'), '_blank');
});
// Radio buttons
$('#dataset_list').on('change', 'input', function() {
jsparc.make_variable_plot_table($(this).val(), $('#' + $(this).attr('name') + '_variables'));
$('#plot_type input:checked').trigger('change');
$('#select_variables').show();
});
$('#select_variables').on('change', 'input', function() {
$('#controls').show();
});
$('#use_y_limits').on('change', function() {
if ($(this).prop('checked')) {
$('#plot_y_limits input[type=number]').prop('disabled', false);}
else {
$('#plot_y_limits input[type=number]').prop('disabled', true);}
});
$('#use_x_limits').on('change', function() {
if ($(this).prop('checked')) {
$('#plot_x_limits input[type=number]').prop('disabled', false);}
else {
$('#plot_x_limits input[type=number]').prop('disabled', true);}
});
$('#plot_type').on('change', 'input', function() {
if ($(this).val() == 'histogram') {
jsparc.enable_radio_set('x-axis');
jsparc.disable_radio_set('y-axis');
$('#hist_bins input').prop('disabled', false);}
else if ($(this).val() == 'scatter') {
jsparc.enable_radio_set('x-axis');
jsparc.enable_radio_set('y-axis');
$('#hist_bins input').prop('disabled', true);}
else if ($(this).val() == 'timeseries') {
jsparc.disable_radio_set('x-axis');
jsparc.enable_radio_set('y-axis');
$('#hist_bins input').prop('disabled', true);}
});
// Set Degree to disabled if polynomial not selected
$("#fit_options").on('change', function(){
$('#fit input').prop('disabled', true);
if ($(this).val() === "polynomial") {
$('#fit input[name=degree]').prop('disabled', false);}
else if ($(this).val() === "sine") {
$('#fit input[name=period]').prop('disabled', false);}
});
// Update plot settings
$('#plot_settings').on('change', 'input[type=radio]', function() {
jsparc.set_flot_options($('#plot_settings input[type=radio]:checked').map(function () {
return jsparc['flot_' + $(this).val()]();}));
});
});
</script>
</head>
<body>
<div id="container">
<div id="header"><div id="pageHeader"></div></div>
<div id="doc_link">
<a href="https://docs.hisparc.nl/jsparc/data_retrieval.html" target="jsparc_doc">➔ Documentation</a>
<a href="index.html">➔ jSparc</a>
</div>
<div id="download_data">
<h2>Download data</h2>
<p>Get data from the HiSPARC server.</p>
<p>Data type:
<span id="type_choice">
<input name="data_type" type="radio" value="events" checked="checked"> Events
<input name="data_type" type="radio" value="weather"> Weather
<input name="data_type" type="radio" value="singles"> Singles
</span>
</p>
<p id="station_choice">Station:
<span id="station_events_choice"><select><option>Loading stations..</option></select></span>
<span style="display:none;" id="station_weather_choice"></span>
<span style="display:none;" id="station_singles_choice"></span></p>
<p>Start date: <input id="startdate_choice" type="datetime-local" min="2004-01-01T00:00:00" step="1"></p>
<p>End date: <input id="enddate_choice" type="datetime-local" min="2004-01-01T00:00:00" step="1"></p>
<input type='button' value="Get Data!" id="download_dataset">
</div>
<div id="load_data">
<h2>Load local file</h2>
<p>Import a downloaded .tsv file (tab-separated values).</p>
<p><input id="file_choice" type="file" accept="text/tab-separated-values,text/csv,text/plain"></p>
<input type='button' value="Load Data!" id="load_dataset">
</div>
<div class="clear"></div>
<div id="select_dataset">
<h3>Select datasets to use</h3>
<p id="dataset_list"></p>
</div>
<div id="select_variables">
<h3>Select variables and settings to plot</h3>
<div id="controls" class="left">
<div id="plot_settings">
<span class="key">Plot type:</span><br>
<span id="plot_type">
<input name="plot_type" type="radio" value="scatter" checked> Scatter<br>
<input name="plot_type" type="radio" value="histogram"> Histogram<br>
<input name="plot_type" type="radio" value="timeseries"> Time series<br>
</span><br>
<span class="key">x-Axis:</span><br>
<span id="plot_x_limits">
<input id="use_x_limits" type="checkbox">
Limits <input name="plot_x_min" style="width: 38px;" type="number" disabled>
<input name="plot_x_max" style="width: 45px;" type="number" disabled><br>
</span>
<span id="plot_x_axis">
<input name="plot_x_axis" type="radio" value="none" checked> Linear<br>
<input name="plot_x_axis" type="radio" value="xlog"> Logarithmic<br>
</span><br>
<span class="key">y-Axis:</span><br>
<span id="plot_y_limits">
<input id="use_y_limits" type="checkbox">
Limits <input name="plot_y_min" style="width: 38px;" type="number" disabled>
<input name="plot_y_max" style="width: 45px;" type="number" disabled><br>
</span>
<span id="plot_y_axis">
<input name="plot_y_axis" type="radio" value="none" checked> Linear<br>
<input name="plot_y_axis" type="radio" value="ylog"> Logarithmic<br>
</span><br>
<span class="key">Histogram:</span><br>
<span id="hist_bins">
Bins: <input name="hist_bins" type="number" min=0 step=10 style="width: 60px;" value="100"><br>
</span><br>
<span class="key">Fit:</span><br>
<span id="fit">
Type:
<select id="fit_options" title="Select the type of function to fit to the selected data.">
<option value="none">No fit</option>
<option value="linear">Linear</option>
<option value="gaussian">Gaussian</option>
<option value="sine">Sine</option>
<option value="exponential">Exponential</option>
<option value="wexponential">Exponential*</option>
<option value="logarithmic">Logarithmic</option>
<option value="power">Power</option>
<option value="polynomial">Polynomial</option>
</select>
Period: <input name="period" type="number" style="width: 66px;" disabled value="86400"
title="Set the period of the sine function, in same units as the x-axis."><br>
Degree: <input name="degree" type="number" min=0 max=10 step=1 style="width: 60px;" disabled value="2"
title="Set the degree (highest power) of the polynomial."><br>
</span><br>
</div>
<input type="button" value="Create Plot" id="make_plot">
</div>
<div id="set1_variables" class="left"></div>
<div id="set2_variables" class="left"></div>
</div>
<div class="clear"></div>
<div id="graph">
<h3>Plot</h3>
<a onclick="jsparc.download_plot()" class="save_image right">Save image</a>
<div id="plot"></div>
</div>
<div id="results">
<p id="preview"></p>
</div>
</div>
</body>
</html>