Skip to content

Commit

Permalink
Merge pull request #15 from eta077/main
Browse files Browse the repository at this point in the history
Prep for 0.0.3 release
  • Loading branch information
eta077 authored Jun 1, 2022
2 parents 6f63456 + d65c286 commit 359c2e8
Show file tree
Hide file tree
Showing 11 changed files with 877 additions and 810 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "weekly"
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[package]
name = "astro-rs"
version = "0.0.2"
version = "0.0.3"
edition = "2021"
license = "MIT"
description = "Astronomy utils"
repository = "https://github.com/eta077/astro-rs"
readme = "README.md"
keywords = ["astronomy", "astrophysics", "fits", "utility"]
categories = ["aerospace", "data-structures", "parser-implementations", "science"]

exclude = [
".github",
Expand Down
79 changes: 27 additions & 52 deletions src/fits/hdu_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ pub use primary_hdu::*;

use super::*;

/// The header keyword indicating the name of a table column.
pub const TTYPE_KEYWORD: [u8; 8] = *b"TTYPE ";
/// The header keyword indicating the size in bytes of a table column.
pub const TFORM_KEYWORD: [u8; 8] = *b"TFORM ";

pub(crate) const DEFAULT_BITPIX_BYTES: [u8; 80] =
*b"BITPIX = 8 ";
pub(crate) const DEFAULT_NAXIS_BYTES: [u8; 80] =
Expand Down Expand Up @@ -117,14 +122,11 @@ pub mod image_hdu {
}
}

/// Functions related to an Image type HDU.
/// Functions related to a Binary Table type HDU.
pub mod binary_table_hdu {
use super::*;
use crate::fits::header_value::TForm;

const TTYPE_BYTES: [u8; 8] = *b"TTYPE ";
const TFORM_BYTES: [u8; 8] = *b"TFORM ";

/// Constructs an HDU pre-populated with the required cards to be a Binary Table HDU.
pub fn default() -> Hdu {
let xtension_card = FitsHeaderCard::from(
Expand Down Expand Up @@ -182,40 +184,24 @@ pub mod binary_table_hdu {
.get_card(naxis_keyword)
.and_then(|card| card.get_value::<u32>().ok())
.unwrap_or_default() as usize;
while n <= num_rows {
let mut keyword = TFORM_BYTES;
let mut i = 5;
if n > 99 {
keyword[i] = (n / 100 + 48) as u8;
i += 1;
}
if n > 9 {
keyword[i] = (n % 100 / 10 + 48) as u8;
i += 1;
}
keyword[i] = (n % 10 + 48) as u8;
if let Some(card) = hdu.header.get_card(keyword) {
let mut tform_keyword = FitsHeaderKeyword::from(TFORM_KEYWORD);
let mut ttype_keyword = FitsHeaderKeyword::from(TTYPE_KEYWORD);
while n as usize <= num_rows {
tform_keyword.append_number(n);
if let Some(card) = hdu.header.get_card(tform_keyword) {
if let Ok(tform_value) = card.get_value::<TForm>() {
let mut keyword = TTYPE_BYTES;
let mut i = 5;
if n > 99 {
keyword[i] = (n / 100 + 48) as u8;
i += 1;
}
if n > 9 {
keyword[i] = (n % 100 / 10 + 48) as u8;
i += 1;
}
keyword[i] = (n % 10 + 48) as u8;
if let Some(card) = hdu.header.get_card(keyword) {
if let Ok(value) = card.get_value::<String>() {
// TODO: ignore case
if value.as_str() == name {
tform = Some(tform_value);
break;
}
ttype_keyword.append_number(n);
if let Some(value) = hdu
.header
.get_card(ttype_keyword)
.and_then(|card| card.get_value::<String>().ok())
{
if value.eq_ignore_ascii_case(name) {
tform = Some(tform_value);
break;
}
}

column_start += tform_value.value();
}
n += 1;
Expand All @@ -224,8 +210,7 @@ pub mod binary_table_hdu {
}
}
if let Some(tform) = tform {
let data = hdu.get_data::<Vec<u8>>().ok()?;
return Some(*tform.get_values(&data, column_start, row_len, num_rows));
return Some(*tform.create_column(hdu.data_raw(), column_start, row_len, num_rows));
}
None
}
Expand All @@ -243,7 +228,7 @@ pub mod binary_table_hdu {
.get_card(naxis_keyword)
.and_then(|card| card.get_value::<u32>().ok())
.unwrap_or_default() as usize;
if index > num_rows as u16 {
if index as usize > num_rows {
return None;
}
naxis_keyword[5] = b'1';
Expand All @@ -252,19 +237,10 @@ pub mod binary_table_hdu {
.get_card(naxis_keyword)
.and_then(|card| card.get_value::<u32>().ok())
.unwrap_or_default() as usize;
let mut tform_keyword = FitsHeaderKeyword::from(TFORM_KEYWORD);
while n <= index {
let mut keyword = TFORM_BYTES;
let mut i = 5;
if n > 99 {
keyword[i] = (n / 100 + 48) as u8;
i += 1;
}
if n > 9 {
keyword[i] = (n % 100 / 10 + 48) as u8;
i += 1;
}
keyword[i] = (n % 10 + 48) as u8;
if let Some(card) = hdu.header.get_card(keyword) {
tform_keyword.append_number(n);
if let Some(card) = hdu.header.get_card(tform_keyword) {
if let Ok(value) = card.get_value::<TForm>() {
if n == index {
tform = Some(value);
Expand All @@ -278,8 +254,7 @@ pub mod binary_table_hdu {
}
}
if let Some(tform) = tform {
let data = hdu.get_data::<Vec<u8>>().ok()?;
return Some(*tform.get_values(&data, column_start, row_len, num_rows));
return Some(*tform.create_column(hdu.data_raw(), column_start, row_len, num_rows));
}
None
}
Expand Down
45 changes: 45 additions & 0 deletions src/fits/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,51 @@ pub struct FitsHeaderKeyword {
raw: [u8; 8],
}

impl FitsHeaderKeyword {
/// Appends the given number to the keyword.
/// If a number is already appended, it is replaced by the given number.
///
/// ```
/// use astro_rs::fits::*;
///
/// let mut naxis_keyword = FitsHeaderKeyword::from(NAXIS_KEYWORD);
/// naxis_keyword.append_number(1);
/// assert_eq!(naxis_keyword, "NAXIS1");
/// naxis_keyword.append_number(2);
/// assert_eq!(naxis_keyword, "NAXIS2");
///
/// let mut tform_keyword = FitsHeaderKeyword::from(TFORM_KEYWORD);
/// tform_keyword.append_number(100);
/// assert_eq!(tform_keyword, "TFORM100");
/// tform_keyword.append_number(10);
/// assert_eq!(tform_keyword, "TFORM10");
/// ```
pub fn append_number(&mut self, number: u16) {
let mut i = 0;
while i < 8 {
let c = self.raw[i];
if c == b' ' || c.is_ascii_digit() {
break;
}
i += 1;
}
if number > 99 {
self.raw[i] = (number / 100 + 48) as u8;
i += 1;
}
if number > 9 {
self.raw[i] = (number % 100 / 10 + 48) as u8;
i += 1;
}
self.raw[i] = (number % 10 + 48) as u8;
i += 1;
while i < 8 {
self.raw[i] = b' ';
i += 1;
}
}
}

impl From<[u8; 8]> for FitsHeaderKeyword {
fn from(raw: [u8; 8]) -> Self {
FitsHeaderKeyword { raw }
Expand Down
Loading

0 comments on commit 359c2e8

Please sign in to comment.