-
Notifications
You must be signed in to change notification settings - Fork 1
/
record.js
executable file
·405 lines (300 loc) · 9.14 KB
/
record.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
const mSEEDHeader = require("./header");
function mSEEDRecord(data) {
/* Class mSEEDRecord
*
* Container for keeping mSEED record information
*/
var dataStart = data.readUInt16BE(44);
if(dataStart < 48) {
throw("The record data start value is invalid.");
}
// Read the mSEED header
this.header = new mSEEDHeader(data.slice(0, dataStart));
// Create an array of unpacked data samples
this.data = this.unpackData(data.slice(dataStart, this.header.recordLength));
}
mSEEDRecord.prototype.unpackData = function(data) {
/* Function mSEEDRecord.UnpackData
*
* Unpacks data frames and returns an
* array of data
*/
// SEED 2.4 Manual page 123
const ENCODING_ASCII = 0;
const ENCODING_INT16 = 1;
const ENCODING_INT32 = 3;
const ENCODING_FLOAT32 = 4;
const ENCODING_FLOAT64 = 5;
const ENCODING_STEIM1 = 10;
const ENCODING_STEIM2 = 11;
// Unpack STEIM1 or STEIM2 depending on the encoding
if(this.header.encoding === ENCODING_ASCII) {
return this.unpackASCII(data);
} else if(this.header.encoding === ENCODING_INT32) {
return this.unpack32BitInt(data);
} else if(this.header.encoding === ENCODING_FLOAT32) {
return this.unpack32BitFloat(data);
} else if(this.header.encoding === ENCODING_FLOAT64) {
return this.unpack64BitFloat(data);
} else if(this.header.encoding === ENCODING_INT16) {
return this.unpack16BitInt(data);
} else if(this.header.encoding === ENCODING_STEIM1) {
return this.unpackSTEIM1(data);
} else if(this.header.encoding === ENCODING_STEIM2) {
return this.unpackSTEIM2(data);
} else {
throw("Unknown encoding: could not unpack data!");
}
}
mSEEDRecord.prototype.id = function() {
/* Function mSEEDRecord.id
*
* Returns the stream identifier
*
*/
return [
this.header.network,
this.header.station,
this.header.location,
this.header.channel
].join(".");
}
mSEEDRecord.prototype.payload = function() {
/* Function mSEEDRecord.payload
*
* Returns payload representation of header
* and record data
*/
return {
'start': this.header.start,
'end': this.header.end,
'data': this.data,
'network': this.header.network,
'station': this.header.station,
'location': this.header.location,
'channel': this.header.channel,
'sampleRate': this.header.sampleRate,
'id': this.id()
}
}
mSEEDRecord.prototype.unpack64BitFloat = function(data) {
/* Function mSEEDRecord.unpack64BitFloat
*
* Unpacks 64F data
*/
var samples = new Array();
for(var i = 0; i < this.header.nSamples; i++) {
samples.push(data.readDoubleBE(i * 8));
}
return samples;
}
mSEEDRecord.prototype.unpackASCII = function(data) {
/* Function mSEEDRecord.unpackASCII
*
* Unpacks ASCII encoded data
*/
return data.toString("ascii");
}
mSEEDRecord.prototype.unpack32BitFloat = function(data) {
/* Function mSEEDRecord.unpack64BitFloat
*
* Unpacks 32F data
*/
var samples = new Array();
for(var i = 0; i < this.header.nSamples; i++) {
samples.push(data.readFloatBE(i * 4));
}
return samples;
}
mSEEDRecord.prototype.unpack16BitInt = function(data) {
/* Function mSEEDRecord.unpack16BitInt
*
* Unpacks int16 data
*/
var samples = new Array();
for(var i = 0; i < this.header.nSamples; i++) {
samples.push(data.readInt16BE(i * 2));
}
return samples;
}
mSEEDRecord.prototype.unpack32BitInt = function(data) {
/* Function mSEEDRecord.unpack32BitInt
*
* Unpacks int32 data
*/
var samples = new Array();
for(var i = 0; i < this.header.nSamples; i++) {
samples.push(data.readInt32BE(i * 4));
}
return samples;
}
mSEEDRecord.prototype.unpackSTEIM1 = function(data) {
/* Function mSEEDRecord.unpackSTEIM1
*
* Unpacks STEIM1 encoded data record
* and returns the array of data
*
* Implemented after SEED manual V2.4
*/
var first = data.readInt32BE(4);
var last = data.readInt32BE(8);
var diff = new Array();
var nibble;
var nFrames = this.header.nFrames || (data.length / 64);
for(var i = 0; i < nFrames; i++) {
w0 = data.readInt32BE(i * 64);
for(var j = 0; j < 16; j++) {
nibble = (w0 >> ((16 - j - 1) * 2)) & 0x03
switch(nibble) {
// Empty nibble
case 0x00:
break;
// Four 8-bit differences
case 0x01:
for(var k = 0; k < 4; k++) {
diff.push(data.readInt8((i * 64) + (j * 4) + k));
}
break;
// Two 16-bit differences
case 0x02:
for(var k = 0; k < 2; k++) {
diff.push(data.readInt16BE((i * 64) + (j * 4) + (k * 2)));
}
break;
// One 32-bit difference
case 0x03:
diff.push(data.readInt32BE((i * 64) + (j * 4)));
break;
}
}
}
// Construct the samples array of data diff
var samples = [first];
for(var i = 1; i < diff.length; i++) {
samples.push(samples[i - 1] + diff[i]);
}
if(samples.length !== this.header.nSamples) {
throw("[STEIM1] Data integrity check failed: number of samples does not match");
}
// Do a final integrity check
if(samples[samples.length - 1] !== last) {
throw("[STEIM1] Data integrity check failed: reverse integration constant does not match");
}
return samples;
}
mSEEDRecord.prototype.unpackSTEIM2 = function(data) {
/* Function unpackSTEIM2
*
* Unpacks STEIM2 encoded data record
* and returns the array of data
*
* Implemented after libmseed
*/
// Read the first (X0) and last sample (XN)
var first = data.readInt32BE(4);
var last = data.readInt32BE(8);
var nibble;
var decodeNibble;
var wn, w0;
var val;
var nFrames = this.header.nFrames || (data.length / 64);
// Array to collect STEIM2 differences
var diff = new Array();
// Go over the data frames
for(var i = 0; i < nFrames; i++) {
w0 = data.readInt32BE(i * 64);
// Go over the 16 32-bit long words in a frame
for(var j = 0; j < 16; j++) {
// Read the Cj of w0 from left to right
nibble = (w0 >> ((16 - j - 1) * 2)) & 0x03;
// Empty nibble
if(nibble === 0) {
continue;
}
if(nibble === 1) {
// STEIM-1 four 8-bit differences
for(var k = 0; k < 4; k++) {
diff.push(data.readInt8((i * 64) + (j * 4) + k));
}
} else if(nibble === 2) {
// Read the top two bits to find the decodeNibble
wn = data.readInt32BE((i * 64) + (j * 4));
decodeNibble = (wn >> 30) & 0x3;
switch(decodeNibble) {
// One 30-bit difference
case 0x01:
for(var k = 0; k < 1; k++) {
val = (wn >> ((1 - k - 1) * 30)) & 0x3fffffff;
val = (val & 0x20000000) ? val | ~0x3fffffff : val;
diff.push(val);
}
break;
// Two 15-bit differences
case 0x02:
for(var k = 0; k < 2; k++) {
val = (wn >> ((2 - k - 1) * 15)) & 0x00007fff;
val = (val & 0x00004000) ? val | ~0x00007fff : val;
diff.push(val);
}
break;
// Three 10-bit differences
case 0x03:
for(var k = 0; k < 3; k++) {
val = (wn >> ((3 - k - 1) * 10)) & 0x000003ff;
val = (val & 0x00000200) ? val | ~0x000003ff : val;
diff.push(val);
}
break;
default:
throw("[STEIM2] Data integrity check failed: unknown decode nibble");
}
} else if(nibble === 3) {
// Read the top two bits to find the decodeNibble
wn = data.readInt32BE((i * 64) + (j * 4));
decodeNibble = (wn >> 30) & 0x3;
switch(decodeNibble) {
// Five 6-bit differences
case 0x00:
for(var k = 0; k < 5; k++) {
val = (wn >> ((5 - k - 1) * 6)) & 0x0000003f;
val = (val & 0x00000020) ? val | ~0x0000003f : val;
diff.push(val);
}
break;
// Six 5-bit differences
case 0x01:
for(var k = 0; k < 6; k++) {
val = (wn >> ((6 - k - 1) * 5)) & 0x0000001f;
val = (val & 0x00000010) ? val | ~0x0000001f : val;
diff.push(val);
}
break;
// Seven 4-bit differences
case 0x02:
for(var k = 0; k < 7; k++) {
val = (wn >> ((7 - k - 1) * 4)) & 0x0000000f;
val = (val & 0x00000008) ? val | ~0x0000000f : val;
diff.push(val);
}
break;
default:
throw("[STEIM2] Data integrity check failed: unknown decode nibble");
}
}
}
}
// Construct the samples array of data diff
var samples = [first];
for(var i = 1; i < diff.length; i++) {
samples.push(samples[i - 1] + diff[i]);
}
if(samples.length !== this.header.nSamples) {
throw("[STEIM2] Data integrity check failed: number of samples does not match");
}
// Do a final integrity check
if(samples[samples.length - 1] !== last) {
throw("[STEIM2] Data integrity check failed: reverse integration constant does not match");
}
return samples;
}
module.exports = mSEEDRecord;