Skip to main content

astroimsim_data/
detector.rs

1use std::time::Instant;
2use astroimsim_geometry::grid2d::GRID2D;
3use astroimsim_geometry::points::Point;
4use astroimsim_geometry::coordinate_system::{CoordinateSystem, Coordinates};
5use uvex_fitrs::{Fits, Hdu};
6use crate::point_sources::FullSpectrumSourceList;
7use crate::psf_grid::PsfGrid;
8use std::fs;
9use crate::spatial_effect::SpatialEffect;
10
11pub enum EffectType{
12    Exposure(f64),
13    Once
14}
15
16#[derive(Clone,Debug)]
17pub struct Detector {
18    pub label: String,
19    pub grid: GRID2D,
20    pub data: Vec<Vec<[f64;2]>>,
21}
22
23impl Detector {
24
25
26    pub fn new(label:String, grid: GRID2D)->Detector{
27        Detector{
28            label,
29            grid,
30            data:vec![vec![]]
31        }
32
33    }
34
35    pub fn write(&mut self,label:usize){
36
37
38        let width = self.data[0].len();
39        let height = self.data.len();
40        let shape = [width, height];
41
42        let mut fuv_counts_path = "/Users/mayabasu/Desktop/Output/fuv/testtt".to_string();
43            fuv_counts_path.push_str(label.to_string().as_str());
44        fuv_counts_path.push_str(".fits");
45
46        let mut nuv_counts_path = "/Users/mayabasu/Desktop/Output/nuv/testtt".to_string();
47            nuv_counts_path.push_str(label.to_string().as_str());
48        nuv_counts_path.push_str(".fits");
49
50        /*
51
52        let mut fuv_ave_path = "/Users/mayabasu/Desktop/Output/fuv/counts_".to_string();
53            fuv_ave_path.push_str(label.to_string().as_str());
54            fuv_ave_path.push_str(".fits");
55
56
57        let mut nuv_ave_path = "/Users/mayabasu/Desktop/Output/nuv/counts".to_string();
58            nuv_ave_path.push_str(label.to_string().as_str());
59                nuv_ave_path.push_str(".fits");
60
61
62
63
64        let fuv_ave:Vec<f64> = self.data.iter().flatten().map(|a|a[0]).collect();
65        let nuv_ave:Vec<f64> = self.data.iter().flatten().map(|a|a[1]).collect();
66
67         */
68
69
70        let fuv_counts:Vec<f64> = self.data.iter().flatten().map(|a|a[0]).collect();
71        let nuv_counts:Vec<f64> = self.data.iter().flatten().map(|a|a[1]).collect();
72
73
74
75
76        /*
77        let mut fuv_primary_hdu = Hdu::new(&shape, fuv_ave);
78        let mut nuv_primary_hdu = Hdu::new(&shape, nuv_ave);
79
80         */
81
82        let mut fuv_counts_primary_hdu = Hdu::new(&shape, fuv_counts);
83        let mut nuv_counts_primary_hdu = Hdu::new(&shape, nuv_counts);
84
85
86        // Insert values in header later
87        //primary_hdu.insert("KEYSTR", "My string");
88        /*
89        println!("{:?}",fuv_ave_path);
90        Fits::create(fuv_ave_path, fuv_primary_hdu).expect("Failed to create");
91        Fits::create(nuv_ave_path, nuv_primary_hdu).expect("Failed to create");
92
93         */
94        Fits::create(fuv_counts_path, fuv_counts_primary_hdu).expect("Failed to create");
95        Fits::create(nuv_counts_path, nuv_counts_primary_hdu).expect("Failed to create");
96
97
98
99
100
101    }
102
103    pub fn show(path:&str){
104        //Users/mayabasu/Desktop/Output/fuv/1.fits
105    }
106
107    pub fn create_constant_background(&mut self, fuv_background_brightness:f64,nuv_background_brightness:f64){
108        let mut data = Vec::with_capacity(self.grid.y_num);
109        for row in 0..self.grid.y_num{
110            let mut row_vec = Vec::with_capacity(self.grid.x_num);
111            for column in 0..self.grid.x_num{
112                row_vec.push([fuv_background_brightness,nuv_background_brightness])
113            }
114            data.push(row_vec);
115        }
116        self.data= data;
117    }
118    pub fn multiply_effect(&mut self, effect:SpatialEffect,index:usize, effect_type: EffectType){
119        assert_eq!(effect.grid.num_points, self.grid.num_points, "Grids are not equal");
120        for row in 0..self.grid.y_num{
121            for column in 0..self.grid.x_num{
122                match effect_type{
123                    EffectType::Exposure(time) => {self.data[column][row][index] = self.data[column][row][index]*effect.data[column][row]*time;}
124                    EffectType::Once => {self.data[column][row][index] = self.data[column][row][index]*effect.data[column][row];}
125                }
126
127            }
128        }
129    }
130
131    pub fn add_effect(&mut self, effect:SpatialEffect,index:usize,effect_type: EffectType){
132        assert_eq!(effect.grid.num_points, self.grid.num_points, "Grids are not equal");
133        for row in 0..self.grid.y_num{
134            for column in 0..self.grid.x_num{
135                match effect_type{
136                    EffectType::Exposure(time) => {self.data[column][row][index] = self.data[column][row][index]+effect.data[column][row]*time;}
137                    EffectType::Once => {self.data[column][row][index] = self.data[column][row][index]+effect.data[column][row];}
138                }
139
140            }
141        }
142    }
143
144}
145
146pub struct DetectorArray{
147    pub label: String,
148    pub detectors: Vec<Detector>,
149    pub coordinate_system: CoordinateSystem
150}
151
152
153
154
155