Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDUList iterator #14

Merged
merged 3 commits into from
Jan 10, 2025
Merged

HDUList iterator #14

merged 3 commits into from
Jan 10, 2025

Conversation

bmatthieu3
Copy link
Collaborator

@bmatthieu3 bmatthieu3 commented Jan 10, 2025

This PR makes API changes to make it more rust idiomatic. The fits object opened is an iterator over HDUs.

Here we loop over the image extensions only

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 std::io::{BufReader, Read};

let f = File::open("<your filename>").unwrap();
let reader = BufReader::new(f);

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

while let Some(Ok(hdu)) = hdu_list.next() {
    match hdu {
        // Iterator over the images only, discarding the other types of extensions
        HDU::XImage(hdu) => {
            // access the header
            let header = hdu.get_header();

            // access the data consuming the hdu object
            match hdu.get_data(&mut hdu_list) {
                DataIter::U8(it) => {
                    let data = it.collect::<Vec<_>>();
                    assert_eq!(num_pixels, data.len())
                },
                _ => todo!()
            }
        },
        _ => (),
    }
}

This PR also fixes UB from #5

@bmatthieu3 bmatthieu3 merged commit d40bb9a into master Jan 10, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant