forked from EarthScope/libmseed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseutils.c
1208 lines (1053 loc) · 45 KB
/
parseutils.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
/***************************************************************************
*
* Routines to parse Mini-SEED.
*
* Written by Chad Trabant
* IRIS Data Management Center
*
* modified: 2015.108
***************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libmseed.h"
/**********************************************************************
* msr_parse:
*
* This routine will attempt to parse (detect and unpack) a Mini-SEED
* record from a specified memory buffer and populate a supplied
* MSRecord structure.
*
* If reclen is less than or equal to 0 the length of record is
* automatically detected otherwise reclen should be the correct
* record length.
*
* For auto detection of record length the record should include a
* 1000 blockette or be followed by another record header in the
* buffer.
*
* dataflag will be passed directly to msr_unpack().
*
* Return values:
* 0 : Success, populates the supplied MSRecord.
* >0 : Data record detected but not enough data is present, the
* return value is a hint of how many more bytes are needed.
* <0 : libmseed error code (listed in libmseed.h) is returned.
*********************************************************************/
int
msr_parse (char *record, int recbuflen, MSRecord **ppmsr, int reclen,
flag dataflag, flag verbose)
{
int detlen = 0;
int retcode = 0;
if (!ppmsr)
return MS_GENERROR;
if (!record)
return MS_GENERROR;
/* Sanity check: record length cannot be larger than buffer */
if (reclen > 0 && reclen > recbuflen)
{
ms_log (2, "ms_parse() Record length (%d) cannot be larger than buffer (%d)\n",
reclen, recbuflen);
return MS_GENERROR;
}
/* Autodetect the record length */
if (reclen <= 0)
{
detlen = ms_detect (record, recbuflen);
/* No data record detected */
if (detlen < 0)
{
return MS_NOTSEED;
}
/* Found record but could not determine length */
if (detlen == 0)
{
return MINRECLEN;
}
if (verbose > 2)
{
ms_log (1, "Detected record length of %d bytes\n", detlen);
}
reclen = detlen;
}
/* Check that record length is in supported range */
if (reclen < MINRECLEN || reclen > MAXRECLEN)
{
ms_log (2, "Record length is out of range: %d (allowed: %d to %d)\n",
reclen, MINRECLEN, MAXRECLEN);
return MS_OUTOFRANGE;
}
/* Check if more data is required, return hint */
if (reclen > recbuflen)
{
if (verbose > 2)
ms_log (1, "Detected %d byte record, need %d more bytes\n",
reclen, (reclen - recbuflen));
return (reclen - recbuflen);
}
/* Unpack record */
if ((retcode = msr_unpack (record, reclen, ppmsr, dataflag, verbose)) != MS_NOERROR)
{
msr_free (ppmsr);
return retcode;
}
return MS_NOERROR;
} /* End of msr_parse() */
/**********************************************************************
* msr_parse_selection:
*
* This routine wraps msr_parse() to parse and return the first record
* from a memory buffer that matches optional Selections. If the
* selections pointer is NULL the effect is to search the buffer for
* the first parsable record.
*
* The offset value specifies the starting offset in the buffer and,
* on success, the offset in the buffer to record parsed.
*
* The caller should manage the value of the offset in two ways:
*
* 1) on subsequent calls after a record has been parsed the caller
* should increment the offset by the record length returned or
* properly manipulate the record buffer pointer, buffer length and
* offset to the same effect.
*
* 2) when the end of the buffer is reached MS_GENERROR (-1) is
* returned, the caller should check the offset value against the
* record buffer length to determine when the entire buffer has been
* searched.
*
* Return values: same as msr_parse() except that MS_GENERROR is
* returned when end-of-buffer is reached.
*********************************************************************/
int
msr_parse_selection (char *recbuf, int recbuflen, int64_t *offset,
MSRecord **ppmsr, int reclen,
Selections *selections, flag dataflag, flag verbose)
{
int retval = MS_GENERROR;
int unpackretval;
flag dataswapflag = 0;
flag bigendianhost = ms_bigendianhost ();
if (!ppmsr)
return MS_GENERROR;
if (!recbuf)
return MS_GENERROR;
if (!offset)
return MS_GENERROR;
while (*offset < recbuflen)
{
retval = msr_parse (recbuf + *offset, (int)(recbuflen - *offset), ppmsr, reclen, 0, verbose);
if (retval)
{
if (verbose)
ms_log (2, "Error parsing record at offset %" PRId64 "\n", *offset);
*offset += MINRECLEN;
}
else
{
if (selections && !msr_matchselect (selections, *ppmsr, NULL))
{
*offset += (*ppmsr)->reclen;
retval = MS_GENERROR;
}
else
{
if (dataflag)
{
/* If BE host and LE data need swapping */
if (bigendianhost && (*ppmsr)->byteorder == 0)
dataswapflag = 1;
/* If LE host and BE data (or bad byte order value) need swapping */
else if (!bigendianhost && (*ppmsr)->byteorder > 0)
dataswapflag = 1;
unpackretval = msr_unpack_data (*ppmsr, dataswapflag, verbose);
if (unpackretval < 0)
return unpackretval;
else
(*ppmsr)->numsamples = unpackretval;
}
break;
}
}
}
return retval;
} /* End of msr_parse_selection() */
/********************************************************************
* ms_detect:
*
* Determine SEED data record length with the following steps:
*
* 1) determine that the buffer contains a SEED data record by
* verifying known signatures (fields with known limited values)
*
* 2) search the record up to recbuflen bytes for a 1000 blockette.
*
* 3) If no blockette 1000 is found search at MINRECLEN-byte offsets
* for the fixed section of the next header or blank/noise record,
* thereby implying the record length.
*
* Returns:
* -1 : data record not detected or error
* 0 : data record detected but could not determine length
* >0 : size of the record in bytes
*********************************************************************/
int
ms_detect (const char *record, int recbuflen)
{
uint16_t blkt_offset; /* Byte offset for next blockette */
uint8_t swapflag = 0; /* Byte swapping flag */
uint8_t foundlen = 0; /* Found record length */
int32_t reclen = -1; /* Size of record in bytes */
uint16_t blkt_type;
uint16_t next_blkt;
struct fsdh_s *fsdh;
struct blkt_1000_s *blkt_1000;
const char *nextfsdh;
/* Buffer must be at least 48 bytes (the fixed section) */
if (recbuflen < 48)
return -1;
/* Check for valid fixed section of header */
if (!MS_ISVALIDHEADER (record))
return -1;
fsdh = (struct fsdh_s *)record;
/* Check to see if byte swapping is needed by checking for sane year and day */
if (!MS_ISVALIDYEARDAY (fsdh->start_time.year, fsdh->start_time.day))
swapflag = 1;
blkt_offset = fsdh->blockette_offset;
/* Swap order of blkt_offset if needed */
if (swapflag)
ms_gswap2 (&blkt_offset);
/* Loop through blockettes as long as number is non-zero and viable */
while (blkt_offset != 0 &&
blkt_offset <= recbuflen)
{
memcpy (&blkt_type, record + blkt_offset, 2);
memcpy (&next_blkt, record + blkt_offset + 2, 2);
if (swapflag)
{
ms_gswap2 (&blkt_type);
ms_gswap2 (&next_blkt);
}
/* Found a 1000 blockette, not truncated */
if (blkt_type == 1000 &&
(int)(blkt_offset + 4 + sizeof (struct blkt_1000_s)) <= recbuflen)
{
blkt_1000 = (struct blkt_1000_s *)(record + blkt_offset + 4);
foundlen = 1;
/* Calculate record size in bytes as 2^(blkt_1000->reclen) */
reclen = (unsigned int)1 << blkt_1000->reclen;
break;
}
/* Safety check for invalid offset */
if (next_blkt != 0 && (next_blkt < 4 || (next_blkt - 4) <= blkt_offset))
{
ms_log (2, "Invalid blockette offset (%d) less than or equal to current offset (%d)\n",
next_blkt, blkt_offset);
return -1;
}
blkt_offset = next_blkt;
}
/* If record length was not determined by a 1000 blockette scan the buffer
* and search for the next record */
if (reclen == -1)
{
nextfsdh = record + MINRECLEN;
/* Check for record header or blank/noise record at MINRECLEN byte offsets */
while (((nextfsdh - record) + 48) < recbuflen)
{
if (MS_ISVALIDHEADER (nextfsdh) || MS_ISVALIDBLANK (nextfsdh))
{
foundlen = 1;
reclen = nextfsdh - record;
break;
}
nextfsdh += MINRECLEN;
}
}
if (!foundlen)
return 0;
else
return reclen;
} /* End of ms_detect() */
/***************************************************************************
* ms_parse_raw:
*
* Parse and verify a SEED data record header (fixed section and
* blockettes) at the lowest level, printing error messages for
* invalid header values and optionally print raw header values. The
* memory at 'record' is assumed to be a Mini-SEED record. Not every
* possible test is performed, common errors and those causing
* libmseed parsing to fail should be detected.
*
* The 'details' argument is interpreted as follows:
*
* details:
* 0 = only print error messages for invalid header fields
* 1 = print basic fields in addition to invalid field errors
* 2 = print all fields in addition to invalid field errors
*
* The 'swapflag' argument is interpreted as follows:
*
* swapflag:
* 1 = swap multibyte quantities
* 0 = do no swapping
* -1 = autodetect byte order using year test, swap if needed
*
* Any byte swapping performed by this routine is applied directly to
* the memory reference by the record pointer.
*
* This routine is primarily intended to diagnose invalid Mini-SEED headers.
*
* Returns 0 when no errors were detected or a positive count of
* errors detected.
***************************************************************************/
int
ms_parse_raw (char *record, int maxreclen, flag details, flag swapflag)
{
struct fsdh_s *fsdh;
double nomsamprate;
char srcname[50];
char *X;
char b;
int retval = 0;
int b1000encoding = -1;
int b1000reclen = -1;
int endofblockettes = -1;
int idx;
if (!record)
return 1;
/* Generate a source name string */
srcname[0] = '\0';
ms_recsrcname (record, srcname, 1);
fsdh = (struct fsdh_s *)record;
/* Check to see if byte swapping is needed by testing the year and day */
if (swapflag == -1 && !MS_ISVALIDYEARDAY (fsdh->start_time.year, fsdh->start_time.day))
swapflag = 1;
else
swapflag = 0;
if (details > 1)
{
if (swapflag == 1)
ms_log (0, "Swapping multi-byte quantities in header\n");
else
ms_log (0, "Not swapping multi-byte quantities in header\n");
}
/* Swap byte order */
if (swapflag)
{
MS_SWAPBTIME (&fsdh->start_time);
ms_gswap2a (&fsdh->numsamples);
ms_gswap2a (&fsdh->samprate_fact);
ms_gswap2a (&fsdh->samprate_mult);
ms_gswap4a (&fsdh->time_correct);
ms_gswap2a (&fsdh->data_offset);
ms_gswap2a (&fsdh->blockette_offset);
}
/* Validate fixed section header fields */
X = record; /* Pointer of convenience */
/* Check record sequence number, 6 ASCII digits */
if (!isdigit ((int)*(X)) || !isdigit ((int)*(X + 1)) ||
!isdigit ((int)*(X + 2)) || !isdigit ((int)*(X + 3)) ||
!isdigit ((int)*(X + 4)) || !isdigit ((int)*(X + 5)))
{
ms_log (2, "%s: Invalid sequence number: '%c%c%c%c%c%c'\n", srcname, X, X + 1, X + 2, X + 3, X + 4, X + 5);
retval++;
}
/* Check header/quality indicator */
if (!MS_ISDATAINDICATOR (*(X + 6)))
{
ms_log (2, "%s: Invalid header indicator (DRQM): '%c'\n", srcname, X + 6);
retval++;
}
/* Check reserved byte, space or NULL */
if (!(*(X + 7) == ' ' || *(X + 7) == '\0'))
{
ms_log (2, "%s: Invalid fixed section reserved byte (Space): '%c'\n", srcname, X + 7);
retval++;
}
/* Check station code, 5 alphanumerics or spaces */
if (!(isalnum ((unsigned char)*(X + 8)) || *(X + 8) == ' ') ||
!(isalnum ((unsigned char)*(X + 9)) || *(X + 9) == ' ') ||
!(isalnum ((unsigned char)*(X + 10)) || *(X + 10) == ' ') ||
!(isalnum ((unsigned char)*(X + 11)) || *(X + 11) == ' ') ||
!(isalnum ((unsigned char)*(X + 12)) || *(X + 12) == ' '))
{
ms_log (2, "%s: Invalid station code: '%c%c%c%c%c'\n", srcname, X + 8, X + 9, X + 10, X + 11, X + 12);
retval++;
}
/* Check location ID, 2 alphanumerics or spaces */
if (!(isalnum ((unsigned char)*(X + 13)) || *(X + 13) == ' ') ||
!(isalnum ((unsigned char)*(X + 14)) || *(X + 14) == ' '))
{
ms_log (2, "%s: Invalid location ID: '%c%c'\n", srcname, X + 13, X + 14);
retval++;
}
/* Check channel codes, 3 alphanumerics or spaces */
if (!(isalnum ((unsigned char)*(X + 15)) || *(X + 15) == ' ') ||
!(isalnum ((unsigned char)*(X + 16)) || *(X + 16) == ' ') ||
!(isalnum ((unsigned char)*(X + 17)) || *(X + 17) == ' '))
{
ms_log (2, "%s: Invalid channel codes: '%c%c%c'\n", srcname, X + 15, X + 16, X + 17);
retval++;
}
/* Check network code, 2 alphanumerics or spaces */
if (!(isalnum ((unsigned char)*(X + 18)) || *(X + 18) == ' ') ||
!(isalnum ((unsigned char)*(X + 19)) || *(X + 19) == ' '))
{
ms_log (2, "%s: Invalid network code: '%c%c'\n", srcname, X + 18, X + 19);
retval++;
}
/* Check start time fields */
if (fsdh->start_time.year < 1900 || fsdh->start_time.year > 2100)
{
ms_log (2, "%s: Unlikely start year (1900-2100): '%d'\n", srcname, fsdh->start_time.year);
retval++;
}
if (fsdh->start_time.day < 1 || fsdh->start_time.day > 366)
{
ms_log (2, "%s: Invalid start day (1-366): '%d'\n", srcname, fsdh->start_time.day);
retval++;
}
if (fsdh->start_time.hour > 23)
{
ms_log (2, "%s: Invalid start hour (0-23): '%d'\n", srcname, fsdh->start_time.hour);
retval++;
}
if (fsdh->start_time.min > 59)
{
ms_log (2, "%s: Invalid start minute (0-59): '%d'\n", srcname, fsdh->start_time.min);
retval++;
}
if (fsdh->start_time.sec > 60)
{
ms_log (2, "%s: Invalid start second (0-60): '%d'\n", srcname, fsdh->start_time.sec);
retval++;
}
if (fsdh->start_time.fract > 9999)
{
ms_log (2, "%s: Invalid start fractional seconds (0-9999): '%d'\n", srcname, fsdh->start_time.fract);
retval++;
}
/* Check number of samples, max samples in 4096-byte Steim-2 encoded record: 6601 */
if (fsdh->numsamples > 20000)
{
ms_log (2, "%s: Unlikely number of samples (>20000): '%d'\n", srcname, fsdh->numsamples);
retval++;
}
/* Sanity check that there is space for blockettes when both data and blockettes are present */
if (fsdh->numsamples > 0 && fsdh->numblockettes > 0 && fsdh->data_offset <= fsdh->blockette_offset)
{
ms_log (2, "%s: No space for %d blockettes, data offset: %d, blockette offset: %d\n", srcname,
fsdh->numblockettes, fsdh->data_offset, fsdh->blockette_offset);
retval++;
}
/* Print raw header details */
if (details >= 1)
{
/* Determine nominal sample rate */
nomsamprate = ms_nomsamprate (fsdh->samprate_fact, fsdh->samprate_mult);
/* Print header values */
ms_log (0, "RECORD -- %s\n", srcname);
ms_log (0, " sequence number: '%c%c%c%c%c%c'\n", fsdh->sequence_number[0], fsdh->sequence_number[1], fsdh->sequence_number[2],
fsdh->sequence_number[3], fsdh->sequence_number[4], fsdh->sequence_number[5]);
ms_log (0, " data quality indicator: '%c'\n", fsdh->dataquality);
if (details > 0)
ms_log (0, " reserved: '%c'\n", fsdh->reserved);
ms_log (0, " station code: '%c%c%c%c%c'\n", fsdh->station[0], fsdh->station[1], fsdh->station[2], fsdh->station[3], fsdh->station[4]);
ms_log (0, " location ID: '%c%c'\n", fsdh->location[0], fsdh->location[1]);
ms_log (0, " channel codes: '%c%c%c'\n", fsdh->channel[0], fsdh->channel[1], fsdh->channel[2]);
ms_log (0, " network code: '%c%c'\n", fsdh->network[0], fsdh->network[1]);
ms_log (0, " start time: %d,%d,%d:%d:%d.%04d (unused: %d)\n", fsdh->start_time.year, fsdh->start_time.day,
fsdh->start_time.hour, fsdh->start_time.min, fsdh->start_time.sec, fsdh->start_time.fract, fsdh->start_time.unused);
ms_log (0, " number of samples: %d\n", fsdh->numsamples);
ms_log (0, " sample rate factor: %d (%.10g samples per second)\n",
fsdh->samprate_fact, nomsamprate);
ms_log (0, " sample rate multiplier: %d\n", fsdh->samprate_mult);
/* Print flag details if requested */
if (details > 1)
{
/* Activity flags */
b = fsdh->act_flags;
ms_log (0, " activity flags: [%u%u%u%u%u%u%u%u] 8 bits\n",
bit (b, 0x01), bit (b, 0x02), bit (b, 0x04), bit (b, 0x08),
bit (b, 0x10), bit (b, 0x20), bit (b, 0x40), bit (b, 0x80));
if (b & 0x01)
ms_log (0, " [Bit 0] Calibration signals present\n");
if (b & 0x02)
ms_log (0, " [Bit 1] Time correction applied\n");
if (b & 0x04)
ms_log (0, " [Bit 2] Beginning of an event, station trigger\n");
if (b & 0x08)
ms_log (0, " [Bit 3] End of an event, station detrigger\n");
if (b & 0x10)
ms_log (0, " [Bit 4] A positive leap second happened in this record\n");
if (b & 0x20)
ms_log (0, " [Bit 5] A negative leap second happened in this record\n");
if (b & 0x40)
ms_log (0, " [Bit 6] Event in progress\n");
if (b & 0x80)
ms_log (0, " [Bit 7] Undefined bit set\n");
/* I/O and clock flags */
b = fsdh->io_flags;
ms_log (0, " I/O and clock flags: [%u%u%u%u%u%u%u%u] 8 bits\n",
bit (b, 0x01), bit (b, 0x02), bit (b, 0x04), bit (b, 0x08),
bit (b, 0x10), bit (b, 0x20), bit (b, 0x40), bit (b, 0x80));
if (b & 0x01)
ms_log (0, " [Bit 0] Station volume parity error possibly present\n");
if (b & 0x02)
ms_log (0, " [Bit 1] Long record read (possibly no problem)\n");
if (b & 0x04)
ms_log (0, " [Bit 2] Short record read (record padded)\n");
if (b & 0x08)
ms_log (0, " [Bit 3] Start of time series\n");
if (b & 0x10)
ms_log (0, " [Bit 4] End of time series\n");
if (b & 0x20)
ms_log (0, " [Bit 5] Clock locked\n");
if (b & 0x40)
ms_log (0, " [Bit 6] Undefined bit set\n");
if (b & 0x80)
ms_log (0, " [Bit 7] Undefined bit set\n");
/* Data quality flags */
b = fsdh->dq_flags;
ms_log (0, " data quality flags: [%u%u%u%u%u%u%u%u] 8 bits\n",
bit (b, 0x01), bit (b, 0x02), bit (b, 0x04), bit (b, 0x08),
bit (b, 0x10), bit (b, 0x20), bit (b, 0x40), bit (b, 0x80));
if (b & 0x01)
ms_log (0, " [Bit 0] Amplifier saturation detected\n");
if (b & 0x02)
ms_log (0, " [Bit 1] Digitizer clipping detected\n");
if (b & 0x04)
ms_log (0, " [Bit 2] Spikes detected\n");
if (b & 0x08)
ms_log (0, " [Bit 3] Glitches detected\n");
if (b & 0x10)
ms_log (0, " [Bit 4] Missing/padded data present\n");
if (b & 0x20)
ms_log (0, " [Bit 5] Telemetry synchronization error\n");
if (b & 0x40)
ms_log (0, " [Bit 6] A digital filter may be charging\n");
if (b & 0x80)
ms_log (0, " [Bit 7] Time tag is questionable\n");
}
ms_log (0, " number of blockettes: %d\n", fsdh->numblockettes);
ms_log (0, " time correction: %ld\n", (long int)fsdh->time_correct);
ms_log (0, " data offset: %d\n", fsdh->data_offset);
ms_log (0, " first blockette offset: %d\n", fsdh->blockette_offset);
} /* Done printing raw header details */
/* Validate and report information in the blockette chain */
if (fsdh->blockette_offset > 46 && fsdh->blockette_offset < maxreclen)
{
int blkt_offset = fsdh->blockette_offset;
int blkt_count = 0;
int blkt_length;
uint16_t blkt_type;
uint16_t next_blkt;
char *blkt_desc;
/* Traverse blockette chain */
while (blkt_offset != 0 && 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 (swapflag)
{
ms_gswap2 (&blkt_type);
ms_gswap2 (&next_blkt);
}
/* Print common header fields */
if (details >= 1)
{
blkt_desc = ms_blktdesc (blkt_type);
ms_log (0, " BLOCKETTE %u: (%s)\n", blkt_type, (blkt_desc) ? blkt_desc : "Unknown");
ms_log (0, " next blockette: %u\n", next_blkt);
}
blkt_length = ms_blktlen (blkt_type, record + blkt_offset, swapflag);
if (blkt_length == 0)
{
ms_log (2, "%s: Unknown blockette length for type %d\n", srcname, blkt_type);
retval++;
}
/* Track end of blockette chain */
endofblockettes = blkt_offset + blkt_length - 1;
/* Sanity check that the blockette is contained in the record */
if (endofblockettes > maxreclen)
{
ms_log (2, "%s: Blockette type %d at offset %d with length %d does not fix in record (%d)\n",
srcname, blkt_type, blkt_offset, blkt_length, maxreclen);
retval++;
break;
}
if (blkt_type == 100)
{
struct blkt_100_s *blkt_100 = (struct blkt_100_s *)(record + blkt_offset + 4);
if (swapflag)
ms_gswap4 (&blkt_100->samprate);
if (details >= 1)
{
ms_log (0, " actual sample rate: %.10g\n", blkt_100->samprate);
if (details > 1)
{
b = blkt_100->flags;
ms_log (0, " undefined flags: [%u%u%u%u%u%u%u%u] 8 bits\n",
bit (b, 0x01), bit (b, 0x02), bit (b, 0x04), bit (b, 0x08),
bit (b, 0x10), bit (b, 0x20), bit (b, 0x40), bit (b, 0x80));
ms_log (0, " reserved bytes (3): %u,%u,%u\n",
blkt_100->reserved[0], blkt_100->reserved[1], blkt_100->reserved[2]);
}
}
}
else if (blkt_type == 200)
{
struct blkt_200_s *blkt_200 = (struct blkt_200_s *)(record + blkt_offset + 4);
if (swapflag)
{
ms_gswap4 (&blkt_200->amplitude);
ms_gswap4 (&blkt_200->period);
ms_gswap4 (&blkt_200->background_estimate);
MS_SWAPBTIME (&blkt_200->time);
}
if (details >= 1)
{
ms_log (0, " signal amplitude: %g\n", blkt_200->amplitude);
ms_log (0, " signal period: %g\n", blkt_200->period);
ms_log (0, " background estimate: %g\n", blkt_200->background_estimate);
if (details > 1)
{
b = blkt_200->flags;
ms_log (0, " event detection flags: [%u%u%u%u%u%u%u%u] 8 bits\n",
bit (b, 0x01), bit (b, 0x02), bit (b, 0x04), bit (b, 0x08),
bit (b, 0x10), bit (b, 0x20), bit (b, 0x40), bit (b, 0x80));
if (b & 0x01)
ms_log (0, " [Bit 0] 1: Dilatation wave\n");
else
ms_log (0, " [Bit 0] 0: Compression wave\n");
if (b & 0x02)
ms_log (0, " [Bit 1] 1: Units after deconvolution\n");
else
ms_log (0, " [Bit 1] 0: Units are digital counts\n");
if (b & 0x04)
ms_log (0, " [Bit 2] Bit 0 is undetermined\n");
ms_log (0, " reserved byte: %u\n", blkt_200->reserved);
}
ms_log (0, " signal onset time: %d,%d,%d:%d:%d.%04d (unused: %d)\n", blkt_200->time.year, blkt_200->time.day,
blkt_200->time.hour, blkt_200->time.min, blkt_200->time.sec, blkt_200->time.fract, blkt_200->time.unused);
ms_log (0, " detector name: %.24s\n", blkt_200->detector);
}
}
else if (blkt_type == 201)
{
struct blkt_201_s *blkt_201 = (struct blkt_201_s *)(record + blkt_offset + 4);
if (swapflag)
{
ms_gswap4 (&blkt_201->amplitude);
ms_gswap4 (&blkt_201->period);
ms_gswap4 (&blkt_201->background_estimate);
MS_SWAPBTIME (&blkt_201->time);
}
if (details >= 1)
{
ms_log (0, " signal amplitude: %g\n", blkt_201->amplitude);
ms_log (0, " signal period: %g\n", blkt_201->period);
ms_log (0, " background estimate: %g\n", blkt_201->background_estimate);
b = blkt_201->flags;
ms_log (0, " event detection flags: [%u%u%u%u%u%u%u%u] 8 bits\n",
bit (b, 0x01), bit (b, 0x02), bit (b, 0x04), bit (b, 0x08),
bit (b, 0x10), bit (b, 0x20), bit (b, 0x40), bit (b, 0x80));
if (b & 0x01)
ms_log (0, " [Bit 0] 1: Dilation wave\n");
else
ms_log (0, " [Bit 0] 0: Compression wave\n");
if (details > 1)
ms_log (0, " reserved byte: %u\n", blkt_201->reserved);
ms_log (0, " signal onset time: %d,%d,%d:%d:%d.%04d (unused: %d)\n", blkt_201->time.year, blkt_201->time.day,
blkt_201->time.hour, blkt_201->time.min, blkt_201->time.sec, blkt_201->time.fract, blkt_201->time.unused);
ms_log (0, " SNR values: ");
for (idx = 0; idx < 6; idx++)
ms_log (0, "%u ", blkt_201->snr_values[idx]);
ms_log (0, "\n");
ms_log (0, " loopback value: %u\n", blkt_201->loopback);
ms_log (0, " pick algorithm: %u\n", blkt_201->pick_algorithm);
ms_log (0, " detector name: %.24s\n", blkt_201->detector);
}
}
else if (blkt_type == 300)
{
struct blkt_300_s *blkt_300 = (struct blkt_300_s *)(record + blkt_offset + 4);
if (swapflag)
{
MS_SWAPBTIME (&blkt_300->time);
ms_gswap4 (&blkt_300->step_duration);
ms_gswap4 (&blkt_300->interval_duration);
ms_gswap4 (&blkt_300->amplitude);
ms_gswap4 (&blkt_300->reference_amplitude);
}
if (details >= 1)
{
ms_log (0, " calibration start time: %d,%d,%d:%d:%d.%04d (unused: %d)\n", blkt_300->time.year, blkt_300->time.day,
blkt_300->time.hour, blkt_300->time.min, blkt_300->time.sec, blkt_300->time.fract, blkt_300->time.unused);
ms_log (0, " number of calibrations: %u\n", blkt_300->numcalibrations);
b = blkt_300->flags;
ms_log (0, " calibration flags: [%u%u%u%u%u%u%u%u] 8 bits\n",
bit (b, 0x01), bit (b, 0x02), bit (b, 0x04), bit (b, 0x08),
bit (b, 0x10), bit (b, 0x20), bit (b, 0x40), bit (b, 0x80));
if (b & 0x01)
ms_log (0, " [Bit 0] First pulse is positive\n");
if (b & 0x02)
ms_log (0, " [Bit 1] Calibration's alternate sign\n");
if (b & 0x04)
ms_log (0, " [Bit 2] Calibration was automatic\n");
if (b & 0x08)
ms_log (0, " [Bit 3] Calibration continued from previous record(s)\n");
ms_log (0, " step duration: %u\n", blkt_300->step_duration);
ms_log (0, " interval duration: %u\n", blkt_300->interval_duration);
ms_log (0, " signal amplitude: %g\n", blkt_300->amplitude);
ms_log (0, " input signal channel: %.3s", blkt_300->input_channel);
if (details > 1)
ms_log (0, " reserved byte: %u\n", blkt_300->reserved);
ms_log (0, " reference amplitude: %u\n", blkt_300->reference_amplitude);
ms_log (0, " coupling: %.12s\n", blkt_300->coupling);
ms_log (0, " rolloff: %.12s\n", blkt_300->rolloff);
}
}
else if (blkt_type == 310)
{
struct blkt_310_s *blkt_310 = (struct blkt_310_s *)(record + blkt_offset + 4);
if (swapflag)
{
MS_SWAPBTIME (&blkt_310->time);
ms_gswap4 (&blkt_310->duration);
ms_gswap4 (&blkt_310->period);
ms_gswap4 (&blkt_310->amplitude);
ms_gswap4 (&blkt_310->reference_amplitude);
}
if (details >= 1)
{
ms_log (0, " calibration start time: %d,%d,%d:%d:%d.%04d (unused: %d)\n", blkt_310->time.year, blkt_310->time.day,
blkt_310->time.hour, blkt_310->time.min, blkt_310->time.sec, blkt_310->time.fract, blkt_310->time.unused);
if (details > 1)
ms_log (0, " reserved byte: %u\n", blkt_310->reserved1);
b = blkt_310->flags;
ms_log (0, " calibration flags: [%u%u%u%u%u%u%u%u] 8 bits\n",
bit (b, 0x01), bit (b, 0x02), bit (b, 0x04), bit (b, 0x08),
bit (b, 0x10), bit (b, 0x20), bit (b, 0x40), bit (b, 0x80));
if (b & 0x04)
ms_log (0, " [Bit 2] Calibration was automatic\n");
if (b & 0x08)
ms_log (0, " [Bit 3] Calibration continued from previous record(s)\n");
if (b & 0x10)
ms_log (0, " [Bit 4] Peak-to-peak amplitude\n");
if (b & 0x20)
ms_log (0, " [Bit 5] Zero-to-peak amplitude\n");
if (b & 0x40)
ms_log (0, " [Bit 6] RMS amplitude\n");
ms_log (0, " calibration duration: %u\n", blkt_310->duration);
ms_log (0, " signal period: %g\n", blkt_310->period);
ms_log (0, " signal amplitude: %g\n", blkt_310->amplitude);
ms_log (0, " input signal channel: %.3s", blkt_310->input_channel);
if (details > 1)
ms_log (0, " reserved byte: %u\n", blkt_310->reserved2);
ms_log (0, " reference amplitude: %u\n", blkt_310->reference_amplitude);
ms_log (0, " coupling: %.12s\n", blkt_310->coupling);
ms_log (0, " rolloff: %.12s\n", blkt_310->rolloff);
}
}
else if (blkt_type == 320)
{
struct blkt_320_s *blkt_320 = (struct blkt_320_s *)(record + blkt_offset + 4);
if (swapflag)
{
MS_SWAPBTIME (&blkt_320->time);
ms_gswap4 (&blkt_320->duration);
ms_gswap4 (&blkt_320->ptp_amplitude);
ms_gswap4 (&blkt_320->reference_amplitude);
}
if (details >= 1)
{
ms_log (0, " calibration start time: %d,%d,%d:%d:%d.%04d (unused: %d)\n", blkt_320->time.year, blkt_320->time.day,
blkt_320->time.hour, blkt_320->time.min, blkt_320->time.sec, blkt_320->time.fract, blkt_320->time.unused);
if (details > 1)
ms_log (0, " reserved byte: %u\n", blkt_320->reserved1);
b = blkt_320->flags;
ms_log (0, " calibration flags: [%u%u%u%u%u%u%u%u] 8 bits\n",
bit (b, 0x01), bit (b, 0x02), bit (b, 0x04), bit (b, 0x08),
bit (b, 0x10), bit (b, 0x20), bit (b, 0x40), bit (b, 0x80));
if (b & 0x04)
ms_log (0, " [Bit 2] Calibration was automatic\n");
if (b & 0x08)
ms_log (0, " [Bit 3] Calibration continued from previous record(s)\n");
if (b & 0x10)
ms_log (0, " [Bit 4] Random amplitudes\n");
ms_log (0, " calibration duration: %u\n", blkt_320->duration);
ms_log (0, " peak-to-peak amplitude: %g\n", blkt_320->ptp_amplitude);
ms_log (0, " input signal channel: %.3s", blkt_320->input_channel);
if (details > 1)
ms_log (0, " reserved byte: %u\n", blkt_320->reserved2);
ms_log (0, " reference amplitude: %u\n", blkt_320->reference_amplitude);
ms_log (0, " coupling: %.12s\n", blkt_320->coupling);
ms_log (0, " rolloff: %.12s\n", blkt_320->rolloff);
ms_log (0, " noise type: %.8s\n", blkt_320->noise_type);
}
}
else if (blkt_type == 390)
{
struct blkt_390_s *blkt_390 = (struct blkt_390_s *)(record + blkt_offset + 4);
if (swapflag)
{
MS_SWAPBTIME (&blkt_390->time);
ms_gswap4 (&blkt_390->duration);
ms_gswap4 (&blkt_390->amplitude);
}
if (details >= 1)
{
ms_log (0, " calibration start time: %d,%d,%d:%d:%d.%04d (unused: %d)\n", blkt_390->time.year, blkt_390->time.day,
blkt_390->time.hour, blkt_390->time.min, blkt_390->time.sec, blkt_390->time.fract, blkt_390->time.unused);
if (details > 1)
ms_log (0, " reserved byte: %u\n", blkt_390->reserved1);
b = blkt_390->flags;
ms_log (0, " calibration flags: [%u%u%u%u%u%u%u%u] 8 bits\n",
bit (b, 0x01), bit (b, 0x02), bit (b, 0x04), bit (b, 0x08),
bit (b, 0x10), bit (b, 0x20), bit (b, 0x40), bit (b, 0x80));
if (b & 0x04)
ms_log (0, " [Bit 2] Calibration was automatic\n");
if (b & 0x08)
ms_log (0, " [Bit 3] Calibration continued from previous record(s)\n");
ms_log (0, " calibration duration: %u\n", blkt_390->duration);
ms_log (0, " signal amplitude: %g\n", blkt_390->amplitude);
ms_log (0, " input signal channel: %.3s", blkt_390->input_channel);
if (details > 1)
ms_log (0, " reserved byte: %u\n", blkt_390->reserved2);
}
}
else if (blkt_type == 395)
{
struct blkt_395_s *blkt_395 = (struct blkt_395_s *)(record + blkt_offset + 4);
if (swapflag)
MS_SWAPBTIME (&blkt_395->time);
if (details >= 1)
{
ms_log (0, " calibration end time: %d,%d,%d:%d:%d.%04d (unused: %d)\n", blkt_395->time.year, blkt_395->time.day,
blkt_395->time.hour, blkt_395->time.min, blkt_395->time.sec, blkt_395->time.fract, blkt_395->time.unused);
if (details > 1)
ms_log (0, " reserved bytes (2): %u,%u\n",
blkt_395->reserved[0], blkt_395->reserved[1]);
}
}
else if (blkt_type == 400)
{
struct blkt_400_s *blkt_400 = (struct blkt_400_s *)(record + blkt_offset + 4);
if (swapflag)
{
ms_gswap4 (&blkt_400->azimuth);
ms_gswap4 (&blkt_400->slowness);
ms_gswap4 (&blkt_400->configuration);
}
if (details >= 1)
{
ms_log (0, " beam azimuth (degrees): %g\n", blkt_400->azimuth);
ms_log (0, " beam slowness (sec/degree): %g\n", blkt_400->slowness);
ms_log (0, " configuration: %u\n", blkt_400->configuration);
if (details > 1)
ms_log (0, " reserved bytes (2): %u,%u\n",
blkt_400->reserved[0], blkt_400->reserved[1]);
}
}
else if (blkt_type == 405)
{
struct blkt_405_s *blkt_405 = (struct blkt_405_s *)(record + blkt_offset + 4);
uint16_t firstvalue = blkt_405->delay_values[0]; /* Work on a private copy */
if (swapflag)
ms_gswap2 (&firstvalue);
if (details >= 1)
ms_log (0, " first delay value: %u\n", firstvalue);
}
else if (blkt_type == 500)
{
struct blkt_500_s *blkt_500 = (struct blkt_500_s *)(record + blkt_offset + 4);
if (swapflag)
{
ms_gswap4 (&blkt_500->vco_correction);
MS_SWAPBTIME (&blkt_500->time);