rsspice 0.1.0

Pure Rust port of the SPICE Toolkit for space geometry
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//
// GENERATED FILE
//

use super::*;
use f2rust_std::*;

const NABCOR: i32 = 15;
const ABATSZ: i32 = 6;
const GEOIDX: i32 = 1;
const LTIDX: i32 = (GEOIDX + 1);
const STLIDX: i32 = (LTIDX + 1);
const CNVIDX: i32 = (STLIDX + 1);
const XMTIDX: i32 = (CNVIDX + 1);
const RELIDX: i32 = (XMTIDX + 1);
const CORLEN: i32 = 5;
const RNAME: &[u8] = b"ZZSPKFLT";
const TOL: f64 = 0.0000000001;
const CNVLIM: f64 = 0.00000000000000001;
const MAXITR: i32 = 5;

struct SaveVars {
    PRVCOR: Vec<u8>,
    PASS1: bool,
    USECN: bool,
    USELT: bool,
    XMIT: bool,
}

impl SaveInit for SaveVars {
    fn new() -> Self {
        let mut PRVCOR = vec![b' '; CORLEN as usize];
        let mut PASS1: bool = false;
        let mut USECN: bool = false;
        let mut USELT: bool = false;
        let mut XMIT: bool = false;

        PASS1 = true;
        fstr::assign(&mut PRVCOR, b" ");

        Self {
            PRVCOR,
            PASS1,
            USECN,
            USELT,
            XMIT,
        }
    }
}

//$Procedure ZZSPKFLT ( SPK function, light time and rate )
pub fn ZZSPKFLT(
    TRGSUB: fn(f64, &[u8], &mut i32, &mut [f64], &mut Context) -> f2rust_std::Result<()>,
    ET: f64,
    REF: &[u8],
    ABCORR: &[u8],
    STOBS: &[f64],
    STARG: &mut [f64],
    LT: &mut f64,
    DLT: &mut f64,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let STOBS = DummyArray::new(STOBS, 1..=6);
    let mut STARG = DummyArrayMut::new(STARG, 1..=6);
    let mut A: f64 = 0.0;
    let mut B: f64 = 0.0;
    let mut C: f64 = 0.0;
    let mut CTRSSB = StackArray::<f64, 6>::new(1..=6);
    let mut DIST: f64 = 0.0;
    let mut EPOCH: f64 = 0.0;
    let mut PRVLT: f64 = 0.0;
    let mut LTERR: f64 = 0.0;
    let mut SSBTRG = StackArray::<f64, 6>::new(1..=6);
    let mut STTCTR = StackArray::<f64, 6>::new(1..=6);
    let mut I: i32 = 0;
    let mut LTSIGN: i32 = 0;
    let mut NUMITR: i32 = 0;
    let mut REFID: i32 = 0;
    let mut TRGCTR: i32 = 0;
    let mut ATTBLK = StackArray::<bool, 15>::new(1..=NABCOR);
    let mut USESTL: bool = false;

    //
    // SPICELIB functions
    //

    //
    // Local parameters
    //

    //
    // TOL is the tolerance used for a division-by-zero test
    // performed prior to computation of DLT.
    //

    //
    // Convergence limit:
    //

    //
    // Maximum number of light time iterations for any
    // aberration correction:
    //

    //
    // Local variables
    //

    //
    // Saved variables
    //

    //
    // Initial values
    //

    //
    // Standard SPICE error handling.
    //
    if RETURN(ctx) {
        return Ok(());
    }

    CHKIN(RNAME, ctx)?;

    if (save.PASS1 || fstr::ne(ABCORR, &save.PRVCOR)) {
        //
        // The aberration correction flag differs from the value it
        // had on the previous call, if any.  Analyze the new flag.
        //
        ZZVALCOR(ABCORR, ATTBLK.as_slice_mut(), ctx)?;

        if FAILED(ctx) {
            CHKOUT(RNAME, ctx)?;
            return Ok(());
        }

        //
        // The aberration correction flag is recognized; save it.
        //
        fstr::assign(&mut save.PRVCOR, ABCORR);

        //
        // Set logical flags indicating the attributes of the requested
        // correction:
        //
        //    XMIT is .TRUE. when the correction is for transmitted
        //    radiation.
        //
        //    USELT is .TRUE. when any type of light time correction
        //    (normal or converged Newtonian) is specified.
        //
        //    USECN indicates converged Newtonian light time correction.
        //
        // The above definitions are consistent with those used by
        // ZZVALCOR.
        //
        save.XMIT = ATTBLK[XMTIDX];
        save.USELT = ATTBLK[LTIDX];
        save.USECN = ATTBLK[CNVIDX];
        USESTL = ATTBLK[STLIDX];

        save.PASS1 = false;
    }

    //
    // See if the reference frame is a recognized inertial frame.
    //
    IRFNUM(REF, &mut REFID, ctx);

    if (REFID == 0) {
        SETMSG(
            b"The requested frame \'#\' is not a recognized inertial frame. ",
            ctx,
        );
        ERRCH(b"#", REF, ctx);
        SIGERR(b"SPICE(UNKNOWNFRAME)", ctx)?;
        CHKOUT(RNAME, ctx)?;
        return Ok(());
    }

    //
    // Find the geometric state of the target body with respect to
    // the solar system barycenter. Subtract the state of the
    // observer to get the relative state. Use this to compute the
    // one-way light time.
    //
    TRGSUB(ET, REF, &mut TRGCTR, STTCTR.as_slice_mut(), ctx)?;
    SPKSSB(TRGCTR, ET, REF, CTRSSB.as_slice_mut(), ctx)?;

    if FAILED(ctx) {
        CHKOUT(RNAME, ctx)?;
        return Ok(());
    }

    VADDG(
        CTRSSB.as_slice(),
        STTCTR.as_slice(),
        6,
        SSBTRG.as_slice_mut(),
    );
    VSUBG(SSBTRG.as_slice(), STOBS.as_slice(), 6, STARG.as_slice_mut());

    DIST = VNORM(STARG.as_slice());
    *LT = (DIST / CLIGHT());

    if (*LT == 0.0) {
        //
        // This can happen only if the observer and target are at the
        // same position. We don't consider this an error, but we're not
        // going to compute the light time derivative.
        //
        *DLT = 0 as f64;

        CHKOUT(RNAME, ctx)?;
        return Ok(());
    }

    if !save.USELT {
        //
        // This is a special case: we're not using light time
        // corrections, so the derivative
        // of light time is just
        //
        //    (1/c) * d(VNORM(STARG))/dt
        //
        *DLT = (VDOT(STARG.as_slice(), STARG.subarray(4)) / (DIST * CLIGHT()));

        //
        // LT and DLT are both set, so we can return.
        //
        CHKOUT(RNAME, ctx)?;
        return Ok(());
    }

    //
    // To correct for light time, find the state of the target body
    // at the current epoch minus the one-way light time. Note that
    // the observer remains where it is.
    //
    // Determine the sign of the light time offset.
    //
    if save.XMIT {
        LTSIGN = 1;
    } else {
        LTSIGN = -1;
    }

    //
    // Let NUMITR be the number of iterations we'll perform to
    // compute the light time.
    //
    if save.USECN {
        NUMITR = MAXITR;
    } else {
        NUMITR = 1;
    }

    I = 0;
    LTERR = 1.0;

    while ((I < NUMITR) && (LTERR > CNVLIM)) {
        //
        // LT was set either prior to this loop or
        // during the previous loop iteration.
        //
        EPOCH = TOUCHD((ET + ((LTSIGN as f64) * *LT)));

        TRGSUB(EPOCH, REF, &mut TRGCTR, STTCTR.as_slice_mut(), ctx)?;
        SPKSSB(TRGCTR, EPOCH, REF, CTRSSB.as_slice_mut(), ctx)?;

        if FAILED(ctx) {
            CHKOUT(RNAME, ctx)?;
            return Ok(());
        }

        VADDG(
            CTRSSB.as_slice(),
            STTCTR.as_slice(),
            6,
            SSBTRG.as_slice_mut(),
        );
        VSUBG(SSBTRG.as_slice(), STOBS.as_slice(), 6, STARG.as_slice_mut());

        PRVLT = *LT;
        *LT = TOUCHD((VNORM(STARG.as_slice()) / CLIGHT()));

        //
        // LTERR is the magnitude of the change between the current
        // estimate of light time and the previous estimate, relative to
        // the previous light time corrected epoch.
        //
        LTERR = TOUCHD((f64::abs((*LT - PRVLT)) / intrinsics::DMAX1(&[1.0, f64::abs(EPOCH)])));
        I = (I + 1);
    }

    //
    // At this point, STARG contains the light time corrected
    // state of the target relative to the observer.
    //
    // Compute the derivative of light time with respect
    // to time: dLT/dt.  Below we derive the formula for
    // this quantity for the reception case. Let
    //
    //    POBS be the position of the observer relative to the
    //    solar system barycenter.
    //
    //    VOBS be the velocity of the observer relative to the
    //    solar system barycenter.
    //
    //    PTARG be the position of the target relative to the
    //    solar system barycenter.
    //
    //    VTARG be the velocity of the target relative to the
    //    solar system barycenter.
    //
    //    S be the sign of the light time correction. S is
    //    negative for the reception case.
    //
    // The light-time corrected position of the target relative to
    // the observer at observation time ET, given the one-way
    // light time LT is:
    //
    //     PTARG(ET+S*LT) - POBS(ET)
    //
    // The light-time corrected velocity of the target relative to
    // the observer at observation time ET is
    //
    //     VTARG(ET+S*LT)*( 1 + S*d(LT)/d(ET) ) - VOBS(ET)
    //
    // We need to compute dLT/dt. Below, we use the facts that,
    // for a time-dependent vector X(t),
    //
    //      ||X||     = <X,X> ** (1/2)
    //
    //    d(||X||)/dt = (1/2)<X,X>**(-1/2) * 2 * <X,dX/dt>
    //
    //                = <X,X>**(-1/2) *  <X,dX/dt>
    //
    //                = <X,dX/dt> / ||X||
    //
    // Newtonian light time equation:
    //
    //    LT     =   (1/c) * || PTARG(ET+S*LT) - POBS(ET)||
    //
    // Differentiate both sides:
    //
    //    dLT/dt =   (1/c) * ( 1 / || PTARG(ET+S*LT) - POBS(ET) || )
    //
    //              * < PTARG(ET+S*LT) - POBS(ET),
    //                  VTARG(ET+S*LT)*(1+S*d(LT)/d(ET)) - VOBS(ET) >
    //
    //
    //           = (1/c) * ( 1 / || PTARG(ET+S*LT) - POBS(ET) || )
    //
    //             * (  < PTARG(ET+S*LT) - POBS(ET),
    //                    VTARG(ET+S*LT) - VOBS(ET) >
    //
    //               +  < PTARG(ET+S*LT) - POBS(ET),
    //                    VTARG(ET+S*LT)           > * (S*d(LT)/d(ET))  )
    //
    // Let
    //
    //    A =   (1/c) * ( 1 / || PTARG(ET+S*LT) - POBS(ET) || )
    //
    //    B =   < PTARG(ET+S*LT) - POBS(ET), VTARG(ET+S*LT) - VOBS(ET) >
    //
    //    C =   < PTARG(ET+S*LT) - POBS(ET), VTARG(ET+S*LT) >
    //
    // Then
    //
    //    d(LT)/d(ET) =  A * ( B  +  C * S*d(LT)/d(ET) )
    //
    // which implies
    //
    //    d(LT)/d(ET) =  A*B / ( 1 - S*C*A )
    //
    //
    //
    A = (1.0 / (CLIGHT() * VNORM(STARG.as_slice())));

    B = VDOT(STARG.as_slice(), STARG.subarray(4));

    C = VDOT(STARG.as_slice(), SSBTRG.subarray(4));

    //
    // For physically realistic target velocities, S*C*A cannot equal 1.
    // We'll check for this case anyway.
    //
    if ((((LTSIGN as f64) * C) * A) > (1.0 - TOL)) {
        SETMSG(b"Target range rate magnitude is approximately the speed of light. The light time derivative cannot be computed.", ctx);
        SIGERR(b"SPICE(DIVIDEBYZERO)", ctx)?;
        CHKOUT(RNAME, ctx)?;
        return Ok(());
    }

    //
    // Compute DLT: the rate of change of light time.
    //
    *DLT = ((A * B) / (1.0 - (((LTSIGN as f64) * C) * A)));

    //
    // Overwrite the velocity portion of the output state
    // with the light-time corrected velocity.
    //
    VLCOM(
        (1.0 + ((LTSIGN as f64) * *DLT)),
        SSBTRG.subarray(4),
        -1.0,
        STOBS.subarray(4),
        STARG.subarray_mut(4),
    );

    CHKOUT(RNAME, ctx)?;
    Ok(())
}