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

Bintable::update_with_parsed_header incorrectly parses non-repeating field types. #8

Open
lowka opened this issue Oct 17, 2024 · 1 comment

Comments

@lowka
Copy link

lowka commented Oct 17, 2024

I have found that the library cannot parse a file with a binary table extension header, when a field type has no repeat count.

Reproducer

#[cfg(test)]
mod test{
    use std::error::Error;
    use std::fs::File;
    use std::io::BufReader;
    use fitsrs::fits::Fits;

    #[test]
    fn parse() -> Result<(), Box<dyn Error>> {

        let file = File::open(".../mastar-combspec-v3_1_1-v1_7_7-lsfpercent60.0.fits")?;
        let mut reader = BufReader::new(file);

        let Fits { hdu } = Fits::from_reader(&mut reader)?;
        let mut primary_hdu = hdu;
        let mut hdu = primary_hdu.next();

        println!("Iterate over HDUs");
        while let Some(mut ext_hdu) = hdu?.take() {
            println!("Got HDU");
            hdu = ext_hdu.next();
        }

        Ok(())
    }
}

Output:

Iterate over HDUs
Error: StaticError("Ascii Table TFORM not recognized")

The file I used in my test
https://data.sdss.org/sas/dr17/manga/spectro/mastar/v3_1_1/v1_7_7/mastar-goodspec-v3_1_1-v1_7_7-lsfpercent60.0.fits.gz

Fix

I think the issue is caused by a typo in a BinTable::update_with_parsed_header:

src/hdu/header/extension/bintable.rs

let (field_type_char, repeat_count) =
   if let Ok((remaining_bytes, Value::Integer(repeat_count))) =
     parse_integer(card_value.as_bytes())
   {
      (remaining_bytes[0].as_char(), repeat_count)
    } else {
       (owned_kw[0].as_char(), 1) // <<<< the problem expression (line 80)
    };
   let repeat_count = repeat_count as u64;

The problem expression attempts to read field type from owned_kw instead of card_value.

When I change line 80 to

 (card_value.as_bytes()[0].as_char(), 1)

A file is parsed without an error.

@bmatthieu3
Copy link
Collaborator

Thanks for the bug report. For the moment, only image extension is parseable by fitsrs. Although I have begun the bintable support it is not fully done. I will soon give some of my time continuing this work.

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

No branches or pull requests

2 participants