use std::fs;
use astroimsim_geometry::coordinate_system::CoordinateSystem;
use astroimsim_geometry::grid2d::{Corners, GRID2D};
use astroimsim_geometry::points::Point;
use crate::psf::{DataFile, PSF, Load};
#[derive(Debug)]
pub struct PsfGrid {
label:&'static str,
grid: GRID2D,
data:Vec<(usize,PSF)>,
valid: bool,
directory_path:&'static str,
center_fits_keys:(&'static str, &'static str),
}
impl PsfGrid{
pub fn new(label:&'static str,grid: GRID2D,directory_path:&'static str,center_fits_keys:(&'static str, &'static str)) -> PsfGrid{
PsfGrid{
label,
data: vec![],
grid: grid,
valid:false,
directory_path,
center_fits_keys,
}
}
pub fn load_data_frames(&mut self, x_num:usize,y_num:usize){
println!("Loading data frames into grid. This overwrites any data previously loaded");
let mut data = vec![];
let paths = fs::read_dir(self.directory_path).unwrap();
let mut counter = 0;
for path in paths {
println!("loading {:?}",path);
counter += 1;
let path = path.unwrap().path();
let frame = PSF::load_file(
path,
(Load::FromKey(self.center_fits_keys.0.to_string()), Load::FromKey(self.center_fits_keys.1.to_string())),
(Load::FromValue(self.grid.x_size),Load::FromValue(self.grid.y_size)),
x_num,y_num,
);
let frame_index = frame.snap_to_grid(&self.grid);
data.push((frame_index,frame))
}
data.sort_by_key(|x|x.0);
self.data = data;
println!("Loaded {counter} files into a Grid Struct from {:?}",self.directory_path);
assert_eq!(counter,self.grid.num_points,"Loaded the wrong number of PSF files");
}
pub fn validate(&mut self) -> bool {
if self.data.len() != self.grid.num_points{
println!("Validation failed: expected {:?} data frames, have {:?}",self.grid.num_points,self.data.len());
self.valid = false;
return false
};
let mut missing = Vec::new();
let mut counter = 0;
for grid_number in 0..self.grid.num_points{
counter += 1;
if !self.data.iter().any(|(index,_)| *index==grid_number) {
missing.push(grid_number)
}
};
if missing.len() > 0{
println!("Validation failed! Missing data frames for the following grid positions: {:?} ",missing);
self.valid = false;
false
}else{
self.data.sort_by_key(|x|x.0);
self.valid = true;
true
}}
pub fn interpolated_psf(&self, point:&Point) -> Vec<Vec<f32>>{
let interpolation_data = self.grid.interpolation_coefficients(&point.clone());
vec![vec![]]
}
pub fn grid_psf(&self, index:usize)-> Vec<Vec<f32>>{
let (i, psf) = self.data[index].clone();
assert_eq!(index,i);
psf.data
}
}