Skip to content

Commit

Permalink
!PATCHED! testing fitsio file_path api
Browse files Browse the repository at this point in the history
  • Loading branch information
d3v-null committed Oct 31, 2024
1 parent d4981b5 commit debf3a1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ marlu = { git = "https://github.com/MWATelescope/Marlu", branch = "mwalib-1.7.1"
# mwa_hyperbeam = { path = "../mwa_hyperbeam" }
mwa_hyperbeam = { git = "https://github.com/MWATelescope/mwa_hyperbeam", branch = "mwalib-1.7.1" }

fitsio = { git = "https://github.com/simonrw/rust-fitsio.git", branch = "feat/filename-method" }
fitsio-sys = { git = "https://github.com/simonrw/rust-fitsio.git", branch = "feat/filename-method" }

# commands to maintain msrv 1.64 after cargo update:
# cargo update -p plotters --precise 0.3.6
# cargo update -p predicates --precise 3.0.3
Expand Down
29 changes: 15 additions & 14 deletions src/io/read/fits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
//! Helper functions for reading FITS files.
mod error;
#[cfg(test)]
mod tests;

pub(crate) use error::FitsError;

use std::{
ffi::{CStr, CString},
fmt::Display,
os::raw::c_char,
path::Path,
ptr,
};

Expand Down Expand Up @@ -43,7 +44,7 @@ pub(crate) fn fits_open_hdu<T: DescribesHdu + Display + Copy>(
let caller = std::panic::Location::caller();
FitsError::Fitsio {
fits_error: Box::new(e),
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_description: format!("{hdu_description}").into_boxed_str(),
source_file: caller.file(),
source_line: caller.line(),
Expand All @@ -70,7 +71,7 @@ pub(crate) fn fits_get_optional_key<T: std::str::FromStr>(
let caller = std::panic::Location::caller();
return Err(FitsError::Fitsio {
fits_error: Box::new(e),
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_description: format!("{}", hdu.number + 1).into_boxed_str(),
source_file: caller.file(),
source_line: caller.line(),
Expand All @@ -82,7 +83,7 @@ pub(crate) fn fits_get_optional_key<T: std::str::FromStr>(
let caller = std::panic::Location::caller();
return Err(FitsError::Fitsio {
fits_error: Box::new(e),
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_description: format!("{}", hdu.number + 1).into_boxed_str(),
source_file: caller.file(),
source_line: caller.line(),
Expand All @@ -98,7 +99,7 @@ pub(crate) fn fits_get_optional_key<T: std::str::FromStr>(
let caller = std::panic::Location::caller();
Err(FitsError::Parse {
key: keyword.to_string().into_boxed_str(),
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_num: hdu.number + 1,
source_file: caller.file(),
source_line: caller.line(),
Expand All @@ -122,7 +123,7 @@ pub(crate) fn fits_get_required_key<T: std::str::FromStr>(
let caller = std::panic::Location::caller();
Err(FitsError::MissingKey {
key: keyword.to_string().into_boxed_str(),
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_num: hdu.number + 1,
source_file: caller.file(),
source_line: caller.line(),
Expand All @@ -144,7 +145,7 @@ pub(crate) fn fits_get_col<T: fitsio::tables::ReadsCol>(
let caller = std::panic::Location::caller();
FitsError::Fitsio {
fits_error: Box::new(e),
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_description: format!("{}", hdu.number + 1).into_boxed_str(),
source_file: caller.file(),
source_line: caller.line(),
Expand Down Expand Up @@ -195,7 +196,7 @@ pub(crate) fn fits_get_optional_key_long_string(
let caller = std::panic::Location::caller();
return Err(FitsError::LongString {
key: keyword.to_string().into_boxed_str(),
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_num: hdu.number + 1,
source_file: caller.file(),
source_line: caller.line(),
Expand Down Expand Up @@ -223,7 +224,7 @@ pub(crate) fn _fits_get_required_key_long_string(
let caller = std::panic::Location::caller();
Err(FitsError::MissingKey {
key: keyword.to_string().into_boxed_str(),
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_num: hdu.number + 1,
source_file: caller.file(),
source_line: caller.line(),
Expand All @@ -237,15 +238,15 @@ pub(crate) fn _fits_get_required_key_long_string(
/// Get the size of the image on the supplied FITS file pointer and HDU.
#[track_caller]
pub(crate) fn _fits_get_image_size<'a>(
_fits_fptr: &FitsFile,
fits_fptr: &FitsFile,
hdu: &'a FitsHdu,
) -> Result<&'a Vec<usize>, FitsError> {
match &hdu.info {
HduInfo::ImageInfo { shape, .. } => Ok(shape),
_ => {
let caller = std::panic::Location::caller();
Err(FitsError::NotImage {
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_num: hdu.number + 1,
source_file: caller.file(),
source_line: caller.line(),
Expand All @@ -266,7 +267,7 @@ pub(crate) fn fits_get_image<T: fitsio::images::ReadImage>(
let caller = std::panic::Location::caller();
FitsError::Fitsio {
fits_error: Box::new(e),
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_description: format!("{}", hdu.number + 1).into_boxed_str(),
source_file: caller.file(),
source_line: caller.line(),
Expand All @@ -276,7 +277,7 @@ pub(crate) fn fits_get_image<T: fitsio::images::ReadImage>(
_ => {
let caller = std::panic::Location::caller();
Err(FitsError::NotImage {
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_num: hdu.number + 1,
source_file: caller.file(),
source_line: caller.line(),
Expand Down Expand Up @@ -319,7 +320,7 @@ pub(crate) fn _fits_get_float_image_into_buffer(
let caller = std::panic::Location::caller();
return Err(FitsError::Fitsio {
fits_error: Box::new(e),
fits_filename: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
fits_filename: fits_fptr.file_path().into(),
hdu_description: format!("{}", hdu.number + 1).into_boxed_str(),
source_file: caller.file(),
source_line: caller.line(),
Expand Down
26 changes: 26 additions & 0 deletions src/io/read/fits/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use fitsio::FitsFile;

use crate::io::read::fits::{fits_get_required_key, FitsError};

// check that the filename and key appear in the error struct
#[test]
fn test_fits_get_required_key() {
let output = tempfile::NamedTempFile::new().unwrap();

// create a fits file with only header A
{
let mut fitsfile = FitsFile::create(output.path()).overwrite().open().unwrap();
let hdu = fitsfile.primary_hdu().unwrap();
hdu.write_key(&mut fitsfile, "A", "1").unwrap();
}

let mut fitsfile = FitsFile::edit(output.path()).unwrap();
let hdu = fitsfile.primary_hdu().unwrap();

// read header B from the fits file (doesn't exist)
let missing: Result<String, FitsError> = fits_get_required_key(&mut fitsfile, &hdu, "B");

assert!(
matches!(missing, Err(FitsError::MissingKey { key, fits_filename, .. }) if key == Box::from("B") && fits_filename == Box::from(output.path()))
);
}
4 changes: 2 additions & 2 deletions src/io/read/uvfits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ impl UvfitsReader {
let unflagged_timesteps = all_timesteps.clone();
let all_timesteps =
Vec1::try_from_vec(all_timesteps).map_err(|_| UvfitsReadError::NoTimesteps {
file: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
file: uvfits_fptr.file_path().to_path_buf(),
})?;
let timestamps =
Vec1::try_from_vec(timestamps).map_err(|_| UvfitsReadError::NoTimesteps {
file: Path::new("<private>").into(), // https://github.com/simonrw/rust-fitsio/issues/366
file: uvfits_fptr.file_path().to_path_buf(),
})?;

// Get the data's time resolution. There is a possibility that the file
Expand Down

0 comments on commit debf3a1

Please sign in to comment.