-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing-4.4.js
35 lines (23 loc) · 929 Bytes
/
listing-4.4.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
"use strict";
const fs = require('fs');
const moment = require('moment');
const records = JSON.parse(fs.readFileSync("./data/earthquakes.json", "utf8"));
const bufferSize = 4 + 8 * 5 * records.length;
const buffer = new Buffer(bufferSize);
buffer.writeInt32LE(records.length);
let bufferOffset = 4;
for (let recordIndex = 0; recordIndex < records.length; ++recordIndex) {
const record = records[recordIndex];
const time = moment(record.Time).toDate().getTime();
buffer.writeDoubleLE(time, bufferOffset);
bufferOffset += 8;
buffer.writeDoubleLE(record.Latitude, bufferOffset);
bufferOffset += 8;
buffer.writeDoubleLE(record.Longitude, bufferOffset);
bufferOffset += 8;
buffer.writeDoubleLE(record.Depth_Km, bufferOffset);
bufferOffset += 8;
buffer.writeDoubleLE(record.Magnitude, bufferOffset);
bufferOffset += 8;
}
fs.writeFileSync("./output/earthquakes.bin", buffer);