-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-normals.js
64 lines (60 loc) · 2.07 KB
/
parse-normals.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
#!/Users/derek/.nvm/v6.3.1/bin/node
'use strict'
const fs = require('fs')
const path = require('path')
const constants = require('./ncdc-constants').normals
let parser = (fileName, statisticType, callback) => {
fs.readFile(fileName, 'utf8', (err, data) => {
let {station, month, value, flag, step} = constants.positions
// Ignore empty rows.
let rows = data.split('\n').filter(r => r.length)
let normals = {}
let flagInfo = { multiple: 0 }
rows.forEach(row => {
let dStation = row.slice(station[0], station[1])
if ( !normals.hasOwnProperty(dStation) ) {
normals[dStation] = { values: [] }
}
let start = value[0]
let end = value[1]
while ( (start + step - 1) <= row.length ) {
let avg = parseInt(row.slice(start, end).trim(), 10) / 10
let flag = row.slice(end, end + 1)
// If flag is empty or value is < 0 then it's not a valid data point.
if ( flag !== '' && avg > -1 ) {
normals[dStation].values.push({
avg: avg,
flag: flag
})
}
start += step
end += step
}
})
// Change format of station.values from array of objects to array of numbers
// and add a flag property, count occurences of various flags.
let stationIds = Object.keys(normals)
stationIds.forEach(n => {
let nums = normals[n].values.map(val => val.avg)
// Get unique values of an array with new Set().
let flags = [...new Set(normals[n].values.map(val => val.flag))]
normals[n].values = nums
normals[n].flags = flags
if ( flags.length === 1 ) {
if ( !flagInfo.hasOwnProperty(flags[0]) ) {
flagInfo[flags[0]] = 0
}
flagInfo[flags[0]] += 1
}
if ( flags.length > 1 ) {
// console.log('Multiple flags for ', n)
flagInfo.multiple += 1
}
})
console.log(`Flag counts for ${statisticType}:\n`, JSON.stringify(flagInfo, null, 2))
normals.flagInfo = flagInfo
normals.statistic = statisticType
callback(null, normals)
})
}
module.exports = parser