gmt_dos_clients_crseo/ngao/
builder.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::{
    cell::RefCell,
    path::{Path, PathBuf},
    rc::Rc,
    sync::Arc,
};

use crseo::{
    wavefrontsensor::PhaseSensorBuilder, AtmosphereBuilder, Builder, GmtBuilder, SourceBuilder,
    WavefrontSensorBuilder,
};
use gmt_dos_clients_domeseeing::DomeSeeing;

use crate::OpticalModel;

use super::optical_model::OpticalModelError;

/// GMT optical model builder
///
/// ```no_run
/// use gmt_dos_clients_crseo::OpticalModel;
/// let optical_model_builder = OpticalModel::builder();
/// ```
#[derive(Debug, Default)]
pub struct OpticalModelBuilder<T = PhaseSensorBuilder> {
    gmt_builder: GmtBuilder,
    src_builder: SourceBuilder,
    atm_builder: Option<AtmosphereBuilder>,
    dome_seeing: Option<(PathBuf, usize)>,
    sampling_frequency: Option<f64>,
    piston: Option<Arc<Vec<f64>>>,
    sensor: Option<T>,
}
impl<T> OpticalModelBuilder<T> {
    /// Configures the GMT
    ///
    /// ```no_run
    /// # use gmt_dos_clients_crseo::OpticalModel;
    /// use crseo::{Gmt, FromBuilder};
    /// let optical_model_builder = OpticalModel::builder().gmt(Gmt::builder());
    /// ```
    pub fn gmt(self, gmt_builder: GmtBuilder) -> Self {
        Self {
            gmt_builder,
            ..self
        }
    }
    /// Configures the light source
    ///
    /// ```no_run
    /// # use gmt_dos_clients_crseo::OpticalModel;
    /// use crseo::{Source, FromBuilder};
    /// let optical_model_builder = OpticalModel::builder().source(Source::builder());
    /// ```
    pub fn source(self, src_builder: SourceBuilder) -> Self {
        Self {
            src_builder,
            ..self
        }
    }
    /// Adds a piston error of each segment to the wavefront in the exit pupil
    pub fn piston(self, piston: Vec<f64>) -> Self {
        Self {
            piston: Some(Arc::new(piston)),
            ..self
        }
    }
    /// Configures the atmospheric turbulence
    ///
    /// ```no_run
    /// # use gmt_dos_clients_crseo::OpticalModel;
    /// use crseo::{Atmosphere, FromBuilder};
    /// let optical_model_builder = OpticalModel::builder().atmosphere(Atmosphere::builder());
    /// ```
    pub fn atmosphere(self, atm_builder: AtmosphereBuilder) -> Self {
        Self {
            atm_builder: Some(atm_builder),
            ..self
        }
    }
    /// Configures the dome seeing
    pub fn dome_seeing<P: AsRef<Path>>(mut self, path: P, upsampling: usize) -> Self {
        self.dome_seeing = Some((path.as_ref().to_owned(), upsampling));
        self
    }
    /// Sets the frequency in Hz to which the optical model is sampled
    ///
    /// ```no_run
    /// # use gmt_dos_clients_crseo::OpticalModel;
    /// let optical_model_builder = OpticalModel::builder().sampling_frequency(1000_f64);
    /// ```
    pub fn sampling_frequency(self, sampling_frequency: f64) -> Self {
        Self {
            sampling_frequency: Some(sampling_frequency),
            ..self
        }
    }
}

// impl OpticalModelBuilder {
//     /// Build the GMT optical model
//     ///
//     /// ```no_run
//     /// # use gmt_dos_clients_crseo::OpticalModel;
//     /// let optical_model_builder = OpticalModel::builder().build()?;
//     /// # Ok::<(), Box<dyn std::error::Error>>(())
//     /// ```
//     pub fn build(self) -> Result<OpticalModel, OpticalModelError> {
//         let gmt = self.gmt_builder.build()?;
//         let src = Rc::new(RefCell::new(self.src_builder.build()?));
//         let atm = if let Some(atm_builder) = self.atm_builder {
//             Some(atm_builder.build()?)
//         } else {
//             None
//         };
//         let dome_seeing = if let Some((path, upsampling)) = self.dome_seeing {
//             Some(DomeSeeing::new(path.to_str().unwrap(), upsampling, None)?)
//         } else {
//             None
//         };
//         Ok(OpticalModel {
//             gmt,
//             src,
//             atm,
//             dome_seeing,
//             tau: self.sampling_frequency.map_or_else(|| 0f64, |x| x.recip()),
//             piston: self.piston,
//             sensor: None,
//         })
//     }
// }

impl<T: Builder + WavefrontSensorBuilder> OpticalModelBuilder<T> {
    /// Sets the wavefront sensor
    pub fn sensor(mut self, sensors: T) -> Self {
        self.sensor = Some(sensors);
        self
    }
    #[deprecated(since  = "4.0.1", note="use `sensor` instead")]
    pub fn pyramid(mut self, sensors: T) -> Self {
        self.sensor = Some(sensors);
        self
    }
    /// Build the GMT optical model
    ///
    /// ```no_run
    /// # use gmt_dos_clients_crseo::OpticalModel;
    /// let optical_model_builder = OpticalModel::builder().build()?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn build(self) -> Result<OpticalModel<T::Component>, OpticalModelError> {
        let gmt = self.gmt_builder.build()?;
        let atm = if let Some(atm_builder) = self.atm_builder {
            Some(atm_builder.build()?)
        } else {
            None
        };
        let dome_seeing = if let Some((path, upsampling)) = self.dome_seeing {
            Some(DomeSeeing::new(path.to_str().unwrap(), upsampling, None)?)
        } else {
            None
        };
        let (sensor, src_builder) = if let Some(sensor) = self.sensor {
            let src_builder =
                <T as WavefrontSensorBuilder>::guide_stars(&sensor, Some(self.src_builder));
            (Some(sensor.build()?), src_builder)
        } else {
            (None, self.src_builder)
        };
        Ok(OpticalModel {
            gmt,
            src: Rc::new(RefCell::new(src_builder.build()?)),
            atm,
            dome_seeing,
            tau: self.sampling_frequency.map_or_else(|| 0f64, |x| x.recip()),
            piston: self.piston,
            sensor,
        })
    }
}