Skip to main content

astroimsim_data/
psf.rs

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 astroimsim_geometry::grid2d::InterpolationData;
10//use crate::point_source::{PointSource, SourceList};
11
12#[derive(Debug,Clone,Serialize)]
13pub struct PSF {
14    pub path: PathBuf,
15    pub data: Vec<Vec<f32>>,
16    pub x_pixels: usize,
17    pub y_pixels: usize,
18    pub center: Point,
19    pub size: (f64,f64),
20}
21
22pub struct DataFile {
23    pub description: String,
24    pub path: PathBuf,
25    pub x_pixels: usize,
26    pub y_pixels: usize,
27}
28
29
30pub enum Load{
31    FromKey(String),
32    FromValue(f64)
33}
34
35
36pub enum FITSType{
37    thirtytwo,
38    sixtyfour,
39}
40
41impl PSF {
42    pub fn load_file(file: PathBuf, center:(Load, Load), size:(Load, Load), x_num:usize, y_num:usize) -> PSF {
43        println!("Loading {:?} into a DataFrame ",file);
44        let fits = Fits::open(file.clone()).expect("Failed to open FITS file");
45        let primary_hdu= fits.iter().next().expect("Couldn't find primary HDU");
46        let (mut data,shape) = match primary_hdu.read_data() {
47            FitsData::FloatingPoint32(FitsDataArray { shape, data }) => (data,shape),
48            _ => panic!("Could not unpack PSF data")
49        }; //TODO add support for f64 etc
50
51        let normalization:f32 = data.iter().sum();
52        let data:Vec<f32> = data.iter().map(|x|x/normalization).collect();
53        println!("PSF has been normalized to {:?}",data.iter().sum::<f32>());
54
55        assert_eq!(shape[0], x_num,"Diva down! Tried to load a file with data of the wrong x size"); //check that the data is the expected size
56        assert_eq!(shape[1], y_num,"Diva down! Tried to load a file with data of the wrong y size");
57
58
59        let center_x:f64 = PSF::load(center.0, &primary_hdu);
60        let center_y:f64 = PSF::load(center.1, &primary_hdu);
61        let size_x:f64 = PSF::load(size.0, &primary_hdu);
62        let size_y:f64 = PSF::load(size.1, &primary_hdu);
63        let data = data.chunks(x_num).map(|i| i.to_vec()).collect();
64      //  println!("{:?}",data);
65        PSF {
66            path: file,
67            data,
68            x_pixels: x_num,
69            y_pixels: y_num,
70            center: Point::new(center_x,center_y,Coordinates::ABSOLUTE),
71            size: (size_x,size_y),
72        }
73    }
74    pub fn snap_to_grid(&self, grid: &GRID2D) -> usize{
75        let index = grid.snap(self.center.clone());
76        index
77    }
78
79
80    pub fn repack_data(flat_data: Vec<f32>) -> Vec<Vec<f32>>{
81        flat_data.chunks(64).map(|i| i.to_vec()).collect()
82    }
83
84    fn load(thingy:Load, header:&Hdu)-> f64{
85        match thingy{
86            Load::FromKey(Key) => {
87                match header.value(&Key).expect("failed to get key") {
88                    HeaderValue::RealFloatingNumber(value)=> *value,
89                    _ => panic!("could not unpack FITS header value")
90                }}
91            Load::FromValue(value) => value
92        }
93    }
94
95}
96
97