forked from EarthScope/libmseed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracelist.c
2316 lines (1981 loc) · 70 KB
/
tracelist.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 handle TraceList and related structures.
*
* This file is part of the miniSEED Library.
*
* Copyright (c) 2019 Chad Trabant, IRIS Data Management Center
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "libmseed.h"
MS3TraceSeg *mstl3_msr2seg (MS3Record *msr, nstime_t endtime);
MS3TraceSeg *mstl3_addmsrtoseg (MS3TraceSeg *seg, MS3Record *msr, nstime_t endtime, int8_t whence);
MS3TraceSeg *mstl3_addsegtoseg (MS3TraceSeg *seg1, MS3TraceSeg *seg2);
MS3RecordPtr *mstl3_add_recordptr (MS3TraceSeg *seg, MS3Record *msr, nstime_t endtime, int8_t whence);
/**********************************************************************/ /**
* @brief Initialize a ::MS3TraceList container
*
* A new ::MS3TraceList is allocated if needed. If the supplied
* ::MS3TraceList is not NULL it will be re-initialized and any
* associated memory will be freed including data at \a prvtptr pointers.
*
* @param[in] mstl ::MS3TraceList to reinitialize or NULL
*
* @returns a pointer to a MS3TraceList struct on success or NULL on error.
***************************************************************************/
MS3TraceList *
mstl3_init (MS3TraceList *mstl)
{
if (mstl)
{
mstl3_free (&mstl, 1);
}
mstl = (MS3TraceList *)libmseed_memory.malloc (sizeof (MS3TraceList));
if (mstl == NULL)
{
ms_log (2, "%s(): Cannot allocate memory\n", __func__);
return NULL;
}
memset (mstl, 0, sizeof (MS3TraceList));
return mstl;
} /* End of mstl3_init() */
/**********************************************************************/ /**
* @brief Free all memory associated with a ::MS3TraceList
*
* The pointer to the target ::MS3TraceList will be set to NULL.
*
* @param[in] ppmstl Pointer-to-pointer to the target ::MS3TraceList to free
* @param[in] freeprvtptr If true, also free any data at the \a
* prvtptr members of ::MS3TraceID.prvtptr, ::MS3TraceSeg.prvtptr, and
* ::MS3RecordPtr.prvtptr (in ::MS3TraceSeg.recordlist)
***************************************************************************/
void
mstl3_free (MS3TraceList **ppmstl, int8_t freeprvtptr)
{
MS3TraceID *id = 0;
MS3TraceID *nextid = 0;
MS3TraceSeg *seg = 0;
MS3TraceSeg *nextseg = 0;
MS3RecordPtr *recordptr;
MS3RecordPtr *nextrecordptr;
if (!ppmstl)
return;
if (*ppmstl)
{
/* Free any associated traces */
id = (*ppmstl)->traces;
while (id)
{
nextid = id->next;
/* Free any associated trace segments */
seg = id->first;
while (seg)
{
nextseg = seg->next;
/* Free private pointer data if present and requested */
if (freeprvtptr && seg->prvtptr)
libmseed_memory.free (seg->prvtptr);
/* Free data array if allocated */
if (seg->datasamples)
libmseed_memory.free (seg->datasamples);
/* Free associated record list and related private pointers */
if (seg->recordlist)
{
recordptr = seg->recordlist->first;
while (recordptr)
{
nextrecordptr = recordptr->next;
if (recordptr->msr)
msr3_free (&recordptr->msr);
if (freeprvtptr && recordptr->prvtptr)
libmseed_memory.free (recordptr->prvtptr);
libmseed_memory.free (recordptr);
recordptr = nextrecordptr;
}
libmseed_memory.free (seg->recordlist);
}
libmseed_memory.free (seg);
seg = nextseg;
}
/* Free private pointer data if present and requested */
if (freeprvtptr && id->prvtptr)
libmseed_memory.free (id->prvtptr);
libmseed_memory.free (id);
id = nextid;
}
libmseed_memory.free (*ppmstl);
*ppmstl = NULL;
}
return;
} /* End of mstl3_free() */
/**********************************************************************/ /**
* @brief Add data coverage from an ::MS3Record to a ::MS3TraceList
*
* Searching the list for the appropriate ::MS3TraceID and
* ::MS3TraceSeg and either add data to existing entries or create new
* ones as needed.
*
* As this routine adds data to a trace list it constructs continuous
* time series, merging segments when possible. The \a tolerance
* pointer to a ::MS3Tolerance identifies function pointers that are
* expected to return time tolerace, in seconds, and sample rate
* tolerance, in Hertz, for the given ::MS3Record. If \a tolerance is
* NULL, or the function pointers it identifies are NULL, default
* tolerances will be used as follows: - Default time tolerance is 1/2
* the sampling period - Default sample rate (sr) tolerance is
* abs(1‐sr1/sr2) < 0.0001
*
* In recommended usage, the \a splitversion flag is \b 0 and
* different publication versions of otherwise matching data are
* merged. If more than one version contributes to a given source
* identifer's segments, its ::MS3TraceID.pubversion will be the set to
* the largest contributing version. If the \a splitversion flag is
* \b 1 the publication versions will be kept separate with each
* version isolated in separate ::MS3TraceID entries.
*
* If the \a autoheal flag is true, extra processing is invoked to
* conjoin trace segments that fit together after the ::MS3Record
* coverage is added. For segments that are removed, any memory at
* the ::MS3TraceSeg.prvtptr will be freed.
*
* If the \a pprecptr is not NULL a @ref record-list will be
* maintained for each segment. If the value of \c *pprecptr is NULL,
* a new ::MS3RecordPtr will be allocated, otherwise the supplied
* structure will be updated. The ::MS3RecordPtr will be added to the
* appropriate record list and the values of ::MS3RecordPtr.msr and
* ::MS3RecordPtr.endtime will be set, all other fields should be set
* by the caller.
*
* The lists are always maintained in a sorted order. An
* ::MS3TraceList is maintained with the ::MS3TraceID entries in
* ascending alphanumeric order of SID. If repeated SIDs are present
* due to splitting on publication version, they are maintained in
* order of ascending version. A ::MS3TraceID is always maintained
* with ::MS3TraceSeg entries in data time time order.
*
* @param[in] mstl Destination ::MS3TraceList to add data to
* @param[in] msr ::MS3Record containing the data to add to list
* @param[in] pprecptr Pointer to pointer to a ::MS3RecordPtr for @ref record-list
* @param[in] splitversion Flag to control splitting of version/quality
* @param[in] autoheal Flag to control automatic merging of segments
* @param[in] flags Flags to control optional functionality (unused)
* @param[in] tolerance Tolerance function pointers as ::MS3Tolerance
*
* @returns a pointer to the ::MS3TraceSeg updated or NULL on error.
*
* \sa mstl3_readbuffer()
* \sa ms3_readtracelist()
***************************************************************************/
MS3TraceSeg *
mstl3_addmsr_recordptr (MS3TraceList *mstl, MS3Record *msr, MS3RecordPtr **pprecptr,
int8_t splitversion, int8_t autoheal, uint32_t flags,
MS3Tolerance *tolerance)
{
MS3TraceID *id = 0;
MS3TraceID *searchid = 0;
MS3TraceID *ltid = 0;
MS3TraceSeg *seg = 0;
MS3TraceSeg *searchseg = 0;
MS3TraceSeg *segbefore = 0;
MS3TraceSeg *segafter = 0;
MS3TraceSeg *followseg = 0;
nstime_t endtime;
nstime_t pregap;
nstime_t postgap;
nstime_t lastgap;
nstime_t firstgap;
nstime_t nsdelta;
nstime_t nstimetol = 0;
nstime_t nnstimetol = 0;
double sampratehz;
double sampratetol = -1.0;
char *s1, *s2;
int8_t whence;
int8_t lastratecheck;
int8_t firstratecheck;
int mag;
int cmp;
int ltmag;
int ltcmp;
if (!mstl || !msr)
return NULL;
/* Calculate end time for MS3Record */
if ((endtime = msr3_endtime (msr)) == NSTERROR)
{
ms_log (2, "%s(): Error calculating record end time\n", __func__);
return NULL;
}
/* Search for matching trace ID starting with last accessed ID and
then looping through the trace ID list. */
if (mstl->last)
{
s1 = mstl->last->sid;
s2 = msr->sid;
while (*s1 == *s2++)
{
if (*s1++ == '\0')
break;
}
cmp = (*s1 - *--s2);
/* If source names matched, check publication version if splitting */
if (!cmp && splitversion && mstl->last->pubversion != msr->pubversion)
{
cmp = (mstl->last->pubversion < msr->pubversion) ? -1 : 1;
}
if (!cmp)
{
id = mstl->last;
}
else
{
/* Loop through trace ID list searching for a match, simultaneously
track the source name which is closest but less than the MS3Record
to allow for later insertion with sort order. */
searchid = mstl->traces;
ltcmp = 0;
ltmag = 0;
while (searchid)
{
/* Compare source names */
s1 = searchid->sid;
s2 = msr->sid;
mag = 0;
while (*s1 == *s2++)
{
mag++;
if (*s1++ == '\0')
break;
}
cmp = (*s1 - *--s2);
/* If source names matched, check publication version if splitting */
if (!cmp && splitversion && searchid->pubversion != msr->pubversion)
{
cmp = (searchid->pubversion < msr->pubversion) ? -1 : 1;
}
/* If source names did not match track closest "less than" value
and continue searching. */
if (cmp != 0)
{
if (cmp < 0)
{
if ((ltcmp == 0 || cmp >= ltcmp) && mag >= ltmag)
{
ltcmp = cmp;
ltmag = mag;
ltid = searchid;
}
else if (mag > ltmag)
{
ltcmp = cmp;
ltmag = mag;
ltid = searchid;
}
}
searchid = searchid->next;
continue;
}
/* If we made it this far we found a match */
id = searchid;
break;
}
}
} /* Done searching for match in trace ID list */
/* If no matching ID was found create new MS3TraceID and MS3TraceSeg entries */
if (!id)
{
if (!(id = (MS3TraceID *)libmseed_memory.malloc (sizeof (MS3TraceID))))
{
ms_log (2, "%s(): Error allocating memory\n", __func__);
return NULL;
}
memset (id, 0, sizeof (MS3TraceID));
/* Populate MS3TraceID */
memcpy (id->sid, msr->sid, sizeof(id->sid));
id->pubversion = msr->pubversion;
id->earliest = msr->starttime;
id->latest = endtime;
id->numsegments = 1;
if (!(seg = mstl3_msr2seg (msr, endtime)))
{
return NULL;
}
id->first = id->last = seg;
/* Add MS3RecordPtr if requested */
if (pprecptr && !(*pprecptr = mstl3_add_recordptr (seg, msr, endtime, 1)))
{
return NULL;
}
/* Add new MS3TraceID to MS3TraceList */
if (!mstl->traces || !ltid)
{
id->next = mstl->traces;
mstl->traces = id;
}
else
{
id->next = ltid->next;
ltid->next = id;
}
mstl->numtraces++;
}
/* Add data coverage to the matching MS3TraceID */
else
{
/* Calculate high-precision sample period, handling different rate notations */
if (msr->samprate > 0.0)
nsdelta = (nstime_t) (NSTMODULUS / msr->samprate); /* samples/second */
else if (msr->samprate < 0.0)
nsdelta = (nstime_t) (NSTMODULUS * -msr->samprate); /* period */
else
nsdelta = 0;
/* Calculate high-precision time tolerance */
if (tolerance && tolerance->time)
nstimetol = (nstime_t) (NSTMODULUS * tolerance->time (msr));
else
nstimetol = (nstime_t) (0.5 * nsdelta); /* Default time tolerance is 1/2 sample period */
nnstimetol = (nstimetol) ? -nstimetol : 0;
/* Calculate sample rate tolerance */
if (tolerance && tolerance->samprate)
sampratetol = tolerance->samprate (msr);
sampratehz = msr3_sampratehz(msr);
/* last/firstgap are negative when the record overlaps the trace
* segment and positive when there is a time gap. */
/* Gap relative to the last segment */
lastgap = msr->starttime - id->last->endtime - nsdelta;
/* Gap relative to the first segment */
firstgap = id->first->starttime - endtime - nsdelta;
/* Sample rate tolerance checks for first and last segments */
if (tolerance && tolerance->samprate)
{
lastratecheck = (sampratetol < 0.0 || ms_dabs (sampratehz - id->last->samprate) > sampratetol) ? 0 : 1;
firstratecheck = (sampratetol < 0.0 || ms_dabs (sampratehz - id->first->samprate) > sampratetol) ? 0 : 1;
}
else
{
lastratecheck = MS_ISRATETOLERABLE (sampratehz, id->last->samprate);
firstratecheck = MS_ISRATETOLERABLE (sampratehz, id->first->samprate);
}
/* Search first for the simple scenarios in order of likelihood:
* - Record fits at end of last segment
* - Record fits after all coverage
* - Record fits before all coverage
* - Record fits at beginning of first segment
*
* If none of those scenarios are true search the complete segment list.
*/
/* Record coverage fits at end of last segment */
if (lastgap <= nstimetol && lastgap >= nnstimetol && lastratecheck)
{
if (!mstl3_addmsrtoseg (id->last, msr, endtime, 1))
return NULL;
seg = id->last;
if (endtime > id->latest)
id->latest = endtime;
/* Add MS3RecordPtr if requested */
if (pprecptr && !(*pprecptr = mstl3_add_recordptr (seg, msr, endtime, 1)))
return NULL;
}
/* Record coverage is after all other coverage */
else if ((msr->starttime - nsdelta - nstimetol) > id->latest)
{
if (!(seg = mstl3_msr2seg (msr, endtime)))
return NULL;
/* Add to end of list */
id->last->next = seg;
seg->prev = id->last;
id->last = seg;
id->numsegments++;
if (endtime > id->latest)
id->latest = endtime;
/* Add MS3RecordPtr if requested */
if (pprecptr && !(*pprecptr = mstl3_add_recordptr (seg, msr, endtime, 0)))
return NULL;
}
/* Record coverage is before all other coverage */
else if ((endtime + nsdelta + nstimetol) < id->earliest)
{
if (!(seg = mstl3_msr2seg (msr, endtime)))
return NULL;
/* Add to beginning of list */
id->first->prev = seg;
seg->next = id->first;
id->first = seg;
id->numsegments++;
if (msr->starttime < id->earliest)
id->earliest = msr->starttime;
/* Add MS3RecordPtr if requested */
if (pprecptr && !(*pprecptr = mstl3_add_recordptr (seg, msr, endtime, 0)))
return NULL;
}
/* Record coverage fits at beginning of first segment */
else if (firstgap <= nstimetol && firstgap >= nnstimetol && firstratecheck)
{
if (!mstl3_addmsrtoseg (id->first, msr, endtime, 2))
return NULL;
seg = id->first;
if (msr->starttime < id->earliest)
id->earliest = msr->starttime;
/* Add MS3RecordPtr if requested */
if (pprecptr && !(*pprecptr = mstl3_add_recordptr (seg, msr, endtime, 2)))
return NULL;
}
/* Search complete segment list for matches */
else
{
searchseg = id->first;
segbefore = NULL; /* Find segment that record fits before */
segafter = NULL; /* Find segment that record fits after */
followseg = NULL; /* Track segment that record follows in time order */
while (searchseg)
{
if (msr->starttime > searchseg->starttime)
followseg = searchseg;
whence = 0;
postgap = msr->starttime - searchseg->endtime - nsdelta;
if (!segbefore && postgap <= nstimetol && postgap >= nnstimetol)
whence = 1;
pregap = searchseg->starttime - endtime - nsdelta;
if (!segafter && pregap <= nstimetol && pregap >= nnstimetol)
whence = 2;
if (!whence)
{
searchseg = searchseg->next;
continue;
}
if (tolerance && tolerance->samprate)
{
if (sampratetol >= 0 && ms_dabs (sampratehz - searchseg->samprate) > sampratetol)
{
searchseg = searchseg->next;
continue;
}
}
else
{
if (!MS_ISRATETOLERABLE (sampratehz, searchseg->samprate))
{
searchseg = searchseg->next;
continue;
}
}
if (whence == 1)
segbefore = searchseg;
else
segafter = searchseg;
/* Done searching if not autohealing */
if (!autoheal)
break;
/* Done searching if both before and after segments are found */
if (segbefore && segafter)
break;
searchseg = searchseg->next;
} /* Done looping through segments */
/* Add MS3Record coverage to end of segment before */
if (segbefore)
{
if (!mstl3_addmsrtoseg (segbefore, msr, endtime, 1))
{
return NULL;
}
/* Add MS3RecordPtr if requested */
if (pprecptr && !(*pprecptr = mstl3_add_recordptr (segbefore, msr, endtime, 1)))
{
return NULL;
}
/* Merge two segments that now fit if autohealing */
if (autoheal && segafter && segbefore != segafter)
{
/* Add segafter coverage to segbefore */
if (!mstl3_addsegtoseg (segbefore, segafter))
{
return NULL;
}
/* Shift last segment pointer if it's going to be removed */
if (segafter == id->last)
id->last = id->last->prev;
/* Remove segafter from list */
if (segafter->prev)
segafter->prev->next = segafter->next;
if (segafter->next)
segafter->next->prev = segafter->prev;
/* Free data samples, record list, private data and segment structure */
if (segafter->datasamples)
libmseed_memory.free (segafter->datasamples);
if (segafter->recordlist)
libmseed_memory.free (segafter->recordlist);
if (segafter->prvtptr)
libmseed_memory.free (segafter->prvtptr);
libmseed_memory.free (segafter);
id->numsegments -= 1;
}
seg = segbefore;
}
/* Add MS3Record coverage to beginning of segment after */
else if (segafter)
{
if (!mstl3_addmsrtoseg (segafter, msr, endtime, 2))
{
return NULL;
}
/* Add MS3RecordPtr if requested */
if (pprecptr && !(*pprecptr = mstl3_add_recordptr (segafter, msr, endtime, 2)))
{
return NULL;
}
seg = segafter;
}
/* Add MS3Record coverage to new segment */
else
{
/* Create new segment */
if (!(seg = mstl3_msr2seg (msr, endtime)))
{
return NULL;
}
/* Add MS3RecordPtr if requested */
if (pprecptr && !(*pprecptr = mstl3_add_recordptr (seg, msr, endtime, 0)))
{
return NULL;
}
/* Add new segment as first in list */
if (!followseg)
{
seg->next = id->first;
if (id->first)
id->first->prev = seg;
id->first = seg;
}
/* Add new segment after the followseg segment */
else
{
seg->next = followseg->next;
seg->prev = followseg;
if (followseg->next)
followseg->next->prev = seg;
followseg->next = seg;
if (followseg == id->last)
id->last = seg;
}
id->numsegments++;
}
} /* End of searching segment list */
/* Track largest publication version */
if (msr->pubversion > id->pubversion)
id->pubversion = msr->pubversion;
/* Track earliest and latest times */
if (msr->starttime < id->earliest)
id->earliest = msr->starttime;
if (endtime > id->latest)
id->latest = endtime;
} /* End of adding coverage to matching ID */
/* Sort modified segment into place, logic above should limit these to few shifts if any */
while (seg->next &&
(seg->starttime > seg->next->starttime ||
(seg->starttime == seg->next->starttime && seg->endtime < seg->next->endtime)))
{
/* Move segment down list, swap seg and seg->next */
segafter = seg->next;
if (seg->prev)
seg->prev->next = segafter;
if (segafter->next)
segafter->next->prev = seg;
segafter->prev = seg->prev;
seg->prev = segafter;
seg->next = segafter->next;
segafter->next = seg;
/* Reset first and last segment pointers if replaced */
if (id->first == seg)
id->first = segafter;
if (id->last == segafter)
id->last = seg;
}
while (seg->prev && (seg->starttime < seg->prev->starttime ||
(seg->starttime == seg->prev->starttime && seg->endtime > seg->prev->endtime)))
{
/* Move segment up list, swap seg and seg->prev */
segbefore = seg->prev;
if (seg->next)
seg->next->prev = segbefore;
if (segbefore->prev)
segbefore->prev->next = seg;
segbefore->next = seg->next;
seg->next = segbefore;
seg->prev = segbefore->prev;
segbefore->prev = seg;
/* Reset first and last segment pointers if replaced */
if (id->first == segbefore)
id->first = seg;
if (id->last == seg)
id->last = segbefore;
}
/* Set MS3TraceID as last accessed */
mstl->last = id;
return seg;
} /* End of mstl3_addmsr_recordptr() */
/****************************************************************/ /**
* @brief Parse miniSEED from a buffer and populate a ::MS3TraceList
*
* For a full description of \a tolerance see mstl3_addmsr().
*
* If the ::MSF_UNPACKDATA flag is set in \a flags, the data samples
* will be unpacked. In most cases the caller probably wants this
* flag set, without it the trace list will merely be a list of
* channels.
*
* If the ::MSF_RECORDLIST flag is set in \a flags, a ::MS3RecordList
* will be built for each ::MS3TraceSeg. The ::MS3RecordPtr entries
* contain the location of the data record, bit flags, extra headers, etc.
*
* @param[in] ppmstl Pointer-to-point to destination MS3TraceList
* @param[in] buffer Source buffer to read miniSEED records from
* @param[in] bufferlength Maximum length of \a buffer
* @param[in] splitversion Flag to control splitting of version/quality
* @param[in] flags Flags to control parsing and optional functionality:
* @parblock
* - \c ::MSF_RECORDLIST : Build a ::MS3RecordList for each ::MS3TraceSeg
* - Flags supported by msr3_parse()
* - Flags supported by mstl3_addmsr()
* @endparblock
* @param[in] tolerance Tolerance function pointers as ::MS3Tolerance
* @param[in] verbose Controls verbosity, 0 means no diagnostic output
*
* @returns The number of records parsed on success, otherwise a
* negative library error code.
*
* \sa mstl3_addmsr()
*********************************************************************/
int64_t
mstl3_readbuffer (MS3TraceList **ppmstl, char *buffer, uint64_t bufferlength,
int8_t splitversion, uint32_t flags,
MS3Tolerance *tolerance, int8_t verbose)
{
return mstl3_readbuffer_selection (ppmstl, buffer, bufferlength,
splitversion, flags, tolerance,
NULL, verbose);
} /* End of mstl3_readbuffer() */
/****************************************************************/ /**
* @brief Parse miniSEED from a buffer and populate a ::MS3TraceList
*
* For a full description of \a tolerance see mstl3_addmsr().
*
* If the ::MSF_UNPACKDATA flag is set in \a flags, the data samples
* will be unpacked. In most cases the caller probably wants this
* flag set, without it the trace list will merely be a list of
* channels.
*
* If the ::MSF_RECORDLIST flag is set in \a flags, a ::MS3RecordList
* will be built for each ::MS3TraceSeg. The ::MS3RecordPtr entries
* contain the location of the data record, bit flags, extra headers, etc.
*
* If \a selections is not NULL, the ::MS3Selections will be used to
* limit what is returned to the caller. Any data not matching the
* selections will be skipped.
*
* @param[in] ppmstl Pointer-to-point to destination MS3TraceList
* @param[in] buffer Source buffer to read miniSEED records from
* @param[in] bufferlength Maximum length of \a buffer
* @param[in] splitversion Flag to control splitting of version/quality
* @param[in] flags Flags to control parsing and optional functionality:
* @parblock
* - \c ::MSF_RECORDLIST : Build a ::MS3RecordList for each ::MS3TraceSeg
* - Flags supported by msr3_parse()
* - Flags supported by mstl3_addmsr()
* @endparblock
* @param[in] tolerance Tolerance function pointers as ::MS3Tolerance
* @param[in] selections Specify limits to which data should be returned, see @ref data-selections
* @param[in] verbose Controls verbosity, 0 means no diagnostic output
*
* @returns The number of records parsed on success, otherwise a
* negative library error code.
*
* \sa mstl3_addmsr()
*********************************************************************/
int64_t
mstl3_readbuffer_selection (MS3TraceList **ppmstl, char *buffer, uint64_t bufferlength,
int8_t splitversion, uint32_t flags,
MS3Tolerance *tolerance, MS3Selections *selections,
int8_t verbose)
{
MS3Record *msr = NULL;
MS3TraceSeg *seg = NULL;
MS3RecordPtr *recordptr = NULL;
uint32_t dataoffset;
uint32_t datasize;
uint64_t offset = 0;
uint32_t pflags = flags;
int64_t reccount = 0;
int parsevalue;
if (!ppmstl)
return MS_GENERROR;
/* Initialize MS3TraceList if needed */
if (!*ppmstl)
{
*ppmstl = mstl3_init (*ppmstl);
if (!*ppmstl)
return MS_GENERROR;
}
/* Defer data unpacking if selections are used by unsetting MSF_UNPACKDATA */
if ((flags & MSF_UNPACKDATA) && selections)
pflags &= ~(MSF_UNPACKDATA);
while ((bufferlength - offset) > MINRECLEN)
{
parsevalue = msr3_parse (buffer + offset, bufferlength - offset, &msr, pflags, verbose);
if (parsevalue < 0)
{
if (msr)
msr3_free (&msr);
return parsevalue;
}
if (parsevalue > 0)
break;
/* Test data against selections if specified */
if (selections)
{
if (!ms3_matchselect (selections, msr->sid, msr->starttime,
msr3_endtime (msr), msr->pubversion, NULL))
{
if (verbose > 1)
{
ms_log (1, "Skipping (selection) record for %s (%d bytes) starting at offset %" PRIu64 "\n",
msr->sid, msr->reclen, offset);
}
offset += msr->reclen;
continue;
}
/* Unpack data samples if this has been deferred */
if ((flags & MSF_UNPACKDATA) && msr->samplecnt > 0)
{
if (msr3_unpack_data (msr, verbose) != msr->samplecnt)
{
if (msr)
msr3_free (&msr);
return MS_GENERROR;
}
}
}
/* Add record to trace list */
seg = mstl3_addmsr_recordptr (*ppmstl, msr, (flags & MSF_RECORDLIST) ? &recordptr : NULL,
splitversion, 1, flags, tolerance);
if (seg == NULL)
{
if (msr)
msr3_free (&msr);
return MS_GENERROR;
}
/* Populate remaining fields of record pointer */
if (recordptr)
{
/* Determine offset to data and length of data payload */
if (msr3_data_bounds (msr, &dataoffset, &datasize))
return MS_GENERROR;
recordptr->bufferptr = buffer + offset;
recordptr->fileptr = NULL;
recordptr->filename = NULL;
recordptr->fileoffset = 0;
recordptr->dataoffset = dataoffset;
recordptr->prvtptr = NULL;
}
reccount += 1;
offset += msr->reclen;
}
if (msr)
msr3_free (&msr);
return reccount;
} /* End of mstl3_readbuffer_selection() */
/***************************************************************************
* Create an MS3TraceSeg structure from an MS3Record structure.
*
* Return a pointer to a MS3TraceSeg otherwise 0 on error.
***************************************************************************/
MS3TraceSeg *
mstl3_msr2seg (MS3Record *msr, nstime_t endtime)
{
MS3TraceSeg *seg = 0;
size_t datasize = 0;
int samplesize;
if (!(seg = (MS3TraceSeg *)libmseed_memory.malloc (sizeof (MS3TraceSeg))))
{
ms_log (2, "%s(): Error allocating memory\n", __func__);
return 0;
}
memset (seg, 0, sizeof (MS3TraceSeg));
/* Populate MS3TraceSeg */
seg->starttime = msr->starttime;
seg->endtime = endtime;
seg->samprate = msr3_sampratehz(msr);
seg->samplecnt = msr->samplecnt;
seg->sampletype = msr->sampletype;
seg->numsamples = msr->numsamples;
/* Allocate space for and copy datasamples */
if (msr->datasamples && msr->numsamples)
{
if (!(samplesize = ms_samplesize (msr->sampletype)))
{
ms_log (2, "%s(): Unknown sample size for sample type: %c\n", __func__, msr->sampletype);
return 0;
}
datasize = samplesize * msr->numsamples;
if (!(seg->datasamples = libmseed_memory.malloc ((size_t) (datasize))))
{
ms_log (2, "%s(): Error allocating memory\n", __func__);
return 0;
}
seg->datasize = datasize;
/* Copy data samples from MS3Record to MS3TraceSeg */
memcpy (seg->datasamples, msr->datasamples, datasize);
}
return seg;
} /* End of mstl3_msr2seg() */
/***************************************************************************
* Add data coverage from a MS3Record structure to a MS3TraceSeg structure.
*
* Data coverage is added to the beginning or end of MS3TraceSeg
* according to the whence flag:
* 1 : add coverage to the end
* 2 : add coverage to the beginninig
*
* Return a pointer to a MS3TraceSeg otherwise 0 on error.
***************************************************************************/
MS3TraceSeg *
mstl3_addmsrtoseg (MS3TraceSeg *seg, MS3Record *msr, nstime_t endtime, int8_t whence)
{