Skip to content

Commit

Permalink
Use a more generic approach for skymaps (SkyMap now a quite generic t…
Browse files Browse the repository at this point in the history
…rait)
  • Loading branch information
fxpineau committed Sep 20, 2024
1 parent cdb790d commit abee56d
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 169 deletions.
36 changes: 9 additions & 27 deletions src/nested/map/fits/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use byteorder::{BigEndian, ReadBytesExt};
use log::{debug, warn};

use super::{
super::skymap::{SkyMap, SkyMapArray},
super::skymap::{ImplicitSkyMapArray, SkyMapEnum},
error::FitsError,
gz::{is_gz, uncompress},
keywords::{
Expand Down Expand Up @@ -62,7 +62,7 @@ use crate::{depth, is_nside, n_hash};
/// # Info
/// Supports gz input stream
///
pub fn from_fits_skymap<R: Read + Seek>(mut reader: BufReader<R>) -> Result<SkyMap, FitsError> {
pub fn from_fits_skymap<R: Read + Seek>(mut reader: BufReader<R>) -> Result<SkyMapEnum, FitsError> {
if is_gz(&mut reader)? {
// Probably need to build an explicit map:
// highly compressed skymaps are IMPLICIT with a lot of zeros.
Expand All @@ -72,7 +72,7 @@ pub fn from_fits_skymap<R: Read + Seek>(mut reader: BufReader<R>) -> Result<SkyM
}
}

pub fn from_fits_skymap_internal<R: BufRead>(mut reader: R) -> Result<SkyMap, FitsError> {
pub fn from_fits_skymap_internal<R: BufRead>(mut reader: R) -> Result<SkyMapEnum, FitsError> {
let mut header_block = [b' '; 2880];
consume_primary_hdu(&mut reader, &mut header_block)?;
// Read the extension HDU
Expand Down Expand Up @@ -184,45 +184,27 @@ pub fn from_fits_skymap_internal<R: BufRead>(mut reader: R) -> Result<SkyMap, Fi
TForm1::B(_) => (0..n_hash)
.map(|_| reader.read_u8())
.collect::<Result<Vec<u8>, io::Error>>()
.map(|v| SkyMap {
depth,
values: SkyMapArray::U8(v.into_boxed_slice()),
}),
.map(|v| SkyMapEnum::ImplicitU64U8(ImplicitSkyMapArray::new(depth, v.into_boxed_slice()))),
TForm1::I(_) => (0..n_hash)
.map(|_| reader.read_i16::<BigEndian>())
.collect::<Result<Vec<i16>, io::Error>>()
.map(|v| SkyMap {
depth,
values: SkyMapArray::I16(v.into_boxed_slice()),
}),
.map(|v| SkyMapEnum::ImplicitU64I16(ImplicitSkyMapArray::new(depth, v.into_boxed_slice()))),
TForm1::J(_) => (0..n_hash)
.map(|_| reader.read_i32::<BigEndian>())
.collect::<Result<Vec<i32>, io::Error>>()
.map(|v| SkyMap {
depth,
values: SkyMapArray::I32(v.into_boxed_slice()),
}),
.map(|v| SkyMapEnum::ImplicitU64I32(ImplicitSkyMapArray::new(depth, v.into_boxed_slice()))),
TForm1::K(_) => (0..n_hash)
.map(|_| reader.read_i64::<BigEndian>())
.collect::<Result<Vec<i64>, io::Error>>()
.map(|v| SkyMap {
depth,
values: SkyMapArray::I64(v.into_boxed_slice()),
}),
.map(|v| SkyMapEnum::ImplicitU64I64(ImplicitSkyMapArray::new(depth, v.into_boxed_slice()))),
TForm1::E(_) => (0..n_hash)
.map(|_| reader.read_f32::<BigEndian>())
.collect::<Result<Vec<f32>, io::Error>>()
.map(|v| SkyMap {
depth,
values: SkyMapArray::F32(v.into_boxed_slice()),
}),
.map(|v| SkyMapEnum::ImplicitU64F32(ImplicitSkyMapArray::new(depth, v.into_boxed_slice()))),
TForm1::D(_) => (0..n_hash)
.map(|_| reader.read_f64::<BigEndian>())
.collect::<Result<Vec<f64>, io::Error>>()
.map(|v| SkyMap {
depth,
values: SkyMapArray::F64(v.into_boxed_slice()),
}),
.map(|v| SkyMapEnum::ImplicitU64F64(ImplicitSkyMapArray::new(depth, v.into_boxed_slice()))),
}
.map_err(FitsError::Io)
}
Expand Down
95 changes: 4 additions & 91 deletions src/nested/map/fits/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,18 @@ use std::io::Write;

use num_traits::ToBytes;

use crate::{n_hash, nside};
use crate::{n_hash, nested::map::skymap::SkyMapValue, nside};

use super::error::FitsError;

/// Trait marking the type of the values written in the FITS map.
pub trait FitsCompatibleValue: ToBytes {
/// Size, in bytes, of a value.
fn fits_naxis1() -> u8;
fn fits_tform() -> &'static str;
}

impl FitsCompatibleValue for u8 {
fn fits_naxis1() -> u8 {
size_of::<Self>() as u8
}

fn fits_tform() -> &'static str {
"B"
}
}

impl FitsCompatibleValue for i16 {
fn fits_naxis1() -> u8 {
size_of::<Self>() as u8
}

fn fits_tform() -> &'static str {
"I"
}
}

impl FitsCompatibleValue for i32 {
fn fits_naxis1() -> u8 {
4
}

fn fits_tform() -> &'static str {
"J"
}
}

impl FitsCompatibleValue for i64 {
fn fits_naxis1() -> u8 {
size_of::<Self>() as u8
}

fn fits_tform() -> &'static str {
"K"
}
}

impl FitsCompatibleValue for u32 {
fn fits_naxis1() -> u8 {
size_of::<Self>() as u8
}

fn fits_tform() -> &'static str {
"J"
}
}

impl FitsCompatibleValue for u64 {
fn fits_naxis1() -> u8 {
size_of::<Self>() as u8
}

fn fits_tform() -> &'static str {
"K"
}
}

impl FitsCompatibleValue for f32 {
fn fits_naxis1() -> u8 {
size_of::<Self>() as u8
}

fn fits_tform() -> &'static str {
"E"
}
}

impl FitsCompatibleValue for f64 {
fn fits_naxis1() -> u8 {
size_of::<Self>() as u8
}

fn fits_tform() -> &'static str {
"D"
}
}

// Add read map from fits!!

/// Write the values in a FITS HEALPix map.
///
/// # Params
/// * `values`: array of values, the index of the value correspond to the HEALPix cell the value is
/// associated with.
pub fn write_implicit_skymap_fits<R: Write, T: FitsCompatibleValue>(
pub fn write_implicit_skymap_fits<R: Write, T: SkyMapValue>(
mut writer: R,
values: &[T],
) -> Result<(), FitsError> {
Expand Down Expand Up @@ -133,7 +46,7 @@ pub fn write_implicit_skymap_fits_from_it<R, I>(
where
R: Write,
I: Iterator,
I::Item: FitsCompatibleValue,
I::Item: SkyMapValue,
{
let n_cells = n_hash(depth);
write_skymap_fits_header::<_, I::Item>(&mut writer, depth)?;
Expand Down Expand Up @@ -219,7 +132,7 @@ fn write_primary_hdu<R: Write>(writer: &mut R) -> Result<(), FitsError> {
writer.write_all(&header_block[..]).map_err(FitsError::Io)
}

fn write_skymap_fits_header<R: Write, T: FitsCompatibleValue>(
fn write_skymap_fits_header<R: Write, T: SkyMapValue>(
mut writer: R,
depth: u8,
) -> Result<(), FitsError> {
Expand Down
Loading

0 comments on commit abee56d

Please sign in to comment.