-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform_data.js
63 lines (52 loc) · 1.35 KB
/
transform_data.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
const fs = require('fs');
const readline = require('readline');
const base_path = './dataset/';
// I'm too lazy to parse the directory here
const inputfiles = [
'pegel_11.csv',
'pegel_12.csv',
'pegel_13.csv',
'pegel_14.csv',
'pegel_15.csv'
];
const out_file = base_path + 'transformed.json';
// transformed data goes here
const dates_with_values = [];
const parseFile = file_name => {
return new Promise((res) => {
const inputstream = fs.createReadStream(file_name);
const rl = readline.createInterface({
input: inputstream,
crlfDelay: Infinity
});
// start at -1 to make the third row first of month
let linecount = -1;
let year = NaN;
rl.on('line', (line) => {
const fields = line.split(',',12);
if (linecount < 0) {
year = fields[0];
} else if (linecount > 0) {
for (const fidx in fields) {
const val = parseInt(fields[fidx]);
//eliminate empty fields
if (isNaN(val))
continue;
dates_with_values.push({
timestamp: new Date(Date.UTC(year, fidx, linecount)),
value: val
});
}
}
linecount++;
});
inputstream.on('end', res);
});
};
const files_to_wait_for = [];
inputfiles.forEach(name => {
files_to_wait_for.push(parseFile(base_path + name));
});
Promise.all(files_to_wait_for).then(() => {
fs.writeFileSync(out_file, JSON.stringify(dates_with_values, null, '\t'));
});