git ssb

0+

Dominic / weather



Commit f00e66e6a3461caa28ea6a0a484bb2f24cdfb518

generate a table from the summary

Dominic Tarr committed on 3/31/2019, 11:22:57 AM
Parent: 73e597787040ac71ae3842007ec3d7b30a672619

Files changed

table.jsadded
table.jsView
@@ -1,0 +1,192 @@
1 +var lpad = require('left-pad')
2 +var summary = require('./summary')
3 +
4 +function pad(n) {
5 + return lpad(n, 4)
6 +}
7 +
8 +var areas = Object.keys(summary)
9 +
10 +function toPercent(o) {
11 + var count = 0
12 + for(var k in o)
13 + count += o[k]
14 + var _o = {}
15 + for(var k in o)
16 + _o[k] = Math.round((o[k]/count)*10000)/100
17 + return _o
18 +}
19 +function merge (a, b) {
20 + var c = {}
21 + for(var k in a) c[k] = (c[k] || 0) + a[k]
22 + for(var k in b) c[k] = (c[k] || 0) + b[k]
23 + return c
24 +}
25 +
26 +function round(r) {
27 + return Math.round(r*100)/100
28 +}
29 +
30 +var M = {
31 + northerly: 'N',
32 + northeast: 'NE',
33 + east: 'E',
34 + easterly: 'E',
35 + southeast: 'SE',
36 + southerly: 'S',
37 + southwest: 'SW',
38 + west: 'W',
39 + westerly: 'W',
40 + northwest: 'NW'
41 +}
42 +
43 +//directions of foul winds. if we are heading N we prefer winds not N,NE,NW
44 +var foul = {
45 + N: {N: true, NE: true, NW: true},
46 + NE: {N: true, NE: true, E: true},
47 + E: {NE: true, E: true, SE: true},
48 + SE: {E: true, SE: true, S: true},
49 + S: {SE: true, S: true, SW: true},
50 + SW: {S: true, SW: true, W: true},
51 + W: {SW: true, W: true, NW: true},
52 + NW: {W: true, NW: true, N:true},
53 +
54 +}
55 +
56 +var winds = {
57 + N: true,
58 + NE: true,
59 + E: true,
60 + SE: true,
61 + S: true,
62 + SW: true,
63 + W: true,
64 + NW: true
65 +}
66 +
67 +var speeds = {
68 + 5: true, 10: true, 15: true, 20: true, 25: true,
69 + 30: true, 35: true, 40: true, 45: true, 50: true, 55: true, 60: true, 65: true, 70: true
70 +}
71 +
72 +var months = {}
73 +
74 +for(var area in summary)
75 + for(var month in summary[area]) {
76 + months[month] = true
77 + var _winds = {}
78 + for(var wind in summary[area][month].winds)
79 + _winds[M[wind]] = merge(_winds[M[wind]], summary[area][month].winds[wind])
80 +
81 + var count = summary[area][month].count
82 + summary[area][month] = map(_winds, function (e) {
83 + return map(e, function (e) {
84 + return Math.round((e / count)*1000)/10
85 + })
86 + })
87 + }
88 +
89 +//console.log(JSON.stringify(summary, null, 2))
90 +//return
91 +
92 +
93 +function map(o, iter) {
94 + var _o = {}
95 + for(var k in o)
96 + _o[k] = iter(o[k])
97 + return _o
98 +}
99 +
100 +//console.log(areas)
101 +
102 +function aggregate (_area) {
103 + var months = {}, speeds = {}, winds = {}
104 + var speeds_by_month = {}
105 + for(var area in summary)
106 + if(!_area || area == _area)
107 + for(var month in summary[area]) {
108 + months[month] = true
109 + speeds_by_month[month] = speeds_by_month[month] || {}
110 + for(var wind in summary[area][month].winds) {
111 + winds[wind] = true
112 + for(var speed in summary[area][month].winds[wind]) {
113 + speeds[speed] = (speeds[speed] || 0) + summary[area][month].winds[wind][speed]
114 + speeds_by_month[month][speed] = (speeds_by_month[month][speed] || 0) + summary[area][month].winds[wind][speed]
115 + }
116 + }
117 + }
118 + delete speeds[8]
119 + delete speeds[29]
120 + return {overall: speeds, byMonth: speeds_by_month}
121 +}
122 +
123 +var area = process.argv[2]
124 +if(area && !summary[area]) return console.error(area, 'must be one of:', areas)
125 +
126 +var a = aggregate()
127 +//speeds, overall
128 +
129 +//console.log(toPercent(a.speeds))
130 +//console.log(map(a.byMonth, toPercent))
131 +//console.error(winds)
132 +
133 +console.log('Area, Month, Speed, N, NE, E, SE, S, SW, W, NW, TOTAL')
134 +
135 +//console.log(areas)
136 +//console.log(months)
137 +//console.log(winds)
138 +//console.log(speeds)
139 +
140 +
141 +for(var area in summary) {
142 + for(var month in months) {
143 + console.log(area.toUpperCase(), month)
144 + console.log([''].concat(Object.keys(winds).concat('T')).map(pad).join(', '))
145 + var totals = ['']
146 + var gales = ['']
147 + for(var speed in speeds) {
148 + var a = [speed], i = 0, total = 0
149 + for(var wind in winds) {
150 + var measurement = (summary[area][month] && summary[area][month][wind] ? summary[area][month][wind][speed] || 0 : 0)
151 + a.push(measurement)
152 + total += measurement
153 + i++
154 + if(speed < 30)
155 + totals[i] = (totals[i] || 0) + (+measurement)
156 + else
157 + gales[i] = (gales[i] || 0) + (+measurement)
158 + }
159 + a.push(round(total))
160 + console.log(a.map(pad).join(', '))
161 + }
162 + totals.push(totals.reduce(function (a, b) { return (a || 0) + (b || 0) }, 0))
163 + totals = totals.map(round)
164 + totals[0] = 'C' //for comfort
165 + console.log(totals.map(pad).join(', '))
166 +
167 + var i = 1
168 + var F = ['']
169 + for(var heading in winds) {
170 + var fair = 0
171 + var j = 0
172 + for(var wind in winds) {
173 + ++j
174 + if(!foul[heading][wind])
175 + fair += totals[j]
176 + }
177 + F.push(fair)
178 + }
179 +
180 + gales.push(gales.reduce(function (a, b) { return (a || 0) + (b || 0) }, 0))
181 + gales = gales.map(round)
182 + gales[0] = 'G' //for gales
183 + console.log(gales.map(pad).join(', '))
184 +
185 + console.log(F.map(round).map(pad).join(', '))
186 +
187 + }
188 +}
189 +
190 +
191 +
192 +

Built with git-ssb-web