diff --git a/CHANGELOG.md b/CHANGELOG.md index 154c226..85c14b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Cargo.toml b/Cargo.toml index 75e6d8a..f511f4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/antenna/mod.rs b/src/antenna/mod.rs index 81ceeb4..480dced 100644 --- a/src/antenna/mod.rs +++ b/src/antenna/mod.rs @@ -83,9 +83,23 @@ impl Antenna { /// pub(crate) fn populate_antennas(rf_inputs: &[Rfinput]) -> Vec { let mut antennas: Vec = 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 } } diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs index 2333721..a93bde6 100644 --- a/src/ffi/mod.rs +++ b/src/ffi/mod.rs @@ -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, diff --git a/src/ffi/test.rs b/src/ffi/test.rs index 395f321..d0de0f4 100644 --- a/src/ffi/test.rs +++ b/src/ffi/test.rs @@ -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 = + 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); + } + } } } @@ -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 = + 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 } } diff --git a/src/metafits_context/test.rs b/src/metafits_context/test.rs index 26a0742..4e2b019 100644 --- a/src/metafits_context/test.rs +++ b/src/metafits_context/test.rs @@ -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] diff --git a/src/voltage_context/test.rs b/src/voltage_context/test.rs index 3662347..d4854ea 100644 --- a/src/voltage_context/test.rs +++ b/src/voltage_context/test.rs @@ -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]