astroimsim_data/
psf_grid.rs1use std::fs;
2use astroimsim_geometry::coordinate_system::CoordinateSystem;
3use astroimsim_geometry::grid2d::{Corners, GRID2D};
4use astroimsim_geometry::points::Point;
5use crate::psf::{DataFile, PSF, Load};
6
7#[derive(Debug)]
8pub struct PsfGrid {
9 label:&'static str,
10 grid: GRID2D,
11 data:Vec<(usize,PSF)>,
12 valid: bool,
13 directory_path:&'static str,
14 center_fits_keys:(&'static str, &'static str),
15
16}
17
18impl PsfGrid{
19 pub fn new(label:&'static str,grid: GRID2D,directory_path:&'static str,center_fits_keys:(&'static str, &'static str)) -> PsfGrid{
20 PsfGrid{
21 label,
22 data: vec![],
23 grid: grid,
24 valid:false,
25 directory_path,
26 center_fits_keys,
27 }
28 }
29 pub fn load_data_frames(&mut self, x_num:usize,y_num:usize){
30 println!("Loading data frames into grid. This overwrites any data previously loaded");
31 let mut data = vec![];
32 let paths = fs::read_dir(self.directory_path).unwrap();
33 let mut counter = 0;
34 for path in paths {
35 println!("loading {:?}",path);
36 counter += 1;
37 let path = path.unwrap().path();
38
39 let frame = PSF::load_file(
40 path,
41 (Load::FromKey(self.center_fits_keys.0.to_string()), Load::FromKey(self.center_fits_keys.1.to_string())),
42 (Load::FromValue(self.grid.x_size),Load::FromValue(self.grid.y_size)),
43 x_num,y_num,
44 );
45 let frame_index = frame.snap_to_grid(&self.grid);
46 data.push((frame_index,frame))
47 }
48 data.sort_by_key(|x|x.0);
49 self.data = data;
50 println!("Loaded {counter} files into a Grid Struct from {:?}",self.directory_path);
51 assert_eq!(counter,self.grid.num_points,"Loaded the wrong number of PSF files");
52 }
53
54
55
56 pub fn validate(&mut self) -> bool {
57 if self.data.len() != self.grid.num_points{
59 println!("Validation failed: expected {:?} data frames, have {:?}",self.grid.num_points,self.data.len());
60 self.valid = false;
61 return false
62 };
63 let mut missing = Vec::new();
65 let mut counter = 0;
66 for grid_number in 0..self.grid.num_points{
67 counter += 1;
68 if !self.data.iter().any(|(index,_)| *index==grid_number) {
69 missing.push(grid_number)
70 }
71 };
72 if missing.len() > 0{
73 println!("Validation failed! Missing data frames for the following grid positions: {:?} ",missing);
74 self.valid = false;
75 false
76 }else{
77 self.data.sort_by_key(|x|x.0);
78 self.valid = true;
79 true
80 }}
81
82
83 pub fn interpolated_psf(&self, point:&Point) -> Vec<Vec<f32>>{
84 let interpolation_data = self.grid.interpolation_coefficients(&point.clone());
90
91 vec![vec![]]
117
118
119
120 }
121
122
123
124
125 pub fn grid_psf(&self, index:usize)-> Vec<Vec<f32>>{
126 let (i, psf) = self.data[index].clone();
127 assert_eq!(index,i);
128 psf.data
129
130 }
131
132}
133
134
135