File: msrutils.c

package info (click to toggle)
libmseed 2.19.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,684 kB
  • sloc: ansic: 10,810; makefile: 145; sh: 114
file content (1252 lines) | stat: -rw-r--r-- 44,799 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
/***************************************************************************
 * msrutils.c:
 *
 * Generic routines to operate on Mini-SEED records.
 *
 * Written by Chad Trabant
 *   ORFEUS/EC-Project MEREDIAN
 *   IRIS Data Management Center
 *
 * modified: 2016.283
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "libmseed.h"

/***************************************************************************
 * msr_init:
 *
 * Initialize and return an MSRecord struct, allocating memory if
 * needed.  If memory for the fsdh and datasamples fields has been
 * allocated the pointers will be retained for reuse.  If a blockette
 * chain is present all associated memory will be released.
 *
 * Returns a pointer to a MSRecord struct on success or NULL on error.
 ***************************************************************************/
MSRecord *
msr_init (MSRecord *msr)
{
  void *fsdh        = 0;
  void *datasamples = 0;

  if (!msr)
  {
    msr = (MSRecord *)malloc (sizeof (MSRecord));
  }
  else
  {
    fsdh        = msr->fsdh;
    datasamples = msr->datasamples;

    if (msr->blkts)
      msr_free_blktchain (msr);

    if (msr->ststate)
      free (msr->ststate);
  }

  if (msr == NULL)
  {
    ms_log (2, "msr_init(): Cannot allocate memory\n");
    return NULL;
  }

  memset (msr, 0, sizeof (MSRecord));

  msr->fsdh        = fsdh;
  msr->datasamples = datasamples;

  msr->reclen    = -1;
  msr->samplecnt = -1;
  msr->byteorder = -1;
  msr->encoding  = -1;

  return msr;
} /* End of msr_init() */

/***************************************************************************
 * msr_free:
 *
 * Free all memory associated with a MSRecord struct.
 ***************************************************************************/
void
msr_free (MSRecord **ppmsr)
{
  if (ppmsr != NULL && *ppmsr != 0)
  {
    /* Free fixed section header if populated */
    if ((*ppmsr)->fsdh)
      free ((*ppmsr)->fsdh);

    /* Free blockette chain if populated */
    if ((*ppmsr)->blkts)
      msr_free_blktchain (*ppmsr);

    /* Free datasamples if present */
    if ((*ppmsr)->datasamples)
      free ((*ppmsr)->datasamples);

    /* Free stream processing state if present */
    if ((*ppmsr)->ststate)
      free ((*ppmsr)->ststate);

    free (*ppmsr);

    *ppmsr = NULL;
  }
} /* End of msr_free() */

/***************************************************************************
 * msr_free_blktchain:
 *
 * Free all memory associated with a blockette chain in a MSRecord
 * struct and set MSRecord->blkts to NULL.  Also reset the shortcut
 * blockette pointers.
 ***************************************************************************/
void
msr_free_blktchain (MSRecord *msr)
{
  if (msr)
  {
    if (msr->blkts)
    {
      BlktLink *bc = msr->blkts;
      BlktLink *nb = NULL;

      while (bc)
      {
        nb = bc->next;

        if (bc->blktdata)
          free (bc->blktdata);

        free (bc);

        bc = nb;
      }

      msr->blkts = 0;
    }

    msr->Blkt100  = 0;
    msr->Blkt1000 = 0;
    msr->Blkt1001 = 0;
  }
} /* End of msr_free_blktchain() */

/***************************************************************************
 * msr_addblockette:
 *
 * Add a blockette to the blockette chain of an MSRecord.  'blktdata'
 * should be the body of the blockette type 'blkttype' of 'length'
 * bytes without the blockette header (type and next offsets).  The
 * 'chainpos' value controls which end of the chain the blockette is
 * added to.  If 'chainpos' is 0 the blockette will be added to the
 * end of the chain (last blockette), other wise it will be added to
 * the beginning of the chain (first blockette).
 *
 * Returns a pointer to the BlktLink added to the chain on success and
 * NULL on error.
 ***************************************************************************/
BlktLink *
msr_addblockette (MSRecord *msr, char *blktdata, int length, int blkttype,
                  int chainpos)
{
  BlktLink *blkt;

  if (!msr)
    return NULL;

  blkt = msr->blkts;

  if (blkt)
  {
    if (chainpos != 0)
    {
      blkt = (BlktLink *)malloc (sizeof (BlktLink));

      blkt->next = msr->blkts;
      msr->blkts = blkt;
    }
    else
    {
      /* Find the last blockette */
      while (blkt && blkt->next)
      {
        blkt = blkt->next;
      }

      blkt->next = (BlktLink *)malloc (sizeof (BlktLink));

      blkt       = blkt->next;
      blkt->next = 0;
    }

    if (blkt == NULL)
    {
      ms_log (2, "msr_addblockette(): Cannot allocate memory\n");
      return NULL;
    }
  }
  else
  {
    msr->blkts = (BlktLink *)malloc (sizeof (BlktLink));

    if (msr->blkts == NULL)
    {
      ms_log (2, "msr_addblockette(): Cannot allocate memory\n");
      return NULL;
    }

    blkt       = msr->blkts;
    blkt->next = 0;
  }

  blkt->blktoffset = 0;
  blkt->blkt_type  = blkttype;
  blkt->next_blkt  = 0;

  blkt->blktdata = (char *)malloc (length);

  if (blkt->blktdata == NULL)
  {
    ms_log (2, "msr_addblockette(): Cannot allocate memory\n");
    return NULL;
  }

  memcpy (blkt->blktdata, blktdata, length);
  blkt->blktdatalen = length;

  /* Setup the shortcut pointer for common blockettes */
  switch (blkttype)
  {
  case 100:
    msr->Blkt100 = blkt->blktdata;
    break;
  case 1000:
    msr->Blkt1000 = blkt->blktdata;
    break;
  case 1001:
    msr->Blkt1001 = blkt->blktdata;
    break;
  }

  return blkt;
} /* End of msr_addblockette() */

/***************************************************************************
 * msr_normalize_header:
 *
 * Normalize header values between the MSRecord struct and the
 * associated fixed-section of the header and blockettes.  Essentially
 * this updates the SEED structured data in the MSRecord.fsdh struct
 * and MSRecord.blkts chain with values stored at the MSRecord level.
 *
 * Returns the header length in bytes on success or -1 on error.
 ***************************************************************************/
int
msr_normalize_header (MSRecord *msr, flag verbose)
{
  struct blkt_link_s *cur_blkt;
  hptime_t hptimems;
  int8_t usecoffset;
  char seqnum[7];
  int offset    = 0;
  int blktcnt   = 0;
  int reclenexp = 0;
  int reclenfind;

  if (!msr)
    return -1;

  /* Get start time rounded to tenths of milliseconds and microsecond offset */
  ms_hptime2tomsusecoffset (msr->starttime, &hptimems, &usecoffset);

  /* Update values in fixed section of data header */
  if (msr->fsdh)
  {
    if (verbose > 2)
      ms_log (1, "Normalizing fixed section of data header\n");

    /* Roll-over sequence number if necessary */
    if (msr->sequence_number > 999999)
      msr->sequence_number = 1;

    /* Update values in the MSRecord.fsdh struct */
    snprintf (seqnum, 7, "%06d", msr->sequence_number);
    memcpy (msr->fsdh->sequence_number, seqnum, 6);
    msr->fsdh->dataquality = msr->dataquality;
    msr->fsdh->reserved    = ' ';
    ms_strncpopen (msr->fsdh->network, msr->network, 2);
    ms_strncpopen (msr->fsdh->station, msr->station, 5);
    ms_strncpopen (msr->fsdh->location, msr->location, 2);
    ms_strncpopen (msr->fsdh->channel, msr->channel, 3);
    ms_hptime2btime (hptimems, &(msr->fsdh->start_time));

    /* Determine the factor and multipler for sample rate */
    if (ms_genfactmult (msr->samprate,
                        &(msr->fsdh->samprate_fact),
                        &(msr->fsdh->samprate_mult)))
    {
      if (verbose > 1)
        ms_log (1, "Sampling rate out of range, cannot generate factor & multiplier: %g\n",
                msr->samprate);
      msr->fsdh->samprate_fact = 0;
      msr->fsdh->samprate_mult = 0;
    }

    offset += 48;

    if (msr->blkts)
      msr->fsdh->blockette_offset = offset;
    else
      msr->fsdh->blockette_offset = 0;
  }

  /* Traverse blockette chain and perform necessary updates */
  cur_blkt = msr->blkts;

  if (cur_blkt && verbose > 2)
    ms_log (1, "Normalizing blockette chain\n");

  while (cur_blkt)
  {
    offset += 4;

    if (cur_blkt->blkt_type == 100 && msr->Blkt100)
    {
      msr->Blkt100->samprate = (float)msr->samprate;
      offset += sizeof (struct blkt_100_s);
    }
    else if (cur_blkt->blkt_type == 1000 && msr->Blkt1000)
    {
      msr->Blkt1000->byteorder = msr->byteorder;
      msr->Blkt1000->encoding  = msr->encoding;

      /* Calculate the record length as an exponent of 2 */
      for (reclenfind = 1, reclenexp = 1; reclenfind <= MAXRECLEN; reclenexp++)
      {
        reclenfind *= 2;
        if (reclenfind == msr->reclen)
          break;
      }

      if (reclenfind != msr->reclen)
      {
        ms_log (2, "msr_normalize_header(): Record length %d is not a power of 2\n",
                msr->reclen);
        return -1;
      }

      msr->Blkt1000->reclen = reclenexp;

      offset += sizeof (struct blkt_1000_s);
    }

    else if (cur_blkt->blkt_type == 1001)
    {
      msr->Blkt1001->usec = usecoffset;
      offset += sizeof (struct blkt_1001_s);
    }

    blktcnt++;
    cur_blkt = cur_blkt->next;
  }

  if (msr->fsdh)
    msr->fsdh->numblockettes = blktcnt;

  return offset;
} /* End of msr_normalize_header() */

/***************************************************************************
 * msr_duplicate:
 *
 * Duplicate an MSRecord struct including the fixed-section data
 * header and blockette chain.  If the datadup flag is true and the
 * source MSRecord has associated data samples copy them as well.
 *
 * Returns a pointer to a new MSRecord on success and NULL on error.
 ***************************************************************************/
MSRecord *
msr_duplicate (MSRecord *msr, flag datadup)
{
  MSRecord *dupmsr = 0;
  int samplesize   = 0;

  if (!msr)
    return NULL;

  /* Allocate target MSRecord structure */
  if ((dupmsr = msr_init (NULL)) == NULL)
    return NULL;

  /* Copy MSRecord structure */
  memcpy (dupmsr, msr, sizeof (MSRecord));

  /* Reset pointers to not alias memory held by other structures */
  dupmsr->fsdh = NULL;
  dupmsr->blkts = NULL;
  dupmsr->datasamples = NULL;
  dupmsr->ststate = NULL;

  /* Copy fixed-section data header structure */
  if (msr->fsdh)
  {
    /* Allocate memory for new FSDH structure */
    if ((dupmsr->fsdh = (struct fsdh_s *)malloc (sizeof (struct fsdh_s))) == NULL)
    {
      ms_log (2, "msr_duplicate(): Error allocating memory\n");
      msr_free (&dupmsr);
      return NULL;
    }

    /* Copy the contents */
    memcpy (dupmsr->fsdh, msr->fsdh, sizeof (struct fsdh_s));
  }

  /* Copy the blockette chain */
  if (msr->blkts)
  {
    BlktLink *blkt = msr->blkts;
    BlktLink *next = NULL;

    dupmsr->blkts = 0;
    while (blkt)
    {
      next = blkt->next;

      /* Add blockette to chain of new MSRecord */
      if (msr_addblockette (dupmsr, blkt->blktdata, blkt->blktdatalen,
                            blkt->blkt_type, 0) == NULL)
      {
        ms_log (2, "msr_duplicate(): Error adding blockettes\n");
        msr_free (&dupmsr);
        return NULL;
      }

      blkt = next;
    }
  }

  /* Copy data samples if requested and available */
  if (datadup && msr->datasamples)
  {
    /* Determine size of samples in bytes */
    samplesize = ms_samplesize (msr->sampletype);

    if (samplesize == 0)
    {
      ms_log (2, "msr_duplicate(): unrecognized sample type: '%c'\n",
              msr->sampletype);
      msr_free (&dupmsr);
      return NULL;
    }

    /* Allocate memory for new data array */
    if ((dupmsr->datasamples = (void *)malloc ((size_t) (msr->numsamples * samplesize))) == NULL)
    {
      ms_log (2, "msr_duplicate(): Error allocating memory\n");
      msr_free (&dupmsr);
      return NULL;
    }

    /* Copy the data array */
    memcpy (dupmsr->datasamples, msr->datasamples, ((size_t) (msr->numsamples * samplesize)));
  }
  /* Otherwise make sure the sample array and count are zero */
  else
  {
    dupmsr->datasamples = 0;
    dupmsr->numsamples  = 0;
  }

  return dupmsr;
} /* End of msr_duplicate() */

/***************************************************************************
 * msr_samprate:
 *
 * Calculate and return a double precision sample rate for the
 * specified MSRecord.  If a Blockette 100 was included and parsed,
 * the "Actual sample rate" (field 3) will be returned, otherwise a
 * nominal sample rate will be calculated from the sample rate factor
 * and multiplier in the fixed section data header.
 *
 * Returns the positive sample rate on success and -1.0 on error.
 ***************************************************************************/
double
msr_samprate (MSRecord *msr)
{
  if (!msr)
    return -1.0;

  if (msr->Blkt100)
    return (double)msr->Blkt100->samprate;
  else
    return msr_nomsamprate (msr);
} /* End of msr_samprate() */

/***************************************************************************
 * msr_nomsamprate:
 *
 * Calculate a double precision nominal sample rate from the sample
 * rate factor and multiplier in the FSDH struct of the specified
 * MSRecord.
 *
 * Returns the positive sample rate on success and -1.0 on error.
 ***************************************************************************/
double
msr_nomsamprate (MSRecord *msr)
{
  if (!msr)
    return -1.0;

  return ms_nomsamprate (msr->fsdh->samprate_fact, msr->fsdh->samprate_mult);
} /* End of msr_nomsamprate() */

/***************************************************************************
 * msr_starttime:
 *
 * Convert a btime struct of a FSDH struct of a MSRecord (the record
 * start time) into a high precision epoch time and apply time
 * corrections if any are specified in the header and bit 1 of the
 * activity flags indicates that it has not already been applied.  If
 * a Blockette 1001 is included and has been parsed the microseconds
 * of field 4 are also applied.
 *
 * Returns a high precision epoch time on success and HPTERROR on
 * error.
 ***************************************************************************/
hptime_t
msr_starttime (MSRecord *msr)
{
  hptime_t starttime = msr_starttime_uc (msr);

  if (!msr || starttime == HPTERROR)
    return HPTERROR;

  /* Check if a correction is included and if it has been applied,
     bit 1 of activity flags indicates if it has been appiled */

  if (msr->fsdh->time_correct != 0 &&
      !(msr->fsdh->act_flags & 0x02))
  {
    starttime += (hptime_t)msr->fsdh->time_correct * (HPTMODULUS / 10000);
  }

  /* Apply microsecond precision in a parsed Blockette 1001 */
  if (msr->Blkt1001)
  {
    starttime += (hptime_t)msr->Blkt1001->usec * (HPTMODULUS / 1000000);
  }

  return starttime;
} /* End of msr_starttime() */

/***************************************************************************
 * msr_starttime_uc:
 *
 * Convert a btime struct of a FSDH struct of a MSRecord (the record
 * start time) into a high precision epoch time.  This time has no
 * correction(s) applied to it.
 *
 * Returns a high precision epoch time on success and HPTERROR on
 * error.
 ***************************************************************************/
hptime_t
msr_starttime_uc (MSRecord *msr)
{
  if (!msr)
    return HPTERROR;

  if (!msr->fsdh)
    return HPTERROR;

  return ms_btime2hptime (&msr->fsdh->start_time);
} /* End of msr_starttime_uc() */

/***************************************************************************
 * msr_endtime:
 *
 * Calculate the time of the last sample in the record; this is the
 * actual last sample time and *not* the time "covered" by the last
 * sample.
 *
 * On the epoch time scale the value of a leap second is the same as
 * the second following the leap second, without external information
 * the values are ambiguous.
 *
 * Leap second handling: when a record completely contains a leap
 * second, starts before and ends after, the calculated end time will
 * be adjusted (reduced) by one second.
 *
 * Returns the time of the last sample as a high precision epoch time
 * on success and HPTERROR on error.
 ***************************************************************************/
hptime_t
msr_endtime (MSRecord *msr)
{
  hptime_t span      = 0;
  LeapSecond *lslist = leapsecondlist;

  if (!msr)
    return HPTERROR;

  if (msr->samprate > 0.0 && msr->samplecnt > 0)
    span = (hptime_t) (((double)(msr->samplecnt - 1) / msr->samprate * HPTMODULUS) + 0.5);

  /* Check if the record contains a leap second, if list is available */
  if (lslist)
  {
    while (lslist)
    {
      if (lslist->leapsecond > msr->starttime &&
          lslist->leapsecond <= (msr->starttime + span - HPTMODULUS))
      {
        span -= HPTMODULUS;
        break;
      }

      lslist = lslist->next;
    }
  }
  else
  {
    /* If a positive leap second occurred during this record as denoted by
     * bit 4 of the activity flags being set, reduce the end time to match
     * the now shifted UTC time. */
    if (msr->fsdh)
      if (msr->fsdh->act_flags & 0x10)
        span -= HPTMODULUS;
  }

  return (msr->starttime + span);
} /* End of msr_endtime() */

/***************************************************************************
 * msr_srcname:
 *
 * Generate a source name string for a specified MSRecord in the
 * format: 'NET_STA_LOC_CHAN' or, if the quality flag is true:
 * 'NET_STA_LOC_CHAN_QUAL'.  The passed srcname must have enough room
 * for the resulting string.
 *
 * Returns a pointer to the resulting string or NULL on error.
 ***************************************************************************/
char *
msr_srcname (MSRecord *msr, char *srcname, flag quality)
{
  char *src = srcname;
  char *cp  = srcname;

  if (!msr || !srcname)
    return NULL;

  /* Build the source name string */
  cp = msr->network;
  while (*cp)
  {
    *src++ = *cp++;
  }
  *src++ = '_';
  cp     = msr->station;
  while (*cp)
  {
    *src++ = *cp++;
  }
  *src++ = '_';
  cp     = msr->location;
  while (*cp)
  {
    *src++ = *cp++;
  }
  *src++ = '_';
  cp     = msr->channel;
  while (*cp)
  {
    *src++ = *cp++;
  }

  if (quality)
  {
    *src++ = '_';
    *src++ = msr->dataquality;
  }

  *src = '\0';

  return srcname;
} /* End of msr_srcname() */

/***************************************************************************
 * msr_print:
 *
 * Prints header values in an MSRecord struct, if 'details' is greater
 * than 0 then detailed information about each blockette is printed.
 * If 'details' is greater than 1 very detailed information is
 * printed.  If no FSDH (msr->fsdh) is present only a single line with
 * basic information is printed.
 ***************************************************************************/
void
msr_print (MSRecord *msr, flag details)
{
  double nomsamprate;
  char srcname[50];
  char time[25];
  char b;
  int idx;

  if (!msr)
    return;

  /* Generate a source name string */
  srcname[0] = '\0';
  msr_srcname (msr, srcname, 0);

  /* Generate a start time string */
  ms_hptime2seedtimestr (msr->starttime, time, 1);

  /* Report information in the fixed header */
  if (details > 0 && msr->fsdh)
  {
    nomsamprate = msr_nomsamprate (msr);

    ms_log (0, "%s, %06d, %c\n", srcname, msr->sequence_number, msr->dataquality);
    ms_log (0, "             start time: %s\n", time);
    ms_log (0, "      number of samples: %d\n", msr->fsdh->numsamples);
    ms_log (0, "     sample rate factor: %d  (%.10g samples per second)\n",
            msr->fsdh->samprate_fact, nomsamprate);
    ms_log (0, " sample rate multiplier: %d\n", msr->fsdh->samprate_mult);

    if (details > 1)
    {
      /* Activity flags */
      b = msr->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 = msr->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 = msr->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", msr->fsdh->numblockettes);
    ms_log (0, "        time correction: %ld\n", (long int)msr->fsdh->time_correct);
    ms_log (0, "            data offset: %d\n", msr->fsdh->data_offset);
    ms_log (0, " first blockette offset: %d\n", msr->fsdh->blockette_offset);
  }
  else
  {
    ms_log (0, "%s, %06d, %c, %d, %" PRId64 " samples, %-.10g Hz, %s\n",
            srcname, msr->sequence_number, msr->dataquality,
            msr->reclen, msr->samplecnt, msr->samprate, time);
  }

  /* Report information in the blockette chain */
  if (details > 0 && msr->blkts)
  {
    BlktLink *cur_blkt = msr->blkts;

    while (cur_blkt)
    {
      if (cur_blkt->blkt_type == 100)
      {
        struct blkt_100_s *blkt_100 = (struct blkt_100_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        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 (cur_blkt->blkt_type == 200)
      {
        struct blkt_200_s *blkt_200 = (struct blkt_200_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        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_btime2seedtimestr (&blkt_200->time, time);
        ms_log (0, "           signal onset time: %s\n", time);
        ms_log (0, "               detector name: %.24s\n", blkt_200->detector);
      }

      else if (cur_blkt->blkt_type == 201)
      {
        struct blkt_201_s *blkt_201 = (struct blkt_201_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        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_btime2seedtimestr (&blkt_201->time, time);
        ms_log (0, "           signal onset time: %s\n", time);
        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 (cur_blkt->blkt_type == 300)
      {
        struct blkt_300_s *blkt_300 = (struct blkt_300_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        ms_btime2seedtimestr (&blkt_300->time, time);
        ms_log (0, "      calibration start time: %s\n", time);
        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 (cur_blkt->blkt_type == 310)
      {
        struct blkt_310_s *blkt_310 = (struct blkt_310_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        ms_btime2seedtimestr (&blkt_310->time, time);
        ms_log (0, "      calibration start time: %s\n", time);
        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 (cur_blkt->blkt_type == 320)
      {
        struct blkt_320_s *blkt_320 = (struct blkt_320_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        ms_btime2seedtimestr (&blkt_320->time, time);
        ms_log (0, "      calibration start time: %s\n", time);
        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 (cur_blkt->blkt_type == 390)
      {
        struct blkt_390_s *blkt_390 = (struct blkt_390_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        ms_btime2seedtimestr (&blkt_390->time, time);
        ms_log (0, "      calibration start time: %s\n", time);
        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 (cur_blkt->blkt_type == 395)
      {
        struct blkt_395_s *blkt_395 = (struct blkt_395_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        ms_btime2seedtimestr (&blkt_395->time, time);
        ms_log (0, "        calibration end time: %s\n", time);
        if (details > 1)
          ms_log (0, "          reserved bytes (2): %u,%u\n",
                  blkt_395->reserved[0], blkt_395->reserved[1]);
      }

      else if (cur_blkt->blkt_type == 400)
      {
        struct blkt_400_s *blkt_400 = (struct blkt_400_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        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 (cur_blkt->blkt_type == 405)
      {
        struct blkt_405_s *blkt_405 = (struct blkt_405_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s, incomplete)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        ms_log (0, "           first delay value: %u\n", blkt_405->delay_values[0]);
      }

      else if (cur_blkt->blkt_type == 500)
      {
        struct blkt_500_s *blkt_500 = (struct blkt_500_s *)cur_blkt->blktdata;

        ms_log (0, "          BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        ms_log (0, "              VCO correction: %g%%\n", blkt_500->vco_correction);
        ms_btime2seedtimestr (&blkt_500->time, time);
        ms_log (0, "           time of exception: %s\n", time);
        ms_log (0, "                        usec: %d\n", blkt_500->usec);
        ms_log (0, "           reception quality: %u%%\n", blkt_500->reception_qual);
        ms_log (0, "             exception count: %u\n", blkt_500->exception_count);
        ms_log (0, "              exception type: %.16s\n", blkt_500->exception_type);
        ms_log (0, "                 clock model: %.32s\n", blkt_500->clock_model);
        ms_log (0, "                clock status: %.128s\n", blkt_500->clock_status);
      }

      else if (cur_blkt->blkt_type == 1000)
      {
        struct blkt_1000_s *blkt_1000 = (struct blkt_1000_s *)cur_blkt->blktdata;
        int recsize;
        char order[40];

        /* Calculate record size in bytes as 2^(blkt_1000->rec_len) */
        recsize = (unsigned int)1 << blkt_1000->reclen;

        /* Big or little endian? */
        if (blkt_1000->byteorder == 0)
          strncpy (order, "Little endian", sizeof (order) - 1);
        else if (blkt_1000->byteorder == 1)
          strncpy (order, "Big endian", sizeof (order) - 1);
        else
          strncpy (order, "Unknown value", sizeof (order) - 1);

        ms_log (0, "         BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        ms_log (0, "                    encoding: %s (val:%u)\n",
                (char *)ms_encodingstr (blkt_1000->encoding), blkt_1000->encoding);
        ms_log (0, "                  byte order: %s (val:%u)\n",
                order, blkt_1000->byteorder);
        ms_log (0, "               record length: %d (val:%u)\n",
                recsize, blkt_1000->reclen);

        if (details > 1)
          ms_log (0, "               reserved byte: %u\n", blkt_1000->reserved);
      }

      else if (cur_blkt->blkt_type == 1001)
      {
        struct blkt_1001_s *blkt_1001 = (struct blkt_1001_s *)cur_blkt->blktdata;

        ms_log (0, "         BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        ms_log (0, "              timing quality: %u%%\n", blkt_1001->timing_qual);
        ms_log (0, "                micro second: %d\n", blkt_1001->usec);

        if (details > 1)
          ms_log (0, "               reserved byte: %u\n", blkt_1001->reserved);

        ms_log (0, "                 frame count: %u\n", blkt_1001->framecnt);
      }

      else if (cur_blkt->blkt_type == 2000)
      {
        struct blkt_2000_s *blkt_2000 = (struct blkt_2000_s *)cur_blkt->blktdata;
        char order[40];

        /* Big or little endian? */
        if (blkt_2000->byteorder == 0)
          strncpy (order, "Little endian", sizeof (order) - 1);
        else if (blkt_2000->byteorder == 1)
          strncpy (order, "Big endian", sizeof (order) - 1);
        else
          strncpy (order, "Unknown value", sizeof (order) - 1);

        ms_log (0, "         BLOCKETTE %u: (%s)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
        ms_log (0, "            blockette length: %u\n", blkt_2000->length);
        ms_log (0, "                 data offset: %u\n", blkt_2000->data_offset);
        ms_log (0, "               record number: %u\n", blkt_2000->recnum);
        ms_log (0, "                  byte order: %s (val:%u)\n",
                order, blkt_2000->byteorder);
        b = blkt_2000->flags;
        ms_log (0, "                  data 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 (details > 1)
        {
          if (b & 0x01)
            ms_log (0, "                         [Bit 0] 1: Stream oriented\n");
          else
            ms_log (0, "                         [Bit 0] 0: Record oriented\n");
          if (b & 0x02)
            ms_log (0, "                         [Bit 1] 1: Blockette 2000s may NOT be packaged\n");
          else
            ms_log (0, "                         [Bit 1] 0: Blockette 2000s may be packaged\n");
          if (!(b & 0x04) && !(b & 0x08))
            ms_log (0, "                      [Bits 2-3] 00: Complete blockette\n");
          else if (!(b & 0x04) && (b & 0x08))
            ms_log (0, "                      [Bits 2-3] 01: First blockette in span\n");
          else if ((b & 0x04) && (b & 0x08))
            ms_log (0, "                      [Bits 2-3] 11: Continuation blockette in span\n");
          else if ((b & 0x04) && !(b & 0x08))
            ms_log (0, "                      [Bits 2-3] 10: Final blockette in span\n");
          if (!(b & 0x10) && !(b & 0x20))
            ms_log (0, "                      [Bits 4-5] 00: Not file oriented\n");
          else if (!(b & 0x10) && (b & 0x20))
            ms_log (0, "                      [Bits 4-5] 01: First blockette of file\n");
          else if ((b & 0x10) && !(b & 0x20))
            ms_log (0, "                      [Bits 4-5] 10: Continuation of file\n");
          else if ((b & 0x10) && (b & 0x20))
            ms_log (0, "                      [Bits 4-5] 11: Last blockette of file\n");
        }

        ms_log (0, "           number of headers: %u\n", blkt_2000->numheaders);

        /* Crude display of the opaque data headers */
        if (details > 1)
          ms_log (0, "                     headers: %.*s\n",
                  (blkt_2000->data_offset - 15), blkt_2000->payload);
      }

      else
      {
        ms_log (0, "         BLOCKETTE %u: (%s, not parsed)\n", cur_blkt->blkt_type,
                ms_blktdesc (cur_blkt->blkt_type));
        ms_log (0, "              next blockette: %u\n", cur_blkt->next_blkt);
      }

      cur_blkt = cur_blkt->next;
    }
  }
} /* End of msr_print() */

/***************************************************************************
 * msr_host_latency:
 *
 * Calculate the latency based on the host time in UTC accounting for
 * the time covered using the number of samples and sample rate; in
 * other words, the difference between the host time and the time of
 * the last sample in the specified Mini-SEED record.
 *
 * Double precision is returned, but the true precision is dependent
 * on the accuracy of the host system clock among other things.
 *
 * Returns seconds of latency or 0.0 on error (indistinguishable from
 * 0.0 latency).
 ***************************************************************************/
double
msr_host_latency (MSRecord *msr)
{
  double span = 0.0; /* Time covered by the samples */
  double epoch;      /* Current epoch time */
  double latency = 0.0;
  time_t tv;

  if (msr == NULL)
    return 0.0;

  /* Calculate the time covered by the samples */
  if (msr->samprate > 0.0 && msr->samplecnt > 0)
    span = (1.0 / msr->samprate) * (msr->samplecnt - 1);

  /* Grab UTC time according to the system clock */
  epoch = (double)time (&tv);

  /* Now calculate the latency */
  latency = epoch - ((double)msr->starttime / HPTMODULUS) - span;

  return latency;
} /* End of msr_host_latency() */