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

Add get_hdus(), silence warnings & implement Display for Image #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ rayon = "1"
dyn-clone = "1"
indexmap = "1"
rustronomy-core = "0.1"
anyhow = "1"

[dev-dependencies]
dirs = "4"
Expand Down
6 changes: 4 additions & 2 deletions src/api/fits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

use std::{error::Error, fmt::Display, path::Path};

use anyhow::{Result};

use rustronomy_core::universal_containers::*;

use super::hdu::Hdu;
Expand All @@ -60,15 +62,15 @@ pub struct Fits {
}

impl Fits {
pub fn read(path: &Path) -> Result<Self, Box<dyn Error>> {
pub fn read(path: &Path) -> Result<Self> {
//(1) First we try to open the file
let mut reader = crate::intern::FitsReader::new(path)?;

//(2) Then we read the primary HDU
let (global_tags, hdu0) = crate::intern::read_primary_hdu(&mut reader)?;
todo!()
}
pub fn write(self, path: &Path) -> Result<(), Box<dyn Error>> {
pub fn write(self, path: &Path) -> Result<()> {
todo!()
}
pub fn empty() -> Self {
Expand Down
6 changes: 4 additions & 2 deletions src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use std::{
fmt::{Display, Formatter},
};

use anyhow::Result;

use crate::{
io_err::{self, InvalidFitsFileErr as IFFErr},
raw::{raw_io::RawFitsWriter, BlockSized},
Expand Down Expand Up @@ -72,10 +74,10 @@ impl Display for Extension {
}

impl Extension {
pub(crate) fn write_to_buffer(self, writer: &mut RawFitsWriter) -> Result<(), Box<dyn Error>> {
pub(crate) fn write_to_buffer(self, writer: &mut RawFitsWriter) -> Result<()> {
use Extension::*;
match self {
Corrupted => return Err(Box::new(IFFErr::new(io_err::CORRUPTED))),
Corrupted => return Err(IFFErr::new(io_err::CORRUPTED).into()),
Image(img) => ImgParser::encode_img(img, writer),
AsciiTable(tbl) => AsciiTblParser::encode_tbl(tbl, writer),
}
Expand Down
15 changes: 15 additions & 0 deletions src/extensions/image/generic_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ where
}
}

impl<T> Display for Image<T>
where
T: Debug + Num + Sized + Decode + Encode + Display + Clone,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (i,&n) in self.shape.iter().enumerate() {
if i > 0 {
write!(f,"x")?;
}
write!(f,"{}",n)?;
}
write!(f,"/{}",self.block_size)
}
}

impl<T> Image<T>
where
T: Debug + Num + Sized + Decode + Encode + Display + Clone,
Expand Down
12 changes: 7 additions & 5 deletions src/extensions/image/image_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use std::{
mem::size_of,
};

use anyhow::Result;

//Rustronomy Imports
use rustronomy_core::data_type_traits::io_utils::{Decode, Encode};

Expand Down Expand Up @@ -60,7 +62,7 @@ impl ImgParser {
reader: &mut RawFitsReader,
shape: &Vec<usize>,
bitpix: Bitpix,
) -> Result<Extension, Box<dyn Error>> {
) -> Result<Extension> {
use Bitpix::*;
use TypedImage::*;

Expand All @@ -77,7 +79,7 @@ impl ImgParser {
fn decode_helper<T>(
reader: &mut RawFitsReader,
shape: &Vec<usize>,
) -> Result<Image<T>, Box<dyn Error>>
) -> Result<Image<T>>
where
T: Debug + Num + Sized + Decode + Encode + Display + Clone + Send,
{
Expand Down Expand Up @@ -156,7 +158,7 @@ impl ImgParser {
pub(crate) fn encode_img(
typed_img: TypedImage,
writer: &mut RawFitsWriter,
) -> Result<(), Box<dyn Error>> {
) -> Result<()> {
//This function only matches the typed image and calls the appropriate
//helper function
use TypedImage::*;
Expand All @@ -174,7 +176,7 @@ impl ImgParser {
Ok(())
}

fn encode_helper<T>(img: Image<T>, writer: &mut RawFitsWriter) -> Result<(), Box<dyn Error>>
fn encode_helper<T>(img: Image<T>, writer: &mut RawFitsWriter) -> Result<()>
where
T: Debug + Num + Sized + Decode + Encode + Display + Clone,
{
Expand Down Expand Up @@ -207,7 +209,7 @@ impl ImgParser {
match img.get_data().as_slice_memory_order() {
None => {
//Data is NOT continuous, return error!
return Err(Box::new(IMLErr::new()));
return Err(IMLErr::new().into());
}
_ => {} //Data IS continuous, continue!
}
Expand Down
61 changes: 35 additions & 26 deletions src/extensions/image/typed_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::{
fmt::{Display, Write},
};

use anyhow::Result;
use ndarray::{Array, IxDyn};

use crate::{
Expand Down Expand Up @@ -108,8 +109,16 @@ impl ExtensionPrint for TypedImage {

impl Display for TypedImage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//TODO: make pretty display for image!
todo!()
write!(f,"TypedImage(")?;
match self {
Self::ByteImg(img) => write!(f,"u16,{}",img)?,
Self::I16Img(img)=> write!(f,"i16,{}",img)?,
Self::I32Img(img)=> write!(f,"i32,{}",img)?,
Self::I64Img(img)=> write!(f,"i64,{}",img)?,
Self::SpfImg(img)=> write!(f,"f32,{}",img)?,
Self::DpfImg(img)=> write!(f,"f64,{}",img)?,
}
write!(f,")")
}
}

Expand All @@ -128,87 +137,87 @@ impl TypedImage {
}
}

pub fn as_u8_array(&self) -> Result<&Array<u8, IxDyn>, Box<dyn Error>> {
pub fn as_u8_array(&self) -> Result<&Array<u8, IxDyn>> {
match &self {
Self::ByteImg(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::byte()))),
&var => Err(WITErr::new(var, Bitpix::byte()).into()),
}
}

pub fn as_i16_array(&self) -> Result<&Array<i16, IxDyn>, Box<dyn Error>> {
pub fn as_i16_array(&self) -> Result<&Array<i16, IxDyn>> {
match &self {
Self::I16Img(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::short()))),
&var => Err(WITErr::new(var, Bitpix::short()).into()),
}
}

pub fn as_i32_array(&self) -> Result<&Array<i32, IxDyn>, Box<dyn Error>> {
pub fn as_i32_array(&self) -> Result<&Array<i32, IxDyn>> {
match &self {
Self::I32Img(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::int()))),
&var => Err(WITErr::new(var, Bitpix::int()).into()),
}
}

pub fn as_i64_array(&self) -> Result<&Array<i64, IxDyn>, Box<dyn Error>> {
pub fn as_i64_array(&self) -> Result<&Array<i64, IxDyn>> {
match &self {
Self::I64Img(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::long()))),
&var => Err(WITErr::new(var, Bitpix::long()).into()),
}
}

pub fn as_f32_array(&self) -> Result<&Array<f32, IxDyn>, Box<dyn Error>> {
pub fn as_f32_array(&self) -> Result<&Array<f32, IxDyn>> {
match &self {
Self::SpfImg(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::spf()))),
&var => Err(WITErr::new(var, Bitpix::spf()).into()),
}
}

pub fn as_f64_array(&self) -> Result<&Array<f64, IxDyn>, Box<dyn Error>> {
pub fn as_f64_array(&self) -> Result<&Array<f64, IxDyn>> {
match &self {
Self::DpfImg(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::dpf()))),
&var => Err(WITErr::new(var, Bitpix::dpf()).into()),
}
}

pub fn as_owned_u8_array(self) -> Result<Array<u8, IxDyn>, Box<dyn Error>> {
pub fn as_owned_u8_array(self) -> Result<Array<u8, IxDyn>> {
match self {
Self::ByteImg(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::byte()))),
var => Err(WITErr::new(&var, Bitpix::byte()).into()),
}
}

pub fn as_owned_i16_array(self) -> Result<Array<i16, IxDyn>, Box<dyn Error>> {
pub fn as_owned_i16_array(self) -> Result<Array<i16, IxDyn>> {
match self {
Self::I16Img(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::short()))),
var => Err(WITErr::new(&var, Bitpix::short()).into()),
}
}

pub fn as_owned_i32_array(self) -> Result<Array<i32, IxDyn>, Box<dyn Error>> {
pub fn as_owned_i32_array(self) -> Result<Array<i32, IxDyn>> {
match self {
Self::I32Img(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::int()))),
var => Err(WITErr::new(&var, Bitpix::int()).into()),
}
}

pub fn as_owned_i64_array(self) -> Result<Array<i64, IxDyn>, Box<dyn Error>> {
pub fn as_owned_i64_array(self) -> Result<Array<i64, IxDyn>> {
match self {
Self::I64Img(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::long()))),
var => Err(WITErr::new(&var, Bitpix::long()).into()),
}
}

pub fn as_owned_f32_array(self) -> Result<Array<f32, IxDyn>, Box<dyn Error>> {
pub fn as_owned_f32_array(self) -> Result<Array<f32, IxDyn>> {
match self {
Self::SpfImg(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::spf()))),
var => Err(WITErr::new(&var, Bitpix::spf()).into()),
}
}

pub fn as_owned_f64_array(self) -> Result<Array<f64, IxDyn>, Box<dyn Error>> {
pub fn as_owned_f64_array(self) -> Result<Array<f64, IxDyn>> {
match self {
Self::DpfImg(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::dpf()))),
var => Err(WITErr::new(&var, Bitpix::dpf()).into()),
}
}
}
6 changes: 4 additions & 2 deletions src/extensions/table/ascii_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use std::{
fmt::{self, Display, Formatter},
};

use anyhow::Result;

use crate::{
extensions::ExtensionPrint,
raw::{table_entry_format::TableEntryFormat, BlockSized},
Expand Down Expand Up @@ -138,10 +140,10 @@ impl AsciiTable {
AsciiTable { cols: cols, block_size: Some(size) }
}

pub(crate) fn add_row(&mut self, row: Vec<TableEntry>) -> Result<(), Box<dyn Error>> {
pub(crate) fn add_row(&mut self, row: Vec<TableEntry>) -> Result<()> {
//Adds row to table
if row.len() != self.cols.len() {
return Err(Box::new(ShapeMisMatchErr::new(&row, &self)));
return Err(ShapeMisMatchErr::new(&row, &self).into());
}

//Add row to the table
Expand Down
6 changes: 4 additions & 2 deletions src/extensions/table/ascii_tbl_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use std::{
str::{self, Utf8Error},
};

use anyhow::Result;

use crate::{
extensions::{table::column::AsciiCol, Extension},
raw::{
Expand All @@ -49,7 +51,7 @@ impl AsciiTblParser {
row_index_col_start: Vec<usize>, //row index where each column starts
field_format: Vec<String>, //data format (incl length) of each field
field_labels: Option<Vec<String>>, //field labels
) -> Result<Extension, Box<dyn Error>> {
) -> Result<Extension> {
/* (1)
Tables are usually pretty small compared to images. Hence it's
probably ok to read the whole table in one go. We should be careful
Expand Down Expand Up @@ -190,7 +192,7 @@ impl AsciiTblParser {
pub(crate) fn encode_tbl(
tbl: AsciiTable,
writer: &mut RawFitsWriter,
) -> Result<(), Box<dyn Error>> {
) -> Result<()> {
/* Note:
This parser assumes that certain necessary keywords to decode a HDU
containing a table have already been set while encoding the header
Expand Down
9 changes: 7 additions & 2 deletions src/fits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::{
fmt::{Display, Formatter},
path::Path,
};
use anyhow::Result;

use crate::{
header_data_unit::HeaderDataUnit,
Expand All @@ -42,7 +43,7 @@ pub struct Fits {
}

impl Fits {
pub fn open(path: &Path) -> Result<Self, Box<dyn Error>> {
pub fn open<P:AsRef<Path>>(path: P) -> Result<Self> {
//(1) Construct a RawFitsReader
let mut reader = RawFitsReader::new(path)?;

Expand All @@ -57,7 +58,7 @@ impl Fits {
Ok(Fits { hdus: hdus })
}

pub fn write(self, path: &Path) -> Result<(), Box<dyn Error>> {
pub fn write(self, path: &Path) -> Result<()> {
//(1) Construct a RawFitsWriter
let mut writer = RawFitsWriter::new(path)?;

Expand All @@ -83,6 +84,10 @@ impl Fits {
}
Some(self.hdus.remove(index))
}

pub fn get_hdus(&self)->&[HeaderDataUnit] {
&self.hdus
}
}

impl BlockSized for Fits {
Expand Down
Loading