-
Notifications
You must be signed in to change notification settings - Fork 34
/
libmseed.h
1480 lines (1256 loc) · 61.9 KB
/
libmseed.h
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
/**********************************************************************/ /**
* @file libmseed.h
*
* Interface declarations for the miniSEED Library (libmseed).
*
* This file is part of the miniSEED Library.
*
* 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.
*
* Copyright (C) 2024:
* @author Chad Trabant, EarthScope Data Services
***************************************************************************/
#ifndef LIBMSEED_H
#define LIBMSEED_H 1
#ifdef __cplusplus
extern "C" {
#endif
#define LIBMSEED_VERSION "3.1.3" //!< Library version
#define LIBMSEED_RELEASE "2024.165" //!< Library release date
/** @defgroup io-functions File and URL I/O */
/** @defgroup miniseed-record Record Handling */
/** @defgroup trace-list Trace List */
/** @defgroup data-selections Data Selections */
/** @defgroup string-functions Source Identifiers */
/** @defgroup extra-headers Extra Headers */
/** @defgroup record-list Record List */
/** @defgroup time-related Time definitions and functions */
/** @defgroup logging Central Logging */
/** @defgroup utility-functions General Utility Functions */
/** @defgroup leapsecond Leap Second Handling */
/** @defgroup low-level Low level definitions
@brief The low-down, the nitty gritty, the basics */
/** @defgroup memory-allocators Memory Allocators
@ingroup low-level */
/** @defgroup encoding-values Data Encodings
@ingroup low-level */
/** @defgroup byte-swap-flags Byte swap flags
@ingroup low-level */
/** @defgroup return-values Return codes
@ingroup low-level */
/** @defgroup control-flags Control flags
@ingroup low-level */
/* C99 standard headers */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
/** @def PRIsize_t
@brief A printf() macro for portably printing size_t values */
#define PRIsize_t "zu"
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
#define LMP_WIN 1
#endif
/* Set platform specific features, Windows versus everything else */
#if defined(LMP_WIN)
#include <windows.h>
#include <sys/types.h>
/* Re-define print conversion for size_t values */
#undef PRIsize_t
#if defined(WIN64) || defined(_WIN64)
#define PRIsize_t "I64u"
#else
#define PRIsize_t "I32u"
#endif
/* For MSVC 2012 and earlier define standard int types, otherwise use inttypes.h */
#if defined(_MSC_VER) && _MSC_VER <= 1700
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int int16_t;
typedef unsigned short int uint16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
#include <inttypes.h>
#endif
/* For MSVC define PRId64 and SCNd64 and alternate functions */
#if defined(_MSC_VER)
#if !defined(PRId64)
#define PRId64 "I64d"
#endif
#if !defined(SCNd64)
#define SCNd64 "I64d"
#endif
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#define strtoull _strtoui64
#define fileno _fileno
#define fdopen _fdopen
#endif
/* Extras needed for MinGW */
#if defined(__MINGW32__) || defined(__MINGW64__)
#include <fcntl.h>
#define _fseeki64 fseeko64
#define _ftelli64 ftello64
#define fstat _fstat
#define stat _stat
#endif
#else
/* All other platforms */
#include <inttypes.h>
#endif
#define MINRECLEN 40 //!< Minimum miniSEED record length supported
#define MAXRECLEN 10485760 //!< Maximum miniSEED record length supported (10MiB)
#define MAXRECLENv2 131172 //!< Maximum v2 miniSEED record length supported (131+ KiB or 2^17)
#define LM_SIDLEN 64 //!< Length of source ID string
/** @def MS_ISRATETOLERABLE
@brief Macro to test default sample rate tolerance: abs(1-sr1/sr2) < 0.0001 */
#define MS_ISRATETOLERABLE(A,B) (fabs (1.0 - ((A) / (B))) < 0.0001)
/** @def MS2_ISDATAINDICATOR
@brief Macro to test a character for miniSEED 2.x data record/quality indicators */
#define MS2_ISDATAINDICATOR(X) ((X)=='D' || (X)=='R' || (X)=='Q' || (X)=='M')
/** @def MS3_ISVALIDHEADER
* @hideinitializer
* Macro to test a buffer for a miniSEED 3.x data record signature by checking
* header values at known byte offsets:
* - 0 = "M"
* - 1 = "S"
* - 2 = 3
* - 12 = valid hour (0-23)
* - 13 = valid minute (0-59)
* - 14 = valid second (0-60)
*
* Usage, X buffer must contain at least 15 bytes:
* @code
* MS3_ISVALIDHEADER ((char *)X)
* @endcode
*/
#define MS3_ISVALIDHEADER(X) ( \
*(X) == 'M' && *((X) + 1) == 'S' && *((X) + 2) == 3 && \
(uint8_t) (*((X) + 12)) >= 0 && (uint8_t) (*((X) + 12)) <= 23 && \
(uint8_t) (*((X) + 13)) >= 0 && (uint8_t) (*((X) + 13)) <= 59 && \
(uint8_t) (*((X) + 14)) >= 0 && (uint8_t) (*((X) + 14)) <= 60)
/** @def MS2_ISVALIDHEADER
* @hideinitializer
* Macro to test a buffer for a miniSEED 2.x data record signature by checking
* header values at known byte offsets:
* - [0-5] = Digits, spaces or NULL, SEED sequence number
* - 6 = Data record quality indicator
* - 7 = Space or NULL [not valid SEED]
* - 24 = Start hour (0-23)
* - 25 = Start minute (0-59)
* - 26 = Start second (0-60)
*
* Usage, X buffer must contain at least 27 bytes:
* @code
* MS2_ISVALIDHEADER ((char *)X)
* @endcode
*/
#define MS2_ISVALIDHEADER(X) ( \
(isdigit ((uint8_t) * (X)) || *(X) == ' ' || !*(X)) && \
(isdigit ((uint8_t) * ((X) + 1)) || *((X) + 1) == ' ' || !*((X) + 1)) && \
(isdigit ((uint8_t) * ((X) + 2)) || *((X) + 2) == ' ' || !*((X) + 2)) && \
(isdigit ((uint8_t) * ((X) + 3)) || *((X) + 3) == ' ' || !*((X) + 3)) && \
(isdigit ((uint8_t) * ((X) + 4)) || *((X) + 4) == ' ' || !*((X) + 4)) && \
(isdigit ((uint8_t) * ((X) + 5)) || *((X) + 5) == ' ' || !*((X) + 5)) && \
MS2_ISDATAINDICATOR (*((X) + 6)) && \
(*((X) + 7) == ' ' || *((X) + 7) == '\0') && \
(uint8_t) (*((X) + 24)) >= 0 && (uint8_t) (*((X) + 24)) <= 23 && \
(uint8_t) (*((X) + 25)) >= 0 && (uint8_t) (*((X) + 25)) <= 59 && \
(uint8_t) (*((X) + 26)) >= 0 && (uint8_t) (*((X) + 26)) <= 60)
/** A simple bitwise AND test to return 0 or 1 */
#define bit(x,y) ((x)&(y)) ? 1 : 0
/** Annotation for deprecated API components */
#ifdef _MSC_VER
#define DEPRECATED __declspec(deprecated)
#elif defined(__GNUC__) | defined(__clang__)
#define DEPRECATED __attribute__((__deprecated__))
#else
#define DEPRECATED
#endif
/** @addtogroup time-related
@brief Definitions and functions for related to library time values
Internally the library uses an integer value to represent time as
the number of nanoseconds since the Unix/POSIX epoch (Jan 1 1970).
@{ */
/** @brief libmseed time type, integer nanoseconds since the Unix/POSIX epoch (00:00:00 Thursday, 1 January 1970)
This time scale can represent a range from before year 0 through mid-year 2262.
*/
typedef int64_t nstime_t;
/** @def NSTMODULUS
@brief Define the high precision time tick interval as 1/modulus seconds
corresponding to **nanoseconds**. **/
#define NSTMODULUS 1000000000
/** @def NSTERROR
@brief Error code for routines that normally return a high precision time.
The time value corresponds to '1902-01-01T00:00:00.000000000Z'. **/
#define NSTERROR -2145916800000000000LL
/** @def NSTUNSET
@brief Special nstime_t value meaning "unset".
The time value corresponds to '1902-01-01T00:00:00.000000001Z'. **/
#define NSTUNSET -2145916799999999999LL
/** @def MS_EPOCH2NSTIME
@brief macro to convert Unix/POSIX epoch time to high precision epoch time */
#define MS_EPOCH2NSTIME(X) (X) * (nstime_t) NSTMODULUS
/** @def MS_NSTIME2EPOCH
@brief Macro to convert high precision epoch time to Unix/POSIX epoch time */
#define MS_NSTIME2EPOCH(X) (X) / NSTMODULUS
/** @def MS_HPTIME2NSTIME
@brief Convert a hptime_t value (used by previous releases) to nstime_t
An HTPTIME/hptime_t value, used by libmseed major version <= 2,
defines microsecond ticks. An NSTIME/nstime_t value, used by this
version of the library, defines nanosecond ticks.
*/
#define MS_HPTIME2NSTIME(X) (X) * (nstime_t) 1000
/** @def MS_NSTIME2HPTIME
@brief Convert an nstime_t value to hptime_t (used by previous releases)
An HTPTIME/hptime_t value, used by libmseed major version <= 2,
defines microsecond ticks. An NSTIME/nstime_t value, used by this
version of the library, defines nanosecond ticks.
*/
#define MS_NSTIME2HPTIME(X) (X) / 1000
/** @enum ms_timeformat_t
@brief Time format identifiers
Formats values:
- \b ISOMONTHDAY - \c "YYYY-MM-DDThh:mm:ss.sssssssss", ISO 8601 in month-day format
- \b ISOMONTHDAY_Z - \c "YYYY-MM-DDThh:mm:ss.sssssssss", ISO 8601 in month-day format with trailing Z
- \b ISOMONTHDAY_DOY - \c "YYYY-MM-DD hh:mm:ss.sssssssss (doy)", ISOMONTHDAY with day-of-year
- \b ISOMONTHDAY_DOY_Z - \c "YYYY-MM-DD hh:mm:ss.sssssssss (doy)", ISOMONTHDAY with day-of-year and trailing Z
- \b ISOMONTHDAY_SPACE - \c "YYYY-MM-DD hh:mm:ss.sssssssss", same as ISOMONTHDAY with space separator
- \b ISOMONTHDAY_SPACE_Z - \c "YYYY-MM-DD hh:mm:ss.sssssssss", same as ISOMONTHDAY with space separator and trailing Z
- \b SEEDORDINAL - \c "YYYY,DDD,hh:mm:ss.sssssssss", SEED day-of-year format
- \b UNIXEPOCH - \c "ssssssssss.sssssssss", Unix epoch value
- \b NANOSECONDEPOCH - \c "sssssssssssssssssss", Nanosecond epoch value
*/
typedef enum
{
ISOMONTHDAY = 0,
ISOMONTHDAY_Z = 1,
ISOMONTHDAY_DOY = 2,
ISOMONTHDAY_DOY_Z = 3,
ISOMONTHDAY_SPACE = 4,
ISOMONTHDAY_SPACE_Z = 5,
SEEDORDINAL = 6,
UNIXEPOCH = 7,
NANOSECONDEPOCH = 8
} ms_timeformat_t;
/** @enum ms_subseconds_t
@brief Subsecond format identifiers
Formats values:
- \b NONE - No subseconds
- \b MICRO - Microsecond resolution
- \b NANO - Nanosecond resolution
- \b MICRO_NONE - Microsecond resolution if subseconds are non-zero, otherwise no subseconds
- \b NANO_NONE - Nanosecond resolution if subseconds are non-zero, otherwise no subseconds
- \b NANO_MICRO - Nanosecond resolution if there are sub-microseconds, otherwise microseconds resolution
- \b NANO_MICRO_NONE - Nanosecond resolution if present, microsecond if present, otherwise no subseconds
*/
typedef enum
{
NONE = 0,
MICRO = 1,
NANO = 2,
MICRO_NONE = 3,
NANO_NONE = 4,
NANO_MICRO = 5,
NANO_MICRO_NONE = 6
} ms_subseconds_t;
extern int ms_nstime2time (nstime_t nstime, uint16_t *year, uint16_t *yday,
uint8_t *hour, uint8_t *min, uint8_t *sec, uint32_t *nsec);
extern char* ms_nstime2timestr (nstime_t nstime, char *timestr,
ms_timeformat_t timeformat, ms_subseconds_t subsecond);
DEPRECATED extern char* ms_nstime2timestrz (nstime_t nstime, char *timestr,
ms_timeformat_t timeformat, ms_subseconds_t subsecond);
extern nstime_t ms_time2nstime (int year, int yday, int hour, int min, int sec, uint32_t nsec);
extern nstime_t ms_timestr2nstime (const char *timestr);
extern nstime_t ms_mdtimestr2nstime (const char *timestr);
extern nstime_t ms_seedtimestr2nstime (const char *seedtimestr);
extern int ms_doy2md (int year, int yday, int *month, int *mday);
extern int ms_md2doy (int year, int month, int mday, int *yday);
/** @} */
/** @page sample-types Sample Types
@brief Data sample types used by the library.
Sample types are represented using a single character as follows:
- \c 't' - Text data samples
- \c 'i' - 32-bit integer data samples
- \c 'f' - 32-bit float (IEEE) data samples
- \c 'd' - 64-bit float (IEEE) data samples
*/
/** @def MS_PACK_DEFAULT_RECLEN
@brief Default record length to use when ::MS3Record.reclen == -1
*/
#define MS_PACK_DEFAULT_RECLEN 4096
/** @def MS_PACK_DEFAULT_ENCODING
@brief Default data encoding to use when ::MS3Record.encoding == -1
*/
#define MS_PACK_DEFAULT_ENCODING DE_STEIM2
/** @addtogroup miniseed-record
@brief Definitions and functions related to individual miniSEED records
@{ */
/** @brief miniSEED record container */
typedef struct MS3Record {
const char *record; //!< Raw miniSEED record, if available
int32_t reclen; //!< Length of miniSEED record in bytes
uint8_t swapflag; //!< Byte swap indicator (bitmask), see @ref byte-swap-flags
/* Common header fields in accessible form */
char sid[LM_SIDLEN]; //!< Source identifier as URN, max length @ref LM_SIDLEN
uint8_t formatversion; //!< Format major version
uint8_t flags; //!< Record-level bit flags
nstime_t starttime; //!< Record start time (first sample)
double samprate; //!< Nominal sample rate as samples/second (Hz) or period (s)
int16_t encoding; //!< Data encoding format, see @ref encoding-values
uint8_t pubversion; //!< Publication version
int64_t samplecnt; //!< Number of samples in record
uint32_t crc; //!< CRC of entire record
uint16_t extralength; //!< Length of extra headers in bytes
uint32_t datalength; //!< Length of data payload in bytes
char *extra; //!< Pointer to extra headers
/* Data sample fields */
void *datasamples; //!< Data samples, \a numsamples of type \a sampletype
uint64_t datasize; //!< Size of datasamples buffer in bytes
int64_t numsamples; //!< Number of data samples in datasamples
char sampletype; //!< Sample type code: t, i, f, d @ref sample-types
} MS3Record;
extern int msr3_parse (const char *record, uint64_t recbuflen, MS3Record **ppmsr,
uint32_t flags, int8_t verbose);
extern int msr3_pack (const MS3Record *msr,
void (*record_handler) (char *, int, void *),
void *handlerdata, int64_t *packedsamples,
uint32_t flags, int8_t verbose);
extern int msr3_repack_mseed3 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_t verbose);
extern int msr3_pack_header3 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_t verbose);
extern int msr3_pack_header2 (const MS3Record *msr, char *record, uint32_t recbuflen, int8_t verbose);
extern int64_t msr3_unpack_data (MS3Record *msr, int8_t verbose);
extern int msr3_data_bounds (const MS3Record *msr, uint32_t *dataoffset, uint32_t *datasize);
extern int64_t ms_decode_data (const void *input, uint64_t inputsize, uint8_t encoding,
uint64_t samplecount, void *output, uint64_t outputsize,
char *sampletype, int8_t swapflag, const char *sid, int8_t verbose);
extern MS3Record* msr3_init (MS3Record *msr);
extern void msr3_free (MS3Record **ppmsr);
extern MS3Record* msr3_duplicate (const MS3Record *msr, int8_t datadup);
extern nstime_t msr3_endtime (const MS3Record *msr);
extern void msr3_print (const MS3Record *msr, int8_t details);
extern int msr3_resize_buffer (MS3Record *msr);
extern double msr3_sampratehz (const MS3Record *msr);
extern nstime_t msr3_nsperiod (const MS3Record *msr);
extern double msr3_host_latency (const MS3Record *msr);
extern int64_t ms3_detect (const char *record, uint64_t recbuflen, uint8_t *formatversion);
extern int ms_parse_raw3 (const char *record, int maxreclen, int8_t details);
extern int ms_parse_raw2 (const char *record, int maxreclen, int8_t details, int8_t swapflag);
/** @} */
/** @addtogroup data-selections
@brief Data selections to be used as filters
Selections are the identification of data, by source identifier
and time ranges, that are desired. Capability is included to read
selections from files and to match data against a selection list.
For data to be selected it must only match one of the selection
entries. In other words, multiple selection entries are treated
with OR logic.
The ms3_readmsr_selection() and ms3_readtracelist_selection()
routines accept ::MS3Selections and allow selective (and
efficient) reading of data from files.
@{ */
/** @brief Data selection structure time window definition containers */
typedef struct MS3SelectTime {
nstime_t starttime; //!< Earliest data for matching channels, use ::NSTUNSET for open
nstime_t endtime; //!< Latest data for matching channels, use ::NSTUNSET for open
struct MS3SelectTime *next; //!< Pointer to next selection time, NULL if the last
} MS3SelectTime;
/** @brief Data selection structure definition containers */
typedef struct MS3Selections {
char sidpattern[100]; //!< Matching (globbing) pattern for source ID
struct MS3SelectTime *timewindows; //!< Pointer to time window list for this source ID
struct MS3Selections *next; //!< Pointer to next selection, NULL if the last
uint8_t pubversion; //!< Selected publication version, use 0 for any
} MS3Selections;
extern const MS3Selections* ms3_matchselect (const MS3Selections *selections, const char *sid,
nstime_t starttime, nstime_t endtime,
int pubversion, const MS3SelectTime **ppselecttime);
extern const MS3Selections* msr3_matchselect (const MS3Selections *selections, const MS3Record *msr,
const MS3SelectTime **ppselecttime);
extern int ms3_addselect (MS3Selections **ppselections, const char *sidpattern,
nstime_t starttime, nstime_t endtime, uint8_t pubversion);
extern int ms3_addselect_comp (MS3Selections **ppselections,
char *network, char* station, char *location, char *channel,
nstime_t starttime, nstime_t endtime, uint8_t pubversion);
extern int ms3_readselectionsfile (MS3Selections **ppselections, const char *filename);
extern void ms3_freeselections (MS3Selections *selections);
extern void ms3_printselections (const MS3Selections *selections);
/** @} */
/** @addtogroup record-list
@{ */
/** @brief A miniSEED record pointer and metadata
*
* Used to construct a list of data records that contributed to a
* trace segment.
*
* The location of the record is identified at a memory address (\a
* bufferptr), the location in an open file (\a fileptr and \a
* fileoffset), or the location in a file (\a filename and \a
* fileoffset).
*
* A ::MS3Record is stored with and contains the bit flags, extra
* headers, etc. for the record.
*
* The \a dataoffset to the encoded data is stored to enable direct
* decoding of data samples without re-parsing the header, used by
* mstl3_unpack_recordlist().
*
* Note: the list is stored in the time order that the entries
* contributed to the segment.
*
* @see mstl3_unpack_recordlist()
*/
typedef struct MS3RecordPtr
{
const char *bufferptr; //!< Pointer in buffer to record, NULL if not used
FILE *fileptr; //!< Pointer to open FILE containing record, NULL if not used
const char *filename; //!< Pointer to file name containing record, NULL if not used
int64_t fileoffset; //!< Offset into file to record for \a fileptr or \a filename
MS3Record *msr; //!< Pointer to ::MS3Record for this record
nstime_t endtime; //!< End time of record, time of last sample
uint32_t dataoffset; //!< Offset from start of record to encoded data
void *prvtptr; //!< Private pointer, will not be populated by library but will be free'd
struct MS3RecordPtr *next; //!< Pointer to next entry, NULL if the last
} MS3RecordPtr;
/** @brief Record list, holds ::MS3RecordPtr entries that contribute to a given ::MS3TraceSeg */
typedef struct MS3RecordList
{
uint64_t recordcnt; //!< Count of records in the list (for convenience)
MS3RecordPtr *first; //!< Pointer to first entry, NULL if the none
MS3RecordPtr *last; //!< Pointer to last entry, NULL if the none
} MS3RecordList;
/** @} */
/** @addtogroup trace-list
@brief A container for continuous data
Trace lists are a container to organize continuous segments of
data. By combining miniSEED data records into trace lists, the
time series is reconstructed and ready for processing or
summarization.
A trace list container starts with an ::MS3TraceList, which
contains one or more ::MS3TraceID entries, which each contain one
or more ::MS3TraceSeg entries. The ::MS3TraceID and ::MS3TraceSeg
entries are easily traversed as linked structures.
The overall structure is illustrated as:
- MS3TraceList
- MS3TraceID
- MS3TraceSeg
- MS3TraceSeg
- ...
- MS3TraceID
- MS3TraceSeg
- MS3TraceSeg
- ...
- ...
\sa ms3_readtracelist()
\sa ms3_readtracelist_timewin()
\sa ms3_readtracelist_selection()
\sa mstl3_writemseed()
@{ */
/** @brief Maximum skip list height for MSTraceIDs */
#define MSTRACEID_SKIPLIST_HEIGHT 8
/** @brief Container for a continuous trace segment, linkable */
typedef struct MS3TraceSeg {
nstime_t starttime; //!< Time of first sample
nstime_t endtime; //!< Time of last sample
double samprate; //!< Nominal sample rate (Hz)
int64_t samplecnt; //!< Number of samples in trace coverage
void *datasamples; //!< Data samples, \a numsamples of type \a sampletype
uint64_t datasize; //!< Size of datasamples buffer in bytes
int64_t numsamples; //!< Number of data samples in datasamples
char sampletype; //!< Sample type code, see @ref sample-types
void *prvtptr; //!< Private pointer for general use, unused by library
struct MS3RecordList *recordlist; //!< List of pointers to records that contributed
struct MS3TraceSeg *prev; //!< Pointer to previous segment
struct MS3TraceSeg *next; //!< Pointer to next segment, NULL if the last
} MS3TraceSeg;
/** @brief Container for a trace ID, linkable */
typedef struct MS3TraceID {
char sid[LM_SIDLEN]; //!< Source identifier as URN, max length @ref LM_SIDLEN
uint8_t pubversion; //!< Largest contributing publication version
nstime_t earliest; //!< Time of earliest sample
nstime_t latest; //!< Time of latest sample
void *prvtptr; //!< Private pointer for general use, unused by library
uint32_t numsegments; //!< Number of segments for this ID
struct MS3TraceSeg *first; //!< Pointer to first of list of segments
struct MS3TraceSeg *last; //!< Pointer to last of list of segments
struct MS3TraceID *next[MSTRACEID_SKIPLIST_HEIGHT]; //!< Next trace ID at first pointer, NULL if the last
uint8_t height; //!< Height of skip list at \a next
} MS3TraceID;
/** @brief Container for a collection of continuous trace segment, linkable */
typedef struct MS3TraceList {
uint32_t numtraceids; //!< Number of traces IDs in list
struct MS3TraceID traces; //!< Head node of trace skip list, first entry at \a traces.next[0]
uint64_t prngstate; //!< INTERNAL: State for Pseudo RNG
} MS3TraceList;
/** @brief Callback functions that return time and sample rate tolerances
*
* A container for function pointers that return time and sample rate
* tolerances that are used for merging data into ::MS3TraceList
* containers. The functions are provided with a ::MS3Record and must
* return the acceptable tolerances to merge this with other data.
*
* The \c time(MS3Record) function must return a time tolerance in seconds.
*
* The \c samprate(MS3Record) function must return a sampling rate tolerance in Hertz.
*
* For any function pointer set to NULL a default tolerance will be used.
*
* Illustrated usage:
* @code
* MS3Tolerance tolerance;
*
* tolerance.time = my_time_tolerance_function;
* tolerance.samprate = my_samprate_tolerance_function;
*
* mstl3_addmsr (mstl, msr, 0, 1, &tolerance);
* @endcode
*
* \sa mstl3_addmsr()
*/
typedef struct MS3Tolerance
{
double (*time) (const MS3Record *msr); //!< Pointer to function that returns time tolerance
double (*samprate) (const MS3Record *msr); //!< Pointer to function that returns sample rate tolerance
} MS3Tolerance;
/** @def MS3Tolerance_INITIALIZER
@brief Initialializer for the tolerances ::MS3Tolerance */
#define MS3Tolerance_INITIALIZER \
{ \
.time = NULL, .samprate = NULL \
}
extern MS3TraceList* mstl3_init (MS3TraceList *mstl);
extern void mstl3_free (MS3TraceList **ppmstl, int8_t freeprvtptr);
extern MS3TraceID* mstl3_findID (MS3TraceList *mstl, const char *sid, uint8_t pubversion, MS3TraceID **prev);
/** @def mstl3_addmsr
@brief Add a ::MS3Record to a ::MS3TraceList @see mstl3_addmsr_recordptr() */
#define mstl3_addmsr(mstl, msr, splitversion, autoheal, flags, tolerance) \
mstl3_addmsr_recordptr (mstl, msr, NULL, splitversion, autoheal, flags, tolerance)
extern MS3TraceSeg* mstl3_addmsr_recordptr (MS3TraceList *mstl, const MS3Record *msr, MS3RecordPtr **pprecptr,
int8_t splitversion, int8_t autoheal, uint32_t flags,
const MS3Tolerance *tolerance);
extern int64_t mstl3_readbuffer (MS3TraceList **ppmstl, const char *buffer, uint64_t bufferlength,
int8_t splitversion, uint32_t flags,
const MS3Tolerance *tolerance, int8_t verbose);
extern int64_t mstl3_readbuffer_selection (MS3TraceList **ppmstl, const char *buffer, uint64_t bufferlength,
int8_t splitversion, uint32_t flags,
const MS3Tolerance *tolerance, const MS3Selections *selections,
int8_t verbose);
extern int64_t mstl3_unpack_recordlist (MS3TraceID *id, MS3TraceSeg *seg, void *output,
uint64_t outputsize, int8_t verbose);
extern int mstl3_convertsamples (MS3TraceSeg *seg, char type, int8_t truncate);
extern int mstl3_resize_buffers (MS3TraceList *mstl);
extern int64_t mstl3_pack (MS3TraceList *mstl, void (*record_handler) (char *, int, void *),
void *handlerdata, int reclen, int8_t encoding,
int64_t *packedsamples, uint32_t flags, int8_t verbose, char *extra);
extern void mstl3_printtracelist (const MS3TraceList *mstl, ms_timeformat_t timeformat,
int8_t details, int8_t gaps, int8_t versions);
extern void mstl3_printsynclist (const MS3TraceList *mstl, const char *dccid, ms_subseconds_t subseconds);
extern void mstl3_printgaplist (const MS3TraceList *mstl, ms_timeformat_t timeformat,
double *mingap, double *maxgap);
/** @} */
/** @addtogroup io-functions
@brief Reading and writing interfaces for miniSEED to/from files or URLs
The miniSEED reading interfaces read from either regular files or
URLs (if optional support is included). The miniSEED writing
interfaces write to regular files.
URL support for reading is included by building the library with the
\b LIBMSEED_URL variable defined, see the
<a class="el" href="https://github.com/earthscope/libmseed/tree/master/INSTALL.md">INSTALL instructions</a>
for more information. Only URL path-specified resources can be read,
e.g. HTTP GET requests. More advanced POST or form-based requests are not supported.
The function @ref libmseed_url_support() can be used as a run-time test
to determine if URL support is included in the library.
Some parameters can be set that affect the reading of data from URLs, including:
- set the User-Agent header with @ref ms3_url_useragent()
- set username and password for authentication with @ref ms3_url_userpassword()
- set arbitrary headers with @ref ms3_url_addheader()
- disable TLS/SSL peer and host verficiation by setting **LIBMSEED_SSL_NOVERIFY** environment variable
Diagnostics: Setting environment variable **LIBMSEED_URL_DEBUG** enables
detailed verbosity of URL protocol exchanges.
\sa ms3_readmsr()
\sa ms3_readmsr_selection()
\sa ms3_readtracelist()
\sa ms3_readtracelist_selection()
\sa msr3_writemseed()
\sa mstl3_writemseed()
@{ */
/** @brief Type definition for data source I/O: file-system versus URL */
typedef struct LMIO
{
enum
{
LMIO_NULL = 0, //!< IO handle type is undefined
LMIO_FILE = 1, //!< IO handle is FILE-type
LMIO_URL = 2, //!< IO handle is URL-type
LMIO_FD = 3 //!< IO handle is a provided file descriptor
} type; //!< IO handle type
void *handle; //!< Primary IO handle, either file or URL
void *handle2; //!< Secondary IO handle for URL
int still_running; //!< Fetch status flag for URL transmissions
} LMIO;
/** @def LMIO_INITIALIZER
@brief Initialializer for the internal stream handle ::LMIO */
#define LMIO_INITIALIZER \
{ \
.type = LMIO_NULL, .handle = NULL, .handle2 = NULL, .still_running = 0 \
}
/** @brief State container for reading miniSEED records from files or URLs.
In general these values should not be directly set or accessed. It is
possible to allocate a structure and set the \c path, \c startoffset,
and \c endoffset values for advanced usage. Note that file/URL start
and end offsets can also be parsed from the path name as well.
*/
typedef struct MS3FileParam
{
char path[512]; //!< INPUT: File name or URL
int64_t startoffset; //!< INPUT: Start position in input stream
int64_t endoffset; //!< INPUT: End position in input stream, 0 == unknown (e.g. pipe)
int64_t streampos; //!< OUTPUT: Read position of input stream
int64_t recordcount; //!< OUTPUT: Count of records read from this stream/file so far
char *readbuffer; //!< INTERNAL: Read buffer, allocated internally
int readlength; //!< INTERNAL: Length of data in read buffer
int readoffset; //!< INTERNAL: Read offset in read buffer
uint32_t flags; //!< INTERNAL: Stream reading state flags
LMIO input; //!< INTERNAL: IO handle, file or URL
} MS3FileParam;
/** @def MS3FileParam_INITIALIZER
@brief Initialializer for the internal file or URL I/O parameters ::MS3FileParam */
#define MS3FileParam_INITIALIZER \
{ \
.path = "", .startoffset = 0, .endoffset = 0, .streampos = 0, \
.recordcount = 0, .readbuffer = NULL, .readlength = 0, \
.readoffset = 0, .flags = 0, .input = LMIO_INITIALIZER \
}
extern int ms3_readmsr (MS3Record **ppmsr, const char *mspath, uint32_t flags, int8_t verbose);
extern int ms3_readmsr_r (MS3FileParam **ppmsfp, MS3Record **ppmsr, const char *mspath,
uint32_t flags, int8_t verbose);
extern int ms3_readmsr_selection (MS3FileParam **ppmsfp, MS3Record **ppmsr, const char *mspath,
uint32_t flags, const MS3Selections *selections, int8_t verbose);
extern int ms3_readtracelist (MS3TraceList **ppmstl, const char *mspath, const MS3Tolerance *tolerance,
int8_t splitversion, uint32_t flags, int8_t verbose);
extern int ms3_readtracelist_timewin (MS3TraceList **ppmstl, const char *mspath, const MS3Tolerance *tolerance,
nstime_t starttime, nstime_t endtime, int8_t splitversion, uint32_t flags,
int8_t verbose);
extern int ms3_readtracelist_selection (MS3TraceList **ppmstl, const char *mspath, const MS3Tolerance *tolerance,
const MS3Selections *selections, int8_t splitversion, uint32_t flags, int8_t verbose);
extern int ms3_url_useragent (const char *program, const char *version);
extern int ms3_url_userpassword (const char *userpassword);
extern int ms3_url_addheader (const char *header);
extern void ms3_url_freeheaders (void);
extern int64_t msr3_writemseed (MS3Record *msr, const char *mspath, int8_t overwrite,
uint32_t flags, int8_t verbose);
extern int64_t mstl3_writemseed (MS3TraceList *mst, const char *mspath, int8_t overwrite,
int maxreclen, int8_t encoding, uint32_t flags, int8_t verbose);
extern int libmseed_url_support (void);
extern MS3FileParam *ms3_mstl_init_fd (int fd);
/** @} */
/** @addtogroup string-functions
@brief Source identifier (SID) and string manipulation functions
A source identifier uniquely identifies the generator of data in a
record. This is a small string, usually in the form of a URI.
For data identified with FDSN codes, the SID is usally a simple
combination of the codes.
@{ */
extern int ms_sid2nslc (const char *sid, char *net, char *sta, char *loc, char *chan);
extern int ms_nslc2sid (char *sid, int sidlen, uint16_t flags,
const char *net, const char *sta, const char *loc, const char *chan);
extern int ms_seedchan2xchan (char *xchan, const char *seedchan);
extern int ms_xchan2seedchan (char *seedchan, const char *xchan);
extern int ms_strncpclean (char *dest, const char *source, int length);
extern int ms_strncpcleantail (char *dest, const char *source, int length);
extern int ms_strncpopen (char *dest, const char *source, int length);
/** @} */
/** @addtogroup extra-headers
@brief Structures and funtions to support extra headers
Extra headers are stored as JSON within a data record header using
an anonymous, root object as a container for all extra headers.
For a full description consult the format specification.
The library functions supporting extra headers allow specific
header identification using JSON Pointer identification. In this
notation each path element is an object until the final element
which is a key to specified header value.
For example, a \a path specified as:
\code
"/objectA/objectB/header"
\endcode
would correspond to the single JSON value in:
\code
{
"objectA": {
"objectB": {
"header":VALUE
}
}
}
\endcode
@{ */
/**
* @brief Container for event detection parameters for use in extra headers
*
* Actual values are optional, with special values indicating an unset
* state.
*
* @see mseh_add_event_detection_r
*/
typedef struct MSEHEventDetection
{
char type[30]; /**< Detector type (e.g. "MURDOCK"), zero length = not included */
char detector[30]; /**< Detector name, zero length = not included */
double signalamplitude; /**< SignalAmplitude, 0.0 = not included */
double signalperiod; /**< Signal period, 0.0 = not included */
double backgroundestimate; /**< Background estimate, 0.0 = not included */
char wave[30]; /**< Detection wave (e.g. "DILATATION"), zero length = not included */
char units[30]; /**< Units of amplitude and background estimate (e.g. "COUNTS"), zero length = not included */
nstime_t onsettime; /**< Onset time, NSTUNSET = not included */
uint8_t medsnr[6]; /**< Signal to noise ratio for Murdock event detection, all zeros = not included */
int medlookback; /**< Murdock event detection lookback value, -1 = not included */
int medpickalgorithm; /**< Murdock event detection pick algoritm, -1 = not included */
struct MSEHEventDetection *next; /**< Pointer to next, NULL if none */
} MSEHEventDetection;
/**
* @brief Container for calibration parameters for use in extra headers
*
* Actual values are optional, with special values indicating an unset
* state.
*
* @see mseh_add_calibration
*/
typedef struct MSEHCalibration
{
char type[30]; /**< Calibration type (e.g. "STEP", "SINE", "PSEUDORANDOM"), zero length = not included */
nstime_t begintime; /**< Begin time, NSTUNSET = not included */
nstime_t endtime; /**< End time, NSTUNSET = not included */
int steps; /**< Number of step calibrations, -1 = not included */
int firstpulsepositive; /**< Boolean, step cal. first pulse, -1 = not included */
int alternatesign; /**< Boolean, step cal. alt. sign, -1 = not included */
char trigger[30]; /**< Trigger, e.g. AUTOMATIC or MANUAL, zero length = not included */
int continued; /**< Boolean, continued from prev. record, -1 = not included */
double amplitude; /**< Amp. of calibration signal, 0.0 = not included */
char inputunits[30]; /**< Units of input (e.g. volts, amps), zero length = not included */
char amplituderange[30]; /**< E.g PEAKTOPTEAK, ZEROTOPEAK, RMS, RANDOM, zero length = not included */
double duration; /**< Duration in seconds, 0.0 = not included */
double sineperiod; /**< Period of sine, 0.0 = not included */
double stepbetween; /**< Interval bewteen steps, 0.0 = not included */
char inputchannel[30]; /**< Channel of input, zero length = not included */
double refamplitude; /**< Reference amplitude, 0.0 = not included */
char coupling[30]; /**< Coupling, e.g. Resistive, Capacitive, zero length = not included */
char rolloff[30]; /**< Rolloff of filters, zero length = not included */
char noise[30]; /**< Noise for PR cals, e.g. White or Red, zero length = not included */
struct MSEHCalibration *next; /**< Pointer to next, NULL if none */
} MSEHCalibration;
/**
* @brief Container for timing exception parameters for use in extra headers
*
* Actual values are optional, with special values indicating an unset
* state.
*
* @see mseh_add_timing_exception
*/
typedef struct MSEHTimingException
{
nstime_t time; /**< Time of exception, NSTUNSET = not included */
float vcocorrection; /**< VCO correction, from 0 to 100%, <0 = not included */
int usec; /**< [DEPRECATED] microsecond time offset, 0 = not included */
int receptionquality; /**< Reception quality, 0 to 100% clock accurracy, <0 = not included */
uint32_t count; /**< The count thereof, 0 = not included */
char type[16]; /**< E.g. "MISSING" or "UNEXPECTED", zero length = not included */
char clockstatus[128]; /**< Description of clock-specific parameters, zero length = not included */
} MSEHTimingException;
/**
* @brief Container for recenter parameters for use in extra headers
*
* Actual values are optional, with special values indicating an unset
* state.
*
* @see mseh_add_recenter
*/
typedef struct MSEHRecenter
{
char type[30]; /**< Recenter type (e.g. "MASS", "GIMBAL"), zero length = not included */
nstime_t begintime; /**< Begin time, NSTUNSET = not included */
nstime_t endtime; /**< Estimated end time, NSTUNSET = not included */
char trigger[30]; /**< Trigger, e.g. AUTOMATIC or MANUAL, zero length = not included */
} MSEHRecenter;
/**
* @brief Internal structure for holding parsed JSON extra headers.
* @see mseh_get_ptr_r()
* @see mseh_set_ptr_r()
*/
typedef struct LM_PARSED_JSON_s LM_PARSED_JSON;
/** @def mseh_get
@brief A simple wrapper to access any type of extra header */
#define mseh_get(msr, ptr, valueptr, type, maxlength) \
mseh_get_ptr_r (msr, ptr, valueptr, type, maxlength, NULL)
/** @def mseh_get_number
@brief A simple wrapper to access a number type extra header */
#define mseh_get_number(msr, ptr, valueptr) \
mseh_get_ptr_r (msr, ptr, valueptr, 'n', 0, NULL)
/** @def mseh_get_int64
@brief A simple wrapper to access a number type extra header */
#define mseh_get_int64(msr, ptr, valueptr) \
mseh_get_ptr_r (msr, ptr, valueptr, 'i', 0, NULL)
/** @def mseh_get_string
@brief A simple wrapper to access a string type extra header */
#define mseh_get_string(msr, ptr, buffer, maxlength) \
mseh_get_ptr_r (msr, ptr, buffer, 's', maxlength, NULL)
/** @def mseh_get_boolean
@brief A simple wrapper to access a boolean type extra header */
#define mseh_get_boolean(msr, ptr, valueptr) \
mseh_get_ptr_r (msr, ptr, valueptr, 'b', 0, NULL)
/** @def mseh_exists
@brief A simple wrapper to test existence of an extra header */
#define mseh_exists(msr, ptr) \
(!mseh_get_ptr_r (msr, ptr, NULL, 0, 0, NULL))
extern int mseh_get_ptr_r (const MS3Record *msr, const char *ptr,
void *value, char type, uint32_t maxlength,
LM_PARSED_JSON **parsestate);
/** @def mseh_set
@brief A simple wrapper to set any type of extra header */
#define mseh_set(msr, ptr, valueptr, type) \
mseh_set_ptr_r (msr, ptr, valueptr, type, NULL)
/** @def mseh_set_number
@brief A simple wrapper to set a number type extra header */
#define mseh_set_number(msr, ptr, valueptr) \
mseh_set_ptr_r (msr, ptr, valueptr, 'n', NULL)
/** @def mseh_set_int64
@brief A simple wrapper to set a number type extra header */
#define mseh_set_int64(msr, ptr, valueptr) \
mseh_set_ptr_r (msr, ptr, valueptr, 'i', NULL)
/** @def mseh_set_string
@brief A simple wrapper to set a string type extra header */
#define mseh_set_string(msr, ptr, valueptr) \
mseh_set_ptr_r (msr, ptr, valueptr, 's', NULL)
/** @def mseh_set_boolean
@brief A simple wrapper to set a boolean type extra header */
#define mseh_set_boolean(msr, ptr, valueptr) \
mseh_set_ptr_r (msr, ptr, valueptr, 'b', NULL)
extern int mseh_set_ptr_r (MS3Record *msr, const char *ptr,
void *value, char type,
LM_PARSED_JSON **parsestate);
extern int mseh_add_event_detection_r (MS3Record *msr, const char *ptr,
MSEHEventDetection *eventdetection,
LM_PARSED_JSON **parsestate);
extern int mseh_add_calibration_r (MS3Record *msr, const char *ptr,
MSEHCalibration *calibration,
LM_PARSED_JSON **parsestate);
extern int mseh_add_timing_exception_r (MS3Record *msr, const char *ptr,
MSEHTimingException *exception,
LM_PARSED_JSON **parsestate);
extern int mseh_add_recenter_r (MS3Record *msr, const char *ptr,
MSEHRecenter *recenter,
LM_PARSED_JSON **parsestate);
extern int mseh_serialize (MS3Record *msr, LM_PARSED_JSON **parsestate);
extern void mseh_free_parsestate (LM_PARSED_JSON **parsestate);
extern int mseh_replace (MS3Record *msr, char *jsonstring);
extern int mseh_print (const MS3Record *msr, int indent);
/** @} */
/** @addtogroup record-list