forked from EarthScope/libmseed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceutils.c
1854 lines (1592 loc) · 50.5 KB
/
traceutils.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
/***************************************************************************
* traceutils.c:
*
* Generic routines to handle Traces.
*
* Written by Chad Trabant, IRIS Data Management Center
*
* modified: 2015.108
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libmseed.h"
static int mst_groupsort_cmp (MSTrace *mst1, MSTrace *mst2, flag quality);
/***************************************************************************
* mst_init:
*
* Initialize and return a MSTrace struct, allocating memory if needed.
* If the specified MSTrace includes data samples they will be freed.
*
* Returns a pointer to a MSTrace struct on success or NULL on error.
***************************************************************************/
MSTrace *
mst_init (MSTrace *mst)
{
/* Free datasamples, prvtptr and stream state if present */
if (mst)
{
if (mst->datasamples)
free (mst->datasamples);
if (mst->prvtptr)
free (mst->prvtptr);
if (mst->ststate)
free (mst->ststate);
}
else
{
mst = (MSTrace *)malloc (sizeof (MSTrace));
}
if (mst == NULL)
{
ms_log (2, "mst_init(): Cannot allocate memory\n");
return NULL;
}
memset (mst, 0, sizeof (MSTrace));
return mst;
} /* End of mst_init() */
/***************************************************************************
* mst_free:
*
* Free all memory associated with a MSTrace struct and set the pointer
* to 0.
***************************************************************************/
void
mst_free (MSTrace **ppmst)
{
if (ppmst && *ppmst)
{
/* Free datasamples if present */
if ((*ppmst)->datasamples)
free ((*ppmst)->datasamples);
/* Free private memory if present */
if ((*ppmst)->prvtptr)
free ((*ppmst)->prvtptr);
/* Free stream processing state if present */
if ((*ppmst)->ststate)
free ((*ppmst)->ststate);
free (*ppmst);
*ppmst = 0;
}
} /* End of mst_free() */
/***************************************************************************
* mst_initgroup:
*
* Initialize and return a MSTraceGroup struct, allocating memory if
* needed. If the supplied MSTraceGroup is not NULL any associated
* memory it will be freed.
*
* Returns a pointer to a MSTraceGroup struct on success or NULL on error.
***************************************************************************/
MSTraceGroup *
mst_initgroup (MSTraceGroup *mstg)
{
MSTrace *mst = 0;
MSTrace *next = 0;
if (mstg)
{
mst = mstg->traces;
while (mst)
{
next = mst->next;
mst_free (&mst);
mst = next;
}
}
else
{
mstg = (MSTraceGroup *)malloc (sizeof (MSTraceGroup));
}
if (mstg == NULL)
{
ms_log (2, "mst_initgroup(): Cannot allocate memory\n");
return NULL;
}
memset (mstg, 0, sizeof (MSTraceGroup));
return mstg;
} /* End of mst_initgroup() */
/***************************************************************************
* mst_freegroup:
*
* Free all memory associated with a MSTraceGroup struct and set the
* pointer to 0.
***************************************************************************/
void
mst_freegroup (MSTraceGroup **ppmstg)
{
MSTrace *mst = 0;
MSTrace *next = 0;
if (*ppmstg)
{
mst = (*ppmstg)->traces;
while (mst)
{
next = mst->next;
mst_free (&mst);
mst = next;
}
free (*ppmstg);
*ppmstg = 0;
}
} /* End of mst_freegroup() */
/***************************************************************************
* mst_findmatch:
*
* Traverse the MSTrace chain starting at 'startmst' until a MSTrace
* is found that matches the given name identifiers. If the dataquality
* byte is not 0 it must also match.
*
* Return a pointer a matching MSTrace otherwise 0 if no match found.
***************************************************************************/
MSTrace *
mst_findmatch (MSTrace *startmst, char dataquality,
char *network, char *station, char *location, char *channel)
{
int idx;
if (!startmst)
return 0;
while (startmst)
{
if (dataquality && dataquality != startmst->dataquality)
{
startmst = startmst->next;
continue;
}
/* Compare network */
idx = 0;
while (network[idx] == startmst->network[idx])
{
if (network[idx] == '\0')
break;
idx++;
}
if (network[idx] != '\0' || startmst->network[idx] != '\0')
{
startmst = startmst->next;
continue;
}
/* Compare station */
idx = 0;
while (station[idx] == startmst->station[idx])
{
if (station[idx] == '\0')
break;
idx++;
}
if (station[idx] != '\0' || startmst->station[idx] != '\0')
{
startmst = startmst->next;
continue;
}
/* Compare location */
idx = 0;
while (location[idx] == startmst->location[idx])
{
if (location[idx] == '\0')
break;
idx++;
}
if (location[idx] != '\0' || startmst->location[idx] != '\0')
{
startmst = startmst->next;
continue;
}
/* Compare channel */
idx = 0;
while (channel[idx] == startmst->channel[idx])
{
if (channel[idx] == '\0')
break;
idx++;
}
if (channel[idx] != '\0' || startmst->channel[idx] != '\0')
{
startmst = startmst->next;
continue;
}
/* A match was found if we made it this far */
break;
}
return startmst;
} /* End of mst_findmatch() */
/***************************************************************************
* mst_findadjacent:
*
* Find a MSTrace in a MSTraceGroup matching the given name
* identifiers, samplerate and is adjacent with a time span. If the
* dataquality byte is not 0 it must also match.
*
* The time tolerance and sample rate tolerance are used to determine
* if traces abut. If timetol is -1.0 the default tolerance of 1/2
* the sample period will be used. If samprratetol is -1.0 the
* default tolerance check of abs(1-sr1/sr2) < 0.0001 is used (defined
* in libmseed.h). If timetol or sampratetol is -2.0 the respective
* tolerance check will not be performed.
*
* The 'whence' flag will be set, when a matching MSTrace is found, to
* indicate where the indicated time span is adjacent to the MSTrace
* using the following values:
* 1: time span fits at the end of the MSTrace
* 2: time span fits at the beginning of the MSTrace
*
* Return a pointer a matching MSTrace and set the 'whence' flag
* otherwise 0 if no match found.
***************************************************************************/
MSTrace *
mst_findadjacent (MSTraceGroup *mstg, flag *whence, char dataquality,
char *network, char *station, char *location, char *channel,
double samprate, double sampratetol,
hptime_t starttime, hptime_t endtime, double timetol)
{
MSTrace *mst = 0;
hptime_t pregap;
hptime_t postgap;
hptime_t hpdelta;
hptime_t hptimetol = 0;
hptime_t nhptimetol = 0;
int idx;
if (!mstg)
return 0;
*whence = 0;
/* Calculate high-precision sample period */
hpdelta = (hptime_t) ((samprate) ? (HPTMODULUS / samprate) : 0.0);
/* Calculate high-precision time tolerance */
if (timetol == -1.0)
hptimetol = (hptime_t) (0.5 * hpdelta); /* Default time tolerance is 1/2 sample period */
else if (timetol >= 0.0)
hptimetol = (hptime_t) (timetol * HPTMODULUS);
nhptimetol = (hptimetol) ? -hptimetol : 0;
mst = mstg->traces;
while (mst)
{
/* post/pregap are negative when the record overlaps the trace
* segment and positive when there is a time gap. */
postgap = starttime - mst->endtime - hpdelta;
pregap = mst->starttime - endtime - hpdelta;
/* If not checking the time tolerance decide if beginning or end is a better fit */
if (timetol == -2.0)
{
if (ms_dabs ((double)postgap) < ms_dabs ((double)pregap))
*whence = 1;
else
*whence = 2;
}
else
{
if (postgap <= hptimetol && postgap >= nhptimetol)
{
/* Span fits right at the end of the trace */
*whence = 1;
}
else if (pregap <= hptimetol && pregap >= nhptimetol)
{
/* Span fits right at the beginning of the trace */
*whence = 2;
}
else
{
/* Span does not fit with this Trace */
mst = mst->next;
continue;
}
}
/* Perform samprate tolerance check if requested */
if (sampratetol != -2.0)
{
/* Perform default samprate tolerance check if requested */
if (sampratetol == -1.0)
{
if (!MS_ISRATETOLERABLE (samprate, mst->samprate))
{
mst = mst->next;
continue;
}
}
/* Otherwise check against the specified sample rate tolerance */
else if (ms_dabs (samprate - mst->samprate) > sampratetol)
{
mst = mst->next;
continue;
}
}
/* Compare data qualities */
if (dataquality && dataquality != mst->dataquality)
{
mst = mst->next;
continue;
}
/* Compare network */
idx = 0;
while (network[idx] == mst->network[idx])
{
if (network[idx] == '\0')
break;
idx++;
}
if (network[idx] != '\0' || mst->network[idx] != '\0')
{
mst = mst->next;
continue;
}
/* Compare station */
idx = 0;
while (station[idx] == mst->station[idx])
{
if (station[idx] == '\0')
break;
idx++;
}
if (station[idx] != '\0' || mst->station[idx] != '\0')
{
mst = mst->next;
continue;
}
/* Compare location */
idx = 0;
while (location[idx] == mst->location[idx])
{
if (location[idx] == '\0')
break;
idx++;
}
if (location[idx] != '\0' || mst->location[idx] != '\0')
{
mst = mst->next;
continue;
}
/* Compare channel */
idx = 0;
while (channel[idx] == mst->channel[idx])
{
if (channel[idx] == '\0')
break;
idx++;
}
if (channel[idx] != '\0' || mst->channel[idx] != '\0')
{
mst = mst->next;
continue;
}
/* A match was found if we made it this far */
break;
}
return mst;
} /* End of mst_findadjacent() */
/***************************************************************************
* mst_addmsr:
*
* Add MSRecord time coverage to a MSTrace. The start or end time will
* be updated and samples will be copied if they exist. No checking
* is done to verify that the record matches the trace in any way.
*
* If whence is 1 the coverage will be added at the end of the trace,
* whereas if whence is 2 the coverage will be added at the beginning
* of the trace.
*
* Return 0 on success and -1 on error.
***************************************************************************/
int
mst_addmsr (MSTrace *mst, MSRecord *msr, flag whence)
{
int samplesize = 0;
if (!mst || !msr)
return -1;
/* Reallocate data sample buffer if samples are present */
if (msr->datasamples && msr->numsamples >= 0)
{
/* Check that the entire record was decompressed */
if (msr->samplecnt != msr->numsamples)
{
ms_log (2, "mst_addmsr(): Sample counts do not match, record not fully decompressed?\n");
ms_log (2, " The sample buffer will likely contain a discontinuity.\n");
}
if ((samplesize = ms_samplesize (msr->sampletype)) == 0)
{
ms_log (2, "mst_addmsr(): Unrecognized sample type: '%c'\n",
msr->sampletype);
return -1;
}
if (msr->sampletype != mst->sampletype)
{
ms_log (2, "mst_addmsr(): Mismatched sample type, '%c' and '%c'\n",
msr->sampletype, mst->sampletype);
return -1;
}
mst->datasamples = realloc (mst->datasamples,
(size_t) (mst->numsamples * samplesize + msr->numsamples * samplesize));
if (mst->datasamples == NULL)
{
ms_log (2, "mst_addmsr(): Cannot allocate memory\n");
return -1;
}
}
/* Add samples at end of trace */
if (whence == 1)
{
if (msr->datasamples && msr->numsamples >= 0)
{
memcpy ((char *)mst->datasamples + (mst->numsamples * samplesize),
msr->datasamples,
(size_t) (msr->numsamples * samplesize));
mst->numsamples += msr->numsamples;
}
mst->endtime = msr_endtime (msr);
if (mst->endtime == HPTERROR)
{
ms_log (2, "mst_addmsr(): Error calculating record end time\n");
return -1;
}
}
/* Add samples at the beginning of trace */
else if (whence == 2)
{
if (msr->datasamples && msr->numsamples >= 0)
{
/* Move any samples to end of buffer */
if (mst->numsamples > 0)
{
memmove ((char *)mst->datasamples + (msr->numsamples * samplesize),
mst->datasamples,
(size_t) (mst->numsamples * samplesize));
}
memcpy (mst->datasamples,
msr->datasamples,
(size_t) (msr->numsamples * samplesize));
mst->numsamples += msr->numsamples;
}
mst->starttime = msr->starttime;
}
/* If two different data qualities reset the MSTrace.dataquality to 0 */
if (mst->dataquality && msr->dataquality && mst->dataquality != msr->dataquality)
mst->dataquality = 0;
/* Update MSTrace sample count */
mst->samplecnt += msr->samplecnt;
return 0;
} /* End of mst_addmsr() */
/***************************************************************************
* mst_addspan:
*
* Add a time span to a MSTrace. The start or end time will be updated
* and samples will be copied if they are provided. No checking is done to
* verify that the record matches the trace in any way.
*
* If whence is 1 the coverage will be added at the end of the trace,
* whereas if whence is 2 the coverage will be added at the beginning
* of the trace.
*
* Return 0 on success and -1 on error.
***************************************************************************/
int
mst_addspan (MSTrace *mst, hptime_t starttime, hptime_t endtime,
void *datasamples, int64_t numsamples, char sampletype,
flag whence)
{
int samplesize = 0;
if (!mst)
return -1;
if (datasamples && numsamples > 0)
{
if ((samplesize = ms_samplesize (sampletype)) == 0)
{
ms_log (2, "mst_addspan(): Unrecognized sample type: '%c'\n",
sampletype);
return -1;
}
if (sampletype != mst->sampletype)
{
ms_log (2, "mst_addspan(): Mismatched sample type, '%c' and '%c'\n",
sampletype, mst->sampletype);
return -1;
}
mst->datasamples = realloc (mst->datasamples,
(size_t) (mst->numsamples * samplesize + numsamples * samplesize));
if (mst->datasamples == NULL)
{
ms_log (2, "mst_addspan(): Cannot allocate memory\n");
return -1;
}
}
/* Add samples at end of trace */
if (whence == 1)
{
if (datasamples && numsamples > 0)
{
memcpy ((char *)mst->datasamples + (mst->numsamples * samplesize),
datasamples,
(size_t) (numsamples * samplesize));
mst->numsamples += numsamples;
}
mst->endtime = endtime;
}
/* Add samples at the beginning of trace */
else if (whence == 2)
{
if (datasamples && numsamples > 0)
{
/* Move any samples to end of buffer */
if (mst->numsamples > 0)
{
memmove ((char *)mst->datasamples + (numsamples * samplesize),
mst->datasamples,
(size_t) (mst->numsamples * samplesize));
}
memcpy (mst->datasamples,
datasamples,
(size_t) (numsamples * samplesize));
mst->numsamples += numsamples;
}
mst->starttime = starttime;
}
/* Update MSTrace sample count */
if (numsamples > 0)
mst->samplecnt += numsamples;
return 0;
} /* End of mst_addspan() */
/***************************************************************************
* mst_addmsrtogroup:
*
* Add data samples from a MSRecord to a MSTrace in a MSTraceGroup by
* searching the group for the approriate MSTrace and either adding data
* to it or creating a new MSTrace if no match found.
*
* Matching traces are found using the mst_findadjacent() routine. If
* the dataquality flag is true the data quality bytes must also match
* otherwise they are ignored.
*
* Return a pointer to the MSTrace updated or 0 on error.
***************************************************************************/
MSTrace *
mst_addmsrtogroup (MSTraceGroup *mstg, MSRecord *msr, flag dataquality,
double timetol, double sampratetol)
{
MSTrace *mst = 0;
hptime_t endtime;
flag whence;
char dq;
if (!mstg || !msr)
return 0;
dq = (dataquality) ? msr->dataquality : 0;
endtime = msr_endtime (msr);
if (endtime == HPTERROR)
{
ms_log (2, "mst_addmsrtogroup(): Error calculating record end time\n");
return 0;
}
/* Find matching, time adjacent MSTrace */
mst = mst_findadjacent (mstg, &whence, dq,
msr->network, msr->station, msr->location, msr->channel,
msr->samprate, sampratetol,
msr->starttime, endtime, timetol);
/* If a match was found update it otherwise create a new MSTrace and
add to end of MSTrace chain */
if (mst)
{
/* Records with no time coverage do not contribute to a trace */
if (msr->samplecnt <= 0 || msr->samprate <= 0.0)
return mst;
if (mst_addmsr (mst, msr, whence))
{
return 0;
}
}
else
{
mst = mst_init (NULL);
mst->dataquality = dq;
strncpy (mst->network, msr->network, sizeof (mst->network));
strncpy (mst->station, msr->station, sizeof (mst->station));
strncpy (mst->location, msr->location, sizeof (mst->location));
strncpy (mst->channel, msr->channel, sizeof (mst->channel));
mst->starttime = msr->starttime;
mst->samprate = msr->samprate;
mst->sampletype = msr->sampletype;
if (mst_addmsr (mst, msr, 1))
{
mst_free (&mst);
return 0;
}
/* Link new MSTrace into the end of the chain */
if (!mstg->traces)
{
mstg->traces = mst;
}
else
{
MSTrace *lasttrace = mstg->traces;
while (lasttrace->next)
lasttrace = lasttrace->next;
lasttrace->next = mst;
}
mstg->numtraces++;
}
return mst;
} /* End of mst_addmsrtogroup() */
/***************************************************************************
* mst_addtracetogroup:
*
* Add a MSTrace to a MSTraceGroup at the end of the MSTrace chain.
*
* Return a pointer to the MSTrace added or 0 on error.
***************************************************************************/
MSTrace *
mst_addtracetogroup (MSTraceGroup *mstg, MSTrace *mst)
{
MSTrace *lasttrace;
if (!mstg || !mst)
return 0;
if (!mstg->traces)
{
mstg->traces = mst;
}
else
{
lasttrace = mstg->traces;
while (lasttrace->next)
lasttrace = lasttrace->next;
lasttrace->next = mst;
}
mst->next = 0;
mstg->numtraces++;
return mst;
} /* End of mst_addtracetogroup() */
/***************************************************************************
* mst_groupheal:
*
* Check if traces in MSTraceGroup can be healed, if contiguous segments
* belong together they will be merged. This routine is only useful
* if the trace group was assembled from segments out of time order
* (e.g. a file of Mini-SEED records not in time order) but forming
* contiguous time coverage. The MSTraceGroup will be sorted using
* mst_groupsort() before healing.
*
* The time tolerance and sample rate tolerance are used to determine
* if the traces are indeed the same. If timetol is -1.0 the default
* tolerance of 1/2 the sample period will be used. If samprratetol
* is -1.0 the default tolerance check of abs(1-sr1/sr2) < 0.0001 is
* used (defined in libmseed.h).
*
* Return number of trace mergings on success otherwise -1 on error.
***************************************************************************/
int
mst_groupheal (MSTraceGroup *mstg, double timetol, double sampratetol)
{
int mergings = 0;
MSTrace *curtrace = 0;
MSTrace *nexttrace = 0;
MSTrace *searchtrace = 0;
MSTrace *prevtrace = 0;
int8_t merged = 0;
double postgap, pregap, delta;
if (!mstg)
return -1;
/* Sort MSTraceGroup before any healing */
if (mst_groupsort (mstg, 1))
return -1;
curtrace = mstg->traces;
while (curtrace)
{
nexttrace = mstg->traces;
prevtrace = mstg->traces;
while (nexttrace)
{
searchtrace = nexttrace;
nexttrace = searchtrace->next;
/* Do not process the same MSTrace we are trying to match */
if (searchtrace == curtrace)
{
prevtrace = searchtrace;
continue;
}
/* Check if this trace matches the curtrace */
if (strcmp (searchtrace->network, curtrace->network) ||
strcmp (searchtrace->station, curtrace->station) ||
strcmp (searchtrace->location, curtrace->location) ||
strcmp (searchtrace->channel, curtrace->channel))
{
prevtrace = searchtrace;
continue;
}
/* Perform default samprate tolerance check if requested */
if (sampratetol == -1.0)
{
if (!MS_ISRATETOLERABLE (searchtrace->samprate, curtrace->samprate))
{
prevtrace = searchtrace;
continue;
}
}
/* Otherwise check against the specified sample rates tolerance */
else if (ms_dabs (searchtrace->samprate - curtrace->samprate) > sampratetol)
{
prevtrace = searchtrace;
continue;
}
merged = 0;
/* post/pregap are negative when searchtrace overlaps curtrace
segment and positive when there is a time gap. */
delta = (curtrace->samprate) ? (1.0 / curtrace->samprate) : 0.0;
postgap = ((double)(searchtrace->starttime - curtrace->endtime) / HPTMODULUS) - delta;
pregap = ((double)(curtrace->starttime - searchtrace->endtime) / HPTMODULUS) - delta;
/* Calculate default time tolerance (1/2 sample period) if needed */
if (timetol == -1.0)
timetol = 0.5 * delta;
/* Fits right at the end of curtrace */
if (ms_dabs (postgap) <= timetol)
{
/* Merge searchtrace with curtrace */
mst_addspan (curtrace, searchtrace->starttime, searchtrace->endtime,
searchtrace->datasamples, searchtrace->numsamples,
searchtrace->sampletype, 1);
/* If no data is present, make sure sample count is updated */
if (searchtrace->numsamples <= 0)
curtrace->samplecnt += searchtrace->samplecnt;
/* If qualities do not match reset the indicator */
if (curtrace->dataquality != searchtrace->dataquality)
curtrace->dataquality = 0;
merged = 1;
}
/* Fits right at the beginning of curtrace */
else if (ms_dabs (pregap) <= timetol)
{
/* Merge searchtrace with curtrace */
mst_addspan (curtrace, searchtrace->starttime, searchtrace->endtime,
searchtrace->datasamples, searchtrace->numsamples,
searchtrace->sampletype, 2);
/* If no data is present, make sure sample count is updated */
if (searchtrace->numsamples <= 0)
curtrace->samplecnt += searchtrace->samplecnt;
/* If qualities do not match reset the indicator */
if (curtrace->dataquality != searchtrace->dataquality)
curtrace->dataquality = 0;
merged = 1;
}
/* If searchtrace was merged with curtrace remove it from the chain */
if (merged)
{
/* Re-link trace chain and free searchtrace */
if (searchtrace == mstg->traces)
mstg->traces = nexttrace;
else
prevtrace->next = nexttrace;
mst_free (&searchtrace);
mstg->numtraces--;
mergings++;
}
else
{
prevtrace = searchtrace;
}
}
curtrace = curtrace->next;
}
return mergings;
} /* End of mst_groupheal() */
/***************************************************************************
* mst_groupsort:
*
* Sort a MSTraceGroup using a mergesort algorithm. MSTrace entries
* are compared using the mst_groupsort_cmp() function.
*
* The mergesort implementation was inspired by the listsort function
* published and copyright 2001 by Simon Tatham.
*
* Return 0 on success and -1 on error.
***************************************************************************/
int
mst_groupsort (MSTraceGroup *mstg, flag quality)
{
MSTrace *p, *q, *e, *top, *tail;
int nmerges;
int insize, psize, qsize, i;
if (!mstg)
return -1;
if (!mstg->traces)
return 0;
top = mstg->traces;
insize = 1;
for (;;)
{
p = top;
top = NULL;
tail = NULL;
nmerges = 0; /* count number of merges we do in this pass */
while (p)
{
nmerges++; /* there exists a merge to be done */
/* step `insize' places along from p */
q = p;
psize = 0;
for (i = 0; i < insize; i++)
{
psize++;
q = q->next;
if (!q)
break;
}
/* if q hasn't fallen off end, we have two lists to merge */
qsize = insize;
/* now we have two lists; merge them */
while (psize > 0 || (qsize > 0 && q))
{
/* decide whether next element of merge comes from p or q */
if (psize == 0)
{ /* p is empty; e must come from q. */
e = q;
q = q->next;
qsize--;
}
else if (qsize == 0 || !q)
{ /* q is empty; e must come from p. */
e = p;
p = p->next;
psize--;
}
else if (mst_groupsort_cmp (p, q, quality) <= 0)
{ /* First element of p is lower (or same), e must come from p. */
e = p;
p = p->next;
psize--;
}
else
{ /* First element of q is lower; e must come from q. */
e = q;
q = q->next;
qsize--;
}
/* add the next element to the merged list */
if (tail)