From 5609d8fbb2add2cea207f20ee89117d1f7100b87 Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Sat, 20 Jul 2024 01:07:08 +0200 Subject: [PATCH] add naxis to cards hashmap --- Cargo.toml | 2 +- src/hdu/header/extension/asciitable.rs | 14 ++++++-------- src/hdu/header/extension/bintable.rs | 2 ++ src/hdu/header/extension/image.rs | 15 +++++++++++++++ src/hdu/header/extension/mod.rs | 4 +++- src/hdu/header/mod.rs | 7 ++++--- 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3053164..2889d07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fitsrs" -version = "0.2.10" +version = "0.2.11" authors = ["Matthieu Baumann "] edition = "2018" description = "Implementation of the FITS image parser" diff --git a/src/hdu/header/extension/asciitable.rs b/src/hdu/header/extension/asciitable.rs index 0186f64..e43f51c 100644 --- a/src/hdu/header/extension/asciitable.rs +++ b/src/hdu/header/extension/asciitable.rs @@ -235,6 +235,7 @@ impl Xtension for AsciiTable { reader: &mut R, num_bytes_read: &mut u64, card_80_bytes_buf: &mut [u8; 80], + _cards: &mut HashMap<[u8; 8], Value>, ) -> Result { // BITPIX consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?; @@ -252,11 +253,9 @@ impl Xtension for AsciiTable { } // NAXIS1 consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?; - let naxis1 = - check_card_keyword(card_80_bytes_buf, NAXIS_KW[0])?.check_for_float()? as u64; + let naxis1 = check_card_keyword(card_80_bytes_buf, NAXIS_KW[0])?.check_for_float()? as u64; consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?; - let naxis2 = - check_card_keyword(card_80_bytes_buf, NAXIS_KW[1])?.check_for_float()? as u64; + let naxis2 = check_card_keyword(card_80_bytes_buf, NAXIS_KW[1])?.check_for_float()? as u64; // PCOUNT consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?; @@ -297,6 +296,7 @@ impl Xtension for AsciiTable { reader: &mut R, num_bytes_read: &mut u64, card_80_bytes_buf: &mut [u8; 80], + _cards: &mut HashMap<[u8; 8], Value>, ) -> Result where R: AsyncRead + std::marker::Unpin, @@ -318,11 +318,9 @@ impl Xtension for AsciiTable { } // NAXIS1 consume_next_card_async(reader, card_80_bytes_buf, num_bytes_read).await?; - let naxis1 = - check_card_keyword(card_80_bytes_buf, NAXIS_KW[0])?.check_for_float()? as u64; + let naxis1 = check_card_keyword(card_80_bytes_buf, NAXIS_KW[0])?.check_for_float()? as u64; consume_next_card_async(reader, card_80_bytes_buf, num_bytes_read).await?; - let naxis2 = - check_card_keyword(card_80_bytes_buf, NAXIS_KW[1])?.check_for_float()? as u64; + let naxis2 = check_card_keyword(card_80_bytes_buf, NAXIS_KW[1])?.check_for_float()? as u64; // PCOUNT consume_next_card_async(reader, card_80_bytes_buf, num_bytes_read).await?; diff --git a/src/hdu/header/extension/bintable.rs b/src/hdu/header/extension/bintable.rs index 314ed6d..22c2ef2 100644 --- a/src/hdu/header/extension/bintable.rs +++ b/src/hdu/header/extension/bintable.rs @@ -114,6 +114,7 @@ impl Xtension for BinTable { reader: &mut R, num_bytes_read: &mut u64, card_80_bytes_buf: &mut [u8; 80], + _cards: &mut HashMap<[u8; 8], Value>, ) -> Result { // BITPIX consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?; @@ -172,6 +173,7 @@ impl Xtension for BinTable { reader: &mut R, num_bytes_read: &mut u64, card_80_bytes_buf: &mut [u8; 80], + _cards: &mut HashMap<[u8; 8], Value>, ) -> Result where R: AsyncRead + std::marker::Unpin, diff --git a/src/hdu/header/extension/image.rs b/src/hdu/header/extension/image.rs index 052e675..c3e4d0d 100644 --- a/src/hdu/header/extension/image.rs +++ b/src/hdu/header/extension/image.rs @@ -5,6 +5,7 @@ use async_trait::async_trait; use futures::AsyncRead; use serde::Serialize; +use crate::card::Keyword; use crate::card::Value; use crate::error::Error; use crate::hdu::header::consume_next_card_async; @@ -68,6 +69,7 @@ impl Xtension for Image { reader: &mut R, num_bytes_read: &mut u64, card_80_bytes_buf: &mut [u8; 80], + cards: &mut HashMap, ) -> Result { // BITPIX consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?; @@ -85,6 +87,12 @@ impl Xtension for Image { }) .collect::, _>>()?; + for (i, &naxisi) in naxisn.iter().enumerate() { + cards.insert(*NAXIS_KW[i], Value::Integer(naxisi as i64)); + } + + cards.insert(*b"NAXIS ", Value::Integer(naxis as i64)); + Ok(Image { bitpix, naxis, @@ -96,6 +104,7 @@ impl Xtension for Image { reader: &mut R, num_bytes_read: &mut u64, card_80_bytes_buf: &mut [u8; 80], + cards: &mut HashMap, ) -> Result where R: AsyncRead + std::marker::Unpin, @@ -118,6 +127,12 @@ impl Xtension for Image { naxisn.push(naxis_len); } + for (i, &naxisi) in naxisn.iter().enumerate() { + cards.insert(*NAXIS_KW[i], Value::Integer(naxisi as i64)); + } + + cards.insert(*b"NAXIS ", Value::Integer(naxis as i64)); + Ok(Image { bitpix, naxis, diff --git a/src/hdu/header/extension/mod.rs b/src/hdu/header/extension/mod.rs index 1609e30..c4f7b30 100644 --- a/src/hdu/header/extension/mod.rs +++ b/src/hdu/header/extension/mod.rs @@ -7,7 +7,7 @@ use std::io::Read; use async_trait::async_trait; use futures::AsyncRead; -use crate::card::Value; +use crate::card::{Keyword, Value}; use crate::error::Error; use crate::hdu::primary::check_card_keyword; @@ -42,6 +42,7 @@ pub trait Xtension { reader: &mut R, num_bytes_read: &mut u64, card_80_bytes_buf: &mut [u8; 80], + cards: &mut HashMap, ) -> Result where Self: Sized; @@ -51,6 +52,7 @@ pub trait Xtension { reader: &mut R, num_bytes_read: &mut u64, card_80_bytes_buf: &mut [u8; 80], + cards: &mut HashMap, ) -> Result where Self: Sized, diff --git a/src/hdu/header/mod.rs b/src/hdu/header/mod.rs index 1fa33c4..00f2e20 100644 --- a/src/hdu/header/mod.rs +++ b/src/hdu/header/mod.rs @@ -166,7 +166,7 @@ pub enum BitpixValue { #[derive(Debug, PartialEq, Serialize)] pub struct Header { - /* Non mandatory keywords */ + /* Keywords */ cards: HashMap, /* Mandatory keywords for fits ext parsing */ @@ -185,7 +185,8 @@ where let mut cards = HashMap::new(); /* Consume mandatory keywords */ - let mut xtension: X = Xtension::parse(reader, num_bytes_read, card_80_bytes_buf)?; + let mut xtension: X = + Xtension::parse(reader, num_bytes_read, card_80_bytes_buf, &mut cards)?; /* Consume next non mandatory keywords until `END` is reached */ consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?; @@ -212,7 +213,7 @@ where /* Consume mandatory keywords */ let mut xtension: X = - Xtension::parse_async(reader, num_bytes_read, card_80_bytes_buf).await?; + Xtension::parse_async(reader, num_bytes_read, card_80_bytes_buf, &mut cards).await?; /* Consume next non mandatory keywords until `END` is reached */ consume_next_card_async(reader, card_80_bytes_buf, num_bytes_read).await?;