-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild_encost_json.js
executable file
·70 lines (63 loc) · 1.92 KB
/
build_encost_json.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
#!/usr/bin/env node
//const encostfn = 'public/Encost19.txt';
//const encostjs = 'public/Encost.json';
const readline = require('readline');
var encostobj = {};
var section = null;
var years = null;
var subsection = null;
var data = null;
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function (line) {
const l = line.trim();
if (l && l != '') {
let a = l.split(/\t+/);
if (section === null) {
section = a.shift();
//console.log("Section: %s", section);
encostobj[section] = {};
if (a.length > 0) {
years = a;
//console.log(" Years: %o", years);
}
} else if (years === null) {
years = l.split(/\s+/);
//console.log(" Years2: %o", years);
} else if (subsection === null) {
subsection = a.shift();
//console.log(" Subsection: %s", subsection);
encostobj[section][subsection] = {};
if (a.length > 0) {
data = a.map(x => { return parseFloat(x) });
if (data.length !== years.length) {
console.error("WARNING: " + section + "/" + subsection + ": data length does not equal year length");
}
let o = {};
for (let i = 0; i < years.length; i++) {
o[years[i]] = data[i];
}
encostobj[section][subsection] = o;
subsection = null;
}
} else {
data = l.split(/\s+/).map(x => { return parseFloat(x) });
if (data.length !== years.length) {
console.error("WARNING: " + section + "/" + subsection + ": data length does not equal year length");
}
let o = {};
for (let i = 0; i < years.length; i++) {
o[years[i]] = data[i];
}
encostobj[section][subsection] = o;
subsection = null;
}
} else {
section = subsection = null; // removed years here so they would inherit
}
}).on('close', function() {
process.stdout.write(JSON.stringify(encostobj) + "\n");
});