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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
const NINERT: i32 = 21;
const CTRSIZ: i32 = 2;
const RNAME: &[u8] = b"SPKGPS";
const CHLEN: i32 = 20;
const SSB: i32 = 0;
const FRNMLN: i32 = 32;
struct SaveVars {
SVCTR1: StackArray<i32, 2>,
SVREF: Vec<u8>,
SVREFI: i32,
FIRST: bool,
}
impl SaveInit for SaveVars {
fn new() -> Self {
let mut SVCTR1 = StackArray::<i32, 2>::new(1..=CTRSIZ);
let mut SVREF = vec![b' '; FRNMLN as usize];
let mut SVREFI: i32 = 0;
let mut FIRST: bool = false;
FIRST = true;
Self {
SVCTR1,
SVREF,
SVREFI,
FIRST,
}
}
}
fn ISINRT(TMPFRM: i32, CFRAME: i32) -> bool {
((((CFRAME > 0) && (CFRAME <= NINERT)) && (TMPFRM > 0)) && (TMPFRM <= NINERT))
}
/// S/P Kernel, geometric position
///
/// Compute the geometric position of a target body relative to an
/// observing body.
///
/// # Required Reading
///
/// * [SPK](crate::required_reading::spk)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// TARG I Target body.
/// ET I Target epoch.
/// REF I Target reference frame.
/// OBS I Observing body.
/// POS O Position of target.
/// LT O Light time.
/// ```
///
/// # Detailed Input
///
/// ```text
/// TARG is the standard NAIF ID code for a target body.
///
/// ET is the epoch (ephemeris time) at which the position
/// of the target body is to be computed.
///
/// REF is the name of the reference frame to
/// which the vectors returned by the routine should
/// be rotated. This may be any frame supported by
/// the SPICELIB subroutine REFCHG.
///
/// OBS is the standard NAIF ID code for an observing body.
/// ```
///
/// # Detailed Output
///
/// ```text
/// POS is a 3-dimensional vector that contains the position of
/// the target body, relative to the observing body. This
/// vector is rotated into the specified reference frame.
/// Units are always km.
///
/// LT is the one-way light time from the observing body
/// to the geometric position of the target body at the
/// specified epoch.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If insufficient ephemeris data has been loaded to compute
/// the necessary positions, the error SPICE(SPKINSUFFDATA) is
/// signaled.
/// ```
///
/// # Files
///
/// ```text
/// See $Restrictions.
/// ```
///
/// # Particulars
///
/// ```text
/// SPKGPS computes the geometric position, T(t), of the target
/// body and the geometric position, O(t), of the observing body
/// relative to the first common center of motion. Subtracting
/// O(t) from T(t) gives the geometric position of the target
/// body relative to the observer.
///
///
/// CENTER ----- O(t)
/// | /
/// | /
/// | /
/// | / T(t) - O(t)
/// | /
/// T(t)
///
///
/// The one-way light time, tau, is given by
///
///
/// | T(t) - O(t) |
/// tau = -----------------
/// c
///
///
/// For example, if the observing body is -94, the Mars Observer
/// spacecraft, and the target body is 401, Phobos, then the
/// first common center is probably 4, the Mars Barycenter.
/// O(t) is the position of -94 relative to 4 and T(t) is the
/// position of 401 relative to 4.
///
/// The center could also be the Solar System Barycenter, body 0.
/// For example, if the observer is 399, Earth, and the target
/// is 299, Venus, then O(t) would be the position of 399 relative
/// to 0 and T(t) would be the position of 299 relative to 0.
///
/// Ephemeris data from more than one segment may be required
/// to determine the positions of the target body and observer
/// relative to a common center. SPKGPS reads as many segments
/// as necessary, from as many files as necessary, using files
/// that have been loaded by previous calls to SPKLEF (load
/// ephemeris file).
///
/// SPKGPS is similar to SPKGEO but returns geometric positions
/// only.
/// ```
///
/// # Examples
///
/// ```text
/// The following code example computes the geometric
/// position of the moon with respect to the earth and
/// then prints the distance of the moon from the
/// the earth at a number of epochs.
///
/// Assume the SPK file SAMPLE.BSP contains ephemeris data
/// for the moon relative to earth over the time interval
/// from BEGIN to END.
///
/// INTEGER EARTH
/// PARAMETER ( EARTH = 399 )
///
/// INTEGER MOON
/// PARAMETER ( MOON = 301 )
///
/// INTEGER N
/// PARAMETER ( N = 100 )
///
/// INTEGER I
/// CHARACTER*(20) UTC
/// DOUBLE PRECISION BEGIN
/// DOUBLE PRECISION DELTA
/// DOUBLE PRECISION END
/// DOUBLE PRECISION ET
/// DOUBLE PRECISION POS ( 3 )
/// DOUBLE PRECISION LT
///
/// DOUBLE PRECISION VNORM
///
/// C
/// C Load the binary SPK ephemeris file.
/// C
/// CALL FURNSH ( 'SAMPLE.BSP' )
///
/// .
/// .
/// .
///
/// C
/// C Divide the interval of coverage [BEGIN,END] into
/// C N steps. At each step, compute the position, and
/// C print out the epoch in UTC time and position norm.
/// C
/// DELTA = ( END - BEGIN ) / N
///
/// DO I = 0, N
///
/// ET = BEGIN + I*DELTA
///
/// CALL SPKGPS ( MOON, ET, 'J2000', EARTH, POS, LT )
///
/// CALL ET2UTC ( ET, 'C', 0, UTC )
///
/// WRITE (*,*) UTC, VNORM ( POS )
///
/// END DO
/// ```
///
/// # Restrictions
///
/// ```text
/// 1) The ephemeris files to be used by SPKGPS must be loaded
/// by SPKLEF before SPKGPS is called.
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// B.V. Semenov (JPL)
/// W.L. Taber (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 2.1.0, 09-OCT-2021 (JDR) (NJB)
///
/// Bug fix: added calls to FAILED after calls to SPKPVN.
/// Previously only one call to SPKPVN was followed by a FAILED
/// call. Moved some FAILED checks so they will be hit whether
/// or not SPKSFS finds a segment.
///
/// Edited the header to comply with NAIF standard. Removed
/// unnecessary $Revisions section.
///
/// - SPICELIB Version 2.0.0, 08-JAN-2014 (BVS)
///
/// Updated to save the input frame name and POOL state counter
/// and to do frame name-ID conversion only if the counter has
/// changed.
///
/// Updated to map the input frame name to its ID by first calling
/// ZZNAMFRM, and then calling IRFNUM. The side effect of this
/// change is that now the frame with the fixed name 'DEFAULT'
/// that can be associated with any code via CHGIRF's entry point
/// IRFDEF will be fully masked by a frame with identical name
/// defined via a text kernel. Previously the CHGIRF's 'DEFAULT'
/// frame masked the text kernel frame with the same name.
///
/// Replaced SPKLEF with FURNSH and fixed errors in $Examples.
///
/// - SPICELIB Version 1.2.0, 05-NOV-2005 (NJB)
///
/// Updated to remove non-standard use of duplicate arguments
/// in VADD calls.
///
/// - SPICELIB Version 1.1.0, 05-JAN-2005 (NJB)
///
/// Tests of routine FAILED() were added.
///
/// - SPICELIB Version 1.0.0, 09-JUL-1998 (WLT)
/// ```
pub fn spkgps(
ctx: &mut SpiceContext,
targ: i32,
et: f64,
ref_: &str,
obs: i32,
pos: &mut [f64; 3],
lt: &mut f64,
) -> crate::Result<()> {
SPKGPS(targ, et, ref_.as_bytes(), obs, pos, lt, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure SPKGPS ( S/P Kernel, geometric position )
pub fn SPKGPS(
TARG: i32,
ET: f64,
REF: &[u8],
OBS: i32,
POS: &mut [f64],
LT: &mut f64,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let save = ctx.get_vars::<SaveVars>();
let save = &mut *save.borrow_mut();
let mut POS = DummyArrayMut::new(POS, 1..=3);
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, 120>::new(1..=6, 1..=CHLEN);
let mut STEMP = StackArray::<f64, 6>::new(1..=6);
let mut PSXFRM = StackArray2D::<f64, 9>::new(1..=3, 1..=3);
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, 20>::new(1..=CHLEN);
let mut TFRAME = StackArray::<i32, 20>::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 position 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 position 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 position of TARG relative to C and the position of OBS
// relative to C, then subtract the two positions.
//
// 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.
//
//
// Saved frame name length.
//
//
// Local variables
//
//
// Saved frame name/ID item declarations.
//
//
// Saved frame name/ID items.
//
//
// Initial values.
//
//
// In-line Function Definitions
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
} else {
CHKIN(RNAME, ctx)?;
}
//
// Initialization.
//
if save.FIRST {
//
// Initialize counter.
//
ZZCTRUIN(save.SVCTR1.as_slice_mut(), ctx);
save.FIRST = false;
}
//
// 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;
CLEARD(3, POS.as_slice_mut());
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 position of the target body relative
// to CTARG(I). The id-code of the frame of this position is
// stored in TFRAME(I).
//
// COBS and SOBS will contain the centers and positions of the
// observing body. (They are single elements instead of arrays
// because we only need the current center and position 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 position 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 position
// 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 positions 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 position,
// 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 position of the target
// relative to the observer by subtracting the position of
// the observing body relative to the common node from
// the position of the target body relative to the common
// node.
//
// CTPOS is the position in CTARG of the common node.
//
// Since the upgrade to use hashes and counter bypass ZZNAMFRM
// became more efficient in looking up frame IDs than IRFNUM. So the
// original order of calls "IRFNUM first, NAMFRM second" was
// switched to "ZZNAMFRM first, IRFNUM second".
//
// The call to IRFNUM, now redundant for built-in inertial frames,
// was preserved to for a sole reason -- to still support the
// ancient and barely documented ability for the users to associate
// a frame with the fixed name 'DEFAULT' with any CHGIRF inertial
// frame code via CHGIRF's entry point IRFDEF.
//
// Note that in the case of ZZNAMFRM's failure to resolve name and
// IRFNUM's success to do so, the code returned by IRFNUM for
// 'DEFAULT' frame is *not* copied to the saved code SVREFI (which
// would be set to 0 by ZZNAMFRM) to make sure that on subsequent
// calls ZZNAMFRM does not do a bypass (as SVREFI always forced look
// up) and calls IRFNUM again to reset the 'DEFAULT's frame ID
// should it change between the calls.
//
ZZNAMFRM(
save.SVCTR1.as_slice_mut(),
&mut save.SVREF,
&mut save.SVREFI,
REF,
&mut REFID,
ctx,
)?;
if (REFID == 0) {
IRFNUM(REF, &mut REFID, ctx);
}
if (REFID == 0) {
if (FRSTNP(REF) > 0) {
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 SPKGPS; 2. an uninitialized variable. ", ctx);
ERRCH(b"#", REF, ctx);
} else if fstr::eq(REF, b" ") {
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 {
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);
ERRCH(b"#", REF, ctx);
}
SIGERR(b"SPICE(UNKNOWNFRAME)", ctx)?;
if FAILED(ctx) {
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 position of TARG relative
// to itself.
//
I = 1;
CTARG[I] = TARG;
FOUND = true;
CLEARD(6, STARG.subarray_mut([1, I]));
while (((FOUND && (I < CHLEN)) && (CTARG[I] != OBS)) && (CTARG[I] != SSB)) {
//
// Find a file and segment that has position
// data for CTARG(I).
//
SPKSFS(
CTARG[I],
ET,
&mut HANDLE,
DESCR.as_slice_mut(),
&mut IDENT,
&mut FOUND,
ctx,
)?;
if FOUND {
//
// Get the position of CTARG(I) relative to some
// center of motion. This new center goes in
// CTARG(I+1) and the position is called STEMP.
//
I = (I + 1);
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 position 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 FAILED(ctx) {
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 positions 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 position
// data for CTARG(CHLEN).
//
SPKSFS(
CTARG[CHLEN],
ET,
&mut HANDLE,
DESCR.as_slice_mut(),
&mut IDENT,
&mut FOUND,
ctx,
)?;
if FOUND {
//
// Get the position of CTARG(CHLEN) relative to
// some center of motion. The new center
// overwrites the old. The position is called
// STEMP.
//
SPKPVN(
HANDLE,
DESCR.as_slice(),
ET,
&mut TMPFRM,
STEMP.as_slice_mut(),
&mut CTARG[CHLEN],
ctx,
)?;
if FAILED(ctx) {
CHKOUT(RNAME, ctx)?;
return Ok(());
}
//
// Add STEMP to the position of TARG relative to
// the old center to get the position of TARG
// relative to the new center. Overwrite
// the last element of STARG.
//
if (TFRAME[CHLEN] == TMPFRM) {
MOVED(STARG.subarray([1, CHLEN]), 3, VTEMP.as_slice_mut());
} else if ISINRT(TFRAME[CHLEN], TMPFRM) {
IRFROT(TFRAME[CHLEN], TMPFRM, ROT.as_slice_mut(), ctx)?;
MXV(
ROT.as_slice(),
STARG.subarray([1, CHLEN]),
VTEMP.subarray_mut(1),
);
} else {
REFCHG(TFRAME[CHLEN], TMPFRM, ET, PSXFRM.as_slice_mut(), ctx)?;
if FAILED(ctx) {
CHKOUT(RNAME, ctx)?;
return Ok(());
}
MXV(
PSXFRM.as_slice(),
STARG.subarray([1, CHLEN]),
VTEMP.as_slice_mut(),
);
}
VADD(
VTEMP.as_slice(),
STEMP.as_slice(),
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 FAILED(ctx) {
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...3,K) position of body
// CTARG(K-1) relative to body CTARG(K) in the frame
// TFRAME(K)
//
//
// For K = 2,..., NCT.
//
// CTARG(1) = TARG
// STARG(1...3,1) = ( 0, 0, 0 )
// TFRAME(1) = TFRAME(2)
//
//
// Now follow the observer's chain. Assign
// the first values for COBS and SOBS.
//
COBS = OBS;
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
// positions array, just a single center and position
// is sufficient --- we just keep overwriting them.
// When the common node is found, we have everything
// we need in that one center (COBS) and position
// (SOBS-position 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 position
// data for COBS.
//
SPKSFS(
COBS,
ET,
&mut HANDLE,
DESCR.as_slice_mut(),
&mut IDENT,
&mut FOUND,
ctx,
)?;
if FOUND {
//
// Get the position of COBS; call it STEMP.
// The center of motion of COBS becomes the
// new COBS.
//
if (LEGS == 0) {
SPKPVN(
HANDLE,
DESCR.as_slice(),
ET,
&mut TMPFRM,
SOBS.as_slice_mut(),
&mut COBS,
ctx,
)?;
} else {
SPKPVN(
HANDLE,
DESCR.as_slice(),
ET,
&mut TMPFRM,
STEMP.as_slice_mut(),
&mut COBS,
ctx,
)?;
}
if FAILED(ctx) {
CHKOUT(RNAME, ctx)?;
return Ok(());
}
if NOFRM {
NOFRM = false;
CFRAME = TMPFRM;
}
//
// Add STEMP to the position of OBS relative to
// the old COBS to get the position of OBS
// relative to the new COBS.
//
if (CFRAME == TMPFRM) {
//
// On the first leg of the position of the observer, we
// don't have to add anything, the position of the
// observer is already in SOBS. We only have to add when
// the number of legs in the observer position is one or
// greater.
//
if (LEGS > 0) {
VADD(SOBS.as_slice(), STEMP.as_slice(), VTEMP.as_slice_mut());
VEQU(VTEMP.as_slice(), SOBS.as_slice_mut());
}
} else if ISINRT(CFRAME, TMPFRM) {
IRFROT(CFRAME, TMPFRM, ROT.as_slice_mut(), ctx)?;
MXV(ROT.as_slice(), SOBS.subarray(1), VTEMP.subarray_mut(1));
VADD(VTEMP.as_slice(), STEMP.as_slice(), SOBS.as_slice_mut());
CFRAME = TMPFRM;
} else {
REFCHG(CFRAME, TMPFRM, ET, PSXFRM.as_slice_mut(), ctx)?;
if FAILED(ctx) {
CHKOUT(RNAME, ctx)?;
return Ok(());
}
MXV(PSXFRM.as_slice(), SOBS.as_slice(), VTEMP.as_slice_mut());
VADD(VTEMP.as_slice(), STEMP.as_slice(), SOBS.as_slice_mut());
CFRAME = TMPFRM;
}
//
// 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 = ISRCHI(COBS, NCT, CTARG.as_slice());
}
//
// Check failed. We don't want to loop indefinitely.
//
if FAILED(ctx) {
CHKOUT(RNAME, ctx)?;
return Ok(());
}
}
//
// 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) {
BODC2N(TARG, &mut TNAME, &mut FOUND, ctx)?;
if FOUND {
PREFIX(b"# (", 0, &mut TNAME);
SUFFIX(b")", 0, &mut TNAME);
REPMI(&TNAME.clone(), b"#", TARG, &mut TNAME, ctx);
} else {
INTSTR(TARG, &mut TNAME, ctx);
}
BODC2N(OBS, &mut ONAME, &mut FOUND, ctx)?;
if FOUND {
PREFIX(b"# (", 0, &mut ONAME);
SUFFIX(b")", 0, &mut ONAME);
REPMI(&ONAME.clone(), b"#", OBS, &mut ONAME, ctx);
} else {
INTSTR(OBS, &mut ONAME, ctx);
}
SETMSG(b"Insufficient ephemeris data has been loaded to compute the position of TARG relative to OBS at the ephemeris epoch #. ", ctx);
ETCAL(ET, &mut TSTRING, ctx);
ERRCH(b"TARG", &TNAME, ctx);
ERRCH(b"OBS", &ONAME, ctx);
ERRCH(b"#", &TSTRING, ctx);
SIGERR(b"SPICE(SPKINSUFFDATA)", ctx)?;
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 POSITION of the target
// (TARG) relative to the observer (OBS) is just
//
// STARG(1,CTPOS) - SOBS.
//
//
//
// SOBS
// CENTER ---------------->OBS
// | .
// | . N
// S | . O
// T | . I
// A | . T
// R | . I
// G | . S
// | . O
// | . P
// V L
// TARG
//
//
// And the light-time between them is just
//
// | POSITION |
// LT = ---------
// c
//
//
// Compute the position 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)]) {
VADD(
STARG.subarray([1, I]),
STARG.subarray([1, (I + 1)]),
STEMP.as_slice_mut(),
);
MOVED(STEMP.as_slice(), 3, STARG.subarray_mut([1, (I + 1)]));
} else if ISINRT(TFRAME[I], TFRAME[(I + 1)]) {
IRFROT(TFRAME[I], TFRAME[(I + 1)], ROT.as_slice_mut(), ctx)?;
MXV(
ROT.as_slice(),
STARG.subarray([1, I]),
STEMP.subarray_mut(1),
);
VADD(
STEMP.as_slice(),
STARG.subarray([1, (I + 1)]),
VTEMP.as_slice_mut(),
);
MOVED(VTEMP.as_slice(), 3, STARG.subarray_mut([1, (I + 1)]));
} else {
REFCHG(TFRAME[I], TFRAME[(I + 1)], ET, PSXFRM.as_slice_mut(), ctx)?;
if FAILED(ctx) {
CHKOUT(RNAME, ctx)?;
return Ok(());
}
MXV(
PSXFRM.as_slice(),
STARG.subarray([1, I]),
STEMP.as_slice_mut(),
);
VADD(
STEMP.as_slice(),
STARG.subarray([1, (I + 1)]),
VTEMP.as_slice_mut(),
);
MOVED(VTEMP.as_slice(), 3, 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) {
VSUB(
STARG.subarray([1, CTPOS]),
SOBS.as_slice(),
POS.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 position of
// the observer to that frame and then subtract the position
// of the observer from the position of the target.
//
if ISINRT(CFRAME, REFID) {
IRFROT(CFRAME, REFID, ROT.as_slice_mut(), ctx)?;
MXV(ROT.as_slice(), SOBS.subarray(1), STEMP.subarray_mut(1));
} else {
REFCHG(CFRAME, REFID, ET, PSXFRM.as_slice_mut(), ctx)?;
if FAILED(ctx) {
CHKOUT(RNAME, ctx)?;
return Ok(());
}
MXV(PSXFRM.as_slice(), SOBS.as_slice(), STEMP.as_slice_mut());
}
//
// We've now transformed SOBS into the requested reference frame.
// Set CFRAME to reflect this.
//
CFRAME = REFID;
VSUB(
STARG.subarray([1, CTPOS]),
STEMP.as_slice(),
POS.as_slice_mut(),
);
} else if ISINRT(TFRAME[CTPOS], CFRAME) {
//
// If both frames are inertial we use IRFROT instead of
// REFCHG to get things into a common frame.
//
IRFROT(TFRAME[CTPOS], CFRAME, ROT.as_slice_mut(), ctx)?;
MXV(
ROT.as_slice(),
STARG.subarray([1, CTPOS]),
STEMP.subarray_mut(1),
);
VSUB(STEMP.as_slice(), SOBS.as_slice(), POS.as_slice_mut());
} else {
//
// Use the more general routine REFCHG to make the transformation.
//
REFCHG(TFRAME[CTPOS], CFRAME, ET, PSXFRM.as_slice_mut(), ctx)?;
if FAILED(ctx) {
CHKOUT(RNAME, ctx)?;
return Ok(());
}
MXV(
PSXFRM.as_slice(),
STARG.subarray([1, CTPOS]),
STEMP.as_slice_mut(),
);
VSUB(STEMP.as_slice(), SOBS.as_slice(), POS.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.
//
IRFROT(CFRAME, REFID, ROT.as_slice_mut(), ctx)?;
MXV(ROT.as_slice(), POS.subarray(1), STEMP.subarray_mut(1));
MOVED(STEMP.as_slice(), 3, POS.as_slice_mut());
} else {
REFCHG(CFRAME, REFID, ET, PSXFRM.as_slice_mut(), ctx)?;
if FAILED(ctx) {
CHKOUT(RNAME, ctx)?;
return Ok(());
}
MXV(PSXFRM.as_slice(), POS.as_slice(), STEMP.as_slice_mut());
MOVED(STEMP.as_slice(), 3, POS.as_slice_mut());
}
*LT = (VNORM(POS.as_slice()) / CLIGHT());
CHKOUT(RNAME, ctx)?;
Ok(())
}