-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.js
92 lines (80 loc) · 2.94 KB
/
read.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
var Converter=require("csvtojson").core.Converter;
var fs=require("fs");
var INIT = "init",
DONE_READ_DATA = "done reading data",
DONE_READ_COUNTRYMAPPING = "done country mapping",
DONE_READ_REGIONMAPPING = "done region mapping",
DONE_WRITE_RESULT = "done writing results";
var NOT_FOUND = "NOT FOUND";
var model = {};
// hey
// orchestrating function - defines the procedure
// --------------------------------
function hey(what, arg){
console.log("HEY: " + what);
switch(what){
case INIT:
readCSV("dataset1.csv", "data", DONE_READ_DATA); break;
case DONE_READ_DATA:
readCSV("country_synonyms.csv", "countryMapping", DONE_READ_COUNTRYMAPPING); break;
case DONE_READ_COUNTRYMAPPING:
readCSV("country_regions.csv", "regionMapping", DONE_READ_REGIONMAPPING); break;
case DONE_READ_REGIONMAPPING:
writeResult("response_0-mountains.json", "result", DONE_WRITE_RESULT); break;
case DONE_WRITE_RESULT:
console.log("Done"); break;
}
}
// readCSV
// Reads CSV [filename] and writes it as JSON to model under [exportName]
// then transfers control to [next] when done
// --------------------------------
function readCSV(filename, exportName, next){
var fileStream=fs.createReadStream("./" + filename);
var csvConverter=new Converter({});
csvConverter.on("end_parsed",function(jsonObj){
model[exportName] = jsonObj;
hey(next);
});
fileStream.pipe(csvConverter);
}
// writeResult
// reorganizes the data and writes it to [filename]
// then transfers control to [next] when done
// --------------------------------
function writeResult(filename, exportName, next){
model[exportName] = model.data.map(function(d){
return {
"geo": mapper(d.name, "ISO3dig_ext", "countryMapping").toLowerCase(),
"geo.name": d.name,
"geo.category": [
"country"
],
"geo.region": mapper(d.name, "gapminder region", "regionMapping").toLowerCase(),
"time": d.year,
//"gini": d.gini,
"pop": d.pop,
//"gdp_per_cap": d.gdp_per_cap,
"mean": d.mean,
"variance": Math.round((d.stddev*d.stddev)*100000)/100000
};
});
fs.writeFile(filename, JSON.stringify([model[exportName]]), function(err){
hey(next);
})
}
// mapper - helper function
// searches [from] in all properties of [mappingTable] object
// if found, returns the correspinding value of [targetProperty]
// if not, prints a warning ans saves NOT_FOUND
// --------------------------------
function mapper(from, targetProperty, mappingTable) {
var to = NOT_FOUND;
model[mappingTable].forEach(function(d){
for(property in d){ if(d[property]==from) to=d[targetProperty]; }
});
if(to == NOT_FOUND) console.warn("WARNING: property not resolved for: " + from + " (" +targetProperty + ")");
return to;
}
// ignition
hey(INIT);