From 2155c19ad2eebbc6ab2131b012d12e8638d6f493 Mon Sep 17 00:00:00 2001 From: Greg Sleap Date: Thu, 8 Dec 2022 11:16:39 +1100 Subject: [PATCH] Fixes #55 --- CHANGELOG.md | 14 +++++---- src/ffi/mod.rs | 10 +++---- src/metafits_context/mod.rs | 58 ++++++++++++++++++++++-------------- src/metafits_context/test.rs | 12 ++++---- 4 files changed, 55 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 873eec0..2148e2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,15 @@ # Change Log Changes in each release are listed below. -## 0.16.0 22-Nov-2022 (Pre-release) -* FFI/C: `MetafitsMetadata`->`ra_phase_center_deg` is now NaN not 0 when missing from metafits file -* FFI/C: `MetafitsMetadata`->`dec_phase_center_deg` is now NaN not 0 when missing from metafits file - -## 0.16.0 (unreleased) +## 0.16.0 08-Dec-2022 (Pre-release) +* FFI/C: `MetafitsMetadata`->`ra_phase_center_deg` is now NaN not 0 when missing from metafits file. +* FFI/C: `MetafitsMetadata`->`dec_phase_center_deg` is now NaN not 0 when missing from metafits file. +* `MetafitsContext::grid_name` defaults to 'NOGRID' when key is missing from metafits file. +* `MetafitsContext::grid_number` defaults to 0 when key is missing from metafits file. +* `MetafitsContext::sun_alt_deg` is now an Option to handle cases where key is missing from metafits file. Via FFI/C it is represented by a NaN. +* `MetafitsContext::sun_distance_deg` is now an Option to handle cases where key is missing from metafits file. Via FFI/C it is represented by a NaN. +* `MetafitsContext::moon_distance_deg` is now an Option to handle cases where key is missing from metafits file. Via FFI/C it is represented by a NaN. +* `MetafitsContext::jupiter_distance_deg` is now an Option to handle cases where key is missing from metafits file. Via FFI/C it is represented by a NaN. * Update module docs. * Update dependencies. * Fixed a bunch of clippy lints. diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs index cfb1b8f..44f7ba6 100644 --- a/src/ffi/mod.rs +++ b/src/ffi/mod.rs @@ -1730,17 +1730,17 @@ pub unsafe extern "C" fn mwalib_metafits_metadata_get( az_rad: *az_rad, alt_rad: *alt_rad, za_rad: *za_rad, - sun_alt_deg: *sun_alt_deg, - sun_distance_deg: *sun_distance_deg, - moon_distance_deg: *moon_distance_deg, - jupiter_distance_deg: *jupiter_distance_deg, + sun_alt_deg: (*sun_alt_deg).unwrap_or(f64::NAN), + sun_distance_deg: (*sun_distance_deg).unwrap_or(f64::NAN), + moon_distance_deg: (*moon_distance_deg).unwrap_or(f64::NAN), + jupiter_distance_deg: (*jupiter_distance_deg).unwrap_or(f64::NAN), lst_deg: *lst_degrees, lst_rad: *lst_radians, hour_angle_string: CString::new(String::from(hour_angle_string)) .unwrap() .into_raw(), grid_name: CString::new(String::from(grid_name)).unwrap().into_raw(), - grid_number: grid_number.unwrap_or(-1), + grid_number: *grid_number, creator: CString::new(String::from(creator)).unwrap().into_raw(), project_id: CString::new(String::from(project_id)).unwrap().into_raw(), obs_name: CString::new(String::from(obs_name)).unwrap().into_raw(), diff --git a/src/metafits_context/mod.rs b/src/metafits_context/mod.rs index d417e01..a376666 100644 --- a/src/metafits_context/mod.rs +++ b/src/metafits_context/mod.rs @@ -356,13 +356,13 @@ pub struct MetafitsContext { /// Zenith angle of the pointing centre in radians pub za_rad: f64, /// Altitude of Sun - pub sun_alt_deg: f64, + pub sun_alt_deg: Option, /// Distance from pointing center to Sun - pub sun_distance_deg: f64, + pub sun_distance_deg: Option, /// Distance from pointing center to the Moon - pub moon_distance_deg: f64, + pub moon_distance_deg: Option, /// Distance from pointing center to Jupiter - pub jupiter_distance_deg: f64, + pub jupiter_distance_deg: Option, /// Local Sidereal Time in degrees (at the midpoint of the observation) pub lst_deg: f64, /// Local Sidereal Time in radians (at the midpoint of the observation) @@ -372,7 +372,7 @@ pub struct MetafitsContext { /// GRIDNAME pub grid_name: String, /// GRIDNUM - pub grid_number: Option, + pub grid_number: i32, /// CREATOR pub creator: String, /// PROJECT @@ -701,18 +701,21 @@ impl MetafitsContext { let altitude_degrees: f64 = get_required_fits_key!(&mut metafits_fptr, &metafits_hdu, "ALTITUDE")?; let zenith_angle_degrees: f64 = 90.0 - altitude_degrees; - let sun_altitude_degrees: f64 = - get_required_fits_key!(&mut metafits_fptr, &metafits_hdu, "SUN-ALT")?; - let sun_distance_degrees: f64 = - get_required_fits_key!(&mut metafits_fptr, &metafits_hdu, "SUN-DIST")?; - let moon_distance_degrees: f64 = - get_required_fits_key!(&mut metafits_fptr, &metafits_hdu, "MOONDIST")?; - let jupiter_distance_degrees: f64 = - get_required_fits_key!(&mut metafits_fptr, &metafits_hdu, "JUP-DIST")?; + let sun_altitude_degrees: Option = + get_optional_fits_key!(&mut metafits_fptr, &metafits_hdu, "SUN-ALT")?; + let sun_distance_degrees: Option = + get_optional_fits_key!(&mut metafits_fptr, &metafits_hdu, "SUN-DIST")?; + let moon_distance_degrees: Option = + get_optional_fits_key!(&mut metafits_fptr, &metafits_hdu, "MOONDIST")?; + let jupiter_distance_degrees: Option = + get_optional_fits_key!(&mut metafits_fptr, &metafits_hdu, "JUP-DIST")?; let lst_degrees: f64 = get_required_fits_key!(&mut metafits_fptr, &metafits_hdu, "LST")?; let hour_angle_string = get_required_fits_key!(&mut metafits_fptr, &metafits_hdu, "HA")?; - let grid_name = get_required_fits_key!(&mut metafits_fptr, &metafits_hdu, "GRIDNAME")?; - let grid_number = get_optional_fits_key!(&mut metafits_fptr, &metafits_hdu, "GRIDNUM")?; + let grid_name: String = + get_optional_fits_key!(&mut metafits_fptr, &metafits_hdu, "GRIDNAME")? + .unwrap_or(String::from("NOGRID")); + let grid_number = + get_optional_fits_key!(&mut metafits_fptr, &metafits_hdu, "GRIDNUM")?.unwrap_or(0); let creator = get_required_fits_key!(&mut metafits_fptr, &metafits_hdu, "CREATOR")?; let project_id = get_required_fits_key!(&mut metafits_fptr, &metafits_hdu, "PROJECT")?; let observation_name = @@ -1167,17 +1170,26 @@ impl fmt::Display for MetafitsContext { dppc = Some(self.dec_phase_center_degrees), az = self.az_deg, alt = self.alt_deg, - sun_alt = self.sun_alt_deg, - sun_dis = self.sun_distance_deg, - moon_dis = self.moon_distance_deg, - jup_dis = self.jupiter_distance_deg, + sun_alt = match self.sun_alt_deg { + Some(s) => s.to_string(), + None => String::from("None"), + }, + sun_dis = match self.sun_distance_deg { + Some(s) => s.to_string(), + None => String::from("None"), + }, + moon_dis = match self.moon_distance_deg { + Some(s) => s.to_string(), + None => String::from("None"), + }, + jup_dis = match self.jupiter_distance_deg { + Some(s) => s.to_string(), + None => String::from("None"), + }, lst = self.lst_deg, ha = self.hour_angle_string, grid = self.grid_name, - grid_n = match self.grid_number { - Some(g) => g.to_string(), - None => String::from("None"), - }, + grid_n = self.grid_number.to_string(), calib = self.calibrator, calsrc = self.calibrator_source, n_ants = self.num_ants, diff --git a/src/metafits_context/test.rs b/src/metafits_context/test.rs index 1b9a9cf..c5a4472 100644 --- a/src/metafits_context/test.rs +++ b/src/metafits_context/test.rs @@ -153,7 +153,7 @@ fn test_metafits_context_new_corrlegacy_valid() { // Sun altitude: -1.53222775573148 degrees, assert!(approx_eq!( f64, - context.sun_alt_deg, + context.sun_alt_deg.unwrap(), -1.532_227_755_731_48, F64Margin::default() )); @@ -161,7 +161,7 @@ fn test_metafits_context_new_corrlegacy_valid() { // Sun distance: 91.5322277557315 degrees, assert!(approx_eq!( f64, - context.sun_distance_deg, + context.sun_distance_deg.unwrap(), 91.532_227_755_731_5, F64Margin::default() )); @@ -169,7 +169,7 @@ fn test_metafits_context_new_corrlegacy_valid() { // Moon distance: 131.880015235607 degrees, assert!(approx_eq!( f64, - context.moon_distance_deg, + context.moon_distance_deg.unwrap(), 131.880_015_235_607, F64Margin::default() )); @@ -177,7 +177,7 @@ fn test_metafits_context_new_corrlegacy_valid() { // Jupiter distance: 41.401684338269 degrees, assert!(approx_eq!( f64, - context.jupiter_distance_deg, + context.jupiter_distance_deg.unwrap(), 41.401_684_338_269, F64Margin::default() )); @@ -192,10 +192,10 @@ fn test_metafits_context_new_corrlegacy_valid() { // Hour angle: -00:00:00.00 degrees, // Grid name: sweet, - assert_eq!(context.grid_name, "sweet"); + assert_eq!(context.grid_name, String::from("sweet")); // Grid number: 0, - assert_eq!(context.grid_number, Some(0)); + assert_eq!(context.grid_number, 0); // num antennas: 128, assert_eq!(context.num_ants, 128);