Skip to content

Commit

Permalink
Merge pull request #49 from MWATelescope/digital_gains_plus
Browse files Browse the repository at this point in the history
API changes and adding DUT1
  • Loading branch information
gsleap authored Jun 23, 2022
2 parents d3d404b + 2e880f6 commit 4dcd033
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 55 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Changes in each release are listed below.

## Unreleased
* Provide DUT1 in the metafits context.
* API tweaks surrounding generic types.
* Things should be slightly more flexible than before.

## 0.14.0 13-Apr-2022 (Pre-release)
* Specify minimum-required dependency versions.
* Clean up fits long-string functions.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tempdir = "0.3.6"

[build-dependencies]
built = "0.5.0"
cbindgen = "0.21.0"
cbindgen = "0.24.0"

[[example]]
name = "mwalib-data-dump"
Expand Down
8 changes: 4 additions & 4 deletions src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ impl LegacyConversionBaseline {
baseline,
ant1,
ant2,
xx_index: xx.abs() as usize,
xx_index: xx.unsigned_abs() as usize,
xx_conjugate: xx < 0,
xy_index: xy.abs() as usize,
xy_index: xy.unsigned_abs() as usize,
xy_conjugate: xy < 0,
yx_index: yx.abs() as usize,
yx_index: yx.unsigned_abs() as usize,
yx_conjugate: yx < 0,
yy_index: yy.abs() as usize,
yy_index: yy.unsigned_abs() as usize,
yy_conjugate: yy < 0,
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/correlator_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The main interface to MWA data.
*/
use std::collections::BTreeMap;
use std::fmt;
use std::path::Path;

use crate::coarse_channel::*;
use crate::convert::*;
Expand Down Expand Up @@ -132,9 +133,16 @@ impl CorrelatorContext {
/// * Result containing a populated CorrelatorContext object if Ok.
///
///
pub fn new<T: AsRef<std::path::Path>>(
metafits_filename: T,
gpubox_filenames: &[T],
pub fn new<P: AsRef<Path>, P2: AsRef<Path>>(
metafits_filename: P,
gpubox_filenames: &[P2],
) -> Result<Self, MwalibError> {
Self::new_inner(metafits_filename.as_ref(), gpubox_filenames)
}

fn new_inner<P: AsRef<Path>>(
metafits_filename: &Path,
gpubox_filenames: &[P],
) -> Result<Self, MwalibError> {
let mut metafits_context = MetafitsContext::new_internal(metafits_filename)?;

Expand Down
4 changes: 2 additions & 2 deletions src/correlator_context/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
/*!
Unit tests for correlator context
*/
#[cfg(test)]
use super::*;
use float_cmp::*;
use std::path::PathBuf;

#[test]
fn test_context_new_with_only_non000_batch() {
Expand Down Expand Up @@ -63,7 +63,7 @@ fn test_context_new_with_000_and_001_batch() {
#[test]
fn test_context_new_missing_gpubox_files() {
let metafits_filename = "test_files/1101503312_1_timestep/1101503312.metafits";
let gpuboxfiles = Vec::new();
let gpuboxfiles: Vec<PathBuf> = Vec::new();

// No gpubox files provided
let context = CorrelatorContext::new(metafits_filename, &gpuboxfiles);
Expand Down
19 changes: 12 additions & 7 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,9 @@ pub struct MetafitsMetadata {
pub sched_end_gps_time_ms: u64,
/// Scheduled duration of observation
pub sched_duration_ms: u64,
/// DUT1 (i.e. UTC-UT1). The UTC of the obsid is used to determine this
/// value. Calculated by astropy. Made optional for compatibility.
pub dut1: f64,
/// Seconds of bad data after observation starts
pub quack_time_duration_ms: u64,
/// OBSID+QUACKTIM as Unix timestamp (first good timestep)
Expand Down Expand Up @@ -1662,6 +1665,7 @@ pub unsafe extern "C" fn mwalib_metafits_metadata_get(
sched_start_mjd,
sched_end_mjd,
sched_duration_ms,
dut1,
ra_tile_pointing_degrees,
dec_tile_pointing_degrees,
ra_phase_center_degrees,
Expand Down Expand Up @@ -1709,18 +1713,18 @@ pub unsafe extern "C" fn mwalib_metafits_metadata_get(
num_rf_inputs,
rf_inputs: _, // This is populated seperately
num_ant_pols,
num_baselines,
baselines: _, // This is populated seperately
num_visibility_pols,
metafits_timesteps: _, // This is populated seperately
num_metafits_timesteps,
metafits_fine_chan_freqs_hz,
num_metafits_fine_chan_freqs,
metafits_coarse_chans: _, // This is populated seperately
metafits_timesteps: _, // This is populated seperately
num_metafits_coarse_chans,
metafits_coarse_chans: _, // This is populated seperately
num_metafits_fine_chan_freqs,
metafits_fine_chan_freqs_hz,
obs_bandwidth_hz,
coarse_chan_width_hz,
centre_freq_hz,
num_baselines,
baselines: _, // This is populated seperately
num_visibility_pols,
metafits_filename,
} = metafits_context;
MetafitsMetadata {
Expand Down Expand Up @@ -1778,6 +1782,7 @@ pub unsafe extern "C" fn mwalib_metafits_metadata_get(
sched_start_gps_time_ms: *sched_start_gps_time_ms,
sched_end_gps_time_ms: *sched_end_gps_time_ms,
sched_duration_ms: *sched_duration_ms,
dut1: dut1.unwrap_or(0.0),
quack_time_duration_ms: *quack_time_duration_ms,
good_time_unix_ms: *good_time_unix_ms,
good_time_gps_ms: *good_time_gps_ms,
Expand Down
32 changes: 14 additions & 18 deletions src/ffi/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1741,18 +1741,16 @@ fn test_mwalib_metafits_metadata_get_from_metafits_context_legacy_vcs_valid() {
//
// Test antennas
//
let item: Vec<Antenna> =
let items: Vec<Antenna> =
ffi_boxed_slice_to_array(metafits_metadata.antennas, metafits_metadata.num_ants);

assert_eq!(item.len(), 128, "Array length is not correct");

for i in 0..128 {
if item[i].tile_id == 154 {
assert_eq!(item[i].rfinput_y, 1);
}
assert_eq!(items.len(), 128, "Array length is not correct");

if item[i].tile_id == 104 {
assert_eq!(item[i].rfinput_y, 0);
for item in items {
if item.tile_id == 154 {
assert_eq!(item.rfinput_y, 1);
} else if item.tile_id == 104 {
assert_eq!(item.rfinput_y, 0);
}
}
}
Expand Down Expand Up @@ -1872,18 +1870,16 @@ fn test_mwalib_metafits_metadata_get_from_voltage_context_valid() {
//
// Test antennas
//
let item: Vec<Antenna> =
let items: Vec<Antenna> =
ffi_boxed_slice_to_array(metafits_metadata.antennas, metafits_metadata.num_ants);

assert_eq!(item.len(), 128, "Array length is not correct");

for i in 0..128 {
if item[i].tile_id == 154 {
assert_eq!(item[i].rfinput_y, 1);
}
assert_eq!(items.len(), 128, "Array length is not correct");

if item[i].tile_id == 104 {
assert_eq!(item[i].rfinput_y, 0);
for item in items {
if item.tile_id == 154 {
assert_eq!(item.rfinput_y, 1);
} else if item.tile_id == 104 {
assert_eq!(item.rfinput_y, 0);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod voltage_files;

////////////////////////////////////////////////////////////////////////////
/// NOTE: the below constants are here for FFI compatibility
/// If you are using `mwa_rust_core`, then it's recommended to use
/// If you are using `Marlu`, then it's recommended to use
/// the constants from that library
///
/// The MWA's latitude on Earth in radians. This is -26d42m11.94986s.
Expand Down
24 changes: 18 additions & 6 deletions src/metafits_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
/*!
The main interface to MWA data.
*/
use std::fmt;
use std::path::Path;

use chrono::{DateTime, Duration, FixedOffset};
use num_derive::FromPrimitive;
use std::fmt;

use crate::antenna::*;
use crate::baseline::*;
Expand Down Expand Up @@ -331,6 +333,9 @@ pub struct MetafitsContext {
pub sched_end_mjd: f64,
/// Scheduled duration of observation
pub sched_duration_ms: u64,
/// DUT1 (i.e. UTC-UT1). The UTC of the obsid is used to determine this
/// value. Calculated by astropy. Made optional for compatibility.
pub dut1: Option<f64>,
/// RA tile pointing
pub ra_tile_pointing_degrees: f64,
/// DEC tile pointing
Expand Down Expand Up @@ -467,10 +472,14 @@ impl MetafitsContext {
/// * Result containing a populated MetafitsContext object if Ok.
///
///
pub fn new<T: AsRef<std::path::Path>>(
metafits: T,
pub fn new<P: AsRef<Path>>(
metafits: P,
mwa_version: Option<MWAVersion>,
) -> Result<Self, MwalibError> {
Self::new_inner(metafits.as_ref(), mwa_version)
}

fn new_inner(metafits: &Path, mwa_version: Option<MWAVersion>) -> Result<Self, MwalibError> {
// Call the internal new metafits method
let mut new_context = MetafitsContext::new_internal(metafits)?;

Expand Down Expand Up @@ -660,6 +669,8 @@ impl MetafitsContext {
ex * 1000
};

let dut1: Option<f64> = get_optional_fits_key!(&mut metafits_fptr, &metafits_hdu, "DUT1")?;

let num_metafits_timesteps: usize = 0;
let metafits_timesteps: Vec<TimeStep> = Vec::new();

Expand Down Expand Up @@ -830,6 +841,7 @@ impl MetafitsContext {
sched_start_mjd: scheduled_start_mjd,
sched_end_mjd: scheduled_end_mjd,
sched_duration_ms: scheduled_duration_ms,
dut1,
ra_tile_pointing_degrees,
dec_tile_pointing_degrees,
ra_phase_center_degrees,
Expand Down Expand Up @@ -877,19 +889,19 @@ impl MetafitsContext {
num_rf_inputs,
rf_inputs,
num_ant_pols: num_antenna_pols,
num_metafits_timesteps,
metafits_timesteps,
num_metafits_coarse_chans,
metafits_coarse_chans,
num_metafits_fine_chan_freqs,
metafits_fine_chan_freqs_hz: metafits_fine_chan_freqs,
num_metafits_timesteps,
metafits_timesteps,
obs_bandwidth_hz: metafits_observation_bandwidth_hz,
coarse_chan_width_hz: metafits_coarse_chan_width_hz,
centre_freq_hz,
metafits_filename,
num_baselines,
baselines,
num_visibility_pols,
metafits_filename,
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/metafits_context/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn test_metafits_context_new_corrlegacy_valid() {
assert_eq!(context.delays[15], 0);

// Calibrator
assert_eq!(context.calibrator, false);
assert!(!context.calibrator);
assert_eq!(context.calibrator_source, "");

// Global attenuation: 1 dB,
Expand Down
14 changes: 11 additions & 3 deletions src/voltage_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::fmt;
use std::fs::File;
use std::io::BufReader;
use std::io::{Read, Seek, SeekFrom};
use std::path::Path;

#[cfg(test)]
pub(crate) mod test; // It's pub crate because I reuse some test code in the ffi tests.
Expand Down Expand Up @@ -153,9 +154,16 @@ impl VoltageContext {
/// * Result containing a populated VoltageContext object if Ok.
///
///
pub fn new<T: AsRef<std::path::Path>>(
metafits_filename: T,
voltage_filenames: &[T],
pub fn new<P: AsRef<Path>, P2: AsRef<Path>>(
metafits_filename: P,
voltage_filenames: &[P2],
) -> Result<Self, MwalibError> {
Self::new_inner(metafits_filename.as_ref(), voltage_filenames)
}

fn new_inner<P: AsRef<Path>>(
metafits_filename: &Path,
voltage_filenames: &[P],
) -> Result<Self, MwalibError> {
let mut metafits_context = MetafitsContext::new_internal(metafits_filename)?;

Expand Down
15 changes: 6 additions & 9 deletions src/voltage_context/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::*;
use float_cmp::*;
use std::fs::File;
use std::io::{Error, Write};
use std::path::PathBuf;
use std::sync::Once;

// Define two static "once" variables to control creation of VCS test data (so it only happens once, the first time it's needed)
Expand Down Expand Up @@ -150,8 +151,6 @@ pub(crate) fn get_index_for_location_in_test_voltage_file_legacy(
rfinput_index: usize,
) -> usize {
let num_rfinputs = 2;
let rf: usize;
let fc: usize;

// Note for legacy always only have 1 block (i.e. no concept of a block)
let bytes_per_rfinput = 1;
Expand All @@ -164,10 +163,10 @@ pub(crate) fn get_index_for_location_in_test_voltage_file_legacy(
let s = sample_index * bytes_per_sample;

// Now within the sample, move to the correct fine chan
fc = fine_chan_index * bytes_per_fine_chan;
let fc = fine_chan_index * bytes_per_fine_chan;

// Now within the fine channel get the rf_input
rf = rfinput_index * bytes_per_rfinput;
let rf = rfinput_index * bytes_per_rfinput;

// Return the correct index
s + rf + fc
Expand All @@ -182,8 +181,6 @@ pub(crate) fn get_index_for_location_in_test_voltage_file_mwaxv2(
) -> usize {
let num_finechan = 1;
let num_rfinputs = 2;
let vb: usize;
let rf: usize;

let bytes_per_fine_chan = 64000 * 2;

Expand All @@ -192,10 +189,10 @@ pub(crate) fn get_index_for_location_in_test_voltage_file_mwaxv2(
let bytes_per_voltage_block = num_rfinputs * bytes_per_rfinput;

// This will position us at the correct block
vb = voltage_block_index * bytes_per_voltage_block;
let vb = voltage_block_index * bytes_per_voltage_block;

// Now within the block, move to the correct rf_input
rf = rfinput_index * bytes_per_rfinput;
let rf = rfinput_index * bytes_per_rfinput;

// Return the correct index
vb + rf + (sample_index * 2) + value_index
Expand Down Expand Up @@ -308,7 +305,7 @@ pub(crate) fn get_test_voltage_context(mwa_version: MWAVersion) -> VoltageContex
#[test]
fn test_context_new_missing_voltage_files() {
let metafits_filename = "test_files/1101503312_1_timestep/1101503312.metafits";
let voltagefiles = Vec::new();
let voltagefiles: Vec<PathBuf> = Vec::new();

// No gpubox files provided
let context = VoltageContext::new(&metafits_filename, &voltagefiles);
Expand Down

0 comments on commit 4dcd033

Please sign in to comment.