MS_INTRO(3)
ms_intro(3) β Introduction to libmseed π
okay bestie, gather round π«Ά this is the MS_INTRO(3) page from the Library Functions Manual, and we're about to get so cozy with libmseed. Part 1 of 3, so don't go anywhere. β¨
π NAME π
ms_intro β Introduction to libmseed πΈ
β¨ INTRODUCTION β¨
The Mini-SEED library is literally your framework for manipulation of SEED data records, including unpacking and packing of data records. It also handles managing waveform data as continuous traces. No cap. π
All structures of SEED 2.4 data records are supported, with these two exceptions: Blockette 2000 opaque data, which has an unknown data structure by definition (so like, we can't, bestie), and Blockette 405, which depends on full SEED (SEED including full ASCII headers) for a full data description.
The primary purpose of the library is to hide the details of Mini-SEED so you can rapidly develop Mini-SEED reading/writing software. It's giving convenience. The framework lets you do everything from manipulation of Mini-SEED on a record-by-record basis to reading Mini-SEED into continuous trace segments to packing large continuous traces using a record template. Obsessed. π
Certain common tasks have, through library design, been streamlined. For example: reading Mini-SEED records from a file, adding data from unpacked records to a group of traces, or packing a group of continuous traces into Mini-SEED records. We love an efficient queen. π
The following data encoding formats are supported for both unpacking and packing: ASCII, INT16, INT32, FLOAT32, FLOAT64, STEIM1 and STEIM2. The INT and FLOAT encodings each have two versions for quantities with a different number of bits of representation. The STEIM decompression produces 32-bit integers; likewise the compression routines require 32-bit integers as input. Important. π
The following data encoding formats are supported for unpacking only: GEOSCOPE (24-bit, 16/3 and 16/4 gain ranged), CDSN, SRO and DWWSSN. So if you're packing those, sorry babe. π
π DATA RECORDS π
A Mini-SEED record is represented in the library using the data structure below. This structure is used for both unpacking and packing of Mini-SEED records. When unpacking with msr_unpack(3) this structure is populated. When packing with msr_pack(3) this structure is used as a template for the resulting data records and as a source of samples to be packed. Periodt. π
Blockettes following the fixed section of the header are contained in the blockette chain of BlktLink structures. Shortcut pointers to commonly used blockettes are maintained for types 100, 1000 and 1001.
Many common header fields which are not easily accessible/usable in the raw header are available directly from the structure. When this structure is used as a packing template, these common header fields are packed into the appropriate place in the fixed section or blockette. As examples, the ASCII stream identifiers (network, station, location and channel) are available as NULL terminated strings, the start time is available as a high precision epoch time (see ms_time(3)) and the sample rate is available as a double precision floating point value.
The MSRecord data structure:
typedef struct MSRecord_s {
char *record; /* Mini-SEED record */
int32_t reclen; /* Length of Mini-SEED record */
/* Pointers to SEED data record structures */
struct fsdh_s *fsdh; /* Fixed Section of Data Header */
struct BlktLink *blkts; /* Root of blockette chain */
struct blkt_100_s *Blkt100; /* Blockette 100, if present */
struct blkt_1000_s *Blkt1000; /* Blockette 1000, if present */
struct blkt_1001_s *Blkt1001; /* Blockette 1001, if present */
/* Common header fields in accessible form */
int32_t sequence_number; /* SEED record sequence number */
char dataquality; /* Data quality indicator */
char network[11]; /* Network designation */
char station[11]; /* Station designation */
char location[11]; /* Location designation */
char channel[11]; /* Channel designation */
hptime_t starttime; /* Record start time */
double samprate; /* Nominal sample rate (Hz) */
int64_t samplecnt; /* Number of samples in record */
int8_t encoding; /* Data encoding format */
int8_t byteorder; /* Byte order of record */
/* Data sample fields */
void *datasamples; /* Data samples */
int64_t numsamples; /* Number of data samples */
char sampletype; /* Sample type code: a, i, f, d */
/* Stream oriented state information */
StreamState *ststate; /* Stream processing state information */
}
MSRecord;
π¦ Explanation of fields π¦
recordβ Pointer to the Mini-SEED record which was unpacked into theMSRecord.reclenβ When unpacking this is the record length in bytes of the record pointed to by therecordpointer. When packing this is the length of records to pack.fsdhβ A pointer to the Fixed Section of the Data Header, all appropriate multi-byte quantities are in host byte order.blktsβ The root of the blockette chain. The chain is constructed from linkedBlktLinkstructures. All appropriate multi-byte quantities in the blockettes are in host byte order. Themsr_addblockette(3)routine can be used to add blockettes to this chain. TheBlktLinkstructure and SEED blockette structures are defined inlibmseed.h.Blkt100,Blkt1000,Blkt1001β Shortcut pointers to common blockettes in the blockette chain. If a given blockette does not exist in the blockette chain the shortcut pointer will be 0. If more than one of these blockette types exist in the chain this pointer will point to the last one.sequence_numberβ SEED record sequence number, should be between 0 and 999999.dataqualityβ Data record and quality indicator, should be'D','R','Q'or'M'.network,station,location,channelβ SEED stream identifiers as a NULL terminated strings.starttimeβ Record start time, the time of the first sample, as a high precision epoch time (seems_time(3)). This time can be converted using the variousms_hptime2<X>functions.samprateβ The sample rate in samples per second in double precision. During unpacking this value will be set to the sample rate given in the 100 blockette if it is present, otherwise the sample rate derived from the factor and multiplier in the fixed section of the header. In a packing template this value will be used to derive a factor and multiplier for the fixed section of the header and will be written into 100 blockettes if any are in the blockette chain.samplecntβ The sample count, i.e. number of data samples in the record.encodingβ The SEED data sample encoding format. During packing this dictates what format will be used to pack the data samples. Supported packing formats are0(DE_ASCII),1(DE_INT16),3(DE_INT32),4(DE_FLOAT32),5(DE_FLOAT64),10(DE_STEIM1) and11(DE_STEIM2).byteorderβ Byte order of multi-byte quantities in the record. A value of0indicates little endian and a value of1indicates big endian. During packing this dictates the byte order of the final records.datasamplesβ A pointer to the unpacked data samples. If no data samples were unpacked this will be 0. Thenumsamplesfield indicates how many samples are in this array and thesampletypefield indicates what type of samples they are.numsamplesβ The number of samples pointed to by thedatasamplespointer.sampletypeβ The type of samples pointed to by thedatasamplespointer. Supported types are'a'(ASCII),'i'(integer),'f'(float) and'd'(double). The size of each sample type in bytes is returned by theget_samplesize(3)lookup routine.ststateβ Pointer to aStreamStatestruct used internally to track stream oriented state variables. Memory for this only allocated when needed.
πΈ TRACES πΈ
The library includes two facilities to manage collections of continuous trace segments, each represented by their top most structure: MSTraceGroup and MSTraceList. The MSTraceList facility is a next generation version of the MSTraceGroup facility. Whereas the MSTraceGroup facility uses a single linked list of time segments, the MSTraceList facility is slightly more complex with two levels of linked lists and common access pointers. The advantages are that the MSTraceList structure is faster to populate, especially when there are many segments (gappy data), and the list is always maintained in a sorted order. Sorted? We love. πβ¨
π TRACE LISTS π
MSTraceList data structures allow the grouping of MSTraceID structures which are themselves the root of MSTraceSeg structures, see libmseed.h as a reference to these structures. π
πΈ TRACE GROUPS πΈ
Okay bestie, so the MSTraceGroup data structures? They let you group MSTrace structures together. And here's the cute part: a MSTrace structure is normally out here holding trace information and the associated data samples, but you can also use it with no data samples at all β just as a way to keep track of data coverage without any actual samples. Lowkey genius. β¨
There are so many routines for basic MSTrace management, including:
- creating new
MSTracestructures - adding data from Mini-SEED data structures into
MSTracestructures - printing trace information
- and more, etc etc π
The MSTraceGroup data structure is literally just a very simple place to begin a chain of MSTrace structures and keep track of the number of traces. That's it, that's the job. π«Ά
Here are the MSTrace and MSTraceGroup data structures:
typedef struct MSTrace_s {
char network[11]; /* Network designation */
char station[11]; /* Station designation */
char location[11]; /* Location designation */
char channel[11]; /* Channel designation */
char dataquality; /* Data quality indicator */
char type; /* MSTrace type code */
hptime_t starttime; /* Time of first sample */
hptime_t endtime; /* Time of last sample */
double samprate; /* Nominal sample rate (Hz) */
int64_t samplecnt; /* Num. in trace coverage */
void *datasamples; /* Data samples */
int64_t numsamples; /* Num. samples in datasamples */
char sampletype; /* Sample type code: a, i, f, d */
void *prvtptr /* Private pointer for general use */
struct MSTrace_s *next; /* Pointer to next trace */
}
MSTrace;
typedef struct MSTraceGroup_s {
int32_t numtraces; /* Number of MSTraces in trace chain */
struct MSTrace_s *traces; /* Root of the trace chain */
}
MSTraceGroup;
π¦ Explanation of fields π¦
dataquality: SEED data quality indicator, either'D','R','Q'or'M'. This value will be (binary)0when the quality is unknown or mixed. πnetwork:station:location:channel: MSTrace identifiers as a NULL terminated strings.type: A single character trace type indicator. This field is not used by libmseed but could be used for application specific trace identification. πstarttime: MSTrace start time, the time of the first sample, as a high precision epoch time (seems_time(3)). This time can be converted using the variousms_hptime2<X>functions.endtime: MSTrace end time, the time of the last sample, as a high precision epoch time (seems_time(3)). This time can be converted using the variousms_hptime2<X>functions.samprate: The sample rate in samples per second in double precision.samplecnt: The sample count, i.e. number of data samples in the trace.datasamples: A pointer to the data samples. If no data samples are included this will be0. The'numsamples'field indicates how many samples are in this array and the'sampletype'field indicates what type of samples they are. πnumsamples: The number of samples pointed to by the'datasamples'pointer.sampletype: The type of samples pointed to by the'datasamples'pointer. Supported types are'a'(ASCII),'i'(integer),'f'(float) and'd'(double). The size of each sample type in bytes is returned by theget_samplesize(3)lookup routine.prvtptr: A private pointer for general use. This pointer is not used by libmseed and can safely be used by the calling program. πststate: Pointer to aStreamStatestruct used internally to track stream oriented state variables. Memory for this only allocated when needed.next: A pointer to the nextMSTracestructure. The value will be0for the last link in a chain ofMSTracestructures.
π LOG MESSAGES π
Every single log and diagnostic message the library functions emit? Same interface. All of them. And the output from this interface can be controlled β which is highkey useful when the library is getting embedded in a bigger system with its own custom logging facility. See the man page for more details, bestie. π«Ά
ms_log(): the central logging facility. Behavior is controlled by the settings specified withms_loginit().ms_loginit(): set the functions and prefixes used for log, diagnostic and error messages.
The default destination for log messages is standard output (stdout), while all diagnostic (including error) messages go to standard error (stderr). Most of the internal messages emmited by the library are considered diagnostic and will, by default, go to standard error.
The default prefix for log and diagnostic messages is nothing. The default prefix for diagnostic error messages is "Error: ".
There are reentrant versions of these functions that operate directly on a logging parameter MSLogParam struct. These are intended for use in threaded programs or where a complex logging scheme is desired. See the man pages for more details. β¨
π WAVEFORM DATA π
Waveform data samples are managed by libmseed in a couple of different formats, depending on how they are unpacked or will be packed. An array of samples is completely represented by three things:
- an array of sample values
- the number of samples
- a sample type
And listen β the number of samples is always the actual number of sample values, not the number of bytes needed for storing the values. Do not confuse them, bestie, it's giving trap. π
Samples can be either ASCII, 32-bit integer, 32-bit floats or 64-bit double precision floats.
Sample types are identified by a single ASCII type character:
"a" - ASCII (8 bits)
"i" - integer (32 bits)
"f" - float (32 bits)
"d" - double (64 bits)
The size of each sample type in bytes is returned by the get_samplesize(3) lookup routine. β
π BYTE ORDER π
The SEED 2.4 standard allows data only SEED (Mini-SEED) to be either in big (most significant byte first) or little (least significant byte first) endian byte order. Unfortunately it is not well defined what little endian Mini-SEED really means. π While libmseed supports all four combinations of big and little endian header and data, the surest way to avoid compatibility problems is to always create big endian Mini-SEED records (header and data). That's the move. Periodt.
π Reading MiniSEED β how libmseed determines the byte order of a record π
The byte order of a record header is determined by checking if the record start year is a sane value (e.g. between 1920 and 2020). The byte order of (compressed) data samples is determined by the byte order flag in the Blockette 1000, if a Blockette 1000 is not present the byte order is assumed to be the same as the header. To force the byte order determination of either the header or data section of a record the following environment variables can be set:
UNPACK_HEADER_BYTEORDERUNPACK_DATA_BYTEORDER
These variables should be set to either 0 (little endian) or 1 (big endian). A programmatic equivalent of setting these environment variables is provided via the following macros:
MS_UNPACKHEADERBYTEORDER(X)MS_UNPACKDATABYTEORDER(X)
π Writing MiniSEED β in what byte order libmseed creates records π
Normally the byte order of MiniSEED created by libmseed is controlled via a flag in the API. This byte order flag determines the ordering for both the header and data sections of a record. To force the byte order of either the header or data section of a record the following environment variables can be set:
PACK_HEADER_BYTEORDERPACK_DATA_BYTEORDER
These variables should be set to either 0 (little endian) or 1 (big endian). A programmatic equivalent of setting these environment variables is provided via the following macros:
MS_PACKHEADERBYTEORDER(X)MS_PACKDATABYTEORDER(X)
Note that some interpretations of the SEED 2.4 format imply that so-called little endian MiniSEED means that the record header is little endian but that the data section is big endian (as the only defined data encodings must be based on the SEED DDL which, in turn, must be defined in terms of big endian). Libmseed will not create MiniSEED of this flavor by default but can be configured to do so by setting the environment variables described above appropriately. π₯
π COMMON USAGE π
Okay bestie, this is the part where it all comes together. The library ships with example programs, and they're chilling in the examples directory of the source code distribution. Go look, they're cute.
The single most common glow-up task: read a file full of Mini-SEED records and then either do something with the header values or run some process over the data samples. And like⦠this is so easy because of ms_readmsr(3) and ms_readtraces(3).
ms_readmsr(3)β opens the file you point at and hands you backMSRecordstructures, one for every Mini-SEED record it reads. per record, no cap.ms_readtraces(3)β same energy, except it dumps all the data it reads into aMSTraceGroup. This one is ideal for quickly reading data you're about to process. chef's kiss.
Both of these routines are able to automatically detect record length. You don't have to tell them. They just know. β¨
If your application isn't built to read Mini-SEED from files, no stress β the library also has functions to detect and parse Mini-SEED records sitting in memory buffers. For more on that, go see ms_detect(3) and msr_parse(3). π«Ά
π Skeleton code for reading a file with ms_readmsr(3)
main() {
MSRecord *msr = NULL;
int retcode;
while ( (retcode = ms_readmsr (&msr, filename, 0, NULL, NULL, 1, 0, verbose)) == MS_NOERROR )
{
/* Do something with the record here, e.g. print */
msr_print (msr, verbose);
}
if ( retcode != MS_ENDOFFILE )
ms_log (2, "Cannot read %s: %s0, filename, ms_errorstr(retcode));
/* Cleanup memory and close file */
ms_readmsr (&msr, NULL, 0, NULL, NULL, 0, 0);
}
π¦ For reading two files with ms_readtraces(3)
main() {
MSTraceGroup *mstg = NULL;
int retcode;
retcode = ms_readtraces (&mstg, filename, 0, -1.0, -1.0, 0, 1, 0, verbose);
if ( retcode != MS_ENDOFFILE )
ms_log (2, "Cannot read %s: %s0, filename, ms_errorstr(retcode));
retcode = ms_readtraces (&mstg, filename2, 0, -1.0, -1.0, 0, 1, 0, verbose);
if ( retcode != MS_ENDOFFILE )
ms_log (2, "Cannot read %s: %s0, filename2, ms_errorstr(retcode));
if ( ! mstg )
{
fprintf (stderr, "Error reading file\n");
return -1;
}
/* Do something with the traces here, e.g. print */
mst_printtracelist (mstg, 0, verbose, 0);
mst_freegroup (&mstg);
}
Another task that comes up constantly: creating β aka packing β Mini-SEED records. And the library has your back three different ways depending on what you're holding:
msr_pack(3)β pack fromMSRecordstructuresmst_pack(3)β pack fromMSTracestructuresmst_packgroup(3)β pack fromMSTraceGroupcollections
In every single case, you hand the routine the appropriate data structure and parameters, plus a function pointer to a routine that gets called each time a record is complete and needs to be disposed of. it's giving callback energy, and we love that for us. π
πΈ The header template concept πΈ
When you're packing Mini-SEED records, there's this concept of a record header template. The template is always in the form of a MSRecord structure. This lets your calling program dictate the contents of the header in the final data records β with a few exceptions. (yes, there are exceptions. there are always exceptions. iykyk.)
π Skeleton code for creating (packing) Mini-SEED records with mst_pack(3)
static void record_handler (char *record, int reclen, void *srcname) {
if ( fwrite(record, reclen, 1, outfile) != 1 )
{
ms_log (2, "Error writing %s to output file0, (char *)srcname);
}
}
main() {
int64_t psamples;
int precords;
MSTrace *mst;
char srcname[50];
mst = mst_init (NULL);
/* Populate MSTrace values */
strcpy (mst->network, "XX");
strcpy (mst->station, "TEST");
strcpy (mst->channel, "BHE");
mst->starttime = ms_seedtimestr2hptime ("2004,350,00:00:00.000000");
mst->samprate = 40.0;
/* The datasamples pointer and numsamples counter will be adjusted by
the packing routine, the datasamples array must be dynamic memory
allocated by the malloc() family of routines. */
mst->datasamples = dataptr; /* pointer to 32-bit integer data samples */
mst->numsamples = 1234; /* number of samples in array at dataptr */
mst->samplecnt = mst->numsamples;
mst->sampletype = 'i'; /* declare type to be 32-bit integers */
mst_srcname (mst, srcname, 0);
/* Pack 4096 byte, big-endian records, using Steim-2 compression */
precords = mst_pack (mst, &record_handler, srcname, 4096, DE_STEIM2,
1, &psamples, 1, verbose, NULL);
ms_log (0, "Packed %"PRId64" samples into %d records0,
psamples, precords);
/* Disconnect datasamples pointer, otherwise mst_free() will free it */
mst->datasamples = NULL;
mst_free (&mst);
}
β οΈ heads up bestie: thatmst->datasamplesarray must be dynamic memory allocated by themalloc()family of routines. And peep that comment at the bottom β you gotta disconnect thedatasamplespointer, otherwisemst_free()will free it for you and that is not the vibe you want. π
β¨ SEE ALSO β¨
msr_unpack(3), ms_time(3) and msr_pack(3)
go read those next, bestie, they're the sequel. π
π AUTHOR π
Chad Trabent IRIS Data Management Center
iconic. legendary. the blueprint. πβ¨
and that's the whole page, babes β this was MS_INTRO(3) from the libmseed source/version, dated 2013/07/17. π go forth and read some seismograms, you absolute main character. π¦πΈπ
π¦ where this page comes from
- debian bookworm: libmseed-doc 2.19.8-1 Β· LGPL-3.0-or-later
- debian trixie: libmseed-doc 2.19.8-1 Β· LGPL-3.0-or-later
- ubuntu noble: libmseed-doc 2.19.8-1ubuntu2 Β· LGPL-3.0-or-later
- ubuntu questing: libmseed-doc 2.19.8-1ubuntu2
- ubuntu resolute: libmseed-doc 2.19.8-1ubuntu3 Β· LGPL-3.0-or-later
- ubuntu stonking: libmseed-doc 2.19.8-1ubuntu3 Β· LGPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms