rao 0.2.1

Robust and scalable Adaptive Optics tools
Documentation
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! # rao
//! 
//! `rao` - Adaptive Optics tools in Rust - is a set of fast and robust adaptive
//! optics utilities. The current scope of `rao` is for the calculation of
//! large matrices in AO, used in the configuration of real-time adaptive optics,
//! control. Specifically, we aim to provide fast, scalable, and reliable APIs for
//! generating:
//!  - `rao::IMat` - the interaction matrix between measurements and actuators,
//!  - `rao::CovMat` - the covariance matrix between measurements.
//!
//! These two matrices are typically the largest computational burden in the
//! configuration of real-time control (RTC) for AO, and also the most 
//! performance-sensitive parts of the RTC.
//!
//! # Examples
//! Building an interaction matrix for a square-grid DM and a square-grid SH-WFS:
//! ```
//! use crate::rao::Matrix;
//! const N_SUBX: i32 = 8;  // 8 x 8 subapertures
//! const PITCH: f64 = 0.2;  // 0.2 metres gap between actuators
//! const COUPLING: f64 = 0.5;  // actuator cross-coupling
//! 
//! // build list of measurements
//! let mut measurements = vec![];
//! for i in 0..N_SUBX {
//!     for j in 0..N_SUBX {
//!         let x0 = ((j-N_SUBX/2) as f64 + 0.5)*PITCH;
//!         let y0 = ((i-N_SUBX/2) as f64 + 0.5)*PITCH;
//!         let xz = 0.0;  // angular x-component (radians)
//!         let yz = 0.0;  // angular y-compenent (radians)
//!         // define the optical axis of subaperture
//!         let line = rao::Line::new(x0,xz,y0,yz);
//!         // slope-style measurement
//!         // x-slope
//!         measurements.push(rao::Measurement::SlopeTwoEdge{
//!             central_line: line.clone(),
//!             edge_separation: PITCH,
//!             edge_length: PITCH,
//!             npoints: 5,
//!             gradient_axis: rao::Vec2D::x_unit(),
//!             altitude: f64::INFINITY,
//!         });
//!         // y-slope
//!         measurements.push(rao::Measurement::SlopeTwoEdge{
//!             central_line: line.clone(),
//!             edge_separation: PITCH,
//!             edge_length: PITCH,
//!             npoints: 5,
//!             gradient_axis: rao::Vec2D::y_unit(),
//!             altitude: f64::INFINITY,
//!         });
//!     }
//! }
//! 
//! // build list of actuators
//! let mut actuators = vec![];
//! for i in 0..(N_SUBX+1) {
//!     for j in 0..(N_SUBX+1) {
//!         let x = ((j-N_SUBX/2) as f64)*PITCH;
//!         let y = ((i-N_SUBX/2) as f64)*PITCH;
//!         actuators.push(
//!             // Gaussian influence functions
//!             rao::Actuator::Gaussian{
//!                 // std defined by coupling and pitch
//!                 sigma: rao::coupling_to_sigma(COUPLING, PITCH),
//!                 // position of actuator in 3D (z=altitude)
//!                 position: rao::Vec3D::new(x, y, 0.0),
//!                 // microns per volt of actuator
//!                 microns_per_volt: 1.0,
//!             }
//!         );
//!     }
//! }
//! 
//! // instanciate imat from (actu,meas)
//! let imat = rao::IMat::new(&measurements, &actuators);
//! // serialise imat for saving
//! let data: Vec<f64> = imat.flattened_array();
//! ```

#[macro_use] extern crate impl_ops;

pub mod utils;
pub use utils::coupling_to_sigma;
mod geometry;
pub use crate::geometry::{
    Vec2D,
    Vec3D,
    Line,
};
mod linalg;
pub use crate::linalg::Matrix;
mod core;
pub use crate::core::{
    Sampler,
    Sampleable,
    CoSampleable,
    IMat,
    CovMat,
};
use serde::{Serialize,Deserialize};


/// Common [Sampler]s in Adaptive Optics
/// 
/// A [Measurement] provides a scalar-valued sample of an AO system. A single
/// measurement device (e.g., a Shack Hartmann WFS) is typically comprised of 
/// many [Measurement]s, e.g., `&[Measurement; N]`.
#[derive(Debug,Clone,PartialEq,Serialize,Deserialize)]
pub enum Measurement{
    /// The null measurement, always returning 0.0 regardless of the measured object.
    Zero,
    /// Phase measurement along a given [Line].
    Phase {
        /// [Line] in 3D space to trace through [Sampleable] objects.
        line: Line,
    },
    /// Slope measurement, defined by two [Line]s in 3D space.
    ///
    /// The slope measured is equal to the *sampled function at the point where
    /// `line_pos` intersects it* minus the *sampled function at the point
    /// where `line_neg` intersects it*, divided by the *distance between the
    /// two lines at zero-altitude*.
    SlopeTwoLine {
        /// positive end of slope (by convention)
        line_pos: Line,
        /// negative end of slope (by convention)
        line_neg: Line,
    },
    /// Slope measurement, defined by a [Line] in 3D space and  the sampled edges
    /// a rectangle in 2D space.
    ///
    /// Approximate the average slope over a region defined by two edges of a rectangle.
    /// The edges are defined by their length, their separation, and the axis along which
    /// they are separated (the *gradient axis*). The edges of the rectangle are sampled
    /// so that the spacing between points is uniform and the furthest distance to an
    /// unsampled point of the edge is minimal, i.e.:
    ///  - `npoints=1` => `[----x----]`,
    ///  - `npoints=2` => `[--x---x--]`,
    ///  - `npoints=3` => `[-x--x--x-]`,
    ///  - *etc*
    ///
    /// This is not a typical "linspace", which is ill-defined for 1 point, though one
    /// could implement that as a [Sampler] wrapper around [`Measurement::SlopeTwoLine`].
    SlopeTwoEdge {
        /// Principle axis of the WFS
        central_line: Line,
        /// Length of edges (e.g., subaperture height for x-slope measurement)
        edge_length: f64,
        /// Separation of edges (e.g., subaperture width for x-slope measurement)
        edge_separation: f64,
        /// Axis of gradient vector to sample, e.g., x-slope -> `Vec2D::new(1.0, 0.0)`
        gradient_axis: Vec2D,
        /// Number of points to sample along each edge (more points can be more accurate).
        npoints: u32,
        /// Altitude of target to raytrace through to.
        altitude: f64,
    },
}

/// [Measurement] is the prototypical [Sampler] type.
impl Sampler for Measurement {
    /// The [`Sampler::get_bundle`] method implementation for the [Measurement] variants
    /// should serve as an example for implementing other [Sampler] types. Inspect the
    /// source code for the reference implementation. In short:
    ///  - a [`Measurement::Zero`] variant returns an empty vector,
    ///  - a [`Measurement::Phase`] variant returns a single line with a coefficient of `1.0`,
    ///  - a [`Measurement::SlopeTwoLine`] variant returns two lines, with a positive
    ///    and negative coefficient (for the *start* and *end* of the slope), scaled by
    ///    the inverse of the ground separation of the lines, so that the resulting units
    ///    are in *sampled function units per distance unit*.
    ///  - a [`Measurement::SlopeTwoEdge`] variant returns `2 * npoints` lines, consisting of
    ///    `npoints` positive coefficients, and `npoints` negative coefficients, each scaled
    ///    by the inverse of the ground-layer separation and a factor of `1.0 / npoints as f64`,
    ///    in order to get a result which is in units of *sampled function units per distance unit*. 
    fn get_bundle(&self) -> Vec<(Line,f64)> {
        match self {
            Measurement::Zero => vec![],
            Measurement::Phase{line} => vec![(line.clone(),1.0)],
            Measurement::SlopeTwoLine{line_pos, line_neg} => {
                let coeff = 1.0/line_pos.distance_at_ground(line_neg);
                vec![
                    (line_pos.clone(),  coeff),
                    (line_neg.clone(), -coeff),
                ]
            },
            Measurement::SlopeTwoEdge{central_line, edge_length, edge_separation, gradient_axis, npoints, altitude} => {
                let coeff = (1.0 / f64::from(*npoints)) / edge_separation;
                let offset_vec = gradient_axis * edge_separation * 0.5;
                let point_a =  edge_length * 0.5 * gradient_axis.ortho();
                let point_b = -point_a.clone();
                match *altitude {
                    f64::INFINITY => {
                        Vec2D::linspread(&point_a, &point_b, *npoints)
                        .iter()
                        .flat_map(|p|
                            vec![
                                (
                                    central_line + (p + &offset_vec),
                                    coeff
                                ),
                                (
                                    central_line + (p - &offset_vec),
                                    -coeff
                                ),
                            ])
                        .collect()
                    },
                    altitude => {
                        Vec2D::linspread(&point_a, &point_b, *npoints)
                        .iter()
                        .flat_map(|p| {
                            let p_alt = Vec3D::new(central_line.xz*altitude, central_line.yz*altitude, altitude);
                            vec![
                                (
                                    Line::new_from_two_points(
                                        &((central_line + (p + &offset_vec)).position_at_altitude(0.0) + Vec3D::origin()),
                                        &p_alt,
                                    ),
                                    coeff
                                ),
                                (
                                    Line::new_from_two_points(
                                        &((central_line + (p - &offset_vec)).position_at_altitude(0.0) + Vec3D::origin()),
                                        &p_alt,
                                    ),
                                    -coeff
                                ),
                            ]})
                        .collect()
                    }
                }
            },
        }
    }
}



/// Common [Sampleable]s in Adaptive Optics.
/// 
/// An [Actuator]'s state is defined by a scalar value, so a device with `N`
/// actuatable degrees of freedom is considered as `N` different [Actuator]s,
/// e.g., `&[Actuator; N]`.
#[derive(Debug,Clone,PartialEq,Serialize,Deserialize)]
pub enum Actuator{
    /// A null actuator, making zero impact on any `Measurement`
    Zero,
    /// A circularly symmetric Gaussian actuator, centred at `position` with
    /// a specified scalar `sigma`. See [`utils::gaussian`] for more info.
    Gaussian {
        /// sigma of gaussian function in metres. 
        sigma: f64,
        /// position of actuator in 3d space, z=altitude.
        position: Vec3D,
        /// microns of displacement per volt
        microns_per_volt: f64,
    },
    TipTilt {
        /// position along slope of TT actuator surface where the amplitude
        /// of the phase is equal to +1.0 units. This vector is colinear with
        /// the acuation axis. E.g., if this vector is `(1.0,0.0)`, then the 
        /// response of the actuator will be a 
        ///Deliberately not in arcseconds
        /// so that you have to be deliberate and careful about your units.
        unit_response: Vec2D,
    },
}

impl Sampleable for Actuator {
    fn sample(&self, line: &Line) -> f64 {
        match self {
            Self::Zero => 0.0,
            Self::Gaussian{sigma, position, microns_per_volt } => {
                let distance = position.distance_at_altitude(line);
                utils::gaussian(distance / sigma) * microns_per_volt
            },
            Self::TipTilt{unit_response} => {
                line.position_at_altitude(0.0).dot(unit_response)
            }
        }
    }
}

/// Simple covariance model, this might be refactored into an enum of models.
#[derive(Debug,Clone,PartialEq,Serialize,Deserialize)]
pub struct VonKarmanLayer {
    pub r0: f64,
    pub l0: f64,
    pub alt: f64,
    pub v: Vec2D,
}

impl VonKarmanLayer {
    /// Construct a new von Karman turbulence layer from its parameters
    #[must_use]
    pub fn new(r0: f64, l0: f64, alt: f64, v: Vec2D) -> VonKarmanLayer {
        VonKarmanLayer {
            r0, l0, alt, v,
        }
    }
}

/// [`VonKarmanLayer`] is (for now) the prototypical [`CoSampleable`] object.
///
/// Perhaps confusingly, this implementation allows the cosampling of the
/// von Karman turbulence statistical model, returning the covariance between
/// two [`Line`]s intercepting that layer.
impl CoSampleable for VonKarmanLayer {
    fn cosample(&self, line_a: &Line, line_b:&Line, dt: f64) -> f64 {
        let p1 = line_a.position_at_altitude(self.alt);
        let p2 = line_b.position_at_altitude(self.alt) - dt * self.v.clone();
        utils::vk_cov((p1-p2).norm(), self.r0, self.l0)
    }
}


#[derive(Debug,Clone,PartialEq,Serialize,Deserialize)]
pub struct Pupil {
    pub rad_outer: f64,
    pub rad_inner: f64,
    pub spider_thickness: f64,
    pub spiders: Vec<(Vec2D,Vec2D)>
}

impl Sampleable for Pupil {
    fn sample(&self, ell: &Line) -> f64 {
        let p = ell.position_at_altitude(0.0);
        let mut out: f64 = 1.0;
        let r = p.norm();
        if r > self.rad_outer {
            out *= 0.0;
        }
        if r < self.rad_inner {
            out *= 0.0;
        }
        for spider in &self.spiders {
            if signed_distance_to_capsule(
                &p, &spider.0, &spider.1, self.spider_thickness/2.0
            ) < 0.0 {
                out *= 0.0;
            }
        }
        out
    }
}

fn signed_distance_to_capsule(
    position: &Vec2D,
    capsule_start: &Vec2D,
    capsule_end: &Vec2D,
    radius: f64
) -> f64 {
    let pa: Vec2D = position - capsule_start;
    let ba: Vec2D = capsule_end - capsule_start;
    let mut h: f64 = pa.dot(&ba)/ba.dot(&ba);
    h = h.clamp(0.0, 1.0);
    (pa - ba*h).norm() - radius
}



#[cfg(test)]
mod tests {
    use std::f64;

    use super::*;
    use approx::{assert_abs_diff_eq, assert_abs_diff_ne};
    
    #[test]
    fn gaussian_on_axis_phase() {
        let actuators = [
            Actuator::Gaussian{
                sigma: coupling_to_sigma(0.5,1.0),
                position: Vec3D::new(0.0, 0.0, 0.0),
                microns_per_volt: 1.0,
            }
        ];
        let measurements = [
            Measurement::Phase{
                line: Line::new_on_axis(0.0, 0.0)
            }
        ];
        let imat = IMat::new(&measurements, &actuators);
        assert_abs_diff_eq!(imat.eval(0,0), 1.0, epsilon = f64::EPSILON);
    }

    #[test]
    fn gaussian_off_axis_phase() {
        let actuators = [
            Actuator::Gaussian{
                sigma: coupling_to_sigma(0.5,1.0),
                position: Vec3D::new(0.0, 0.0, 1000.0),
                microns_per_volt: 1.0,
            }
        ];
        let measurements = [
            Measurement::Phase{
                line: Line::new(0.0, 1.0/1000.0, 0.0, 0.0)
            }
        ];
        let imat = IMat::new(&measurements, &actuators);
        assert_abs_diff_eq!(imat.eval(0,0), 0.5, epsilon = f64::EPSILON);
    }

    #[test]
    fn gaussian_off_axis_phase_twopoint() {
        let actuators = [
            Actuator::Gaussian{
                sigma: coupling_to_sigma(0.5,1.0),
                position: Vec3D::new(0.0, 0.0, 1000.0),
                microns_per_volt: 1.0,
            }
        ];
        let measurements = [
            Measurement::Phase{
                line: Line::new_from_two_points(
                    &Vec3D::new(1.0,1.0,0.0),
                    &Vec3D::new(1.0,-1.0,2000.0),
                )
            }
        ];
        let imat = IMat::new(&measurements, &actuators);
        assert_abs_diff_eq!(imat.eval(0,0), 0.5);
    }

    #[test]
    fn simple_symmetric() {
        let actuators = [
            Actuator::Gaussian{
                sigma: coupling_to_sigma(0.5,1.0),
                position: Vec3D::new(0.0, 0.0, 1000.0),
                microns_per_volt: 1.0,
            },
            Actuator::Gaussian{
                sigma: coupling_to_sigma(0.5,1.0),
                position: Vec3D::new(1.0, 0.0, 1000.0),
                microns_per_volt: 1.0,
            },
        ];
        let measurements = [
            Measurement::Phase{
                line: Line::new_on_axis(0.0, 0.0),
            },
            Measurement::Phase{
                line: Line::new(1.0, 0.0, 0.0, 0.0),
            }
        ];
        let imat = IMat::new(&measurements, &actuators);
        assert!(imat.eval(0,0)>0.0);
        assert_abs_diff_eq!(imat.eval(0,0),imat.eval(1,1));
        assert_abs_diff_eq!(imat.eval(1,0),imat.eval(0,1));
    }

    #[test]
    fn slope_twopoint() {
        let actuators = [
            Actuator::Gaussian{
                sigma: coupling_to_sigma(0.5,1.0),
                position: Vec3D::origin(),
                microns_per_volt: 1.0,
            }
        ];
        let line = Line::new(1.0, 0.0, 0.0, 0.0);
        let measurements = [
            Measurement::SlopeTwoLine{
                line_neg: &line+Vec2D::new(-1e-5, 0.0),
                line_pos: &line+Vec2D::new(1e-5, 0.0)
            },
            Measurement::SlopeTwoLine{
                line_neg: &line+Vec2D::new(-1e-6, 0.0),
                line_pos: &line+Vec2D::new(1e-6, 0.0)
            },
            Measurement::Phase{
                line: Line::new(1.0+1e-7, 0.0, 0.0, 0.0),
            },
            Measurement::Phase{
                line: Line::new(1.0-1e-7, 0.0, 0.0, 0.0),
            },
        ];
        let imat = IMat::new(&measurements, &actuators);
        assert_abs_diff_eq!(imat.eval(0,0),imat.eval(1,0),epsilon=1e-8);
        assert_abs_diff_eq!(
            (imat.eval(2,0)-imat.eval(3,0))/2e-7,
            imat.eval(1,0),
            epsilon=1e-8
        );
    }


    #[test]
    fn slope_twoedge() {
        let actuators = [
            Actuator::Gaussian{
                sigma: coupling_to_sigma(0.5,1.0),
                position: Vec3D::origin(),
                microns_per_volt: 1.0,
            }
        ];

        let line = Line::new(1.0, 0.0, 0.0, 0.0);
        let measurements = [
            Measurement::SlopeTwoLine{
                line_neg: &line+Vec2D::new(-1e-2, 0.0),
                line_pos: &line+Vec2D::new(1e-2, 0.0),
            },
            Measurement::SlopeTwoEdge{
                central_line: line,
                edge_length: 0.0,
                edge_separation: 2e-2,
                gradient_axis: Vec2D::x_unit(),
                npoints: 100,
                altitude: f64::INFINITY,
            }
        ];
        let imat = IMat::new(&measurements, &actuators);
        assert_abs_diff_eq!(imat.eval(0,0),imat.eval(1,0),epsilon=1e-10);
    }

    #[test]
    fn slope_tt_twopoint() {
        let actuators = [
            Actuator::TipTilt{
                unit_response: Vec2D::x_unit()
            }
        ];
        let line = Line::new(1.0, 0.0, 0.0, 0.0);
        let measurements = [
            Measurement::SlopeTwoLine{
                line_neg: &line+Vec2D::new(-1e-6, 0.0),
                line_pos: &line+Vec2D::new(1e-6, 0.0)
            },
            Measurement::SlopeTwoLine{
                line_neg: &line+Vec2D::new(-1e-7, 0.0),
                line_pos: &line+Vec2D::new(1e-7, 0.0)
            },
        ];
        let imat = IMat::new(&measurements, &actuators);
        assert_abs_diff_eq!(imat.eval(0,0),imat.eval(1,0),epsilon=1e-8);
    }

    #[test]
    fn tt_on_axis_phase() {
        let actuators = [
            Actuator::TipTilt{
                unit_response: Vec2D::x_unit()
            }
        ];
        let measurements = [
            Measurement::Phase {
                line: Line::new_on_axis(0.0, 0.0)
            }
        ];
        let imat = IMat::new(&measurements, &actuators);
        assert_abs_diff_eq!(imat.eval(0,0), 0.0, epsilon = f64::EPSILON);
    }

    #[test]
    fn tt_off_axis_phase() {
        let actuators = [
            Actuator::TipTilt{
                unit_response: Vec2D::x_unit()
            }
        ];
        let measurements = [
            Measurement::Phase{
                line: Line::new(2.0, 0.0, 0.0, 0.0)
            }
        ];
        let imat = IMat::new(&measurements, &actuators);
        assert_abs_diff_eq!(imat.eval(0,0), 2.0, epsilon = f64::EPSILON);
    }

    #[test]
    fn test_vkcov() {
        let vk = VonKarmanLayer {
            r0: 0.1,
            l0: 25.0,
            alt: 1000.0,
            v: Vec2D { x: 10.0, y: 0.0 }
        };
        let line = Line::new_on_axis(0.0,0.0);
        let a = vk.cosample(&line, &line, 0.0);
        assert_abs_diff_eq!(a,utils::vk_cov(0.0, vk.r0, vk.l0));
        assert_abs_diff_eq!(a,856.346_613_137_351_7,epsilon=1e-3);
        let a = vk.cosample(&line, &line, 1.0);
        assert_abs_diff_ne!(a,856.346_613_137_351_7,epsilon=1e-3);
    }

    #[test]
    fn test_vkcovmat() {
        let vk = VonKarmanLayer {
            r0: 0.1,
            l0: 25.0,
            alt: 1000.0,
            v: Vec2D { x: 10.0, y: 0.0 }
        };
        let measurements: Vec<Measurement> = (0..10)
        .map(|i| f64::from(i) * 0.8)
        .map(|x|
            Measurement::Phase{
                line: Line::new_on_axis(x, 0.0)
            }
        ).collect();
        let covmat = CovMat::new(&measurements, &measurements, &vk, 1e-3);
        println!("{covmat}");
    }
    
    #[test]
    fn test_mixedvkcovmat() {
        let vk = VonKarmanLayer {
            r0: 0.1,
            l0: 25.0,
            alt: 1000.0,
            v: Vec2D{ x: 10.0, y: 0.0 }
        };
        let line = Line::new_on_axis(0.0, 0.0);
        let measurements = [
            Measurement::Phase {
                line: line.clone(),
            },
            Measurement::SlopeTwoLine {
                line_pos: &line+Vec2D::new(0.1,0.0),
                line_neg: &line+Vec2D::new(-0.1,0.0),
            },
            Measurement::SlopeTwoEdge {
                central_line: line.clone(),
                edge_separation: 0.2,
                edge_length: 0.2,
                gradient_axis: Vec2D::x_unit(),
                npoints: 10,
                altitude: f64::INFINITY,
            },
        ];
        let covmat = CovMat::new(&measurements, &measurements, &vk, 1e-3);
        println!("{covmat}");
    }

    #[test]
    fn make_pupil() {
        let pup = [Pupil{
            rad_outer: 4.0,
            rad_inner: 0.5,
            spider_thickness: 0.1,
            spiders: vec![
                (Vec2D::new(-4.0,-4.0),Vec2D::new(3.0,0.0)),
                (Vec2D::new(-4.0,-4.0),Vec2D::new(3.0,0.0)),
                (Vec2D::new(-4.0,-4.0),Vec2D::new(3.0,0.0)),
                (Vec2D::new(-4.0,-4.0),Vec2D::new(3.0,0.0)),
                ],
        }];
        const NPOINTS: u32 = 1000;
        let x = Vec2D::linspread(
            &Vec2D::new(-4.0, 0.0),
            &Vec2D::new(4.0, 0.0),
            NPOINTS,
        );
        let y = Vec2D::linspread(
            &Vec2D::new(0.0, -4.0),
            &Vec2D::new(0.0, 4.0),
            NPOINTS,
        );
        println!("{y:?}");
        let p: Vec<Measurement> = y.iter().flat_map(|y|
            x.iter().map(|x| Measurement::Phase{
                line:Line::new(x.x, 0.0, y.y, 0.0)
            })).collect();
        let pup_vec = IMat::new(&p, &pup);
        let shape = [NPOINTS as usize, NPOINTS as usize];
        let data: Vec<f64> = pup_vec.flattened_array();
        let primary_hdu = fitrs::Hdu::new(&shape, data);
        fitrs::Fits::create("/tmp/pup.fits", primary_hdu)
        .expect("Failed to create");
    }
}