use crate::types::ReceiverType;
use crate::{
pretty_print_vec, read_cell_array_f64, read_cell_string, read_cell_value, FitsError,
MAX_RECEIVER_CHANNELS,
};
use std::fmt;
use fitsio::hdu::{FitsHdu, HduInfo};
use fitsio::FitsFile;
#[cfg(any(feature = "python", feature = "python-stubgen"))]
use pyo3::prelude::*;
#[cfg(feature = "python-stubgen")]
use pyo3_stub_gen_derive::gen_stub_pyclass;
pub mod ffi;
#[cfg(test)]
pub(crate) mod ffi_test;
#[cfg_attr(feature = "python-stubgen", gen_stub_pyclass)]
#[cfg_attr(
any(feature = "python", feature = "python-stubgen"),
pyclass(get_all, set_all, from_py_object)
)]
#[derive(Clone, Debug, PartialEq)]
pub struct SignalChainCorrection {
pub receiver_type: ReceiverType,
pub whitening_filter: bool,
pub corrections: Vec<f64>,
pub num_corrections: usize,
}
impl fmt::Display for SignalChainCorrection {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Receiver Type: {} Whitening filter: {} Corrections: {}",
self.receiver_type,
self.whitening_filter,
pretty_print_vec(
&self
.corrections
.iter()
.map(|c| format!("{:.3}", c))
.collect::<Vec<String>>(),
4
)
)
}
}
pub(crate) fn populate_signal_chain_corrections(
metafits_fptr: &mut FitsFile,
sig_chain_hdu: &FitsHdu,
) -> Result<Vec<SignalChainCorrection>, FitsError> {
let rows = match &sig_chain_hdu.info {
HduInfo::TableInfo {
column_descriptions: _,
num_rows,
} => *num_rows,
_ => 0,
};
let mut sig_chain_vec: Vec<SignalChainCorrection> = Vec::new();
for row in 0..rows {
let whitening_filter: bool =
read_cell_value(metafits_fptr, sig_chain_hdu, "Whitening_Filter", row).unwrap_or(-1)
== 1;
let rx_type_str: String =
read_cell_string(metafits_fptr, sig_chain_hdu, "Receiver_type", row)?;
let receiver_type: ReceiverType = rx_type_str.parse::<ReceiverType>().unwrap();
let corrections: Vec<f64> = read_cell_array_f64(
metafits_fptr,
sig_chain_hdu,
"Corrections",
row,
MAX_RECEIVER_CHANNELS,
)?;
sig_chain_vec.push(SignalChainCorrection {
receiver_type,
whitening_filter,
num_corrections: corrections.len(),
corrections,
});
}
Ok(sig_chain_vec)
}