Skip to content

Commit

Permalink
add naxis to cards hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatthieu3 committed Jul 19, 2024
1 parent bac4924 commit 5609d8f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fitsrs"
version = "0.2.10"
version = "0.2.11"
authors = ["Matthieu Baumann <[email protected]>"]
edition = "2018"
description = "Implementation of the FITS image parser"
Expand Down
14 changes: 6 additions & 8 deletions src/hdu/header/extension/asciitable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Error> {
// BITPIX
consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?;
Expand All @@ -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)?;
Expand Down Expand Up @@ -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<Self, Error>
where
R: AsyncRead + std::marker::Unpin,
Expand All @@ -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?;
Expand Down
2 changes: 2 additions & 0 deletions src/hdu/header/extension/bintable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Error> {
// BITPIX
consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?;
Expand Down Expand Up @@ -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<Self, Error>
where
R: AsyncRead + std::marker::Unpin,
Expand Down
15 changes: 15 additions & 0 deletions src/hdu/header/extension/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Keyword, Value>,
) -> Result<Self, Error> {
// BITPIX
consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?;
Expand All @@ -85,6 +87,12 @@ impl Xtension for Image {
})
.collect::<Result<Vec<_>, _>>()?;

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,
Expand All @@ -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<Keyword, Value>,
) -> Result<Self, Error>
where
R: AsyncRead + std::marker::Unpin,
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/hdu/header/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Keyword, Value>,
) -> Result<Self, Error>
where
Self: Sized;
Expand All @@ -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<Keyword, Value>,
) -> Result<Self, Error>
where
Self: Sized,
Expand Down
7 changes: 4 additions & 3 deletions src/hdu/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub enum BitpixValue {

#[derive(Debug, PartialEq, Serialize)]
pub struct Header<X> {
/* Non mandatory keywords */
/* Keywords */
cards: HashMap<Keyword, Value>,

/* Mandatory keywords for fits ext parsing */
Expand All @@ -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)?;
Expand All @@ -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?;
Expand Down

0 comments on commit 5609d8f

Please sign in to comment.