Skip to content

Commit

Permalink
Antenna rf_input_x and rf_input_y are now correctly indexed/sorted fo…
Browse files Browse the repository at this point in the history
…r the VCSLegacyRecombined case.
  • Loading branch information
gsleap committed Aug 12, 2021
1 parent 3a7566b commit 8461b54
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Changes in each release are listed below.

## 0.9.3 10-Aug-2021 (Pre-release)
* Antenna rf_input_x and rf_input_y are now correctly indexed/sorted for the VCSLegacyRecombined case.

## 0.9.2 09-Aug-2021 (Pre-release)
* When using a stand-alone MetafitsContext, the rf_inputs are now correctly sorted for the VCSLegacyRecombined case.

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mwalib"
version = "0.9.2"
version = "0.9.3"
homepage = "https://github.com/MWATelescope/mwalib"
repository = "https://github.com/MWATelescope/mwalib"
readme = "README.md"
Expand Down
18 changes: 16 additions & 2 deletions src/antenna/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,23 @@ impl Antenna {
///
pub(crate) fn populate_antennas(rf_inputs: &[Rfinput]) -> Vec<Antenna> {
let mut antennas: Vec<Antenna> = Vec::with_capacity(rf_inputs.len() / 2);
for index in (0..rf_inputs.len()).step_by(2) {
antennas.push(Antenna::new(&rf_inputs[index], &rf_inputs[index + 1]));

// Loop through the rf inputs
for x_index in 0..rf_inputs.len() {
// Find the X pol
if rf_inputs[x_index].pol == Pol::X {
// Find the Y pol
for y_index in 0..rf_inputs.len() {
if rf_inputs[y_index].pol == Pol::Y
&& rf_inputs[x_index].tile_id == rf_inputs[y_index].tile_id
{
// Create the antenna
antennas.push(Antenna::new(&rf_inputs[x_index], &rf_inputs[y_index]));
}
}
}
}

antennas
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,8 +1527,16 @@ pub unsafe extern "C" fn mwalib_metafits_metadata_get(
ant: *ant,
tile_id: *tile_id,
tile_name: CString::new(tile_name.as_str()).unwrap().into_raw(),
rfinput_x: rfinput_x.subfile_order as usize,
rfinput_y: rfinput_y.subfile_order as usize,
rfinput_x: metafits_context
.rf_inputs
.iter()
.position(|x| x == rfinput_x)
.unwrap(),
rfinput_y: metafits_context
.rf_inputs
.iter()
.position(|y| y == rfinput_y)
.unwrap(),
electrical_length_m: *electrical_length_m,
north_m: *north_m,
east_m: *east_m,
Expand Down
89 changes: 81 additions & 8 deletions src/ffi/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1686,7 +1686,69 @@ fn test_mwalib_metafits_metadata_get_from_metafits_context_valid() {
assert_eq!(item[0].unix_time_ms, 1_417_468_096_000);
assert_eq!(item[55].unix_time_ms, 1_417_468_206_000);

// Note- don't try to do any free's here since, in order to test, we have reconstituded some of the arrays
// Note- don't try to do any free's here since, in order to test, we have had to reconstituded some of the arrays which will result in a double free
}
}

#[test]
fn test_mwalib_metafits_metadata_get_from_metafits_context_legacy_vcs_valid() {
// This tests for a valid metafits context and metadata returned
let error_len: size_t = 128;
let error_message = CString::new(" ".repeat(error_len)).unwrap();
let error_message_ptr = error_message.as_ptr() as *const c_char;
// Create a MetafitsContext
let metafits_context_ptr: *mut MetafitsContext =
get_test_ffi_metafits_context(MWAVersion::VCSLegacyRecombined);
unsafe {
// Check we got valid MetafitsContext pointer
let context_ptr = metafits_context_ptr.as_mut();
assert!(context_ptr.is_some());

// Populate a mwalibMetafitsMetadata struct
let mut metafits_metadata_ptr: &mut *mut MetafitsMetadata = &mut std::ptr::null_mut();
let retval = mwalib_metafits_metadata_get(
metafits_context_ptr,
std::ptr::null_mut(),
std::ptr::null_mut(),
&mut metafits_metadata_ptr,
error_message_ptr,
error_len,
);

// Check return value
let mut ret_error_message: String = String::new();

if retval != 0 {
let c_str: &CStr = CStr::from_ptr(error_message_ptr);
let str_slice: &str = c_str.to_str().unwrap();
ret_error_message = str_slice.to_owned();
}
assert_eq!(
retval, 0,
"mwalib_metafits_metadata_get failure {}",
ret_error_message
);

// Get the mwalibMetadata struct from the pointer
let metafits_metadata = Box::from_raw(*metafits_metadata_ptr);

//
// Test antennas
//
let item: 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);
}

if item[i].tile_id == 104 {
assert_eq!(item[i].rfinput_y, 0);
}
}
}
}

Expand Down Expand Up @@ -1801,14 +1863,25 @@ fn test_mwalib_metafits_metadata_get_from_voltage_context_valid() {
// We should get a valid obsid and no error message
assert_eq!(metafits_metadata.obs_id, 1_101_503_312);

// Now ensure we can free the rust memory
assert_eq!(
mwalib_metafits_metadata_free(Box::into_raw(metafits_metadata)),
0
);
//
// Test antennas
//
let item: Vec<Antenna> =
ffi_boxed_slice_to_array(metafits_metadata.antennas, metafits_metadata.num_ants);

// Now ensure we don't panic if we try to free a null pointer
assert_eq!(mwalib_metafits_metadata_free(std::ptr::null_mut()), 0);
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);
}

if item[i].tile_id == 104 {
assert_eq!(item[i].rfinput_y, 0);
}
}

// Note- don't try to do any free's here since, in order to test, we have had to reconstituded some of the arrays which will result in a double free
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/metafits_context/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ fn test_metafits_context_new_vcslegacy_valid() {

assert_eq!(context.volt_fine_chan_width_hz, 10_000);
assert_eq!(context.num_volt_fine_chans_per_coarse, 128);

for i in 0..128 {
if context.antennas[i].tile_id == 154 {
assert_eq!(context.antennas[i].rfinput_y.vcs_order, 1);
}

if context.antennas[i].tile_id == 104 {
assert_eq!(context.antennas[i].rfinput_y.vcs_order, 0);
}
}
}

#[test]
Expand Down
10 changes: 10 additions & 0 deletions src/voltage_context/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@ fn test_context_legacy_v1() {
rf_input_copy.sort_by_key(|k| k.vcs_order);
// Now compare this copy with the 'real' rf_inputs
assert_eq!(&rf_input_copy, &context.metafits_context.rf_inputs);
// Ensure the antenna->rf_input mapping is still in tact
for i in 0..128 {
if context.metafits_context.antennas[i].tile_id == 154 {
assert_eq!(context.metafits_context.antennas[i].rfinput_y.vcs_order, 1);
}

if context.metafits_context.antennas[i].tile_id == 104 {
assert_eq!(context.metafits_context.antennas[i].rfinput_y.vcs_order, 0);
}
}
}

#[test]
Expand Down

0 comments on commit 8461b54

Please sign in to comment.