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
use std::iter::repeat;
use std::path::Path;
use peakbag::{PeakDetector, Peak};
use sdc;
use Result;
use error::Error;
use file::{Block, Channel, File, FileInfo, Record};
const HIGH_WIDTH: usize = 2;
const HIGH_FLOOR: u16 = 15;
const HIGH_CEILING: u16 = 255;
const LOW_WIDTHS: (usize, usize) = (3, 2);
const LOW_FLOOR: u16 = 15;
const LOW_CEILINGS: (u16, u16) = (250, 255);
const MIN_HEIGHT_ABOVE_BACKGROUND: f64 = 5.0;
const MAX_KURTOSIS: f64 = 0.04;
pub fn discretize(record: &Record, file_info: &FileInfo) -> Result<Vec<Point>> {
let mut high_blocks = Vec::new();
let mut low_blocks = Vec::new();
let mut reference_block = None;
for block in &record.blocks {
match block.channel {
Channel::High => high_blocks.push(block),
Channel::Low => low_blocks.push(block),
Channel::Reference => {
if reference_block.is_some() {
panic!("More than one reference block in this record");
} else {
reference_block = Some(block);
}
}
_ => {}
}
}
let reference_block = reference_block.unwrap();
let high_detector = PeakDetector::new(HIGH_WIDTH, HIGH_FLOOR, HIGH_CEILING)
.min_height_above_background(MIN_HEIGHT_ABOVE_BACKGROUND)
.max_kurtosis(MAX_KURTOSIS);
let (low_width, low_ceiling) = match high_blocks.len() {
0 => (LOW_WIDTHS.0, LOW_CEILINGS.0),
_ => (LOW_WIDTHS.1, LOW_CEILINGS.1),
};
let low_detector = PeakDetector::new(low_width, LOW_FLOOR, low_ceiling)
.min_height_above_background(MIN_HEIGHT_ABOVE_BACKGROUND)
.max_kurtosis(MAX_KURTOSIS)
.saturation(LOW_CEILINGS.1);
let reference_detector = high_detector;
let reference_peaks = reference_detector.detect_peaks(&reference_block.samples[..]);
if reference_peaks.len() != 1 {
debug!("Could not get a single reference peak out of: {:?}",
reference_block.samples);
return Err(Error::NeedSingleReferencePeak(reference_peaks.len()));
}
let ref reference_peak = reference_peaks[0];
let timestamp = |peak: &Peak<_>, block: &Block| {
block.time_sosbl + peak.index as f64 * file_info.sampling_time
};
let t_ref = timestamp(reference_peak, &reference_block);
let mut points = Vec::new();
for (block, detector) in low_blocks.iter()
.zip(repeat(low_detector))
.chain(high_blocks.iter().zip(repeat(high_detector))) {
let peaks = detector.detect_peaks(&block.samples[..]);
let num_target = peaks.len();
for (i, peak) in peaks.into_iter().enumerate() {
let time = timestamp(&peak, block);
let range = file_info.v_group / 2.0 * (time - t_ref);
let theta = (record.direction[2] / record.direction[0])
.atan()
.to_degrees();
let point = Point {
time: time,
range: range as f32,
theta: theta as f32,
x: (record.origin[0] + record.direction[0] * range) as f32,
y: (record.origin[1] + record.direction[1] * range) as f32,
z: (record.origin[2] + record.direction[2] * range) as f32,
target: (i + 1) as u8,
num_target: num_target as u8,
facet: record.facet,
peak: peak,
high_channel: block.channel == Channel::High,
};
points.push(point);
}
}
Ok(points)
}
#[derive(Clone, Copy, Debug)]
pub struct Point {
pub time: f64,
pub range: f32,
pub theta: f32,
pub x: f32,
pub y: f32,
pub z: f32,
pub target: u8,
pub num_target: u8,
pub facet: u16,
pub peak: Peak<u16>,
pub high_channel: bool,
}
impl File {
pub fn convert<T: Into<Vec<u8>>, P: AsRef<Path>>(sdf_path: T, sdc_path: P) -> Result<()> {
let mut sdf_file = try!(File::open(sdf_path));
let ref file_info = try!(sdf_file.info());
let mut sdc_file = try!(sdc::Writer::from_path(sdc_path));
for (i, record) in sdf_file.into_iter().enumerate() {
let points = match discretize(&record, file_info) {
Ok(points) => points,
Err(Error::NeedSingleReferencePeak(_)) => {
warn!("No reference peak detected for pulse {}, skipping", i);
continue;
}
Err(err) => return Err(err),
};
for p in points {
let ref point = sdc::Point {
time: p.time,
range: p.range,
theta: p.theta,
x: p.x,
y: p.y,
z: p.z,
amplitude: p.peak.amplitude,
width: 1,
target_type: sdc::TargetType::Peak,
target: p.target,
num_target: p.num_target,
rg_index: 1,
facet_number: p.facet as u8,
high_channel: p.high_channel,
class_id: None,
rho: None,
reflectance: None,
};
try!(sdc_file.write_point(point));
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use file::File;
#[test]
fn first_point() {
let mut file = File::open("data/110630_174316.sdf").unwrap();
file.reindex().unwrap();
let ref file_info = file.info().unwrap();
let ref record = file.read().unwrap();
let points = discretize(record, file_info).unwrap();
assert_eq!(4, points.len());
}
}