Skip to content

Commit

Permalink
rebase on master, making the tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatthieu3 committed Jan 30, 2025
1 parent a83bffa commit 34436d2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 103 deletions.
93 changes: 14 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,77 +53,10 @@ It uses code adapted from the famous [CFITSIO](https://github.com/HEASARC/cfitsi
Example
----------

For files that can fit in memory

```rust
use std::fs::File;
use std::io::Cursor;
use std::io::Read;
use fitsrs::{Fits, HDU};
use fitsrs::{hdu::header::Xtension, hdu::data::Data};

let mut f = File::open("samples/fits.gsfc.nasa.gov/EUVE.fits").unwrap();

let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();
let reader = Cursor::new(&buf[..]);

let mut hdu_list = Fits::from_reader(reader);

// Access the HDU extensions
while let Some(Ok(hdu)) = hdu_list.next() {
match hdu {
HDU::Primary(_) => (),
HDU::XImage(hdu) => {
let xtension = hdu.get_header().get_xtension();

let naxis1 = *xtension.get_naxisn(1).unwrap() as usize;
let naxis2 = *xtension.get_naxisn(2).unwrap() as usize;

let num_pixels = naxis2 * naxis1;

match hdu.get_data(&mut hdu_list) {
Data::U8(mem) => assert_eq!(num_pixels, mem.len()),
Data::I16(mem) => assert_eq!(num_pixels, mem.len()),
Data::I32(mem) => assert_eq!(num_pixels, mem.len()),
Data::I64(mem) => assert_eq!(num_pixels, mem.len()),
Data::F32(mem) => assert_eq!(num_pixels, mem.len()),
Data::F64(mem) => assert_eq!(num_pixels, mem.len()),
}
},
HDU::XBinaryTable(hdu) => {
let num_bytes = hdu.get_header()
.get_xtension()
.get_num_bytes_data_block();

match hdu.get_data(&mut hdu_list) {
Data::U8(mem) => assert_eq!(num_bytes as usize, mem.len()),
_ => unreachable!()
}
},
HDU::XASCIITable(hdu) => {
let num_bytes = hdu.get_header()
.get_xtension()
.get_num_bytes_data_block();

match hdu.get_data(&mut hdu_list) {
Data::U8(mem) => assert_eq!(num_bytes as usize, mem.len()),
_ => unreachable!()
}
},
}
}
```

For BufReader

```rust
use std::fs::File;
use std::io::Cursor;
use fitsrs::hdu::HDU;
use fitsrs::hdu::data::DataIter;
use fitsrs::fits::Fits;
use fitsrs::hdu::header::Xtension;
use fitsrs::{Fits, ImageData, HDU, hdu::header::Xtension};

use std::io::{BufReader, Read};

Expand All @@ -144,28 +77,28 @@ while let Some(Ok(hdu)) = hdu_list.next() {

let num_pixels = (naxis2 * naxis1) as usize;

match hdu.get_data(&mut hdu_list) {
DataIter::U8(it) => {
match hdu_list.get_data(hdu) {
ImageData::U8(it) => {
let data = it.collect::<Vec<_>>();
assert_eq!(num_pixels, data.len())
},
DataIter::I16(it) => {
ImageData::I16(it) => {
let data = it.collect::<Vec<_>>();
assert_eq!(num_pixels, data.len())
},
DataIter::I32(it) => {
ImageData::I32(it) => {
let data = it.collect::<Vec<_>>();
assert_eq!(num_pixels, data.len())
},
DataIter::I64(it) => {
ImageData::I64(it) => {
let data = it.collect::<Vec<_>>();
assert_eq!(num_pixels, data.len())
},
DataIter::F32(it) => {
ImageData::F32(it) => {
let data = it.collect::<Vec<_>>();
assert_eq!(num_pixels, data.len())
},
DataIter::F64(it) => {
ImageData::F64(it) => {
let data = it.collect::<Vec<_>>();
assert_eq!(num_pixels, data.len())
},
Expand All @@ -176,17 +109,19 @@ while let Some(Ok(hdu)) = hdu_list.next() {
.get_xtension()
.get_num_bytes_data_block();

let it_bytes = hdu.get_data(&mut hdu_list);
let data = it_bytes.collect::<Vec<_>>();
let data = hdu_list.get_data(hdu)
.collect::<Vec<_>>();

assert_eq!(num_bytes as usize, data.len());
},
HDU::XASCIITable(hdu) => {
let num_bytes = hdu.get_header()
.get_xtension()
.get_num_bytes_data_block();

let it_bytes = hdu.get_data(&mut hdu_list);
let data = it_bytes.collect::<Vec<_>>();
let data = hdu_list.get_data(hdu)
.collect::<Vec<_>>();

assert_eq!(num_bytes as usize, data.len());
},
}
Expand Down
5 changes: 4 additions & 1 deletion src/fits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ where
Err(Error::Io(kind))
// an EOF has been encountered but the number of bytes read is 0
// this is valid since we have terminated the previous HDU
if kind == std::io::ErrorKind::UnexpectedEof && num_bytes_read == 0 => {
if dbg!(kind) == std::io::ErrorKind::UnexpectedEof && num_bytes_read == 0 => {
None
},
Err(e) => Some(Err(e))
Expand Down Expand Up @@ -216,6 +216,7 @@ where
where
R: FitsRead<'a, X> + 'a,
{
/* 1. Parse the header first */
let header = Header::parse(cards)?;
/* 2. Skip the next bytes to a new 2880 multiple of bytes
This is where the data block should start */
Expand All @@ -233,6 +234,8 @@ where
*num_bytes_read += num_off_bytes;
}

// Data block

Ok(Self { header })
}

Expand Down
2 changes: 1 addition & 1 deletion src/hdu/header/extension/bintable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ mod tests {
TFormType::I { repeat_count: 640 },
TFormType::E { repeat_count: 640 },
],
ttypes: vec![Some("APERTURE".to_owned()), Some("NPOINTS ".to_owned()), Some("WAVELENGTH".to_owned()), Some("DELTAW ".to_owned()), Some("NET ".to_owned()), Some("BACKGROUND".to_owned()), Some("SIGMA ".to_owned()), Some("QUALITY ".to_owned()), Some("FLUX ".to_owned())],
ttypes: vec![Some("APERTURE".to_owned()), Some("NPOINTS".to_owned()), Some("WAVELENGTH".to_owned()), Some("DELTAW".to_owned()), Some("NET".to_owned()), Some("BACKGROUND".to_owned()), Some("SIGMA".to_owned()), Some("QUALITY".to_owned()), Some("FLUX".to_owned())],
theap: 11535,
// Should be 0
pcount: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/hdu/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ mod tests {
.next()
.expect("Should contain a primary HDU");

assert_eq!(Err(Error::StaticError("Fail reading the header without encountering the END card")), hdu);
assert_eq!(Err(Error::Io(std::io::ErrorKind::UnexpectedEof)), hdu);
// As the primary hdu parsing failed (EOF reached), next call to fits should result in None
assert_eq!(fits.next(), None);
}
Expand Down
8 changes: 6 additions & 2 deletions src/hdu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::hdu::Value::Logical;
use crate::async_fits;
use crate::fits;
use crate::hdu::primary::consume_next_card;

use log::error;
/// An enueration of the supported FITS Header Data Unit types.
#[derive(Debug, PartialEq)]
pub enum HDU {
Expand All @@ -51,7 +51,11 @@ where
loop {
consume_next_card(reader, &mut card_80_bytes_buf, num_bytes_read)
// Precise the error that we did not encounter the END stopping card
.map_err(|_| Error::StaticError("Fail reading the header without encountering the END card"))?;
.map_err(|e| {
error!("Fail reading the header without encountering the END card");

e
})?;

if let Ok(card) = Card::try_from(&card_80_bytes_buf) {
cards.push(card);
Expand Down
30 changes: 11 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
//! use std::fs::File;
//! use std::io::BufReader;
//!
//! use fitsrs::{Fits, HDU};
//! use fitsrs::hdu::data::DataIter;
//! use fitsrs::{Fits, HDU, ImageData};
//!
//! let f = File::open("samples/fits.gsfc.nasa.gov/HST_FOC.fits").unwrap();
//! let reader = BufReader::new(f);
Expand All @@ -20,7 +19,7 @@
//! let naxis1 = *xtension.get_naxisn(1).unwrap() as usize;
//! let naxis2 = *xtension.get_naxisn(2).unwrap() as usize;
//!
//! if let DataIter::F32(it) = hdu_list.get_data(hdu) {
//! if let ImageData::F32(it) = hdu_list.get_data(hdu) {
//! let data = it.collect::<Vec<_>>();
//! assert_eq!(data.len(), naxis1 * naxis2);
//! } else {
Expand Down Expand Up @@ -107,13 +106,7 @@ mod tests {
num_asciitable_ext: usize,
num_bintable_ext: usize,
) {
let f = File::open(filename).unwrap();
//let bytes: Result<Vec<_>, _> = f.bytes().collect();
//let buf = bytes.unwrap();

//let reader = Cursor::new(&buf[..]);
//let mut hdu_list = Fits::from_reader(reader);
let mut hdu_list = Fits::from_reader(BufReader::new(f));
let mut hdu_list = FITSFile::open(filename).unwrap();

let mut n_image_ext = 1; // because the primary hdu is an image
let mut n_bintable_ext = 0;
Expand Down Expand Up @@ -215,25 +208,24 @@ mod tests {
#[test_case("samples/vizier/VAR.358.R.fits", false)]
#[test_case("samples/fits.gsfc.nasa.gov/IUE_LWP.fits", false)]
#[test_case("samples/misc/bonn.fits", false)]
// FIXME too slow, to retest when we implement the seek of the data unit part
//#[test_case("samples/misc/EUC_MER_MOSAIC-VIS-FLAG_TILE100158585-1EC1C5_20221211T132329.822037Z_00.00.fits", false)]
//#[test_case("samples/misc/P122_49.fits", false)]
#[test_case("samples/misc/EUC_MER_MOSAIC-VIS-FLAG_TILE100158585-1EC1C5_20221211T132329.822037Z_00.00.fits", false)]
#[test_case("samples/misc/P122_49.fits", false)]
#[test_case("samples/misc/skv1678175163788.fits", false)]
#[test_case("samples/misc/SN2923fxjA.fits", false)]
fn test_fits_opening(filename: &str, corrupted: bool) {
fn test_fits_opening(filename: &str, ground_truth: bool) {
let hdu_list = FITSFile::open(filename).expect("Can find fits file");

let mut correctly_opened = true;
let mut corrupted = false;
for hdu in hdu_list {
match hdu {
Err(_) => {
correctly_opened = false;
corrupted = true;
}
_ => (),
}
}

assert_eq!(!corrupted, correctly_opened);
assert_eq!(ground_truth, corrupted);
}

#[test]
Expand Down Expand Up @@ -328,7 +320,7 @@ mod tests {

#[test_case("samples/misc/SN2923fxjA.fits.gz", 5415.0, 6386.0)]
#[test_case("samples/misc/SN2923fxjA.fits", 5415.0, 6386.0)]
fn open_external_gzipped_file(filename: &str, min: f32, max: f32) {
fn test_fits_open_external_gzipped_file(filename: &str, min: f32, max: f32) {
let mut hdu_list = FITSFile::open(filename).unwrap();
use std::iter::Iterator;

Expand Down Expand Up @@ -359,7 +351,7 @@ mod tests {
}

#[test_case("samples/fits.gsfc.nasa.gov/m13_gzip.fits")]
fn open_tile_compressed_image(filename: &str) {
fn test_fits_open_tile_compressed_image(filename: &str) {
use std::fs::File;

use crate::hdu::data::bintable::DataValue;
Expand Down

0 comments on commit 34436d2

Please sign in to comment.