#![allow(non_snake_case)]
#![allow(dead_code)]
use super::main_interface::MainInterface;
use once_cell::sync::Lazy;
use pyo3::exceptions::PyException;
use pyo3::prelude::*;
use std::os::raw::c_char;
use std::sync::Mutex;
static EXTEPH_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
extern "C" {
pub fn ExtEphInit(apAddr: i64) -> i32;
pub fn ExtEphGetInfo(infoStr: *const c_char);
pub fn ExtEphLoadFile(extEphFile: *const c_char) -> i32;
pub fn ExtEphSaveFile(extEphFile: *const c_char, saveMode: i32, saveForm: i32) -> i32;
pub fn ExtEphRemoveSat(satKey: i64) -> i32;
pub fn ExtEphRemoveAllSats() -> i32;
pub fn ExtEphGetCount() -> i32;
pub fn ExtEphGetLoaded(order: i32, satKeys: *mut i64);
pub fn ExtEphAddSat(satNum: i32, epochDs50UTC: f64, ae: f64, ke: f64, coordSys: i32) -> i64;
pub fn ExtEphAddSatEphem(satKey: i64, ds50UTC: f64, pos: *const [f64; 3], vel: *const [f64; 3], revNum: i32)
-> i32;
pub fn ExtEphAddSatEphemCovMtx(
satKey: i64,
ds50UTC: f64,
pos: *const [f64; 3],
vel: *const [f64; 3],
revNum: i32,
covUVW: *const [f64; 21],
) -> i32;
pub fn ExtEphAddSatEphemExt(
satKey: i64,
ds50UTC: f64,
pos: *const [f64; 3],
vel: *const [f64; 3],
revNum: i32,
extArr: *const [f64; 128],
) -> i32;
pub fn ExtEphAddSatFrFile(extEphFile: *const c_char) -> i64;
pub fn ExtEphGetNumPts(satKey: i64, numOfPts: *mut i32) -> i32;
pub fn ExtEphGetAllFields(
satKey: i64,
satNum: *mut i32,
satName: *const c_char,
recName: *const c_char,
epochDs50UTC: *mut f64,
ae: *mut f64,
ke: *mut f64,
pos: *mut [f64; 3],
vel: *mut [f64; 3],
coordSys: *mut i32,
numOfPts: *mut i32,
fileLoc: *const c_char,
) -> i32;
pub fn ExtEphGetField(satKey: i64, xf_ExtEph: i32, valueStr: *const c_char) -> i32;
pub fn ExtEphSetField(satKey: i64, xf_ExtEph: i32, valueStr: *const c_char) -> i32;
pub fn ExtEphStartEndTime(satKey: i64, startDs50UTC: *mut f64, endDs50UTC: *mut f64) -> i32;
pub fn ExtEphGetEphemeris(
satKey: i64,
index: i32,
ds50UTC: *mut f64,
pos: *mut [f64; 3],
vel: *mut [f64; 3],
revNum: *mut i32,
) -> i32;
pub fn ExtEphGetCovMtx(
satKey: i64,
index: i32,
ds50UTC: *mut f64,
pos: *mut [f64; 3],
vel: *mut [f64; 3],
revNum: *mut i32,
covMtx: *mut [[f64; 6]; 6],
) -> i32;
pub fn ExtEphMse(
satKey: i64,
mse: f64,
ds50UTC: *mut f64,
pos: *mut [f64; 3],
vel: *mut [f64; 3],
revNum: *mut i32,
) -> i32;
pub fn ExtEphMseCovMtx(
satKey: i64,
mse: f64,
ds50UTC: *mut f64,
pos: *mut [f64; 3],
vel: *mut [f64; 3],
revNum: *mut i32,
covMtx: *mut [[f64; 6]; 6],
) -> i32;
pub fn ExtEphDs50UTC(
satKey: i64,
ds50UTC: f64,
mse: *mut f64,
pos: *mut [f64; 3],
vel: *mut [f64; 3],
revNum: *mut i32,
) -> i32;
pub fn ExtEphDs50UTCCovMtx(
satKey: i64,
ds50UTC: f64,
mse: *mut f64,
pos: *mut [f64; 3],
vel: *mut [f64; 3],
revNum: *mut i32,
covMtx: *mut [[f64; 6]; 6],
) -> i32;
pub fn ExtEphXten(satKey: i64, xf_getEph: i32, inVal: f64, extArr: *mut [f64; 128]) -> i32;
pub fn ExtEphGetLine(satKey: i64, line: *const c_char) -> i32;
pub fn ExtEphGetSatKey(satNum: i32) -> i64;
pub fn ExtEphFieldsToSatKey(satNum: i32, epochDtg: *const c_char) -> i64;
}
pub static COORD_ECI: i32 = 1;
pub static COORD_J2K: i32 = 2;
pub static COORD_EFG: i32 = 3;
pub static COORD_ECR: i32 = 4;
pub static COORD_LLH: i32 = 5;
pub static COORD_SEN: i32 = 6;
pub static COORD_ECIFP: i32 = 11;
pub static COORD_J2KFP: i32 = 12;
pub static COORD_EFGFP: i32 = 13;
pub static COORD_ECRFP: i32 = 14;
pub static COORD_LLHOV: i32 = 15;
pub static COORD_SENOV: i32 = 16;
pub static COORD_HCSRL: i32 = 17;
pub static COORD_WPTRL: i32 = 18;
pub static COORD_HCSGC: i32 = 19;
pub static COORD_WPTGC: i32 = 20;
pub static COORD_INVALID: i32 = 100;
pub static COVMTX_UVW_DATE: i32 = 0;
pub static COVMTX_XYZ_DATE: i32 = 10;
pub static COVMTX_EQNX_DATE: i32 = 20;
pub static COVMTX_UVW_J2K: i32 = 30;
pub static COVMTX_XYZ_J2K: i32 = 40;
pub static COVMTX_EQNX_J2K: i32 = 50;
pub static XF_GETEPH_MSE: i32 = 1;
pub static XF_GETEPH_UTC: i32 = 2;
pub static XF_GETEPH_IDX: i32 = 3;
pub static XF_EXTEPH_SATNUM: i32 = 1;
pub static XF_EXTEPH_EPOCH: i32 = 2;
pub static XF_EXTEPH_AE: i32 = 3;
pub static XF_EXTEPH_KE: i32 = 4;
pub static XF_EXTEPH_POSX: i32 = 5;
pub static XF_EXTEPH_POSY: i32 = 6;
pub static XF_EXTEPH_POSZ: i32 = 7;
pub static XF_EXTEPH_VELX: i32 = 8;
pub static XF_EXTEPH_VELY: i32 = 9;
pub static XF_EXTEPH_VELZ: i32 = 10;
pub static XF_EXTEPH_COORD: i32 = 11;
pub static XF_EXTEPH_NUMOFPTS: i32 = 12;
pub static XF_EXTEPH_FILEPATH: i32 = 13;
pub static XF_EXTEPH_SATNAME: i32 = 14;
pub static XF_EXTEPH_RECNAME: i32 = 15;
pub fn add_satellite(sat_num: i32, epoch_ds50_utc: f64, ae: f64, ke: f64, coord_sys: i32) -> i64 {
unsafe { ExtEphAddSat(sat_num, epoch_ds50_utc, ae, ke, coord_sys) }
}
pub fn add_satellite_state(
sat_key: i64,
ds50_utc: f64,
pos: &[f64; 3],
vel: &[f64; 3],
cov: Option<&[f64; 21]>,
) -> Result<(), String> {
let status = match cov {
Some(cov) => unsafe { ExtEphAddSatEphemCovMtx(sat_key, ds50_utc, pos, vel, 0, cov) },
None => unsafe { ExtEphAddSatEphem(sat_key, ds50_utc, pos, vel, 0) },
};
match status {
0 => Ok(()),
_ => Err(MainInterface::get_last_error_message()),
}
}
pub fn remove_key(key: i64) {
unsafe {
ExtEphRemoveSat(key);
}
}
pub fn get_posvel_at_ds50(key: i64, ds50_utc: f64) -> Result<([f64; 3], [f64; 3]), String> {
let mut pos = [0.0; 3];
let mut vel = [0.0; 3];
let mut rev_num = 0;
let mut mse = 0.0;
let status = unsafe { ExtEphDs50UTC(key, ds50_utc, &mut mse, &mut pos, &mut vel, &mut rev_num) };
match status {
0 => Ok((pos, vel)),
_ => Err(MainInterface::get_last_error_message()),
}
}
pub fn get_posvel_at_index(key: i64, index: i32) -> Result<([f64; 3], [f64; 3]), String> {
let mut pos = [0.0; 3];
let mut vel = [0.0; 3];
let mut rev_num = 0;
let mut ds50_utc = 0.0;
let status = unsafe { ExtEphGetEphemeris(key, index, &mut ds50_utc, &mut pos, &mut vel, &mut rev_num) };
match status {
0 => Ok((pos, vel)),
_ => Err(MainInterface::get_last_error_message()),
}
}
pub fn get_number_of_states(key: i64) -> Result<i32, String> {
let mut num_of_pts = 0;
let status = unsafe { ExtEphGetNumPts(key, &mut num_of_pts) };
match status {
0 => Ok(num_of_pts),
_ => Err(MainInterface::get_last_error_message()),
}
}
pub fn get_ds50_utc_range(key: i64) -> Result<(f64, f64), String> {
let mut start_ds50_utc = 0.0;
let mut end_ds50_utc = 0.0;
let status = unsafe { ExtEphStartEndTime(key, &mut start_ds50_utc, &mut end_ds50_utc) };
match status {
0 => Ok((start_ds50_utc, end_ds50_utc)),
_ => Err(MainInterface::get_last_error_message()),
}
}
#[pyclass]
#[derive(Debug, Clone, PartialEq)]
pub struct EphemInterface {}
impl EphemInterface {
pub fn add_satellite(sat_num: i32, epoch_ds50_utc: f64, ae: f64, ke: f64, coord_sys: i32) -> Result<i64, String> {
let _lock = EXTEPH_LOCK.lock().unwrap();
let key = unsafe { ExtEphAddSat(sat_num, epoch_ds50_utc, ae, ke, coord_sys) };
if key >= 0 {
Ok(key)
} else {
Err(MainInterface::get_last_error_message())
}
}
pub fn add_state(
sat_key: i64,
ds50_utc: f64,
pos: &[f64; 3],
vel: &[f64; 3],
cov: Option<&[f64; 21]>,
) -> Result<(), String> {
let status = match cov {
Some(cov) => unsafe { ExtEphAddSatEphemCovMtx(sat_key, ds50_utc, pos, vel, 0, cov) },
None => unsafe { ExtEphAddSatEphem(sat_key, ds50_utc, pos, vel, 0) },
};
match status {
0 => Ok(()),
_ => Err(MainInterface::get_last_error_message()),
}
}
pub fn get_posvel_at_ds50(key: i64, ds50_utc: f64) -> Result<([f64; 3], [f64; 3]), String> {
let mut pos = [0.0; 3];
let mut vel = [0.0; 3];
let mut rev_num = 0;
let mut mse = 0.0;
let status = unsafe { ExtEphDs50UTC(key, ds50_utc, &mut mse, &mut pos, &mut vel, &mut rev_num) };
match status {
0 => Ok((pos, vel)),
_ => Err(MainInterface::get_last_error_message()),
}
}
pub fn get_state_at_index(key: i64, index: i32) -> Result<(f64, [f64; 3], [f64; 3], [[f64; 6]; 6]), String> {
let mut pos = [0.0; 3];
let mut vel = [0.0; 3];
let mut cov = [[0.0; 6]; 6];
let mut rev_num = 0;
let mut ds50_utc = 0.0;
let status = unsafe { ExtEphGetCovMtx(key, index, &mut ds50_utc, &mut pos, &mut vel, &mut rev_num, &mut cov) };
match status {
0 => Ok((ds50_utc, pos, vel, cov)),
_ => Err(MainInterface::get_last_error_message()),
}
}
pub fn get_number_of_states(key: i64) -> Result<i32, String> {
let mut num_of_pts = 0;
let status = unsafe { ExtEphGetNumPts(key, &mut num_of_pts) };
match status {
0 => Ok(num_of_pts),
_ => Err(MainInterface::get_last_error_message()),
}
}
pub fn get_ds50_utc_range(key: i64) -> Result<(f64, f64), String> {
let mut start_ds50_utc = 0.0;
let mut end_ds50_utc = 0.0;
let status = unsafe { ExtEphStartEndTime(key, &mut start_ds50_utc, &mut end_ds50_utc) };
match status {
0 => Ok((start_ds50_utc, end_ds50_utc)),
_ => Err(MainInterface::get_last_error_message()),
}
}
pub fn clone(key: i64) -> Result<i64, String> {
let mut sat_num = 0;
let mut sat_name = [0 as c_char; 100];
let mut rec_name = [0 as c_char; 100];
let mut epoch_ds50_utc = 0.0;
let mut ae = 0.0;
let mut ke = 0.0;
let mut pos = [0.0; 3];
let mut vel = [0.0; 3];
let mut coord_sys = 0;
let mut num_of_pts = 0;
let mut file_loc = [0 as c_char; 260];
let status = unsafe {
ExtEphGetAllFields(
key,
&mut sat_num,
sat_name.as_mut_ptr(),
rec_name.as_mut_ptr(),
&mut epoch_ds50_utc,
&mut ae,
&mut ke,
&mut pos,
&mut vel,
&mut coord_sys,
&mut num_of_pts,
file_loc.as_mut_ptr(),
)
};
if status != 0 {
return Err(MainInterface::get_last_error_message());
}
let new_key = EphemInterface::add_satellite(sat_num, epoch_ds50_utc, ae, ke, coord_sys)?;
for i in 0..num_of_pts {
let (ds50, pos, vel, _) = EphemInterface::get_state_at_index(key, i + 1)?;
EphemInterface::add_state(new_key, ds50, &pos, &vel, None)?;
}
Ok(new_key)
}
pub fn remove_key(key: i64) {
unsafe {
ExtEphRemoveSat(key);
}
}
}
#[pymethods]
impl EphemInterface {
#[classattr]
pub const COORD_ECI: i32 = COORD_ECI;
#[classattr]
pub const COORD_J2K: i32 = COORD_J2K;
#[classattr]
pub const COORD_EFG: i32 = COORD_EFG;
#[classattr]
pub const COORD_ECR: i32 = COORD_ECR;
#[staticmethod]
#[pyo3(name = "add_satellite")]
pub fn py_add_satellite(sat_num: i32, epoch_ds50_utc: f64, ae: f64, ke: f64, coord_sys: i32) -> PyResult<i64> {
EphemInterface::add_satellite(sat_num, epoch_ds50_utc, ae, ke, coord_sys).map_err(PyException::new_err)
}
#[staticmethod]
#[pyo3(name = "add_state")]
pub fn py_add_state(
sat_key: i64,
ds50_utc: f64,
pos: [f64; 3],
vel: [f64; 3],
cov: Option<[f64; 21]>,
) -> PyResult<()> {
EphemInterface::add_state(sat_key, ds50_utc, &pos, &vel, cov.as_ref()).map_err(PyException::new_err)
}
#[staticmethod]
#[pyo3(name = "remove_key")]
pub fn py_remove_key(key: i64) {
EphemInterface::remove_key(key);
}
#[staticmethod]
#[pyo3(name = "get_posvel_at_ds50")]
pub fn py_get_posvel_at_ds50(key: i64, ds50_utc: f64) -> PyResult<([f64; 3], [f64; 3])> {
EphemInterface::get_posvel_at_ds50(key, ds50_utc).map_err(PyException::new_err)
}
#[staticmethod]
#[pyo3(name = "get_posvel_at_index")]
pub fn py_get_posvel_at_index(key: i64, index: i32) -> PyResult<(f64, [f64; 3], [f64; 3], [[f64; 6]; 6])> {
EphemInterface::get_state_at_index(key, index).map_err(PyException::new_err)
}
#[staticmethod]
#[pyo3(name = "get_number_of_states")]
pub fn py_get_number_of_states(key: i64) -> PyResult<i32> {
EphemInterface::get_number_of_states(key).map_err(PyException::new_err)
}
}