Skip to content

Commit

Permalink
Add 'moc op momsum' method in moc-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
fxpineau committed May 7, 2024
1 parent e668d8f commit 5ae3c1f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 56 deletions.
37 changes: 21 additions & 16 deletions crates/cli/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
use std::error::Error;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::str::FromStr;
use std::{
error::Error,
fs::File,
io::{BufRead, BufReader},
path::PathBuf,
str::FromStr,
};

use structopt::StructOpt;

use moclib::deser::ascii::{from_ascii_ivoa, from_ascii_stream, moc2d_from_ascii_ivoa};
use moclib::deser::fits::{from_fits_ivoa, MocIdxType, MocQtyType, MocType as RMocType, STMocType};
use moclib::deser::json::{cellmoc2d_from_json_aladin, from_json_aladin};
use moclib::qty::{Frequency, Hpx, Time};

use moclib::moc::{
CellMOCIntoIterator, CellMOCIterator, CellOrCellRangeMOCIntoIterator, CellOrCellRangeMOCIterator,
use moclib::{
deser::{
ascii::{from_ascii_ivoa, from_ascii_stream, moc2d_from_ascii_ivoa},
fits::{from_fits_ivoa, MocIdxType, MocQtyType, MocType as RMocType, STMocType},
json::{cellmoc2d_from_json_aladin, from_json_aladin},
},
moc::{
CellMOCIntoIterator, CellMOCIterator, CellOrCellRangeMOCIntoIterator,
CellOrCellRangeMOCIterator,
},
moc2d::{CellMOC2IntoIterator, CellOrCellRangeMOC2IntoIterator, RangeMOC2IntoIterator},
qty::{Frequency, Hpx, Time},
};
use moclib::moc2d::{CellMOC2IntoIterator, CellOrCellRangeMOC2IntoIterator, RangeMOC2IntoIterator};

use super::input::{fmt_from_extension, InputFormat};
use super::output::OutputFormat;
use super::{input::InputFormat, output::OutputFormat};

#[derive(Debug)]
pub enum MocType {
Expand Down Expand Up @@ -77,7 +82,7 @@ impl Convert {
} else {
let input_fmt = match self.input_fmt {
Some(input_fmt) => Ok(input_fmt),
None => fmt_from_extension(&path),
None => InputFormat::from_extension(&path),
}?;
let f = File::open(path)?;
exec(BufReader::new(f), input_fmt, self.moc_type, self.output)
Expand Down
7 changes: 2 additions & 5 deletions crates/cli/src/hprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ use moclib::{
qty::{Frequency, Time},
};

use super::{
input::{fmt_from_extension, InputFormat},
N_MICROSEC_IN_DAY,
};
use super::{input::InputFormat, N_MICROSEC_IN_DAY};

#[derive(Debug)]
pub enum MocType {
Expand Down Expand Up @@ -82,7 +79,7 @@ impl HumanPrint {
} else {
let input_fmt = match self.input_fmt {
Some(input_fmt) => Ok(input_fmt),
None => fmt_from_extension(&path),
None => InputFormat::from_extension(&path),
}?;
let f = File::open(path)?;
exec(BufReader::new(f), input_fmt, self.moc_type, !self.no_header)
Expand Down
31 changes: 16 additions & 15 deletions crates/cli/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,29 @@ impl FromStr for InputFormat {

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"ascii" => Ok(InputFormat::Ascii),
"json" => Ok(InputFormat::Json),
"fits" => Ok(InputFormat::Fits),
"stream" => Ok(InputFormat::Stream),
"ascii" => Ok(Self::Ascii),
"json" => Ok(Self::Json),
"fits" => Ok(Self::Fits),
"stream" => Ok(Self::Stream),
_ => Err(format!(
"Unrecognized format '{}'. Expected: 'ascii, 'json', 'fits' or 'stream'",
s
)),
}
}
}

/// Guess the file format from the extension.
pub fn fmt_from_extension(path: &Path) -> Result<InputFormat, String> {
match path.extension().and_then(|e| e.to_str()) {
Some("fits") => Ok(InputFormat::Fits),
Some("json") => Ok(InputFormat::Json),
Some("ascii") | Some("txt") => Ok(InputFormat::Ascii),
Some("stream") => Ok(InputFormat::Stream),
_ => Err(String::from(
"Unable to guess the MOC format from the file extension, see options.",
)),
impl InputFormat {
/// Guess the file format from the given path extension.
pub fn from_extension(path: &Path) -> Result<Self, String> {
match path.extension().and_then(|e| e.to_str()) {
Some("fits") => Ok(Self::Fits),
Some("json") => Ok(Self::Json),
Some("ascii") | Some("txt") => Ok(Self::Ascii),
Some("stream") => Ok(Self::Stream),
_ => Err(String::from(
"Unable to guess the MOC format from the file extension, see options.",
)),
}
}
}

Expand Down
70 changes: 52 additions & 18 deletions crates/cli/src/op.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use std::{error::Error, fs::File, io::BufReader, path::PathBuf};

use structopt::StructOpt;

use moclib::deser::fits::{from_fits_ivoa, MocIdxType, MocQtyType, MocType, STMocType};
use moclib::elemset::range::MocRanges;
use moclib::hpxranges2d::TimeSpaceMoc;
use moclib::idx::Idx;
use moclib::moc::{
range::{RangeMOC, RangeMocIter},
CellMOCIntoIterator, CellMOCIterator, RangeMOCIntoIterator, RangeMOCIterator,
use moclib::{
deser::fits::{
from_fits_ivoa, multiordermap::sum_from_fits_multiordermap, MocIdxType, MocQtyType, MocType,
STMocType,
},
elemset::range::MocRanges,
hpxranges2d::TimeSpaceMoc,
idx::Idx,
moc::{
range::{RangeMOC, RangeMocIter},
CellMOCIntoIterator, CellMOCIterator, RangeMOCIntoIterator, RangeMOCIterator,
},
moc2d::{range::RangeMOC2Elem, RangeMOC2Iterator},
qty::{Frequency, Hpx, MocQty, Time},
};
use moclib::moc2d::{range::RangeMOC2Elem, RangeMOC2Iterator};
use moclib::qty::{Frequency, Hpx, MocQty, Time};

use crate::input::ReducedInputFormat;
use crate::output::OutputFormat;
use crate::{
input::{self, ReducedInputFormat},
output::OutputFormat,
};

#[derive(StructOpt, Debug)]
pub enum Op {
Expand Down Expand Up @@ -97,9 +101,19 @@ pub enum Op {
#[structopt(name = "sfold")]
/// Returns the union of the T-MOCs associated to S-MOCs intersecting the given S-MOC. Left: S-MOC, right: ST-MOC, res: T-MOC.
SpaceFold(Op2Args),
// Add (?):
// * moc contains (exit code=0 + output="true", else exit code=1 + output="false")
// * moc overlaps

#[structopt(name = "momsum")]
/// Returns the sum of the values of the given Multi-Order Map which are in the given MOC.
MultiOrderMapSum {
#[structopt(parse(from_os_str))]
/// Input Multi-Order healpix Map FITS file in which the value is a density of probability.
mom: PathBuf,
#[structopt(parse(from_os_str))]
/// Input MOC FITS file.
moc: PathBuf,
}, // Add (?):
// * moc contains (exit code=0 + output="true", else exit code=1 + output="false")
// * moc overlaps
}

impl Op {
Expand Down Expand Up @@ -131,6 +145,26 @@ impl Op {
Op::Minus(op) => op.exec(Op2::Minus),
Op::TimeFold(op) => op.exec(Op2::TimeFold),
Op::SpaceFold(op) => op.exec(Op2::SpaceFold),
Op::MultiOrderMapSum { mom, moc } => input::from_fits_file(moc).and_then(|moc| {
let moc = match moc {
MocIdxType::U16(moc) => match moc {
MocQtyType::Hpx(moc) => moc.collect_to_u64::<Hpx<u64>>(),
_ => return Err("Input MOC is not a S-MOC!".to_string().into()),
},
MocIdxType::U32(moc) => match moc {
MocQtyType::Hpx(moc) => moc.collect_to_u64::<Hpx<u64>>(),
_ => return Err("Input MOC is not a S-MOC!".to_string().into()),
},
MocIdxType::U64(moc) => match moc {
MocQtyType::Hpx(moc) => moc.collect(),
_ => return Err("Input MOC is not a S-MOC!".to_string().into()),
},
};
let f = File::open(mom)?;
let sum = sum_from_fits_multiordermap(BufReader::new(f), &moc)?;
println!("{}", sum);
Ok(())
}),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use moclib::{
qty::Hpx,
};

use crate::input::{fmt_from_extension, InputFormat};
use crate::input::InputFormat;

#[derive(Debug)]
pub struct Bound(RangeInclusive<f64>);
Expand Down Expand Up @@ -131,7 +131,7 @@ impl View {
} else {
let input_fmt = match self.input_fmt {
Some(input_fmt) => Ok(input_fmt),
None => fmt_from_extension(&path),
None => InputFormat::from_extension(&path),
}?;
let f = File::open(path)?;
exec(
Expand Down

0 comments on commit 5ae3c1f

Please sign in to comment.