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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
const NINERT: i32 = 21;
const RNAME: &[u8] = b"SPKGEO_S";
const CHLEN: i32 = 3;
const SSB: i32 = 0;
fn ISINRT(TMPFRM: i32, CFRAME: i32) -> bool {
((((CFRAME > 0) && (CFRAME <= NINERT)) && (TMPFRM > 0)) && (TMPFRM <= NINERT))
}
//$Procedure SPKGEO_S ( S/P Kernel, geometric state )
pub fn SPKGEO_S(
TARG: i32,
ET: f64,
REF: &[u8],
OBS: i32,
STATE: &mut [f64],
LT: &mut f64,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let mut STATE = DummyArrayMut::new(STATE, 1..=6);
let mut IDENT = [b' '; 40 as usize];
let mut TNAME = [b' '; 40 as usize];
let mut ONAME = [b' '; 40 as usize];
let mut TSTRING = [b' '; 80 as usize];
let mut DESCR = StackArray::<f64, 5>::new(1..=5);
let mut SOBS = StackArray::<f64, 6>::new(1..=6);
let mut STARG = StackArray2D::<f64, 18>::new(1..=6, 1..=CHLEN);
let mut STEMP = StackArray::<f64, 6>::new(1..=6);
let mut STXFRM = StackArray2D::<f64, 36>::new(1..=6, 1..=6);
let mut ROT = StackArray2D::<f64, 9>::new(1..=3, 1..=3);
let mut VTEMP = StackArray::<f64, 6>::new(1..=6);
let mut CFRAME: i32 = 0;
let mut COBS: i32 = 0;
let mut CTARG = StackArray::<i32, 3>::new(1..=CHLEN);
let mut TFRAME = StackArray::<i32, 3>::new(1..=CHLEN);
let mut CTPOS: i32 = 0;
let mut HANDLE: i32 = 0;
let mut I: i32 = 0;
let mut LEGS: i32 = 0;
let mut NCT: i32 = 0;
let mut REFID: i32 = 0;
let mut TMPFRM: i32 = 0;
let mut FOUND: bool = false;
let mut NOFRM: bool = false;
//
// This is the idea:
//
// Every body moves with respect to some center. The center
// is itself a body, which in turn moves about some other
// center. If we begin at the target body (T), follow
// the chain,
//
// T
// \
// SSB \
// \ C[1]
// \ /
// \ /
// \ /
// \ /
// C[3]-----------C[2]
//
// and avoid circular definitions (A moves about B, and B moves
// about A), eventually we get the state relative to the solar
// system barycenter (which, for our purposes, doesn't move).
// Thus,
//
// T = T + C[1] + C[2] + ... + C[n]
// SSB C[1] C[2] [C3] SSB
//
// where
//
// X
// Y
//
// is the state of body X relative to body Y.
//
// However, we don't want to follow each chain back to the SSB
// if it isn't necessary. Instead we will just follow the chain
// of the target body and follow the chain of the observing body
// until we find a common node in the tree.
//
// In the example below, C is the first common node. We compute
// the state of TARG relative to C and the state of OBS relative
// to C, then subtract the two states.
//
// TARG
// \
// SSB \
// \ A
// \ / OBS
// \ / |
// \ / |
// \ / |
// B-------------C-----------------D
//
//
//
//
// SPICELIB functions
//
//
// Local parameters
//
//
// CHLEN is the maximum length of a chain. That is,
// it is the maximum number of bodies in the chain from
// the target or observer to the SSB.
//
//
// Local variables
//
//
// In-line Function Definitions
//
//
// Standard SPICE error handling.
//
if spicelib::RETURN(ctx) {
return Ok(());
} else {
spicelib::CHKIN(RNAME, ctx)?;
}
//
// We take care of the obvious case first. It TARG and OBS are the
// same we can just fill in zero.
//
if (TARG == OBS) {
*LT = 0.0;
spicelib::CLEARD(6, STATE.as_slice_mut());
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
//
// CTARG contains the integer codes of the bodies in the
// target body chain, beginning with TARG itself and then
// the successive centers of motion.
//
// STARG(1,I) is the state of the target body relative
// to CTARG(I). The id-code of the frame of this state is
// stored in TFRAME(I).
//
// COBS and SOBS will contain the centers and states of the
// observing body. (They are single elements instead of arrays
// because we only need the current center and state of the
// observer relative to it.)
//
// First, we construct CTARG and STARG. CTARG(1) is
// just the target itself, and STARG(1,1) is just a zero
// vector, that is, the state of the target relative
// to itself.
//
// Then we follow the chain, filling up CTARG and STARG
// as we go. We use SPKSFS to search through loaded
// files to find the first segment applicable to CTARG(1)
// and time ET. Then we use SPKPVN to compute the state
// of the body CTARG(1) at ET in the segment that was found
// and get its center and frame of motion (CTARG(2) and TFRAME(2).
//
// We repeat the process for CTARG(2) and so on, until
// there is no data found for some CTARG(I) or until we
// reach the SSB.
//
// Next, we find centers and states in a similar manner
// for the observer. It's a similar construction as
// described above, but I is always 1. COBS and SOBS
// are overwritten with each new center and state,
// beginning at OBS. However, we stop when we encounter
// a common center of motion, that is when COBS is equal
// to CTARG(I) for some I.
//
// Finally, we compute the desired state of the target
// relative to the observer by subtracting the state of
// the observing body relative to the common node from
// the state of the target body relative to the common
// node.
//
// CTPOS is the position in CTARG of the common node.
//
//
// Since Inertial frames are the most extensively used frames
// we use the more restrictive routine IRFNUM to attempt to
// look up the id-code for REF. If IRFNUM comes up empty handed
// we then call the more general routine NAMFRM.
//
spicelib::IRFNUM(REF, &mut REFID, ctx);
if (REFID == 0) {
spicelib::NAMFRM(REF, &mut REFID, ctx)?;
}
if (REFID == 0) {
if (spicelib::FRSTNP(REF) > 0) {
spicelib::SETMSG(b"The string supplied to specify the reference frame, (\'#\') contains non-printing characters. The two most common causes for this kind of error are: 1. an error in the call to SPKGEO_S; 2. an uninitialized variable. ", ctx);
spicelib::ERRCH(b"#", REF, ctx);
} else if fstr::eq(REF, b" ") {
spicelib::SETMSG(b"The string supplied to specify the reference frame is blank. The most common cause for this kind of error is an uninitialized variable. ", ctx);
} else {
spicelib::SETMSG(b"The string supplied to specify the reference frame was \'#\'. This frame is not recognized. Possible causes for this error are: 1. failure to load the frame definition into the kernel pool; 2. An out-of-date edition of the toolkit. ", ctx);
spicelib::ERRCH(b"#", REF, ctx);
}
spicelib::SIGERR(b"SPICE(UNKNOWNFRAME)", ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
}
//
// Fill in CTARG and STARG until no more data is found
// or until we reach the SSB. If the chain gets too
// long to fit in CTARG, that is if I equals CHLEN,
// then overwrite the last elements of CTARG and STARG.
//
// Note the check for FAILED in the loop. If SPKSFS
// or SPKPVN happens to fail during execution, and the
// current error handling action is to NOT abort, then
// FOUND may be stuck at TRUE, CTARG(I) will never
// become zero, and the loop will execute indefinitely.
//
//
// Construct CTARG and STARG. Begin by assigning the
// first elements: TARG and the state of TARG relative
// to itself.
//
I = 1;
CTARG[I] = TARG;
FOUND = true;
spicelib::CLEARD(6, STARG.subarray_mut([1, I]));
while (((FOUND && (I < CHLEN)) && (CTARG[I] != OBS)) && (CTARG[I] != SSB)) {
//
// Find a file and segment that has state
// data for CTARG(I).
//
spicelib::SPKSFS(
CTARG[I],
ET,
&mut HANDLE,
DESCR.as_slice_mut(),
&mut IDENT,
&mut FOUND,
ctx,
)?;
if FOUND {
//
// Get the state of CTARG(I) relative to some
// center of motion. This new center goes in
// CTARG(I+1) and the state is called STEMP.
//
I = (I + 1);
spicelib::SPKPVN(
HANDLE,
DESCR.as_slice(),
ET,
&mut TFRAME[I],
STARG.subarray_mut([1, I]),
&mut CTARG[I],
ctx,
)?;
//
// Here's what we have. STARG is the state of CTARG(I-1)
// relative to CTARG(I) in reference frame TFRAME(I)
//
// If one of the routines above failed during
// execution, we just give up and check out.
//
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
}
}
TFRAME[1] = TFRAME[2];
//
// If the loop above ended because we ran out of
// room in the arrays CTARG and STARG, then we
// continue finding states but we overwrite the
// last elements of CTARG and STARG.
//
// If, as a result, the first common node is
// overwritten, we'll just have to settle for
// the last common node. This will cause a small
// loss of precision, but it's better than other
// alternatives.
//
if (I == CHLEN) {
while ((FOUND && (CTARG[CHLEN] != SSB)) && (CTARG[CHLEN] != OBS)) {
//
// Find a file and segment that has state
// data for CTARG(CHLEN).
//
spicelib::SPKSFS(
CTARG[CHLEN],
ET,
&mut HANDLE,
DESCR.as_slice_mut(),
&mut IDENT,
&mut FOUND,
ctx,
)?;
if FOUND {
//
// Get the state of CTARG(CHLEN) relative to
// some center of motion. The new center
// overwrites the old. The state is called
// STEMP.
//
spicelib::SPKPVN(
HANDLE,
DESCR.as_slice(),
ET,
&mut TMPFRM,
STEMP.as_slice_mut(),
&mut CTARG[CHLEN],
ctx,
)?;
//
// Add STEMP to the state of TARG relative to
// the old center to get the state of TARG
// relative to the new center. Overwrite
// the last element of STARG.
//
if (TFRAME[CHLEN] == TMPFRM) {
spicelib::MOVED(STARG.subarray([1, CHLEN]), 6, VTEMP.as_slice_mut());
} else if ISINRT(TFRAME[CHLEN], TMPFRM) {
spicelib::IRFROT(TFRAME[CHLEN], TMPFRM, ROT.as_slice_mut(), ctx)?;
spicelib::MXV(
ROT.as_slice(),
STARG.subarray([1, CHLEN]),
VTEMP.subarray_mut(1),
);
spicelib::MXV(
ROT.as_slice(),
STARG.subarray([4, CHLEN]),
VTEMP.subarray_mut(4),
);
} else {
spicelib::FRMCHG(TFRAME[CHLEN], TMPFRM, ET, STXFRM.as_slice_mut(), ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
spicelib::MXVG(
STXFRM.as_slice(),
STARG.subarray([1, CHLEN]),
6,
6,
VTEMP.as_slice_mut(),
);
}
spicelib::VADDG(
VTEMP.as_slice(),
STEMP.as_slice(),
6,
STARG.subarray_mut([1, CHLEN]),
);
TFRAME[CHLEN] = TMPFRM;
//
// If one of the routines above failed during
// execution, we just give up and check out.
//
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
}
}
}
NCT = I;
//
// NCT is the number of elements in CTARG,
// the chain length. We have in hand the following information
//
// STARG(1...6,K) state of body
// CTARG(K-1) relative to body CTARG(K) in the frame
// TFRAME(K)
//
//
// For K = 2,..., NCT.
//
// CTARG(1) = TARG
// STARG(1...6,1) = ( 0, 0, 0, 0, 0, 0 )
// TFRAME(1) = TFRAME(2)
//
//
// Now follow the observer's chain. Assign
// the first values for COBS and SOBS.
//
COBS = OBS;
spicelib::CLEARD(6, SOBS.as_slice_mut());
//
// Perhaps we have a common node already.
// If so it will be the last node on the
// list CTARG.
//
// We let CTPOS will be the position of the common
// node in CTARG if one is found. It will
// be zero if COBS is not found in CTARG.
//
if (CTARG[NCT] == COBS) {
CTPOS = NCT;
CFRAME = TFRAME[CTPOS];
} else {
CTPOS = 0;
}
//
// Repeat the same loop as above, but each time
// we encounter a new center of motion, check to
// see if it is a common node. (When CTPOS is
// not zero, CTARG(CTPOS) is the first common node.)
//
// Note that we don't need a centers array nor a
// states array, just a single center and state
// is sufficient --- we just keep overwriting them.
// When the common node is found, we have everything
// we need in that one center (COBS) and state
// (SOBS-state of the target relative to COBS).
//
FOUND = true;
NOFRM = true;
LEGS = 0;
while ((FOUND && (COBS != SSB)) && (CTPOS == 0)) {
//
// Find a file and segment that has state
// data for COBS.
//
spicelib::SPKSFS(
COBS,
ET,
&mut HANDLE,
DESCR.as_slice_mut(),
&mut IDENT,
&mut FOUND,
ctx,
)?;
if FOUND {
//
// Get the state of COBS; call it STEMP.
// The center of motion of COBS becomes the
// new COBS.
//
if (LEGS == 0) {
spicelib::SPKPVN(
HANDLE,
DESCR.as_slice(),
ET,
&mut TMPFRM,
SOBS.as_slice_mut(),
&mut COBS,
ctx,
)?;
} else {
spicelib::SPKPVN(
HANDLE,
DESCR.as_slice(),
ET,
&mut TMPFRM,
STEMP.as_slice_mut(),
&mut COBS,
ctx,
)?;
}
if NOFRM {
NOFRM = false;
CFRAME = TMPFRM;
}
//
// Add STEMP to the state of OBS relative to
// the old COBS to get the state of OBS
// relative to the new COBS.
//
if (CFRAME == TMPFRM) {
//
// On the first leg of the state of the observer, we
// don't have to add anything, the state of the observer
// is already in SOBS. We only have to add when the
// number of legs in the observer state is one or greater.
//
if (LEGS > 0) {
spicelib::VADDG(SOBS.as_slice(), STEMP.as_slice(), 6, VTEMP.as_slice_mut());
spicelib::MOVED(VTEMP.as_slice(), 6, SOBS.as_slice_mut());
}
} else if ISINRT(CFRAME, TMPFRM) {
spicelib::IRFROT(CFRAME, TMPFRM, ROT.as_slice_mut(), ctx)?;
spicelib::MXV(ROT.as_slice(), SOBS.subarray(1), VTEMP.subarray_mut(1));
spicelib::MXV(ROT.as_slice(), SOBS.subarray(4), VTEMP.subarray_mut(4));
spicelib::VADDG(VTEMP.as_slice(), STEMP.as_slice(), 6, SOBS.as_slice_mut());
CFRAME = TMPFRM;
} else {
spicelib::FRMCHG(CFRAME, TMPFRM, ET, STXFRM.as_slice_mut(), ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
spicelib::MXVG(
STXFRM.as_slice(),
SOBS.as_slice(),
6,
6,
VTEMP.as_slice_mut(),
);
spicelib::VADDG(VTEMP.as_slice(), STEMP.as_slice(), 6, SOBS.as_slice_mut());
CFRAME = TMPFRM;
}
//
// Check failed. We don't want to loop
// indefinitely.
//
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
//
// We now have one more leg of the path for OBS. Set
// LEGS to reflect this. Then see if the new center
// is a common node. If not, repeat the loop.
//
LEGS = (LEGS + 1);
CTPOS = spicelib::ISRCHI(COBS, NCT, CTARG.as_slice());
}
}
//
// If CTPOS is zero at this point, it means we
// have not found a common node though we have
// searched through all the available data.
//
if (CTPOS == 0) {
spicelib::BODC2N(TARG, &mut TNAME, &mut FOUND, ctx)?;
if FOUND {
spicelib::PREFIX(b"# (", 0, &mut TNAME);
spicelib::SUFFIX(b")", 0, &mut TNAME);
spicelib::REPMI(&TNAME.clone(), b"#", TARG, &mut TNAME, ctx);
} else {
spicelib::INTSTR(TARG, &mut TNAME, ctx);
}
spicelib::BODC2N(OBS, &mut ONAME, &mut FOUND, ctx)?;
if FOUND {
spicelib::PREFIX(b"# (", 0, &mut ONAME);
spicelib::SUFFIX(b")", 0, &mut ONAME);
spicelib::REPMI(&ONAME.clone(), b"#", OBS, &mut ONAME, ctx);
} else {
spicelib::INTSTR(OBS, &mut ONAME, ctx);
}
spicelib::SETMSG(b"Insufficient ephemeris data has been loaded to compute the state of TARG relative to OBS at the ephemeris epoch #. ", ctx);
spicelib::ETCAL(ET, &mut TSTRING, ctx);
spicelib::ERRCH(b"TARG", &TNAME, ctx);
spicelib::ERRCH(b"OBS", &ONAME, ctx);
spicelib::ERRCH(b"#", &TSTRING, ctx);
spicelib::SIGERR(b"SPICE(SPKINSUFFDATA)", ctx)?;
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
//
// If CTPOS is not zero, then we have reached a
// common node, specifically,
//
// CTARG(CTPOS) = COBS = CENTER
//
// (in diagram below). The STATE of the target
// (TARG) relative to the observer (OBS) is just
//
// STARG(1,CTPOS) - SOBS.
//
//
//
// SOBS
// CENTER ---------------->OBS
// | .
// | .
// S | . E
// T | . T
// A | . A
// R | . T
// G | . S
// | .
// | .
// V L
// TARG
//
//
// And the light-time between them is just
//
// | STATE |
// LT = ---------
// c
//
//
// Compute the state of the target relative to CTARG(CTPOS)
//
if (CTPOS == 1) {
TFRAME[1] = CFRAME;
}
{
let m1__: i32 = 2;
let m2__: i32 = (CTPOS - 1);
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
if (TFRAME[I] == TFRAME[(I + 1)]) {
spicelib::VADDG(
STARG.subarray([1, I]),
STARG.subarray([1, (I + 1)]),
6,
VTEMP.as_slice_mut(),
);
spicelib::MOVED(VTEMP.as_slice(), 6, STARG.subarray_mut([1, (I + 1)]));
} else if ISINRT(TFRAME[I], TFRAME[(I + 1)]) {
spicelib::IRFROT(TFRAME[I], TFRAME[(I + 1)], ROT.as_slice_mut(), ctx)?;
spicelib::MXV(
ROT.as_slice(),
STARG.subarray([1, I]),
STEMP.subarray_mut(1),
);
spicelib::MXV(
ROT.as_slice(),
STARG.subarray([4, I]),
STEMP.subarray_mut(4),
);
spicelib::VADDG(
STEMP.as_slice(),
STARG.subarray([1, (I + 1)]),
6,
VTEMP.as_slice_mut(),
);
spicelib::MOVED(VTEMP.as_slice(), 6, STARG.subarray_mut([1, (I + 1)]));
} else {
spicelib::FRMCHG(TFRAME[I], TFRAME[(I + 1)], ET, STXFRM.as_slice_mut(), ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
spicelib::MXVG(
STXFRM.as_slice(),
STARG.subarray([1, I]),
6,
6,
STEMP.as_slice_mut(),
);
spicelib::VADDG(
STEMP.as_slice(),
STARG.subarray([1, (I + 1)]),
6,
VTEMP.as_slice_mut(),
);
spicelib::MOVED(VTEMP.as_slice(), 6, STARG.subarray_mut([1, (I + 1)]));
}
I += m3__;
}
}
//
// To avoid unnecessary frame transformations we'll do
// a bit of extra decision making here. It's a lot
// faster to make logical checks than it is to compute
// frame transformations.
//
if (TFRAME[CTPOS] == CFRAME) {
spicelib::VSUBG(
STARG.subarray([1, CTPOS]),
SOBS.as_slice(),
6,
STATE.as_slice_mut(),
);
} else if (TFRAME[CTPOS] == REFID) {
//
// If the last frame associated with the target is already
// in the requested output frame, we convert the state of
// the observer to that frame and then subtract the state
// of the observer from the state of the target.
//
if ISINRT(CFRAME, REFID) {
spicelib::IRFROT(CFRAME, REFID, ROT.as_slice_mut(), ctx)?;
spicelib::MXV(ROT.as_slice(), SOBS.subarray(1), STEMP.subarray_mut(1));
spicelib::MXV(ROT.as_slice(), SOBS.subarray(4), STEMP.subarray_mut(4));
} else {
spicelib::FRMCHG(CFRAME, REFID, ET, STXFRM.as_slice_mut(), ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
spicelib::MXVG(
STXFRM.as_slice(),
SOBS.as_slice(),
6,
6,
STEMP.as_slice_mut(),
);
}
//
// We've now transformed SOBS into the requested reference frame.
// Set CFRAME to reflect this.
//
CFRAME = REFID;
spicelib::VSUBG(
STARG.subarray([1, CTPOS]),
STEMP.as_slice(),
6,
STATE.as_slice_mut(),
);
} else if ISINRT(TFRAME[CTPOS], CFRAME) {
//
// If both frames are inertial we use IRFROT instead of
// FRMCHG to get things into a common frame.
//
spicelib::IRFROT(TFRAME[CTPOS], CFRAME, ROT.as_slice_mut(), ctx)?;
spicelib::MXV(
ROT.as_slice(),
STARG.subarray([1, CTPOS]),
STEMP.subarray_mut(1),
);
spicelib::MXV(
ROT.as_slice(),
STARG.subarray([4, CTPOS]),
STEMP.subarray_mut(4),
);
spicelib::VSUBG(STEMP.as_slice(), SOBS.as_slice(), 6, STATE.as_slice_mut());
} else {
//
// Use the more general routine FRMCHG to make the transformation.
//
spicelib::FRMCHG(TFRAME[CTPOS], CFRAME, ET, STXFRM.as_slice_mut(), ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
spicelib::MXVG(
STXFRM.as_slice(),
STARG.subarray([1, CTPOS]),
6,
6,
STEMP.as_slice_mut(),
);
spicelib::VSUBG(STEMP.as_slice(), SOBS.as_slice(), 6, STATE.as_slice_mut());
}
//
// Finally, rotate as needed into the requested frame.
//
if (CFRAME == REFID) {
//
// We don't have to do anything in this case.
//
} else if ISINRT(CFRAME, REFID) {
//
// Since both frames are inertial, we use the more direct
// routine IRFROT to get the transformation to REFID.
//
spicelib::IRFROT(CFRAME, REFID, ROT.as_slice_mut(), ctx)?;
spicelib::MXV(ROT.as_slice(), STATE.subarray(1), STEMP.subarray_mut(1));
spicelib::MXV(ROT.as_slice(), STATE.subarray(4), STEMP.subarray_mut(4));
spicelib::MOVED(STEMP.as_slice(), 6, STATE.as_slice_mut());
} else {
spicelib::FRMCHG(CFRAME, REFID, ET, STXFRM.as_slice_mut(), ctx)?;
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(RNAME, ctx)?;
return Ok(());
}
spicelib::MXVG(
STXFRM.as_slice(),
STATE.as_slice(),
6,
6,
STEMP.as_slice_mut(),
);
spicelib::MOVED(STEMP.as_slice(), 6, STATE.as_slice_mut());
}
*LT = (spicelib::VNORM(STATE.as_slice()) / spicelib::CLIGHT());
spicelib::CHKOUT(RNAME, ctx)?;
Ok(())
}