Function pack_raw

Source
pub fn pack_raw<T, F>(
    data_samples: &mut [T],
    start_time: &OffsetDateTime,
    record_handler: F,
    info: &PackInfo,
    flags: MSControlFlags,
) -> MSResult<(usize, usize)>
where F: FnMut(&[u8]),
Expand description

Low level function that packs raw data samples into miniSEED records.

start_time is the time of the first data sample. Buffers containing the packed miniSEED records are passed to the record_handler closure. Returns on success a tuple where the first value is the number of totally packed records and the second value is the number of totally packed samples.

If flags has MSControlFlags::MSF_FLUSHDATA set, all of the data_samples will be packed into miniSEED records even though the last one will probably be smaller than requested or, in the case of miniSEED v2, unfilled. If flags has MSControlFlags::MSF_PACKVER2 set records are packed as miniSEED v2 regardless of PackInfo::format_version.

See also raw::msr3_pack.

ยงExamples

Basic usage

use time::format_description::well_known::Iso8601;
use time::OffsetDateTime;

use mseed::{MSControlFlags, MSDataEncoding, MSRecord, MSSampleType, PackInfo};

let mut pack_info = PackInfo::new("FDSN:XX_TEST__X_Y_Z").unwrap();
pack_info.encoding = MSDataEncoding::Text;

let record_handler = |rec: &[u8]| {
    let mut buf = rec.to_vec();
    let msr = MSRecord::parse(&mut buf, MSControlFlags::MSF_UNPACKDATA).unwrap();

    assert_eq!(msr.sid().unwrap(), "FDSN:XX_TEST__X_Y_Z");
    assert_eq!(msr.encoding().unwrap(), MSDataEncoding::Text);
    assert_eq!(msr.sample_type(), MSSampleType::Text);
};

let flags = MSControlFlags::MSF_FLUSHDATA;
let start_time = OffsetDateTime::parse("2012-01-01T00:00:00Z", &Iso8601::DEFAULT).unwrap();

let mut payload: Vec<u8> = "Hello, miniSEED!".bytes().collect();
let (cnt_records, cnt_samples) = mseed::pack_raw(
    &mut payload,
    &start_time,
    record_handler,
    &pack_info,
    flags,
)
.unwrap();

assert_eq!(cnt_records, 1);
assert_eq!(cnt_samples, 16);

The record_handler closure may be customized to process the injected packed miniSEED record buffers. For instance, writing the records to a file may be implemented as follows:

use std::fs::OpenOptions;
use std::io::{BufWriter, Write};

use time::format_description::well_known::Iso8601;
use time::OffsetDateTime;

use mseed::{self, MSControlFlags, PackInfo};

let pack_info = PackInfo::new("FDSN:XX_TEST__X_Y_Z").unwrap();

let file = OpenOptions::new()
    .create(true)
    .write(true)
    .open("path/to/out.mseed")
    .unwrap();
let mut writer = BufWriter::new(file);

let record_handler = move |rec: &[u8]| {
    let _ = writer.write(rec);
};

let mut data_samples: Vec<i32> = (1..100).collect();
let start_time = OffsetDateTime::parse("2012-01-01T00:00:00Z", &Iso8601::DEFAULT).unwrap();
mseed::pack_raw(
    &mut data_samples,
    &start_time,
    record_handler,
    &pack_info,
    MSControlFlags::MSF_FLUSHDATA,
)
.unwrap();