///|
/// Compute a new record start from the original stream header, keeping its metadata.
pub fn advance_header(
header : @mseed.Header,
offset : Int64,
) -> @mseed.Header raise WaveformError {
if offset == 0L {
return header
}
let t = time_at(SampleTime::from_header(header), header.rate, offset)
{
..header,
year: t.year,
day: t.day,
hour: t.hour,
minute: t.minute,
second: t.second,
nanosecond: t.nanosecond,
}
}
///|
fn pcm(
data : Bytes,
start : Int32,
end : Int32,
size : Int32,
little : Bool,
) -> Array[Int32] {
Array::makei(end - start, fn(i) {
let pos = (start + i) * size
let mut word = 0
for j in 0..> 16
} else {
word
}
})
}
///|
priv enum SampleLayout {
HeaderOnly
Pcm(Int32)
Steim(@codec.Encoding)
}
///|
fn check_pcm_extent(
record : @mseed.Record,
size : Int32,
kind : String,
) -> Unit raise WaveformError {
let count = record.sample_count
let length = record.payload.length()
guard count >= 0 &&
count <= length / size &&
(record.version == 2 || count * size == length) else {
raise InvalidWaveform(
"\{kind} payload length disagrees with the declared sample count",
)
}
}
///|
fn check_sample_range(
start : Int32,
end : Int32,
count : Int32,
) -> Unit raise WaveformError {
guard start >= 0 && start <= end && end <= count else {
let error : @codec.SampleRangeError = @codec.InvalidSampleRange(
start, end, count,
)
raise InvalidWaveform(Show::to_string(error))
}
}
///|
fn sample_layout(record : @mseed.Record) -> SampleLayout raise WaveformError {
if record.sample_count == 0 {
guard record.payload.is_empty() else {
raise InvalidWaveform(
"cannot recover an integer waveform from a nonempty payload with unknown sample count",
)
}
guard record.version == 2 || record.encoding == 0 else {
raise InvalidWaveform(
"a header-only miniSEED 3 record must use encoding 0",
)
}
return HeaderOnly
}
match record.encoding {
1 | 3 => {
let size = if record.encoding == 1 { 2 } else { 4 }
check_pcm_extent(record, size, "integer")
Pcm(size)
}
10 => Steim(@codec.Steim1)
11 => Steim(@codec.Steim2)
code => raise InvalidWaveform("unsupported waveform encoding \{code}")
}
}
///|
/// Decode one integer waveform record. Both byte orders are accepted for legacy
/// input. Header-only records yield an empty series.
pub fn samples(record : @mseed.Record) -> Array[Int32] raise WaveformError {
samples_range(record, 0, record.sample_count)
}
///|
/// Decode only sample indices in `[start, end)`. Check the entire PCM extent
/// or integrate Steim differences through Xn, including for an empty range.
pub fn samples_range(
record : @mseed.Record,
start : Int32,
end : Int32,
) -> Array[Int32] raise WaveformError {
match sample_layout(record) {
Steim(format) =>
@codec.decode_range(
format,
record.payload,
record.sample_count,
start,
end,
little_endian=record.little_endian,
) catch {
e => raise InvalidWaveform(e.to_string())
}
layout => {
check_sample_range(start, end, record.sample_count)
if layout is Pcm(size) {
pcm(record.payload, start, end, size, record.little_endian)
} else {
[]
}
}
}
}
///|
/// Check a numeric waveform without storing decoded samples. Integer and float
/// PCM are checked against the declared count. Steim is integrated through Xn.
pub fn verify_samples(record : @mseed.Record) -> Unit raise WaveformError {
if float_pcm_width(record) is Some(_) {
return
}
if sample_layout(record) is Steim(format) {
@codec.verify(
format,
record.payload,
record.sample_count,
little_endian=record.little_endian,
) catch {
e => raise InvalidWaveform(e.to_string())
}
}
}