Skip to content

Commit

Permalink
ndarray fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
d3v-null committed Sep 27, 2024
1 parent d7a7239 commit 73f00b5
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/releasing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Checklist

- [ ] formatting fixed `cargo fmt -- --emit=files`
- [ ] pipeline is green
- [ ] everything has been merged into main
- [ ] update `RELEASES.md`
Expand Down
110 changes: 110 additions & 0 deletions examples/preprocess.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! BYO Array Example
//! This example demonstrates how to use the `birli` library to preprocess and write out a UVFITS file
//! from a set of gpubox files and a metafits file.
//! It uses the `mwalib` and `marlu` libraries to read in the gpubox files and metafits file.
//! It then uses the `birli` library to preprocess the data and write out a UVFITS file.
//!
//! ```bash
//! cargo run --example preprocess \
//! output.uvfits \
//! tests/data/1196175296_mwa_ord/1196175296.metafits \
//! tests/data/1196175296_mwa_ord/1196175296_20171201145440_gpubox01_00.fits
//! ```

use std::path::PathBuf;

use birli::{
flag_to_weight_array,
flags::get_weight_factor,
io::{read_mwalib, write_uvfits},
marlu::{
constants::{
COTTER_MWA_HEIGHT_METRES, COTTER_MWA_LATITUDE_RADIANS, COTTER_MWA_LONGITUDE_RADIANS,
},
mwalib::CorrelatorContext,
LatLngHeight, RADec,
},
FlagContext, PreprocessContext, VisSelection,
};

fn main() {
let args: Vec<String> = std::env::args().collect();
let uvfits_path = PathBuf::from(&args[1]);
let metafits_path = PathBuf::from(&args[2]);
let gpubox_filenames: Vec<PathBuf> = args[3..].iter().map(PathBuf::from).collect();

let corr_ctx = CorrelatorContext::new(metafits_path, &gpubox_filenames)
.expect("unable to get mwalib context");

let vis_sel = VisSelection::from_mwalib(&corr_ctx).unwrap();

let flag_ctx = FlagContext::from_mwalib(&corr_ctx);
let fine_chans_per_coarse = corr_ctx.metafits_context.num_corr_fine_chans_per_coarse;
let mut flag_array = vis_sel.allocate_flags(fine_chans_per_coarse).unwrap();
flag_ctx
.set_flags(
flag_array.view_mut(),
&vis_sel.timestep_range,
&vis_sel.coarse_chan_range,
&vis_sel.get_ant_pairs(&corr_ctx.metafits_context),
)
.unwrap();
let mut jones_array = vis_sel.allocate_jones(fine_chans_per_coarse).unwrap();
read_mwalib(
&vis_sel,
&corr_ctx,
jones_array.view_mut(),
flag_array.view_mut(),
false,
)
.unwrap();

// generate weights
let weight_factor = get_weight_factor(&corr_ctx);
let mut weight_array = flag_to_weight_array(flag_array.view(), weight_factor);

let prep_ctx = PreprocessContext {
array_pos: LatLngHeight {
longitude_rad: COTTER_MWA_LONGITUDE_RADIANS,
latitude_rad: COTTER_MWA_LATITUDE_RADIANS,
height_metres: COTTER_MWA_HEIGHT_METRES,
},
phase_centre: RADec::from_mwalib_phase_or_pointing(&corr_ctx.metafits_context),
correct_van_vleck: true,
correct_cable_lengths: false,
correct_digital_gains: false,
correct_geometry: false,
draw_progress: false,
passband_gains: None,
calsols: None,
#[cfg(feature = "aoflagger")]
aoflagger_strategy: None,
};

prep_ctx
.preprocess(
&corr_ctx,
jones_array.view_mut(),
weight_array.view_mut(),
flag_array.view_mut(),
&vis_sel,
)
.unwrap();

let (avg_time, avg_freq) = (1, 1);

write_uvfits(
&uvfits_path,
&corr_ctx,
jones_array.view(),
weight_array.view(),
&vis_sel.timestep_range,
&vis_sel.coarse_chan_range,
&vis_sel.baseline_idxs,
Some(prep_ctx.array_pos),
Some(prep_ctx.phase_centre),
avg_time,
avg_freq,
)
.unwrap();
}
8 changes: 5 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ impl<'a> BirliContext<'a> {
};

let args_strings = std::env::args().collect_vec();
let cmd_line = shlex::join(args_strings.iter().map(String::as_str));
let cmd_line = shlex::try_join(args_strings.iter().map(String::as_str))?;
let application = format!("{PKG_NAME} {PKG_VERSION}");
let message = prep_ctx.as_comment();
let history = History {
Expand Down Expand Up @@ -3974,8 +3974,10 @@ mod tests_aoflagger_flagset {
assert_eq!(left_header.num_rows, right_header.num_rows);
assert_eq!(left_header.num_rows as usize, num_rows);

let left_flags = $left_flagset.read_flags().unwrap().into_raw_vec();
let right_flags = $right_flagset.read_flags().unwrap().into_raw_vec();
let (left_flags, left_flags_offset) = $left_flagset.read_flags().unwrap().into_raw_vec_and_offset();
assert_eq!(left_flags_offset.unwrap_or(0), 0);
let (right_flags, right_flags_offset) = $right_flagset.read_flags().unwrap().into_raw_vec_and_offset();
assert_eq!(right_flags_offset.unwrap_or(0), 0);
assert_eq!(left_flags.len(), right_flags.len());
assert_eq!(
left_flags.len(),
Expand Down
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Errors that can occur in Birli

use marlu::{io::error::BadArrayShape, mwalib};
#[cfg(feature = "cli")]
use shlex::QuoteError;
use thiserror::Error;

use crate::{
Expand Down Expand Up @@ -73,6 +75,11 @@ pub enum BirliError {
/// Error derived from [`crate::van_vleck::VanVleckCorrection`]
VanVleckCorrection(#[from] VanVleckCorrection),

#[cfg(feature = "cli")]
#[error(transparent)]
/// Error derived from [`shlex::QuoteError`]
QuoteError(#[from] QuoteError),

#[error("You selected dry run")]
/// enum variant for when a dry run is selected
DryRun {},
Expand Down

0 comments on commit 73f00b5

Please sign in to comment.