-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
361 lines (312 loc) · 10.3 KB
/
index.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// dwd-csv-helper
//
// Copyright 2018 The dwd-csv-helper Developers. See the LICENSE file at
// the top-level directory of this distribution and at
// https://github.com/UdSAES/dwd-csv-helper/LICENSE
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
//
// dwd-csv-helper may be freely used and distributed under the ISC license
'use strict'
const _ = require('lodash')
const moment = require('moment')
const path = require('path')
const fs = require('fs-extra')
const parser = require('xml-js')
const tmp = require('tmp-promise')
const yauzl = require('yauzl-promise')
const COLUMN_SEPARATOR = ';'
const bunyan = require('bunyan')
const log = bunyan.createLogger({
name: 'dwd-csv-handler',
serializers: bunyan.stdSerializers
})
function deriveCsvFilePath (csvBasePath, type, timestamp, stationId) {
let formatString
let contentType
let extension
let subdir
if (type === 'REPORT') {
formatString = 'YYYYMMDD'
contentType = 'BEOB'
extension = '.csv'
subdir = 'poi'
} else if (type === 'MOSMIX_KMZ') {
formatString = 'YYYYMMDDHH'
contentType = 'MOSMIX'
extension = '.kmz'
subdir = 'mos'
} else {
formatString = 'YYYYMMDDHH'
contentType = 'MOSMIX'
extension = '.csv'
subdir = 'poi'
}
const dayDateTimeString = moment.utc(timestamp).format(formatString)
const fileName = stationId + '-' + contentType + extension
return path.join(csvBasePath, subdir, dayDateTimeString, fileName)
}
function parseCsvFile (fileContent) {
fileContent = fileContent.replace(/\r\n/g, '\n')
const lines = fileContent.split('\n')
let headings = []
let values = []
_.forEach(lines, (line, index) => {
if (line === '') {
return
}
const columns = line.split(COLUMN_SEPARATOR)
if (index === 0) {
headings = columns
return
}
if (index < 3) {
return
}
if (index === 3) {
_.forEach(columns, () => {
values.push([])
})
}
_.forEach(columns, (value, index) => {
if (index < 2) {
// do not attempt to parse DD.MM.YY and HH:mm as float
values[index].push(value)
} else if (value === '---') {
// '---' is used to denote 'no data' in files by DWD
values[index].push(null)
} else {
values[index].push(parseFloat(value.replace(',', '.')))
}
})
})
const result = {}
_.forEach(values, (valueColumn, index) => {
if (index < 2) {
return
}
result[headings[index]] = valueColumn
})
_.forEach(values[0], (value, index) => {
if (index === 0) {
result['timestamp'] = []
}
const date = moment.utc(value, 'DD.MM.YY').valueOf()
const time = moment.utc(values[1][index], 'HH:mm').year(1970).month(0).date(1).valueOf()
result['timestamp'].push(date + time)
})
return result
}
async function extractKmlFile (filePath) {
const tmpFile = await tmp.file()
let writeStream = fs.createWriteStream(tmpFile.path)
const zipFile = await yauzl.open(filePath)
const entry = await zipFile.readEntry()
const readStream = await entry.openReadStream()
readStream.pipe(writeStream)
await zipFile.close()
const kmlFileContent = await fs.readFile(tmpFile.path, { encoding: 'utf8' })
tmpFile.cleanup()
return kmlFileContent
}
async function parseKmlFile (fileContent) {
// Read the .kml-file
let xml2jsOptions = {
compact: true,
ignoreComment: true,
alwaysChildren: true
}
let kmzFileJS = await parser.xml2js(fileContent, xml2jsOptions)
// Build the expected object containing all the data
let forecastCollection = {}
let forecastTimeSteps = kmzFileJS['kml:kml']['kml:Document']['kml:ExtendedData']['dwd:ProductDefinition']['dwd:ForecastTimeSteps']['dwd:TimeStep']
forecastTimeSteps = _.map(forecastTimeSteps, (item) => {
return moment(item._text).valueOf()
})
let forecastData = kmzFileJS['kml:kml']['kml:Document']['kml:Placemark']['kml:ExtendedData']['dwd:Forecast']
for (let i = 0; i < forecastData.length; i++) {
let data = _.flatMap(forecastData[i]['dwd:value'], (stringOfValues) => {
let separator = ';'
stringOfValues = _.replace(_.trimStart(stringOfValues), /(\s+)/g, separator)
let listOfValues = _.split(stringOfValues, separator)
return _.map(listOfValues, (value) => {
return parseFloat(value)
})
})
if (forecastTimeSteps.length === data.length) {
let forecast = []
for (let i = 0; i < data.length; i++) {
forecast.push({
timestamp: forecastTimeSteps[i],
value: data[i]
})
}
let key = forecastData[i]['_attributes']['dwd:elementName']
forecastCollection[key] = forecast
}
}
return forecastCollection
}
async function readTimeseriesDataReport (csvBasePath, startTimestamp, endTimestamp, stationId) {
let dayTimestamp = moment.utc(startTimestamp).startOf('day').valueOf()
const result = {}
while (dayTimestamp < endTimestamp) {
const filePath = deriveCsvFilePath(csvBasePath, 'REPORT', dayTimestamp, stationId)
let fileContent
try {
fileContent = await fs.readFile(filePath, { encoding: 'utf8' })
} catch (error) {
log.warn(error, `failed to read measurement data from file ${filePath}`)
dayTimestamp += 86400 * 1000
continue
}
const partialTimeseries = parseCsvFile(fileContent)
// Trim to specified time interval and assemble object to return
const timestamps = partialTimeseries['timestamp']
_.forEach(partialTimeseries, (values, key) => {
if (key === 'timestamp') {
return
}
// Iff key not yet in result, make key property of result
if (!result.hasOwnProperty(key)) {
result[key] = []
}
_.forEach(values, (value, index) => {
if (timestamps[index] < startTimestamp || timestamps[index] >= endTimestamp) {
return
}
result[key].push({
timestamp: timestamps[index],
value: value
})
})
})
dayTimestamp += 86400 * 1000
}
_.forEach(result, (item, key) => {
result[key] = _.sortBy(item, (item) => {
return item.timestamp
})
})
return result
}
async function readTimeseriesDataMosmix (mosmixBasePath, startTimestamp, stationId) {
let dayTimestamp
let filePath
let fileContent
let partialTimeseries
let result = {}
// Take care of the fact that DWD stopped providing .csv-files on 2018-09-17
if (startTimestamp < moment.utc('2018-09-12').valueOf()) {
// TODO: ensure that not only the 6 o'clock-run is used but the others as well
dayTimestamp = moment.utc(startTimestamp).startOf('day').add(6, 'hours').valueOf()
filePath = deriveCsvFilePath(mosmixBasePath, 'MOSMIX', dayTimestamp, stationId)
try {
fileContent = await fs.readFile(filePath, { encoding: 'utf8' })
} catch (error) {
log.warn(error, `failed to read .csv-file ${filePath}`)
throw error
}
partialTimeseries = parseCsvFile(fileContent)
const timestamps = partialTimeseries['timestamp']
_.forEach(partialTimeseries, (values, key) => {
if (key === 'timestamp') {
return
}
_.forEach(values, (value, index) => {
// Perform conversion to new format
switch (key) {
case 'TT':
let newKeyTT = 'TTT'
if (_.isNil(result[newKeyTT])) {
result[newKeyTT] = []
}
result[newKeyTT].push({
timestamp: timestamps[index],
value: value + 273.15 // °C to K
})
break
case 'PPPP':
if (_.isNil(result[key])) {
result[key] = []
}
result[key].push({
timestamp: timestamps[index],
value: value * 100 // hPa to Pa
})
break
case 'Td':
if (_.isNil(result[key])) {
result[key] = []
}
result[key].push({
timestamp: timestamps[index],
value: value + 273.15 // °C to K
})
break
case 'ff':
let newKeyff = 'FF'
if (_.isNil(result[newKeyff])) {
result[newKeyff] = []
}
result[newKeyff].push({
timestamp: timestamps[index],
value: value / 3.6 // km/h to m/s
})
break
case 'dd':
let newKeydd = 'DD'
if (_.isNil(result[newKeydd])) {
result[newKeydd] = []
}
result[newKeydd].push({
timestamp: timestamps[index],
value: value
})
break
// ..unless nothing has changed, which has to be found by manually
// comparing MetElementDefinition.xml to the headings inside a .csv
default:
if (_.isNil(result[key])) {
result[key] = []
}
result[key].push({
timestamp: timestamps[index],
value: value
})
}
})
})
_.forEach(result, (item, key) => {
result[key] = _.sortBy(item, (item) => {
return item.timestamp
})
})
} else {
// TODO: ensure that not only the 3 o'clock-run is used but the others as well
dayTimestamp = moment.utc(startTimestamp).startOf('day').add(3, 'hours').valueOf()
filePath = deriveCsvFilePath(mosmixBasePath, 'MOSMIX_KMZ', dayTimestamp, stationId)
// Unzip the .kmz-file, then parse it
try {
fileContent = await extractKmlFile(filePath)
} catch (error) {
log.warn(error, `failed to extract the .kml-file from ${filePath}`)
throw error
}
result = await parseKmlFile(fileContent)
}
return result
}
exports.parseCsvFile = parseCsvFile
exports.parseKmlFile = parseKmlFile
exports.extractKmlFile = extractKmlFile
exports.deriveCsvFilePath = deriveCsvFilePath
exports.readTimeseriesDataReport = readTimeseriesDataReport
exports.readTimeseriesDataMosmix = readTimeseriesDataMosmix
module.exports = exports