gmt_dos_clients_crseo/pyramid/
processing.rs

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
use std::ops::{Add, DivAssign, Mul, Sub};

use crate::Processing;

use super::{PyramidData, PyramidProcessor};

impl<T> Processing for PyramidProcessor<T>
where
    T: std::fmt::Debug
        + TryFrom<f32>
        + Copy
        + Add<Output = T>
        + Sub<Output = T>
        + DivAssign
        + Mul<Output = T>
        + PartialOrd
        + 'static,
    for<'a> &'a T: Sub<Output = T> + Add<Output = T>,
    <T as TryFrom<f32>>::Error: std::fmt::Debug,
{
    type ProcessorData = PyramidData<T>;

    fn processing(&self) -> Self::ProcessorData {
        let (n, m) = self.frame.resolution;
        assert!(n > 0 && m > 0, "the detector frame resolution is null");

        let crseo::wavefrontsensor::LensletArray { n_side_lenslet, .. } = self.lenslet_array;
        let n0 = n_side_lenslet / 2;
        let n1 = n0 + n / 2;

        /*
        The detector frame of the pyramid is divided in 4 quadrants:
        | I1 I2 |
        | I3 I4 |
         */

        /* left =
        | I1 |
        | I3 |
         */
        let left = self.frame.value.chunks(n).skip(n0).take(n_side_lenslet);
        /* right =
        | I2 |
        | I4 |
         */
        let right = self.frame.value.chunks(n).skip(n1).take(n_side_lenslet);
        // (left - right, left + right)
        let row_diff_sum: Vec<_> = left
            .zip(right)
            .flat_map(|(left, right)| {
                left.iter()
                    .zip(right)
                    .map(|(left, right)| (left - right, left + right))
                    .collect::<Vec<_>>()
            })
            .collect();
        // top = ( I1 - I2 , I1 + I2 )
        let top = (0..n)
            .map(|i| row_diff_sum.iter().skip(i).step_by(m).collect::<Vec<_>>())
            .skip(n0)
            .take(n_side_lenslet);
        // bottom = ( I3 -I4 , I3 + I4)
        let bottom = (0..n)
            .map(|i| row_diff_sum.iter().skip(i).step_by(m).collect::<Vec<_>>())
            .skip(n1)
            .take(n_side_lenslet);
        /*
        top - bottom = I1 - I2 + I3 - I4
        top + bottom = I1 + I2 + I3 + I4
         */
        let (row_col_data, flux): (Vec<_>, Vec<_>) = top
            .zip(bottom)
            .flat_map(|(top, bottom)| {
                top.iter()
                    .zip(bottom)
                    .map(|((top_diff, top_sum), (bottom_diff, bottom_sum))| {
                        (*top_diff + *bottom_diff, *top_sum + *bottom_sum)
                    })
                    .collect::<Vec<_>>()
            })
            .unzip();

        // top = | I1 I2 |
        let top = (0..n)
            .map(|i| {
                self.frame
                    .value
                    .iter()
                    .skip(i)
                    .step_by(m)
                    .collect::<Vec<_>>()
            })
            .skip(n0)
            .take(n_side_lenslet);
        // bottom = | I3 I4 |
        let bottom = (0..n)
            .map(|i| {
                self.frame
                    .value
                    .iter()
                    .skip(i)
                    .step_by(m)
                    .collect::<Vec<_>>()
            })
            .skip(n1)
            .take(n_side_lenslet);
        // top - bottom
        let col_diff: Vec<_> = top
            .zip(bottom)
            .flat_map(|(top, bottom)| {
                top.iter()
                    .zip(bottom)
                    .map(|(&&top, &bottom)| top - bottom)
                    .collect::<Vec<_>>()
            })
            .collect();
        // I1 - I3 + I2 - I4
        let left = col_diff.chunks(n);
        let right = col_diff.chunks(n);
        let col_row_data: Vec<_> = left
            .zip(right)
            .flat_map(|(left, right)| {
                left.iter()
                    .skip(n0)
                    .take(n_side_lenslet)
                    .zip(right.iter().skip(n1).take(n_side_lenslet))
                    .map(|(&left, &right)| left + right)
                    .collect::<Vec<_>>()
            })
            .collect();

        let mut _flux = flux.clone();
        _flux.sort_by(|a, b| a.partial_cmp(b).unwrap());
        let med_flux = match _flux.len() {
            n if n % 2 == 0 => T::try_from(0.5).unwrap() * (_flux[n / 2] + _flux[1 + n / 2]),
            n => _flux[(n + 1) / 2],
        };

        let (sx, sy): (Vec<_>, Vec<_>) = row_col_data
            .into_iter()
            .zip(col_row_data.into_iter())
            .map(|(mut sx, mut sy)| {
                sx /= med_flux;
                sy /= med_flux;
                (sx, sy)
            })
            .unzip();

        /*         let (sx, sy): (Vec<_>, Vec<_>) = row_col_data
        .into_iter()
        .zip(col_row_data.into_iter())
        .zip(flux.iter())
        .map(|((mut sx, mut sy), flux)| {
            sx /= *flux;
            sy /= *flux;
            (sx, sy)
        })
        .unzip(); */

        PyramidData { sx, sy, flux }
    }
}