-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwml-test.js
323 lines (259 loc) · 8.01 KB
/
dwml-test.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'use strict'
/**
* Digital Weather Markup Language (DWML) document parser classes.
*
* Based on https://graphical.weather.gov/xml/mdl/XML/Design/MDL_XML_Design.pdf
*/
const fs = require('fs')
const path = require('path')
const moment = require('moment')
const DOMParser = require('xmldom').DOMParser
/*
Why oh why are the node type constants not exposed? Adding these to our faux Node interface.
SEE: https://github.com/jindw/xmldom/pull/151
*/
const Node = {
ELEMENT_NODE: 1
// NOTE: Not used
// ATTRIBUTE_NODE: 2,
// TEXT_NODE: 3,
// CDATA_SECTION_NODE: 4,
// ENTITY_REFERENCE_NODE: 5,
// ENTITY_NODE: 6,
// PROCESSING_INSTRUCTION_NODE: 7,
// COMMENT_NODE: 8,
// DOCUMENT_NODE: 9,
// DOCUMENT_TYPE_NODE: 10,
// DOCUMENT_FRAGMENT_NODE: 11,
// NOTATION_NODE: 12
}
class DWMLLocation {
constructor (dwmlDoc, locationEl) {
this.element = locationEl
}
get locationKey () {
if (this._locationKey) return this._locationKey
const els = this.element.getElementsByTagName('location-key')
return (this._locationKey = els.length > 0 ? els[0].firstChild.nodeValue : null)
}
get point () {
if (this._point) return this._point
const point = {}
const pointEls = this.element.getElementsByTagName('point')
if (pointEls.length > 0) {
const pointEl = pointEls[0]
point.latitude = parseFloat(pointEl.getAttribute('latitude'))
point.longitude = parseFloat(pointEl.getAttribute('longitude'))
}
return (this._point = point)
}
}
class DWMLTimeLayout {
constructor (dwmlDoc, timeLayoutEl) {
this.element = timeLayoutEl
}
get layoutKey () {
if (this._layoutKey) return this._layoutKey
const els = this.element.getElementsByTagName('layout-key')
return (this._layoutKey = els.length > 0 ? els[0].firstChild.nodeValue : null)
}
get parsedKey () {
const key = this.layoutKey
const parsed = {}
if (key) {
const parts = key.split('-')
parsed.period = parts[1]
parsed.times = parts[2]
parsed.seq = parseInt(parts[3])
}
return parsed
}
get timeCoordinate () {
return this.element.getAttribute('time-coordinate')
}
get validTimes () {
return this._validTimes ? this._validTimes : (this._validTimes = Array.from(this.validTimeGen()))
}
// eslint-disable-next-line
*validTimeGen () {
const startEls = this.element.getElementsByTagName('start-valid-time')
const endEls = this.element.getElementsByTagName('end-valid-time')
for (let i = 0; i < startEls.length; i++) {
const startString = startEls[i].firstChild.nodeValue
const startMoment = moment.parseZone(startString)
const obj = {
startDate: startMoment.toDate(),
startOffset: startMoment.utcOffset() * 60,
startString: startString
}
if (endEls[i]) {
const endString = endEls[i].firstChild.nodeValue
const endMoment = moment.parseZone(endString)
obj.endDate = endMoment.toDate()
obj.endOffset = endMoment.utcOffset() * 60
obj.endString = endString
}
yield obj
}
}
}
class DWMLParameter {
constructor (dwmlDoc, parametersEl, parameterEl) {
this.element = parameterEl
const locationKey = this.locationKey = parametersEl.getAttribute('applicable-location')
if (locationKey) this.location = dwmlDoc.locations[locationKey]
const layoutKey = this.timeLayoutKey = parameterEl.getAttribute('time-layout')
if (layoutKey) this.timeLayout = dwmlDoc.timeLayouts[layoutKey]
}
get elementName () {
return this.element.nodeName
}
get name () {
if (this._name) return this._name
const els = this.element.getElementsByTagName('name')
return (this._name = els.length > 0 ? els[0].firstChild.nodeValue : null)
}
get type () {
return this.element.getAttribute('type')
}
}
class DWMLConditionsIconsParameter extends DWMLParameter {
get iconLinks () {
return Array.from(this.iconLinkGen())
}
// eslint-disable-next-line
*iconLinkGen () {
const iconLinkEls = this.element.getElementsByTagName('icon-link')
for (let i = 0; i < iconLinkEls.length; i++) {
yield iconLinkEls[i].firstChild.nodeValue
}
}
get series () {
return Array.from(this.seriesGen())
}
// eslint-disable-next-line
*seriesGen () {
if (!this.timeLayout) return
const validTimes = this.timeLayout.validTimes
let i = 0
for (const iconLink of this.iconLinkGen()) {
const validTime = validTimes[i++]
if (validTime) {
yield {
time: validTime,
url: iconLink
}
}
}
}
}
class DWMLUnitsParameter extends DWMLParameter {
get units () {
return this.element.getAttribute('units')
}
get values () {
return Array.from(this.valueGen())
}
// eslint-disable-next-line
*valueGen () {
const valueEls = this.element.getElementsByTagName('value')
for (let i = 0; i < valueEls.length; i++) {
yield parseFloat(valueEls[i].firstChild.nodeValue)
}
}
get series () {
return Array.from(this.seriesGen())
}
// eslint-disable-next-line
*seriesGen () {
if (!this.timeLayout) return
const validTimes = this.timeLayout.validTimes
let i = 0
for (const value of this.valueGen()) {
const validTime = validTimes[i++]
if (validTime) {
yield {
time: validTime,
value: value
}
}
}
}
}
const ELEMENT_NAME_TO_PARAMETER_CLASS = {
'conditions-icon': DWMLConditionsIconsParameter,
'conditions-icons': DWMLConditionsIconsParameter,
'probability-of-precipitation': DWMLUnitsParameter,
'temperature': DWMLUnitsParameter
}
class DWMLDocument {
constructor (xmlDoc) {
const dataEls = xmlDoc.getElementsByTagName('data')
if (dataEls.length === 0) throw new Error('Missing data element')
this.dataElement = dataEls[0]
this.xmlDocument = xmlDoc
}
get locations () {
return this._locations ? this._locations : (this._locations = Array.from(this.locationGen()).reduce((obj, cur) => {
obj[cur.locationKey] = cur
return obj
}, {}))
}
// eslint-disable-next-line
*locationGen () {
const locationEls = this.dataElement.getElementsByTagName('location')
for (let i = 0; i < locationEls.length; i++) {
yield new DWMLLocation(this, locationEls[i])
}
}
get parameters () {
return this._parameters ? this._parameters : (this._parameters = Array.from(this.parameterGen()))
}
// eslint-disable-next-line
*parameterGen () {
const parametersEls = this.dataElement.getElementsByTagName('parameters')
for (let i = 0; i < parametersEls.length; i++) {
const parametersEl = parametersEls[i]
const nds = parametersEl.childNodes
for (let j = 0; j < nds.length; j++) {
const nd = nds[j]
if (nd.nodeType === Node.ELEMENT_NODE) {
const Klass = ELEMENT_NAME_TO_PARAMETER_CLASS[nd.nodeName]
if (Klass) yield new Klass(this, parametersEl, nd)
}
}
}
}
get timeLayouts () {
return this._timeLayouts ? this._timeLayouts : (this._timeLayouts = Array.from(this.timeLayoutGen()).reduce((obj, cur) => {
obj[cur.layoutKey] = cur
return obj
}, {}))
}
// eslint-disable-next-line
*timeLayoutGen () {
const timeLayoutEls = this.dataElement.getElementsByTagName('time-layout')
for (let i = 0; i < timeLayoutEls.length; i++) {
yield new DWMLTimeLayout(this, timeLayoutEls[i])
}
}
}
new Promise((resolve, reject) => {
// Read input
fs.readFile(path.join(__dirname, 'input.xml'), 'utf8', (err, data) => {
err ? reject(err) : resolve(data)
})
}).then(data => {
// Parse
return new DOMParser().parseFromString(data, 'text/xml')
}).then(xmlDoc => {
const dwml = new DWMLDocument(xmlDoc)
dwml.parameters.forEach(parameter => {
console.log('>>>', parameter.elementName, parameter.name, parameter.type, parameter.units)
console.log(' ', parameter.timeLayout.parsedKey)
console.log(' ', parameter.location.point)
console.log(' ', parameter.series)
})
}).catch(err => {
console.error(err)
})