///|
// SEED 2.4 chapter 8 specifies the blockette layouts and flag meanings.
// FDSN extra-header schema v1.0 defines the case-sensitive property names.
fn calibration_entry(
data : Bytes,
at : Int32,
kind : Int32,
little : Bool,
header : Header,
) -> Json? {
guard annotation_time(data, at + 4, little, header) is Some(time) else {
return None
}
if kind == 395 {
guard word(data, at + 14, little) == 0 else { return None }
// An abort supplies only an end time. The start can be in another record.
return Some(Json::object({ "EndTime": Json::string(time) }))
}
let (name, allowed_flags, amplitude_offset) = match kind {
300 => ("STEP", 0x0f, 24)
310 => ("SINE", 0x7c, 24)
320 => ("PSEUDORANDOM", 0x1c, 20)
390 => ("GENERIC", 0x0c, 20)
_ => return None
}
let flags = data[at + 15].to_int()
let amplitude = legacy_float(data, at + amplitude_offset, little)
let channel_at = at + amplitude_offset + 4
guard (flags & allowed_flags) == flags &&
(kind == 300 || data[at + 14] == 0) &&
data[channel_at + 3] == 0 &&
amplitude.is_finite() &&
annotation_text(data[channel_at:channel_at + 3]) is Some(channel) else {
return None
}
let fields : Map[String, Json] = {
"Type": Json::string(name),
"BeginTime": Json::string(time),
"Trigger": Json::string(
if (flags & 4) != 0 {
"AUTOMATIC"
} else {
"MANUAL"
},
),
"Continued": Json::boolean((flags & 8) != 0),
"Duration": Json::number(
long_word(data, at + 16, little).to_double() / 10000.0,
),
"Amplitude": Json::number(amplitude),
}
if !channel.is_empty() {
fields["InputChannel"] = Json::string(channel)
} else if kind == 390 || (kind == 320 && (flags & 16) != 0) {
return None
}
if kind != 390 {
let reference_at = channel_at + 4
guard annotation_text(data[reference_at + 4:reference_at + 16])
is Some(coupling) &&
annotation_text(data[reference_at + 16:reference_at + 28])
is Some(rolloff) else {
return None
}
fields["ReferenceAmplitude"] = Json::number(
long_word(data, reference_at, little).to_double(),
)
if !coupling.is_empty() {
fields["Coupling"] = Json::string(coupling)
}
if !rolloff.is_empty() {
fields["Rolloff"] = Json::string(rolloff)
}
}
match kind {
300 => {
fields["Steps"] = Json::number(data[at + 14].to_int().to_double())
fields["StepFirstPulsePositive"] = Json::boolean((flags & 1) != 0)
fields["StepAlternateSign"] = Json::boolean((flags & 2) != 0)
fields["StepBetween"] = Json::number(
long_word(data, at + 20, little).to_double() / 10000.0,
)
}
310 => {
let period = legacy_float(data, at + 20, little)
guard period.is_finite() else { return None }
// SEED 2.4 requires exactly one amplitude range, FDSN stores a single string.
let range = match flags & 0x70 {
0x10 => "PEAKTOPEAK"
0x20 => "ZEROTOPEAK"
0x40 => "RMS"
_ => return None
}
fields["SinePeriod"] = Json::number(period)
fields["AmplitudeRange"] = Json::string(range)
}
320 => {
guard annotation_text(data[at + 56:at + 64]) is Some(noise) else {
return None
}
if !noise.is_empty() {
fields["Noise"] = Json::string(noise)
}
if (flags & 16) != 0 {
fields["AmplitudeRange"] = Json::string("RANDOM")
}
}
_ => ()
}
Some(Json::object(fields))
}