-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse-acs-schemas.js
87 lines (72 loc) · 2.8 KB
/
parse-acs-schemas.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
const request = require('requestretry');
const csv = require('csvtojson');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const argv = require('yargs').argv;
const { dataset } = require('./modules/settings.js');
if (argv._.length === 0) {
console.log('fatal error. Run like: node parse-acs-schemas.js 2015');
process.exit();
}
const YEAR = argv._[0];
const url = `https://www2.census.gov/programs-surveys/acs/summary_file/${YEAR}/documentation/user_tools/ACS_5yr_Seq_Table_Number_Lookup.txt`;
request(url, function(err, resp, body) {
if (err) { return console.log(err); }
csv({ noheader: false })
.fromString(body)
.on('end_parsed', data => {
const fields = {};
// filter out line number if non-integer value
data.forEach(d => {
const line_number = Number(d['Line Number']);
if (Number.isInteger(line_number) && line_number > 0) {
const field_name = d['Table ID'] + String(d['Line Number']).padStart(3, "0");
const seq_num = d['Sequence Number'].slice(1);
if (fields[seq_num]) {
fields[seq_num].push(field_name);
}
else {
fields[seq_num] = ["FILEID", "FILETYPE", "STUSAB", "CHARITER", "SEQUENCE", "LOGRECNO", field_name];
}
}
});
const myBucket = `s3db-acs-metadata-${dataset[YEAR].text}`;
const key = `s${dataset[YEAR].text}.json`;
return new Promise((resolve, reject) => {
const params = { Bucket: myBucket, Key: key, Body: JSON.stringify(fields), ContentType: 'application/json' };
s3.putObject(params, function(err, data) {
if (err) {
console.log(err);
return reject(err);
}
else {
console.log(`Successfully uploaded data to ${myBucket} - ${key}`);
return resolve(data);
}
});
});
})
.on('done', () => {
//parsing finished
console.log('finished parsing schema file');
});
});
/*******************/
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength, padString) {
targetLength = targetLength >> 0; //truncate if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '));
if (this.length > targetLength) {
return String(this);
}
else {
targetLength = targetLength - this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + String(this);
}
};
}