-
Notifications
You must be signed in to change notification settings - Fork 34
/
unpack.c
1667 lines (1434 loc) · 56.6 KB
/
unpack.c
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Generic routines to unpack miniSEED records.
*
* Appropriate values from the record header will be byte-swapped to
* the host order. The purpose of this code is to provide a portable
* way of accessing common SEED data record header information. All
* data structures in SEED 2.4 data records are supported. The data
* samples are optionally decompressed/unpacked.
*
* This file is part of the miniSEED Library.
*
* Copyright (c) 2024 Chad Trabant, EarthScope Data Services
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libmseed.h"
#include "mseedformat.h"
#include "unpack.h"
#include "unpackdata.h"
/* Function(s) internal to this file */
static nstime_t ms_btime2nstime (uint8_t *btime, int8_t swapflag);
/* Test POINTER for alignment with BYTE_COUNT sized quantities */
#define is_aligned(POINTER, BYTE_COUNT) \
(((uintptr_t) (const void *)(POINTER)) % (BYTE_COUNT) == 0)
/***************************************************************************
* Unpack a miniSEED 3.x data record and populate a MS3Record struct.
*
* If MSF_UNPACKDATA is set in flags, the data samples are
* unpacked/decompressed and the ::MS3Record.datasamples pointer is set
* appropriately. The data samples will be either 32-bit integers,
* 32-bit floats or 64-bit floats (doubles) with the same byte order
* as the host machine. The MS3Record->numsamples will be set to the
* actual number of samples unpacked/decompressed and
* MS3Record->sampletype will indicated the sample type.
*
* If MSF_VALIDATECRC is set in flags, the CRC in the record will be
* validated. If the calculated CRC does not match, the MS_INVALIDCRC
* error is returned.
*
* All appropriate values will be byte-swapped to the host order,
* including the data samples.
*
* All MS3Record struct values, including data samples and data
* samples will be overwritten by subsequent calls to this function.
*
* If the 'msr' struct is NULL it will be allocated.
*
* Returns MS_NOERROR and populates the MS3Record struct at *ppmsr on
* success, otherwise returns a libmseed error code (listed in
* libmseed.h).
*
* \ref MessageOnError - this function logs a message on error
***************************************************************************/
int64_t
msr3_unpack_mseed3 (const char *record, int reclen, MS3Record **ppmsr,
uint32_t flags, int8_t verbose)
{
MS3Record *msr = NULL;
uint32_t calculated_crc;
uint32_t header_crc;
uint8_t sidlength = 0;
int8_t swapflag;
int bigendianhost = ms_bigendianhost ();
int64_t retval;
if (!record || !ppmsr)
{
ms_log (2, "%s(): Required input not defined: 'record' or 'ppmsr'\n", __func__);
return MS_GENERROR;
}
/* Verify that passed record length is within supported range */
if (reclen < MINRECLEN || reclen > MAXRECLEN)
{
ms_log (2, "Record length is out of allowed range: %d\n", reclen);
return MS_OUTOFRANGE;
}
/* Verify that record includes a valid header */
if (!MS3_ISVALIDHEADER (record))
{
ms_log (2, "Record header unrecognized, not a valid miniSEED record\n");
return MS_NOTSEED;
}
/* miniSEED 3 is little endian */
swapflag = (bigendianhost) ? 1 : 0;
if (verbose > 2)
{
if (swapflag)
ms_log (0, "Byte swapping needed for unpacking of header\n");
else
ms_log (0, "Byte swapping NOT needed for unpacking of header\n");
}
sidlength = *pMS3FSDH_SIDLENGTH (record);
/* Record SID length must be at most one less than maximum size to leave a byte for termination */
if (sidlength >= sizeof (msr->sid))
{
ms_log (2, "%.*s: Source identifier is longer (%d) than supported (%d)\n",
sidlength, pMS3FSDH_SID (record), sidlength, (int)sizeof (msr->sid) - 1);
return MS_GENERROR;
}
/* Validate the CRC */
if (flags & MSF_VALIDATECRC)
{
/* Save header CRC, set value to 0, calculate CRC, restore CRC */
header_crc = HO4u (*pMS3FSDH_CRC (record), swapflag);
memset (pMS3FSDH_CRC(record), 0, sizeof(uint32_t));
calculated_crc = ms_crc32c ((const uint8_t*)record, reclen, 0);
*pMS3FSDH_CRC(record) = HO4u (header_crc, swapflag);
if (header_crc != calculated_crc)
{
ms_log (2, "%.*s: CRC is invalid, miniSEED record may be corrupt, header: 0x%X calculated: 0x%X\n",
sidlength, pMS3FSDH_SID (record), header_crc, calculated_crc);
return MS_INVALIDCRC;
}
}
/* Initialize the MS3Record */
if (!(*ppmsr = msr3_init (*ppmsr)))
return MS_GENERROR;
/* Shortcut pointer, historical and helps readability */
msr = *ppmsr;
/* Set raw record pointer and record length */
msr->record = record;
msr->reclen = reclen;
/* Populate the header fields */
msr->swapflag = (swapflag) ? MSSWAP_HEADER : 0;
msr->formatversion = *pMS3FSDH_FORMATVERSION (record);
msr->flags = *pMS3FSDH_FLAGS (record);
memcpy (msr->sid, pMS3FSDH_SID (record), sidlength);
msr->starttime = ms_time2nstime (HO2u (*pMS3FSDH_YEAR (record), msr->swapflag),
HO2u (*pMS3FSDH_DAY (record), msr->swapflag),
*pMS3FSDH_HOUR (record),
*pMS3FSDH_MIN (record),
*pMS3FSDH_SEC (record),
HO4u (*pMS3FSDH_NSEC (record), msr->swapflag));
if (msr->starttime == NSTERROR)
{
ms_log (2, "%.*s: Cannot convert start time to internal time representation\n",
sidlength, pMS3FSDH_SID (record));
return MS_GENERROR;
}
msr->encoding = *pMS3FSDH_ENCODING (record);
msr->samprate = HO8f (*pMS3FSDH_SAMPLERATE (record), msr->swapflag);
msr->samplecnt = HO4u (*pMS3FSDH_NUMSAMPLES (record), msr->swapflag);
msr->crc = HO4u (*pMS3FSDH_CRC (record), msr->swapflag);
msr->pubversion = *pMS3FSDH_PUBVERSION (record);
/* Copy extra headers into a NULL-terminated string */
msr->extralength = HO2u (*pMS3FSDH_EXTRALENGTH (record), msr->swapflag);
if (msr->extralength)
{
if ((msr->extra = (char *)libmseed_memory.malloc (msr->extralength + 1)) == NULL)
{
ms_log (2, "%s: Cannot allocate memory for extra headers\n", msr->sid);
return MS_GENERROR;
}
memcpy (msr->extra, record + MS3FSDH_LENGTH + sidlength, msr->extralength);
msr->extra[msr->extralength] = '\0';
}
msr->datalength = HO4u (*pMS3FSDH_DATALENGTH (record), msr->swapflag);
/* Determine data payload byte swapping.
Steim encodings are big endian.
All other encodings are little endian, matching the header. */
if (msr->encoding == DE_STEIM1 || msr->encoding == DE_STEIM2)
{
if (! bigendianhost)
msr->swapflag |= MSSWAP_PAYLOAD;
}
else if (msr->swapflag & MSSWAP_HEADER)
{
msr->swapflag |= MSSWAP_PAYLOAD;
}
/* Unpack the data samples if requested */
if ((flags & MSF_UNPACKDATA) && msr->samplecnt > 0)
{
retval = msr3_unpack_data (msr, verbose);
if (retval < 0)
return retval;
else
msr->numsamples = retval;
}
else
{
if (msr->datasamples)
libmseed_memory.free (msr->datasamples);
msr->datasamples = NULL;
msr->datasize = 0;
msr->numsamples = 0;
}
return MS_NOERROR;
} /* End of msr3_unpack_mseed3() */
/***************************************************************************
* Unpack a miniSEED 2.x data record and populate a MS3Record struct.
*
* If MSF_UNPACKDATA is set in flags the data samples are
* unpacked/decompressed and the ::MS3Record.datasamples pointer is set
* appropriately. The data samples will be either 32-bit integers,
* 32-bit floats or 64-bit floats (doubles) with the same byte order
* as the host machine. The MS3Record->numsamples will be set to the
* actual number of samples unpacked/decompressed and
* MS3Record->sampletype will indicated the sample type.
*
* All appropriate values will be byte-swapped to the host order,
* including the data samples.
*
* All MS3Record struct values, including data samples and data
* samples will be overwritten by subsequent calls to this function.
*
* If the 'msr' struct is NULL it will be allocated.
*
* Returns MS_NOERROR and populates the MS3Record struct at *ppmsr on
* success, otherwise returns a libmseed error code (listed in
* libmseed.h).
*
* \ref MessageOnError - this function logs a message on error
***************************************************************************/
int64_t
msr3_unpack_mseed2 (const char *record, int reclen, MS3Record **ppmsr,
uint32_t flags, int8_t verbose)
{
int B1000offset = 0;
int B1001offset = 0;
int bigendianhost = ms_bigendianhost ();
int64_t retval;
MS3Record *msr = NULL;
char errorsid[64];
int length;
int ione = 1;
int64_t ival;
double dval;
char sval[64];
/* For blockette parsing */
int blkt_offset;
int blkt_count = 0;
int blkt_length;
int blkt_end = 0;
uint16_t blkt_type;
uint16_t next_blkt;
LM_PARSED_JSON *parsestate = NULL;
MSEHEventDetection eventdetection;
MSEHCalibration calibration;
MSEHTimingException exception;
if (!record || !ppmsr)
{
ms_log (2, "%s(): Required input not defined: 'record' or 'ppmsr'\n", __func__);
return MS_GENERROR;
}
/* Verify that passed record length is within supported range */
if (reclen < 64 || reclen > MAXRECLEN)
{
ms2_recordsid (record, errorsid, sizeof (errorsid));
ms_log (2, "%s: Record length is out of allowed range: %d\n", errorsid, reclen);
return MS_OUTOFRANGE;
}
/* Verify that record includes a valid header */
if (!MS2_ISVALIDHEADER (record))
{
ms2_recordsid (record, errorsid, sizeof (errorsid));
ms_log (2, "%s: Record header unrecognized, not a valid miniSEED record\n", errorsid);
return MS_NOTSEED;
}
/* Initialize the MS3Record */
if (!(*ppmsr = msr3_init (*ppmsr)))
return MS_GENERROR;
/* Shortcut pointer, historical and helps readability */
msr = *ppmsr;
/* Set raw record pointer and record length */
msr->record = record;
msr->reclen = reclen;
/* Check to see if byte swapping is needed by testing the year and day */
if (!MS_ISVALIDYEARDAY (*pMS2FSDH_YEAR (record), *pMS2FSDH_DAY (record)))
msr->swapflag = MSSWAP_HEADER;
/* Report byte swapping status */
if (verbose > 2)
{
if (msr->swapflag)
ms_log (0, "Byte swapping needed for unpacking of header\n");
else
ms_log (0, "Byte swapping NOT needed for unpacking of header\n");
}
/* Populate some of the common header fields */
ms2_recordsid (record, msr->sid, sizeof (msr->sid));
msr->formatversion = 2;
msr->samprate = ms_nomsamprate (HO2d (*pMS2FSDH_SAMPLERATEFACT (record), msr->swapflag),
HO2d (*pMS2FSDH_SAMPLERATEMULT (record), msr->swapflag));
msr->samplecnt = HO2u (*pMS2FSDH_NUMSAMPLES (record), msr->swapflag);
/* Map data quality indicator to publication version */
if (*pMS2FSDH_DATAQUALITY (record) == 'M')
msr->pubversion = 4;
else if (*pMS2FSDH_DATAQUALITY (record) == 'Q')
msr->pubversion = 3;
else if (*pMS2FSDH_DATAQUALITY (record) == 'D')
msr->pubversion = 2;
else if (*pMS2FSDH_DATAQUALITY (record) == 'R')
msr->pubversion = 1;
else
msr->pubversion = 0;
/* Map activity bits */
if (*pMS2FSDH_ACTFLAGS (record) & 0x01) /* Bit 0 */
msr->flags |= 0x01;
if (*pMS2FSDH_ACTFLAGS (record) & 0x04) /* Bit 2 */
mseh_set_ptr_r (msr, "/FDSN/Event/Begin", &ione, 'b', &parsestate);
if (*pMS2FSDH_ACTFLAGS (record) & 0x08) /* Bit 3 */
mseh_set_ptr_r (msr, "/FDSN/Event/End", &ione, 'b', &parsestate);
if (*pMS2FSDH_ACTFLAGS (record) & 0x10) /* Bit 4 */
{
ival = 1;
mseh_set_ptr_r (msr, "/FDSN/Time/LeapSecond", &ival, 'i', &parsestate);
}
if (*pMS2FSDH_ACTFLAGS (record) & 0x20) /* Bit 5 */
{
ival = -1;
mseh_set_ptr_r (msr, "/FDSN/Time/LeapSecond", &ival, 'i', &parsestate);
}
if (*pMS2FSDH_ACTFLAGS (record) & 0x40) /* Bit 6 */
mseh_set_ptr_r (msr, "/FDSN/Event/InProgress", &ione, 'b', &parsestate);
/* Map I/O and clock flags */
if (*pMS2FSDH_IOFLAGS (record) & 0x01) /* Bit 0 */
mseh_set_ptr_r (msr, "/FDSN/Flags/StationVolumeParityError", &ione, 'b', &parsestate);
if (*pMS2FSDH_IOFLAGS (record) & 0x02) /* Bit 1 */
mseh_set_ptr_r (msr, "/FDSN/Flags/LongRecordRead", &ione, 'b', &parsestate);
if (*pMS2FSDH_IOFLAGS (record) & 0x04) /* Bit 2 */
mseh_set_ptr_r (msr, "/FDSN/Flags/ShortRecordRead", &ione, 'b', &parsestate);
if (*pMS2FSDH_IOFLAGS (record) & 0x08) /* Bit 3 */
mseh_set_ptr_r (msr, "/FDSN/Flags/StartOfTimeSeries", &ione, 'b', &parsestate);
if (*pMS2FSDH_IOFLAGS (record) & 0x10) /* Bit 4 */
mseh_set_ptr_r (msr, "/FDSN/Flags/EndOfTimeSeries", &ione, 'b', &parsestate);
if (*pMS2FSDH_IOFLAGS (record) & 0x20) /* Bit 5 */
msr->flags |= 0x04;
/* Map data quality flags */
if (*pMS2FSDH_DQFLAGS (record) & 0x01) /* Bit 0 */
mseh_set_ptr_r (msr, "/FDSN/Flags/AmplifierSaturation", &ione, 'b', &parsestate);
if (*pMS2FSDH_DQFLAGS (record) & 0x02) /* Bit 1 */
mseh_set_ptr_r (msr, "/FDSN/Flags/DigitizerClipping", &ione, 'b', &parsestate);
if (*pMS2FSDH_DQFLAGS (record) & 0x04) /* Bit 2 */
mseh_set_ptr_r (msr, "/FDSN/Flags/Spikes", &ione, 'b', &parsestate);
if (*pMS2FSDH_DQFLAGS (record) & 0x08) /* Bit 3 */
mseh_set_ptr_r (msr, "/FDSN/Flags/Glitches", &ione, 'b', &parsestate);
if (*pMS2FSDH_DQFLAGS (record) & 0x10) /* Bit 4 */
mseh_set_ptr_r (msr, "/FDSN/Flags/MissingData", &ione, 'b', &parsestate);
if (*pMS2FSDH_DQFLAGS (record) & 0x20) /* Bit 5 */
mseh_set_ptr_r (msr, "/FDSN/Flags/TelemetrySyncError", &ione, 'b', &parsestate);
if (*pMS2FSDH_DQFLAGS (record) & 0x40) /* Bit 6 */
mseh_set_ptr_r (msr, "/FDSN/Flags/FilterCharging", &ione, 'b', &parsestate);
if (*pMS2FSDH_DQFLAGS (record) & 0x80) /* Bit 7 */
msr->flags |= 0x02;
dval = (double)HO4d (*pMS2FSDH_TIMECORRECT (record), msr->swapflag);
if (dval != 0.0)
{
dval = dval / 10000.0;
mseh_set_ptr_r (msr, "/FDSN/Time/Correction", &dval, 'n', &parsestate);
}
/* Traverse the blockettes */
blkt_offset = HO2u (*pMS2FSDH_BLOCKETTEOFFSET (record), msr->swapflag);
while ((blkt_offset != 0) &&
(blkt_offset < reclen) &&
(blkt_offset < MAXRECLEN))
{
/* Every blockette has a similar 4 byte header: type and next */
memcpy (&blkt_type, record + blkt_offset, 2);
memcpy (&next_blkt, record + blkt_offset + 2, 2);
if (msr->swapflag)
{
ms_gswap2 (&blkt_type);
ms_gswap2 (&next_blkt);
}
/* Get blockette length */
blkt_length = ms2_blktlen (blkt_type, record + blkt_offset, msr->swapflag);
if (blkt_length == 0)
{
ms_log (2, "%s: Unknown blockette length for type %d\n", msr->sid, blkt_type);
break;
}
/* Make sure blockette is contained within the msrecord buffer */
if ((blkt_offset + blkt_length) > reclen)
{
ms_log (2, "%s: Blockette %d extends beyond record size, truncated?\n", msr->sid, blkt_type);
break;
}
blkt_end = blkt_offset + blkt_length;
if (blkt_type == 100)
{
msr->samprate = HO4f (*pMS2B100_SAMPRATE (record + blkt_offset), msr->swapflag);
}
/* Blockette 200, generic event detection */
else if (blkt_type == 200)
{
memset (&eventdetection, 0, sizeof(eventdetection));
strncpy (eventdetection.type, "GENERIC", sizeof (eventdetection.type));
ms_strncpcleantail (eventdetection.detector, pMS2B200_DETECTOR (record + blkt_offset), 24);
eventdetection.signalamplitude = HO4f (*pMS2B200_AMPLITUDE (record + blkt_offset), msr->swapflag);
eventdetection.signalperiod = HO4f (*pMS2B200_PERIOD (record + blkt_offset), msr->swapflag);
eventdetection.backgroundestimate = HO4f (*pMS2B200_BACKGROUNDEST (record + blkt_offset), msr->swapflag);
/* If bit 2 is set, set compression wave according to bit 0 */
if (*pMS2B200_FLAGS (record + blkt_offset) & 0x04)
{
if (*pMS2B200_FLAGS (record + blkt_offset) & 0x01)
strncpy (eventdetection.wave, "DILATATION", sizeof (eventdetection.wave));
else
strncpy (eventdetection.wave, "COMPRESSION", sizeof (eventdetection.wave));
}
else
eventdetection.wave[0] = '\0';
if (*pMS2B200_FLAGS (record + blkt_offset) & 0x02)
strncpy (eventdetection.units, "DECONVOLVED", sizeof (eventdetection.units));
else
strncpy (eventdetection.units, "COUNTS", sizeof (eventdetection.units));
eventdetection.onsettime = ms_btime2nstime ((uint8_t*)pMS2B200_YEAR (record + blkt_offset), msr->swapflag);
if (eventdetection.onsettime == NSTERROR)
return MS_GENERROR;
memset (eventdetection.medsnr, 0, 6);
eventdetection.medlookback = -1;
eventdetection.medpickalgorithm = -1;
eventdetection.next = NULL;
if (mseh_add_event_detection_r (msr, NULL, &eventdetection, &parsestate))
{
ms_log (2, "%s: Problem mapping Blockette 200 to extra headers\n", msr->sid);
return MS_GENERROR;
}
}
/* Blockette 201, Murdock event detection */
else if (blkt_type == 201)
{
memset (&eventdetection, 0, sizeof(eventdetection));
strncpy (eventdetection.type, "MURDOCK", sizeof (eventdetection.type));
ms_strncpcleantail (eventdetection.detector, pMS2B201_DETECTOR (record + blkt_offset), 24);
eventdetection.signalamplitude = HO4f (*pMS2B201_AMPLITUDE (record + blkt_offset), msr->swapflag);
eventdetection.signalperiod = HO4f (*pMS2B201_PERIOD (record + blkt_offset), msr->swapflag);
eventdetection.backgroundestimate = HO4f (*pMS2B201_BACKGROUNDEST (record + blkt_offset), msr->swapflag);
/* If bit 0 is set, dilatation wave otherwise compression */
if (*pMS2B201_FLAGS (record + blkt_offset) & 0x01)
strncpy (eventdetection.wave, "DILATATION", sizeof (eventdetection.wave));
else
strncpy (eventdetection.wave, "COMPRESSION", sizeof (eventdetection.wave));
eventdetection.onsettime = ms_btime2nstime ((uint8_t*)pMS2B201_YEAR (record + blkt_offset), msr->swapflag);
if (eventdetection.onsettime == NSTERROR)
return MS_GENERROR;
memcpy (eventdetection.medsnr, pMS2B201_MEDSNR (record + blkt_offset), 6);
eventdetection.medlookback = *pMS2B201_LOOPBACK (record + blkt_offset);
eventdetection.medpickalgorithm = *pMS2B201_PICKALGORITHM (record + blkt_offset);
eventdetection.next = NULL;
if (mseh_add_event_detection_r (msr, NULL, &eventdetection, &parsestate))
{
ms_log (2, "%s: Problem mapping Blockette 201 to extra headers\n", msr->sid);
return MS_GENERROR;
}
}
/* Blockette 300, step calibration */
else if (blkt_type == 300)
{
memset (&calibration, 0, sizeof(calibration));
strncpy (calibration.type, "STEP", sizeof (calibration.type));
calibration.begintime = ms_btime2nstime ((uint8_t*)pMS2B300_YEAR (record + blkt_offset), msr->swapflag);
if (calibration.begintime == NSTERROR)
return MS_GENERROR;
calibration.endtime = NSTERROR;
calibration.steps = *pMS2B300_NUMCALIBRATIONS (record + blkt_offset);
/* If bit 0 is set, first puluse is positive */
calibration.firstpulsepositive = -1;
if (*pMS2B300_FLAGS (record + blkt_offset) & 0x01)
calibration.firstpulsepositive = 1;
/* If bit 1 is set, calibration's alternate sign */
calibration.alternatesign = -1;
if (*pMS2B300_FLAGS (record + blkt_offset) & 0x02)
calibration.alternatesign = 1;
/* If bit 2 is set, calibration is automatic, otherwise manual */
if (*pMS2B300_FLAGS (record + blkt_offset) & 0x04)
strncpy (calibration.trigger, "AUTOMATIC", sizeof (calibration.trigger));
else
strncpy (calibration.trigger, "MANUAL", sizeof (calibration.trigger));
/* If bit 3 is set, continued from previous record */
calibration.continued = -1;
if (*pMS2B300_FLAGS (record + blkt_offset) & 0x08)
calibration.continued = 1;
calibration.duration = (double)(HO4u (*pMS2B300_STEPDURATION (record + blkt_offset), msr->swapflag) / 10000.0);
calibration.stepbetween = (double)(HO4u (*pMS2B300_INTERVALDURATION (record + blkt_offset), msr->swapflag) / 10000.0);
calibration.amplitude = HO4f (*pMS2B300_AMPLITUDE (record + blkt_offset), msr->swapflag);
ms_strncpcleantail (calibration.inputchannel, pMS2B300_INPUTCHANNEL (record + blkt_offset), 3);
calibration.inputunits[0] = '\0';
calibration.amplituderange[0] = '\0';
calibration.sineperiod = 0.0;
calibration.refamplitude = (double)(HO4u (*pMS2B300_REFERENCEAMPLITUDE (record + blkt_offset), msr->swapflag));
ms_strncpcleantail (calibration.coupling, pMS2B300_COUPLING (record + blkt_offset), 12);
ms_strncpcleantail (calibration.rolloff, pMS2B300_ROLLOFF (record + blkt_offset), 12);
calibration.noise[0] = '\0';
calibration.next = NULL;
if (mseh_add_calibration_r (msr, NULL, &calibration, &parsestate))
{
ms_log (2, "%s: Problem mapping Blockette 300 to extra headers\n", msr->sid);
return MS_GENERROR;
}
}
/* Blockette 310, sine calibration */
else if (blkt_type == 310)
{
memset (&calibration, 0, sizeof(calibration));
strncpy (calibration.type, "SINE", sizeof (calibration.type));
calibration.begintime = ms_btime2nstime ((uint8_t*)pMS2B310_YEAR (record + blkt_offset), msr->swapflag);
if (calibration.begintime == NSTERROR)
return MS_GENERROR;
calibration.endtime = NSTERROR;
calibration.steps = -1;
calibration.firstpulsepositive = -1;
calibration.alternatesign = -1;
/* If bit 2 is set, calibration is automatic, otherwise manual */
if (*pMS2B310_FLAGS (record + blkt_offset) & 0x04)
strncpy (calibration.trigger, "AUTOMATIC", sizeof (calibration.trigger));
else
strncpy (calibration.trigger, "MANUAL", sizeof (calibration.trigger));
/* If bit 3 is set, continued from previous record */
calibration.continued = -1;
if (*pMS2B310_FLAGS (record + blkt_offset) & 0x08)
calibration.continued = 1;
calibration.amplituderange[0] = '\0';
/* If bit 4 is set, peak to peak amplitude */
if (*pMS2B310_FLAGS (record + blkt_offset) & 0x10)
strncpy (calibration.amplituderange, "PEAKTOPEAK", sizeof (calibration.amplituderange));
/* Otherwise, if bit 5 is set, zero to peak amplitude */
else if (*pMS2B310_FLAGS (record + blkt_offset) & 0x20)
strncpy (calibration.amplituderange, "ZEROTOPEAK", sizeof (calibration.amplituderange));
/* Otherwise, if bit 6 is set, RMS amplitude */
else if (*pMS2B310_FLAGS (record + blkt_offset) & 0x40)
strncpy (calibration.amplituderange, "RMS", sizeof (calibration.amplituderange));
calibration.duration = (double)(HO4u (*pMS2B310_DURATION (record + blkt_offset), msr->swapflag) / 10000.0);
calibration.sineperiod = HO4f (*pMS2B310_PERIOD (record + blkt_offset), msr->swapflag);
calibration.amplitude = HO4f (*pMS2B310_AMPLITUDE (record + blkt_offset), msr->swapflag);
ms_strncpcleantail (calibration.inputchannel, pMS2B310_INPUTCHANNEL (record + blkt_offset), 3);
calibration.refamplitude = (double)(HO4u (*pMS2B310_REFERENCEAMPLITUDE (record + blkt_offset), msr->swapflag));
calibration.stepbetween = 0.0;
calibration.inputunits[0] = '\0';
ms_strncpcleantail (calibration.coupling, pMS2B310_COUPLING (record + blkt_offset), 12);
ms_strncpcleantail (calibration.rolloff, pMS2B310_ROLLOFF (record + blkt_offset), 12);
calibration.noise[0] = '\0';
calibration.next = NULL;
if (mseh_add_calibration_r (msr, NULL, &calibration, &parsestate))
{
ms_log (2, "%s: Problem mapping Blockette 310 to extra headers\n", msr->sid);
return MS_GENERROR;
}
}
/* Blockette 320, pseudo-random calibration */
else if (blkt_type == 320)
{
memset (&calibration, 0, sizeof(calibration));
strncpy (calibration.type, "PSEUDORANDOM", sizeof (calibration.type));
calibration.begintime = ms_btime2nstime ((uint8_t*)pMS2B320_YEAR (record + blkt_offset), msr->swapflag);
if (calibration.begintime == NSTERROR)
return MS_GENERROR;
calibration.endtime = NSTERROR;
calibration.steps = -1;
calibration.firstpulsepositive = -1;
calibration.alternatesign = -1;
/* If bit 2 is set, calibration is automatic, otherwise manual */
if (*pMS2B320_FLAGS (record + blkt_offset) & 0x04)
strncpy (calibration.trigger, "AUTOMATIC", sizeof (calibration.trigger));
else
strncpy (calibration.trigger, "MANUAL", sizeof (calibration.trigger));
/* If bit 3 is set, continued from previous record */
calibration.continued = -1;
if (*pMS2B320_FLAGS (record + blkt_offset) & 0x08)
calibration.continued = 1;
calibration.amplituderange[0] = '\0';
/* If bit 4 is set, peak to peak amplitude */
if (*pMS2B320_FLAGS (record + blkt_offset) & 0x10)
strncpy (calibration.amplituderange, "RANDOM", sizeof (calibration.amplituderange));
calibration.duration = (double)(HO4u (*pMS2B320_DURATION (record + blkt_offset), msr->swapflag) / 10000.0);
calibration.amplitude = HO4f (*pMS2B320_PTPAMPLITUDE (record + blkt_offset), msr->swapflag);
ms_strncpcleantail (calibration.inputchannel, pMS2B320_INPUTCHANNEL (record + blkt_offset), 3);
calibration.refamplitude = (double)(HO4u (*pMS2B320_REFERENCEAMPLITUDE (record + blkt_offset), msr->swapflag));
calibration.sineperiod = 0.0;
calibration.stepbetween = 0.0;
calibration.inputunits[0] = '\0';
ms_strncpcleantail (calibration.coupling, pMS2B320_COUPLING (record + blkt_offset), 12);
ms_strncpcleantail (calibration.rolloff, pMS2B320_ROLLOFF (record + blkt_offset), 12);
ms_strncpcleantail (calibration.noise, pMS2B320_NOISETYPE (record + blkt_offset), 8);
calibration.next = NULL;
if (mseh_add_calibration_r (msr, NULL, &calibration, &parsestate))
{
ms_log (2, "%s: Problem mapping Blockette 320 to extra headers\n", msr->sid);
return MS_GENERROR;
}
}
/* Blockette 390, generic calibration */
else if (blkt_type == 390)
{
memset (&calibration, 0, sizeof(calibration));
strncpy (calibration.type, "GENERIC", sizeof (calibration.type));
calibration.begintime = ms_btime2nstime ((uint8_t*)pMS2B390_YEAR (record + blkt_offset), msr->swapflag);
if (calibration.begintime == NSTERROR)
return MS_GENERROR;
calibration.endtime = NSTERROR;
calibration.steps = -1;
calibration.firstpulsepositive = -1;
calibration.alternatesign = -1;
/* If bit 2 is set, calibration is automatic, otherwise manual */
if (*pMS2B390_FLAGS (record + blkt_offset) & 0x04)
strncpy (calibration.trigger, "AUTOMATIC", sizeof (calibration.trigger));
else
strncpy (calibration.trigger, "MANUAL", sizeof (calibration.trigger));
/* If bit 3 is set, continued from previous record */
calibration.continued = -1;
if (*pMS2B390_FLAGS (record + blkt_offset) & 0x08)
calibration.continued = 1;
calibration.amplituderange[0] = '\0';
calibration.duration = (double)(HO4u (*pMS2B390_DURATION (record + blkt_offset), msr->swapflag) / 10000.0);
calibration.amplitude = HO4f (*pMS2B390_AMPLITUDE (record + blkt_offset), msr->swapflag);
ms_strncpcleantail (calibration.inputchannel, pMS2B390_INPUTCHANNEL (record + blkt_offset), 3);
calibration.refamplitude = 0.0;
calibration.sineperiod = 0.0;
calibration.stepbetween = 0.0;
calibration.inputunits[0] = '\0';
calibration.coupling[0] = '\0';
calibration.rolloff[0] = '\0';
calibration.noise[0] = '\0';
calibration.next = NULL;
if (mseh_add_calibration_r (msr, NULL, &calibration, &parsestate))
{
ms_log (2, "%s: Problem mapping Blockette 390 to extra headers\n", msr->sid);
return MS_GENERROR;
}
}
/* Blockette 395, calibration abort */
else if (blkt_type == 395)
{
memset (&calibration, 0, sizeof(calibration));
strncpy (calibration.type, "ABORT", sizeof (calibration.type));
calibration.begintime = NSTERROR;
calibration.endtime = ms_btime2nstime ((uint8_t*)pMS2B395_YEAR (record + blkt_offset), msr->swapflag);
if (calibration.endtime == NSTERROR)
return MS_GENERROR;
calibration.steps = -1;
calibration.firstpulsepositive = -1;
calibration.alternatesign = -1;
calibration.trigger[0] = '\0';
calibration.continued = -1;
calibration.amplituderange[0] = '\0';
calibration.duration = 0.0;
calibration.amplitude = 0.0;
calibration.inputchannel[0] = '\0';
calibration.refamplitude = 0.0;
calibration.sineperiod = 0.0;
calibration.stepbetween = 0.0;
calibration.inputunits[0] = '\0';
calibration.coupling[0] = '\0';
calibration.rolloff[0] = '\0';
calibration.noise[0] = '\0';
calibration.next = NULL;
if (mseh_add_calibration_r (msr, NULL, &calibration, &parsestate))
{
ms_log (2, "%s: Problem mapping Blockette 395 to extra headers\n", msr->sid);
return MS_GENERROR;
}
}
/* Blockette 400, beam blockette */
else if (blkt_type == 400)
{
ms_log (1, "%s: WARNING Blockette 400 is present but discarded\n", msr->sid);
}
/* Blockette 400, beam delay blockette */
else if (blkt_type == 405)
{
ms_log (1, "%s: WARNING Blockette 405 is present but discarded\n", msr->sid);
}
/* Blockette 500, timing blockette */
else if (blkt_type == 500)
{
memset (&exception, 0, sizeof(exception));
exception.vcocorrection = HO4f (*pMS2B500_VCOCORRECTION (record + blkt_offset), msr->swapflag);
exception.time = ms_btime2nstime ((uint8_t*)pMS2B500_YEAR (record + blkt_offset), msr->swapflag);
if (exception.time == NSTERROR)
return MS_GENERROR;
/* Apply microsecond precision if non-zero */
if (*pMS2B500_MICROSECOND (record + blkt_offset) != 0)
{
exception.time += (nstime_t)*pMS2B500_MICROSECOND (record + blkt_offset) * (NSTMODULUS / 1000000);
}
exception.receptionquality = *pMS2B500_RECEPTIONQUALITY (record + blkt_offset);
exception.count = HO4u (*pMS2B500_EXCEPTIONCOUNT (record + blkt_offset), msr->swapflag);
ms_strncpcleantail (exception.type, pMS2B500_EXCEPTIONTYPE (record + blkt_offset), 16);
ms_strncpcleantail (exception.clockstatus, pMS2B500_CLOCKSTATUS (record + blkt_offset), 128);
if (mseh_add_timing_exception_r (msr, NULL, &exception, &parsestate))
{
ms_log (2, "%s: Problem mapping Blockette 500 to extra headers\n", msr->sid);
return MS_GENERROR;
}
/* Clock model maps to a single value at /FDSN/Clock/Model */
ms_strncpcleantail (sval, pMS2B500_CLOCKMODEL (record + blkt_offset), 32);
mseh_set_ptr_r (msr, "/FDSN/Clock/Model", sval, 's', &parsestate);
}
else if (blkt_type == 1000)
{
B1000offset = blkt_offset;
/* Calculate record length in bytes as 2^(B1000->reclen) */
msr->reclen = (uint32_t)1 << *pMS2B1000_RECLEN (record + blkt_offset);
/* Compare against the specified length */
if (msr->reclen != reclen && verbose)
{
ms_log (1, "%s: Record length in Blockette 1000 (%d) != specified length (%d)\n",
msr->sid, msr->reclen, reclen);
}
msr->encoding = *pMS2B1000_ENCODING (record + blkt_offset);
}
else if (blkt_type == 1001)
{
B1001offset = blkt_offset;
/* Optimization: if no other extra headers yet, directly print this common value */
if (parsestate == NULL)
{
length = snprintf (sval, sizeof(sval), "{\"FDSN\":{\"Time\":{\"Quality\":%d}}}",
*pMS2B1001_TIMINGQUALITY (record + blkt_offset));
if (!(msr->extra = (char *)libmseed_memory.malloc (length + 1)))
{
ms_log (2, "%s: Cannot allocate memory for extra headers\n", msr->sid);
return MS_GENERROR;
}
memcpy (msr->extra, sval, length + 1);
msr->extralength = length;
}
/* Otherwise add it to existing headers */
else
{
ival = *pMS2B1001_TIMINGQUALITY (record + blkt_offset);
mseh_set_ptr_r (msr, "/FDSN/Time/Quality", &ival, 'i', &parsestate);
}
}
else if (blkt_type == 2000)
{
ms_log (1, "%s: WARNING Blockette 2000 is present but discarded\n", msr->sid);
}
else
{ /* Unknown blockette type */
ms_log (1, "%s: WARNING, unsupported blockette type %d, skipping\n", msr->sid, blkt_type);
}
/* Check that the next blockette offset is beyond the current blockette */
if (next_blkt && next_blkt < (blkt_offset + blkt_length))
{
ms_log (2, "%s: Offset to next blockette (%d) is within current blockette ending at byte %d\n",
msr->sid, next_blkt, (blkt_offset + blkt_length));
blkt_offset = 0;
}
/* Check that the offset is within record length */
else if (next_blkt && next_blkt > reclen)
{
ms_log (2, "%s: Offset to next blockette (%d) from type %d is beyond record length\n",
msr->sid, next_blkt, blkt_type);
blkt_offset = 0;
}
else
{
blkt_offset = next_blkt;
}
blkt_count++;
} /* End of while looping through blockettes */
/* Serialize extra header JSON structure and free parsed state */
if (parsestate)
{
mseh_serialize (msr, &parsestate);
mseh_free_parsestate (&parsestate);
}
/* Check for a Blockette 1000 and log warning if not found */
if (B1000offset == 0 && verbose > 1)
{
ms_log (1, "%s: Warning: No Blockette 1000 found\n", msr->sid);
}
/* Check that the data offset is after the blockette chain */
if (blkt_end &&
HO2u (*pMS2FSDH_NUMSAMPLES (record), msr->swapflag) &&
HO2u (*pMS2FSDH_DATAOFFSET (record), msr->swapflag) < blkt_end)
{
ms_log (1, "%s: Warning: Data offset in fixed header (%d) is within the blockette chain ending at %d\n",
msr->sid, HO2u (*pMS2FSDH_DATAOFFSET (record), msr->swapflag), blkt_end);
}
/* Check that the blockette count matches the number parsed */
if (*pMS2FSDH_NUMBLOCKETTES (record) != blkt_count)
{
ms_log (1, "%s: Warning: Number of blockettes in fixed header (%d) does not match the number parsed (%d)\n",
msr->sid, *pMS2FSDH_NUMBLOCKETTES (record), blkt_count);
}
/* Calculate start time */
msr->starttime = ms_btime2nstime ((uint8_t*)pMS2FSDH_YEAR (record), msr->swapflag);
if (msr->starttime == NSTERROR)
{
ms_log (2, "%s: Cannot convert start time to internal time stamp\n", msr->sid);
return MS_GENERROR;
}
/* Check if a time correction is included and if it has been applied,
* bit 1 of activity flags indicates if it has been appiled */
if (HO4d (*pMS2FSDH_TIMECORRECT (record), msr->swapflag) != 0 &&
!(*pMS2FSDH_ACTFLAGS (record) & 0x02))
{
msr->starttime += (nstime_t)HO4d (*pMS2FSDH_TIMECORRECT (record), msr->swapflag) * (NSTMODULUS / 10000);
}
/* Apply microsecond precision if Blockette 1001 is present */
if (B1001offset)
{
msr->starttime += (nstime_t)*pMS2B1001_MICROSECOND (record + B1001offset) * (NSTMODULUS / 1000000);
}
msr->datalength = HO2u (*pMS2FSDH_DATAOFFSET (record), msr->swapflag);
if (msr->datalength > 0)
msr->datalength = msr->reclen - msr->datalength;
/* Determine byte order of the data and set the swapflag as needed;
if no Blkt1000, assume the order is the same as the header */
if (B1000offset)
{
/* If BE host and LE data need swapping */
if (bigendianhost && *pMS2B1000_BYTEORDER (record + B1000offset) == 0)
msr->swapflag |= MSSWAP_PAYLOAD;
/* If LE host and BE data (or bad byte order value) need swapping */
else if (!bigendianhost && *pMS2B1000_BYTEORDER (record + B1000offset) > 0)
msr->swapflag |= MSSWAP_PAYLOAD;
}
else if (msr->swapflag & MSSWAP_HEADER)
{
msr->swapflag |= MSSWAP_PAYLOAD;
}
/* Unpack the data samples if requested */
if ((flags & MSF_UNPACKDATA) && msr->samplecnt > 0)
{
if (verbose > 2 && msr->swapflag & MSSWAP_PAYLOAD)
ms_log (0, "%s: Byte swapping needed for unpacking of data samples\n", msr->sid);
else if (verbose > 2)
ms_log (0, "%s: Byte swapping NOT needed for unpacking of data samples\n", msr->sid);
retval = msr3_unpack_data (msr, verbose);
if (retval < 0)
return retval;
else
msr->numsamples = retval;
}
else
{
if (msr->datasamples)
libmseed_memory.free (msr->datasamples);
msr->datasamples = NULL;
msr->datasize = 0;
msr->numsamples = 0;
}
return MS_NOERROR;
} /* End of msr3_unpack_mseed2() */
/*******************************************************************/ /**
* @brief Determine the data payload bounds for a MS3Record
*
* Bounds are the starting offset in record and size. For miniSEED