astrors/io/hdus/image/
imagehdu.rs

1use std::fs::File;
2use std::io::Result;
3
4use crate::io::Header;
5use crate::io::hdus::image::ImageData;
6
7use crate::io::hdus::image::image::ImageParser;
8
9
10const MANDATORY_KEYWORDS: [&str; 3] = [
11    "XTENSION",
12    "BITPIX",
13    "NAXIS",
14];
15
16pub struct ImageHDU{
17    pub header: Header,
18    pub data: ImageData,
19}
20
21/// Represents an Image Header Data Unit (HDU) in a FITS file.
22///
23/// This struct encapsulates the header and image data of a FITS file.
24/// It provides methods to read from and write to FITS files while ensuring
25/// compliance with the FITS standard.
26impl ImageHDU {
27    /// Creates a new `ImageHDU` instance with the provided header and data.
28    ///
29    /// # Parameters:
30    /// - `header`: The header containing metadata for the image.
31    /// - `data`: The image data associated with the header.
32    ///
33    /// # Returns:
34    /// - A new `ImageHDU` instance.
35    pub fn new(header: Header, data: ImageData) -> Self {
36        Self {
37            header,
38            data,
39        }
40    }
41
42    /// Reads an Image HDU from a file.
43    ///
44    /// This function reads the header and data sections of a FITS file and creates
45    /// an `ImageHDU` instance. It ensures that the mandatory keywords appear in
46    /// the correct order at the start of the header.
47    ///
48    /// # Parameters:
49    /// - `f`: A mutable reference to a file object.
50    ///
51    /// # Returns:
52    /// - `Ok(Self)`: An `ImageHDU` instance containing the header and data.
53    /// - `Err`: If reading from the file fails or the header is corrupted.
54    pub fn read_from_file(f: &mut File) -> Result<Self>  {
55        //TODO: Check for mandatory words
56
57        let mut header = Header::new();
58        header.read_from_file(f)?;
59        
60        if !header.are_mandatory_keywords_first(&MANDATORY_KEYWORDS) {
61            // TODO: Return a proper error
62            // Err(std::io::Error::new(std::io::ErrorKind::Other, "Header corrupted"));
63            panic!("Header corrupted");
64        }
65
66        let data: ImageData = ImageParser::read_from_buffer(f, &mut header)?;
67        Ok(Self::new(header, data))
68    }
69
70    /// Writes the Image HDU to a file.
71    ///
72    /// This function writes the header and image data to a file in the FITS format.
73    /// It ensures that the mandatory keywords are in the correct order and updates
74    /// the header with the correct `NAXISn` keywords based on the image shape.
75    ///
76    /// # Parameters:
77    /// - `f`: A mutable reference to a file object.
78    ///
79    /// # Returns:
80    /// - `Ok(())`: If the HDU is successfully written to the file.
81    /// - `Err`: If writing to the file fails.
82    pub fn write_to_file(&mut self, mut f: &mut File) -> Result<()> {
83        //TODO: This function should not repeat here and in primary hdu
84        self.header.fix_header_w_mandatory_order(&MANDATORY_KEYWORDS);
85
86        //Check for shape of self.data and write NAXISn keywords
87        ImageParser::write_image_header(&mut self.header, &self.data);
88
89        self.header.write_to_buffer(&mut f)?;
90        ImageParser::ndarray_to_buffer(&self.data, f)?;
91
92        Ok(())
93    }
94}