1use std::iter::Flatten;
2use std::path::PathBuf;
3use std::slice::Iter;
4use serde::{Deserialize, Serialize};
5use uvex_fitrs::{Fits, FitsData, FitsDataArray, Hdu, HeaderValue};
6use astroimsim_geometry::coordinate_system::{CoordinateSystem, Coordinates};
7use astroimsim_geometry::grid2d::GRID2D;
8use astroimsim_geometry::points::Point;
9use crate::point_source::{PointSource, SourceList};
10
11#[derive(Debug,Clone,Serialize)]
12pub struct PSF {
13 pub path: PathBuf,
14 pub data: Vec<Vec<f32>>,
15 pub x_pixels: usize,
16 pub y_pixels: usize,
17 pub center: Point,
18 pub size: (f64,f64),
19}
20
21pub struct DataFile {
22 pub description: String,
23 pub path: PathBuf,
24 pub x_pixels: usize,
25 pub y_pixels: usize,
26}
27
28
29pub enum Load{
30 FromKey(String),
31 FromValue(f64)
32}
33
34impl PSF {
35 pub fn load_file(file:DataFile,center:(Load,Load),size:(Load,Load)) -> PSF {
36 println!("Loading {:?} into a DataFrame from {:?}",file.description,file.path);
37 let fits = Fits::open(file.path.clone()).expect("Failed to open FITS file");
38 let primary_hdu= fits.iter().next().expect("Couldn't find primary HDU");
39 let (mut data,shape) = match primary_hdu.read_data() {
40 FitsData::FloatingPoint32(FitsDataArray { shape, data }) => (data,shape),
41 _ => panic!("Could not unpack PSF data")
42 }; assert_eq!(shape[0], file.x_pixels,"Diva down! Tried to load a file with data of the wrong x size"); assert_eq!(shape[1], file.y_pixels,"Diva down! Tried to load a file with data of the wrong y size");
46
47
48 let center_x:f64 = PSF::load(center.0, &primary_hdu);
49 let center_y:f64 = PSF::load(center.1, &primary_hdu);
50 let size_x:f64 = PSF::load(size.0, &primary_hdu);
51 let size_y:f64 = PSF::load(size.1, &primary_hdu);
52 let data = data.chunks(file.x_pixels).map(|i| i.to_vec()).collect();
53 PSF {
54 path: file.path,
55 data,
56 x_pixels: file.x_pixels,
57 y_pixels: file.y_pixels,
58 center: Point::new(center_x,center_y,Coordinates::ABSOLUTE),
59 size: (size_x,size_y),
60 }
61 }
62 pub fn snap_to_grid(&self, grid: &GRID2D) -> usize{
63 let index = grid.snap(self.center.clone());
64 index
65 }
66
67
68 pub fn repack_data(flat_data: Vec<f32>) -> Vec<Vec<f32>>{
69 flat_data.chunks(64).map(|i| i.to_vec()).collect()
70 }
71
72 fn load(thingy:Load, header:&Hdu)-> f64{
73 match thingy{
74 Load::FromKey(Key) => {
75 match header.value(&Key).expect("failed to get key") {
76 HeaderValue::RealFloatingNumber(value)=> *value,
77 _ => panic!("could not unpack FITS header value")
78 }}
79 Load::FromValue(value) => value
80 }
81 }
82 }
130
131
132
133