Provided by: libmseed-doc_2.19.8-1ubuntu2_all 

NAME
ms_intro - Introduction to libmseed
INTRODUCTION
The Mini-SEED library provides a framework for manipulation of SEED data records including the unpacking
and packing of data records. Functionality is also included for managing waveform data as continuous
traces. All structures of SEED 2.4 data records are supported with the following exceptions: Blockette
2000 opaque data which has an unknown data structure by definition 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 in order to allow rapid
development of Mini-SEED reading/writing software. The framework allows everything from manipulation of
Mini-SEED on a record-by-record basis to reading of Mini-SEED into continuous trace segments to packing
of large continuous traces using a record template.
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.
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. The following data encoding formats
are supported for unpacking only: GEOSCOPE (24-bit, 16/3 and 16/4 gain ranged), CDSN, SRO and DWWSSN.
DATA RECORDS
A Mini-SEED record is represented in the library using the data structure given 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.
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 the MSRecord.
reclen:
When unpacking this is the record length in bytes of the record pointed to by the 'record'
pointer. 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 linked BlktLink structures. All
appropriate multi-byte quantities in the blockettes are in host byte order. The
msr_addblockette(3) routine can be used to add blockettes to this chain. The BlktLink structure
and SEED blockette structures are defined in libmseed.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 (see ms_time(3)).
This time can be converted using the various ms_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 are 0 (DE_ASCII), 1 (DE_INT16), 3 (DE_INT32), 4
(DE_FLOAT32), 5 (DE_FLOAT64), 10 (DE_STEIM1) and 11 (DE_STEIM2).
byteorder:
Byte order of multi-byte quantities in the record. A value of 0 indicates little endian and a
value of 1 indicates 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. 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 the
get_samplesize(3) lookup routine.
ststate:
Pointer to a StreamState struct 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.
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
MSTraceGroup data structures allow the grouping of MSTrace structures. While a MSTrace structure is
normally used to hold trace information and associated data samples it can also be used without data
samples as a means to keep trace of data coverage without actual samples.
Numerous routines are provided for basic management of MSTrace structures, including the creation of new
MSTrace structures, adding data from Mini-SEED data structures to MSTrace structures, printing trace
information, etc.
The MSTraceGroup data structure acts as a very simple place to begin a chain of MSTrace structures and
keep track of the number of traces.
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) 0 when 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 (see ms_time(3)).
This time can be converted using the various ms_hptime2<X> functions.
endtime:
MSTrace end time, the time of the last sample, as a high precision epoch time (see ms_time(3)).
This time can be converted using the various ms_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 be 0. 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 the
get_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 a StreamState struct used internally to track stream oriented state variables. Memory
for this only allocated when needed.
next: A pointer to the next MSTrace structure. The value will be 0 for the last link in a chain of
MSTrace structures.
LOG MESSAGES
All of the log and diagnostic messages emitted by the library functions use the same interface. The
output from this interface can be controlled. This is useful when the library will be embedded in a
larger system with a custom logging facility. See the man page for more details.
ms_log() : the central logging facility. Behavior is controlled by
the settings specified with ms_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 an array of sample values,
the number of samples and a sample type. The number of samples is always the actual number of sample
values, not the number of bytes needed for storing the values. 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).
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_BYTEORDER
UNPACK_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_BYTEORDER
PACK_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
Example programs using libmseed are provided in the 'examples' directory of the source code distribution.
One of the most common tasks is to read a file of Mini-SEED records and either perform some action based
on the header values or apply some process to the data samples. This task is greatly simplified by using
the library functions ms_readmsr(3) and ms_readtraces(3). The ms_readmsr(3) routine will open a
specified file and return MSRecord structures for each Mini-SEED record it reads from the file. The
ms_readtraces(3) routine will do the same except add all the data read to a MSTraceGroup, this is ideal
for quickly reading data for processing. Both of these routines are able to automatically detect record
length.
If your application is not designed to read Mini-SEED from files the library also provides functions to
detect and parse Mini-SEED records in memory buffers. For more information 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 common task is to create (pack) Mini-SEED records. The library supports packing of Mini-SEED
either from MSRecord structures, MSTrace structures or MSTraceGroup collections using, respectively,
msr_pack(3), mst_pack(3) or mst_packgroup(3). In each case the appropriate data structure and parameters
are provided to the routine along with a function pointer to a routine that will be called each time a
record is complete and should be disposed of.
When packing Mini-SEED records the concept of a record header template is used, the template is always in
the form of a MSRecord structure. This allows the calling program to dictate the contents, with a few
exceptions, of the header in the final data records.
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);
}
SEE ALSO
msr_unpack(3), ms_time(3) and msr_pack(3)
AUTHOR
Chad Trabant
IRIS Data Management Center
2013/07/17 MS_INTRO(3)