1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! Ephemeris data structures and position calculations
use std::fs::File;
use std::io::Read;
use std::str::FromStr;
use crate::config::AppConfig;
use crate::time::JulianDate;
use crate::{Error, Result};
/// 3D position vector
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Position {
/// X coordinate (AU)
pub x: f64,
/// Y coordinate (AU)
pub y: f64,
/// Z coordinate (AU)
pub z: f64,
}
impl Position {
/// Create a new position
pub fn new(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z }
}
/// Calculate distance from origin
pub fn distance(&self) -> f64 {
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
}
/// Represents a celestial body in the ephemeris
#[derive(Debug, Clone)]
pub struct SpaceObject {
/// Whether this object is active
pub active: bool,
/// Header data for this object
header_data: Vec<i32>,
/// Name of the object
pub name: String,
/// Length of coefficients for this object
pub coefficient_length: i32,
}
impl SpaceObject {
fn new(name: String, active: bool) -> Self {
Self {
active,
header_data: Vec::new(),
name,
coefficient_length: 0,
}
}
}
/// Main ephemeris structure
pub struct Ephemeris {
config: AppConfig,
bodies: Vec<SpaceObject>,
start_year: i32,
end_year: i32,
ncoeff: i32,
emrat: f64,
interval: i32,
julian_start: f64,
julian_end: f64,
}
impl Ephemeris {
/// Create a new ephemeris instance
///
/// # Arguments
/// * `config_path` - Path to the config.toml file
///
/// # Example
/// ```ignore
/// use rust_jpl::Ephemeris;
/// let mut eph = Ephemeris::new("config.toml")?;
/// # Ok::<(), rust_jpl::Error>(())
/// ```
pub fn new(config_path: &str) -> Result<Self> {
let config = AppConfig::new(config_path)?;
let mut eph = Self {
config,
bodies: Vec::new(),
start_year: 0,
end_year: 0,
ncoeff: 0,
emrat: 0.0,
interval: 0,
julian_start: 0.0,
julian_end: 0.0,
};
eph.initialize()?;
Ok(eph)
}
/// Initialize the ephemeris by reading configuration files
fn initialize(&mut self) -> Result<()> {
self.read_init_data()?;
self.read_header()?;
self.calculate_coefficient_lengths();
Ok(())
}
/// Read initial data file
fn read_init_data(&mut self) -> Result<()> {
let path = &self.config.initial_data_dat;
let mut indat = File::open(path)?;
let mut buffer = String::new();
indat.read_to_string(&mut buffer)?;
let lines: Vec<&str> = buffer.lines().collect();
let mut i = 0;
while i < lines.len() {
let line = lines[i];
i += 1;
if line == "BODIES:" {
while i < lines.len() {
let body_line = lines[i];
i += 1;
if body_line == "DATE:" {
break;
}
let mut parts = body_line.split_whitespace();
if let Some(name) = parts.next() {
let mut so = SpaceObject::new(name.to_string(), false);
if let Some(state_str) = parts.next() {
so.active = bool::from_str(state_str).unwrap_or(false);
}
self.bodies.push(so);
}
}
}
if line == "DATE:" {
if i < lines.len() {
let start_year_str = lines[i];
i += 1;
if start_year_str.starts_with("Start_year") {
self.start_year = start_year_str
.split_whitespace()
.last()
.and_then(|s| i32::from_str(s).ok())
.unwrap_or(0);
}
}
if i < lines.len() {
let end_year_str = lines[i];
i += 1;
if end_year_str.starts_with("End_year") {
self.end_year = end_year_str
.split_whitespace()
.last()
.and_then(|s| i32::from_str(s).ok())
.unwrap_or(0);
}
}
}
}
Ok(())
}
/// Read header file
fn read_header(&mut self) -> Result<()> {
let path = &self.config.header_441;
let mut header = File::open(path)?;
let mut buffer = String::new();
header.read_to_string(&mut buffer)?;
let lines: Vec<&str> = buffer.lines().collect();
let mut number_emrat = 0;
for line in lines {
let mut parts = line.split_whitespace();
if let Some(key) = parts.next() {
match key {
"NCOEFF=" => {
self.ncoeff = parts
.next()
.and_then(|s| i32::from_str(s).ok())
.unwrap_or(0);
}
"GROUP" => {
if let Some(group_number_str) = parts.next() {
match group_number_str {
"1030" => {
if let Some(start_str) = parts.next() {
self.julian_start = f64::from_str(start_str).unwrap_or(0.0);
}
if let Some(end_str) = parts.next() {
self.julian_end = f64::from_str(end_str).unwrap_or(0.0);
}
if let Some(interval_str) = parts.next() {
self.interval = i32::from_str(interval_str).unwrap_or(0);
}
}
"1040" => {
if let Some(size_str) = parts.next() {
let size = i32::from_str(size_str).unwrap_or(0);
for i in 0..size {
if let Some(tmp) = parts.next() {
if tmp == "EMRAT" {
number_emrat = i;
break;
}
}
}
}
}
"1041" => {
for _ in 0..number_emrat {
parts.next();
}
if let Some(emrat_str) = parts.next() {
let mut emrat = emrat_str.to_string();
let len = emrat.len();
emrat.truncate(len - 4);
emrat.push('E');
self.emrat = f64::from_str(&emrat).unwrap_or(0.0);
}
}
"1050" => {
for _ in 0..3 {
for body in &mut self.bodies {
if let Some(buf_str) = parts.next() {
let buf = i32::from_str(buf_str).unwrap_or(0);
body.header_data.push(buf);
}
}
}
}
_ => {}
}
}
}
_ => {}
}
}
}
Ok(())
}
/// Calculate coefficient lengths for each body
fn calculate_coefficient_lengths(&mut self) {
let len = self.bodies.len();
for i in 0..len - 1 {
if !self.bodies[i].header_data.is_empty() && !self.bodies[i + 1].header_data.is_empty()
{
self.bodies[i].coefficient_length =
self.bodies[i + 1].header_data[0] - self.bodies[i].header_data[0];
}
}
if !self.bodies.is_empty() && !self.bodies[len - 1].header_data.is_empty() {
self.bodies[len - 1].coefficient_length =
self.ncoeff - self.bodies[len - 1].header_data[0];
}
}
/// Get the position of a celestial body at a given Julian date
///
/// # Arguments
/// * `body_name` - Name of the celestial body (e.g., "Earth", "Moon", "Sun", "Mars")
/// * `jd` - Julian date
///
/// # Returns
/// Position in AU (Astronomical Units)
///
/// # Example
/// ```ignore
/// use rust_jpl::{Ephemeris, JulianDate};
/// let mut eph = Ephemeris::new("config.toml")?;
/// let jd = JulianDate::from_calendar(2024, 1, 15, 12, 0, 0.0)?;
/// let position = eph.get_position("Earth", jd)?;
/// # Ok::<(), rust_jpl::Error>(())
/// ```
pub fn get_position(&self, body_name: &str, jd: JulianDate) -> Result<Position> {
// Validate Julian date is within range
if jd.jd < self.julian_start || jd.jd > self.julian_end {
return Err(Error::Ephemeris(format!(
"Julian date {} is outside valid range [{}, {}]",
jd.jd, self.julian_start, self.julian_end
)));
}
// Find the body
let body = self
.bodies
.iter()
.find(|b| {
b.name.eq_ignore_ascii_case(body_name)
|| b.name.replace("_", "").eq_ignore_ascii_case(body_name)
})
.ok_or_else(|| {
Error::Ephemeris(format!(
"Body '{}' not found. Available bodies: {}",
body_name,
self.bodies
.iter()
.map(|b| b.name.clone())
.collect::<Vec<_>>()
.join(", ")
))
})?;
if !body.active {
return Err(Error::Ephemeris(format!(
"Body '{}' is not active in this ephemeris",
body_name
)));
}
// For now, return a placeholder position
// In a full implementation, this would read the binary ephemeris file
// and interpolate the Chebyshev coefficients
Ok(Position::new(0.0, 0.0, 0.0))
}
/// Get all available celestial bodies
pub fn get_bodies(&self) -> Vec<&SpaceObject> {
self.bodies.iter().collect()
}
/// Get the valid date range for this ephemeris
pub fn get_date_range(&self) -> (f64, f64) {
(self.julian_start, self.julian_end)
}
/// Get ephemeris metadata
pub fn get_metadata(&self) -> EphemerisMetadata {
EphemerisMetadata {
start_year: self.start_year,
end_year: self.end_year,
julian_start: self.julian_start,
julian_end: self.julian_end,
interval_days: self.interval as f64,
earth_moon_ratio: self.emrat,
number_of_coefficients: self.ncoeff,
}
}
}
/// Metadata about the ephemeris
#[derive(Debug, Clone)]
pub struct EphemerisMetadata {
pub start_year: i32,
pub end_year: i32,
pub julian_start: f64,
pub julian_end: f64,
pub interval_days: f64,
pub earth_moon_ratio: f64,
pub number_of_coefficients: i32,
}