1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use astronomy::units::{Dimension, Quantity, QuantityError, Unit, UnitProduct};
use ndarray::array;
use thiserror::Error;
/// Errors that can occur when creating or manipulating a `Channel`.
#[derive(Debug, Error)]
pub enum ChannelError {
#[error("Failed to parse unit string: {0}")]
UnitParseError(String),
#[error("Invalid quantity error: {0}")]
QuantityError(#[from] QuantityError), //Allows converting QuantityError into ChannelError
}
#[derive(Debug, Clone, PartialEq)]
pub struct Channel {
// The data for this channel, e.g., gravitational wave strain data
// The name of the channel, e.g., "H1:GWOSC-4KHZ_RAMP_C00"
// typically follows the format "Detector:ChannelName"
pub name: String,
// In Hz, or with explicit Unit
pub sample_rate: Option<Quantity>,
// The unit of the data for this channel, e.g., "m", "s", "Hz"
pub unit: Option<Unit>,
// Frequency range in Hz (low, high), e.g. (0.0, 1000.0)
pub frequency_range: Option<(f64, f64)>,
// Indicates if the channel is safe to use
pub safe: Option<bool>,
// LDAS name for frames that contain this channel.
// The type of frame this channel belongs to, e.g., "L1_HOFT_C00", "H1_HOFT_C00"
pub frametype: Option<String>,
// The model of the detector, e.g., "LIGO", "Virgo"
pub model: Option<String>,
}
impl Channel {
/// Creates a new Channel with the given name, sample rate, unit, frequency range, safety status, frame type, and model.
/// # Parameters
/// - `name`: The name of the channel, e.g., "H1:GWOSC-4KHZ_RAMP_C00".
/// - `sample_rate`: The sample rate of the channel, e.g., 4096 Hz. If provided, converted to a `Quantity` with "Hz" unit.
/// - `unit`: The unit of the data for this channel, e.g., "m", "s", "Hz". For now, an astronomy::units::Unit must be provided.
/// - `frequency_range`: The frequency range of the channel, e.g., (0.0, 1000.0) Hz.
/// - `safe`: Indicates if the channel is safe to use, e.g., `true` or `false`.
/// - `frametype`: The LDAS name for frames that contain this channel, e.g., "L1_HOFT_C00".
/// - `model`: The model of the detector, e.g., "LIGO", "Virgo".
/// # Returns
/// A `Result` containing the new `Channel` instance or an error if the unit is not valid.
pub fn new(
name: impl Into<String>, // Allows accepting String or &str
sample_rate: Option<f64>,
unit: Option<Unit>,
frequency_range: Option<(f64, f64)>,
safe: Option<bool>,
frametype: Option<String>,
model: Option<String>,
) -> Result<Self, ChannelError> {
let parsed_sample_rate = sample_rate.map(|sr| {
Quantity::new(
array![sr],
// Hz is the inverse of time
// We create a basic Unit for Hz using Dimension::Time.inverse()
Unit::new("Hz", 1.0, UnitProduct::new(Dimension::Time).inverse()),
)
});
Ok(Self {
name: name.into(),
sample_rate: parsed_sample_rate,
unit,
frequency_range,
safe,
frametype,
model,
})
}
/// Returns the name of the channel.
pub fn get_name(&self) -> &str {
&self.name
}
/// Returns the sample rate of the channel, if available, as an Option of Quantity.
pub fn get_sample_rate(&self) -> Option<&Quantity> {
self.sample_rate.as_ref()
}
/// Returns the unit of the channel, if available, as an Option of Unit.
pub fn get_unit(&self) -> Option<&Unit> {
self.unit.as_ref()
}
/// Returns the frequency range of the channel, if available, as an Option of tuple (low, high).
pub fn get_frequency_range(&self) -> Option<(f64, f64)> {
self.frequency_range
}
/// Return the safety status of the channel, if available, as an Option of bool.
pub fn is_safe(&self) -> Option<bool> {
self.safe
}
/// Returns the frame type of the channel, if available, as an Option of &str.
pub fn get_frametype(&self) -> Option<&str> {
self.frametype.as_deref()
}
/// Returns the model of the detector, if available, as an Option of &str.
pub fn get_model(&self) -> Option<&str> {
self.model.as_deref()
}
}
// -- Display implementation for Channel
impl std::fmt::Display for Channel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Channel(name='{}'", self.name)?;
if let Some(sr) = &self.sample_rate {
// Display value and unit of Quantity
write!(f, ", sample_rate={}{}", sr.value[0], sr.unit.name)?;
}
if let Some(u) = &self.unit {
write!(f, ", unit={}", u.name)?;
}
if let Some((low, high)) = self.frequency_range {
write!(f, ", frequency_range=({}, {})", low, high)?;
}
if let Some(safe) = self.safe {
write!(f, ", safe={}", safe)?;
}
if let Some(ft) = &self.frametype {
write!(f, ", frametype='{}'", ft)?;
}
if let Some(model) = &self.model {
write!(f, ", model='{}'", model)?;
}
write!(f, ")")
}
}
// -- Tests for Channel
#[cfg(test)]
mod channel_tests {
use super::*;
use astronomy::units::{METER, Unit};
#[test]
fn test_channel_creation_minimal() {
let channel = Channel::new(
"H1:GWOSC-4KHZ_RAMP_C00",
Some(4096.0),
Some(Unit::new("m", 1.0, UnitProduct::new(Dimension::Length))),
None,
None,
None,
None,
)
.unwrap();
assert_eq!(channel.get_name(), "H1:GWOSC-4KHZ_RAMP_C00");
assert_eq!(channel.get_sample_rate().unwrap().value[0], 4096.0);
assert_eq!(channel.get_unit().unwrap().name, "m");
assert!(channel.get_frequency_range().is_none());
assert!(channel.is_safe().is_none());
assert!(channel.get_frametype().is_none());
assert!(channel.get_model().is_none());
println! {"Channel created: {channel}"};
}
#[test]
fn test_channel_creation() {
let channel = Channel::new(
"H1:GWOSC-4KHZ_RAMP_C00",
Some(4096.0),
Some(METER),
Some((0.0, 1000.0)),
Some(true),
Some("L1_HOFT_C00".to_string()),
Some("LIGO".to_string()),
)
.unwrap();
assert_eq!(channel.get_name(), "H1:GWOSC-4KHZ_RAMP_C00");
assert_eq!(channel.get_sample_rate().unwrap().value[0], 4096.0);
assert_eq!(channel.get_unit().unwrap().name, "m");
assert_eq!(channel.get_frequency_range(), Some((0.0, 1000.0)));
assert_eq!(channel.is_safe(), Some(true));
assert_eq!(channel.get_frametype(), Some("L1_HOFT_C00"));
assert_eq!(channel.get_model(), Some("LIGO"));
}
#[test]
fn test_channel_creation_with_specific_unit_and_rate() {
let rate_unit = Unit::new("Hz", 1.0, UnitProduct::new(Dimension::Time).inverse());
let expected_rate = Quantity::new(array![1024.0], rate_unit);
// Volts ML2T−3I−1
let voltage_unit = Unit::new(
"V",
1.0,
UnitProduct::from_components(&[
(Dimension::Mass, 1),
(Dimension::Length, 2),
(Dimension::Time, -3),
(Dimension::ElectricCurrent, -1),
]),
);
let channel = Channel::new(
"MyChannel",
Some(1024.0),
Some(voltage_unit.clone()),
None,
None,
None,
None,
)
.unwrap();
assert_eq!(channel.get_name(), "MyChannel");
assert_eq!(
channel.get_sample_rate().unwrap().value,
expected_rate.value
);
assert_eq!(channel.get_sample_rate().unwrap().unit, expected_rate.unit);
assert_eq!(channel.get_unit().unwrap(), &voltage_unit);
}
#[test]
fn test_channel_display() {
let channel = Channel::new(
"H1:GWOSC-4KHZ_RAMP_C00",
Some(4096.0),
Some(Unit::new("m", 1.0, UnitProduct::new(Dimension::Length))),
None,
None,
None,
None,
)
.unwrap();
assert_eq!(
format!("{}", channel),
"Channel(name='H1:GWOSC-4KHZ_RAMP_C00', sample_rate=4096Hz, unit=m)"
);
}
}