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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
const MAXDEG: i32 = 27;
const ITRUE: i32 = 1;
const IFALSE: i32 = -1;
const S19TP0: i32 = 0;
const S19TP1: i32 = (S19TP0 + 1);
const S19TP2: i32 = (S19TP1 + 1);
const S19PS0: i32 = 12;
const S19PS1: i32 = 6;
const S19PS2: i32 = 6;
const S19NST: i32 = 3;
const S19MXZ: i32 = S19PS0;
const S19MNZ: i32 = S19PS1;
const MAXRSZ: i32 = (2 + ((MAXDEG + 1) * (S19PS1 + 1)));
const MAXREC: i32 = 198;
const SBTIDX: i32 = 1;
const CNTIDX: i32 = 2;
const PKTIDX: i32 = 3;
/// SPK, evaluate record, type 19
///
/// Evaluate a single data record from a type 19 SPK segment.
///
/// # Required Reading
///
/// * [SPK](crate::required_reading::spk)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// MAXREC P Maximum size of SPK record.
/// ET I Epoch for which a state is desired.
/// RECORD I Record from a type 19 SPK segment valid for ET.
/// STATE O State (position and velocity) at epoch ET.
/// ```
///
/// # Detailed Input
///
/// ```text
/// ET is the epoch for which a state vector is desired,
/// expressed as seconds past J2000 TDB.
///
/// RECORD is a record from a type 19 SPK segment which, when
/// evaluated at epoch ET, will give the state
/// (position and velocity) of some body, relative to
/// some center, in the reference frame associated
/// with the data packets. Usually the body, center
/// and frame are those of the SPK segment from which
/// the packets were read.
///
/// The structure of the record is as follows:
///
/// +----------------------+
/// | subtype code |
/// +----------------------+
/// | number of packets (n)|
/// +----------------------+
/// | packet 1 |
/// +----------------------+
/// .
/// .
/// .
/// +----------------------+
/// | packet n |
/// +----------------------+
/// | epoch 1 |
/// +----------------------+
/// .
/// .
/// .
/// +----------------------+
/// | epoch n |
/// +----------------------+
/// ```
///
/// # Detailed Output
///
/// ```text
/// STATE is the state vector at epoch ET. Its contents are, in
/// order, X, Y, Z, X', Y', and Z'. Units are km and km/sec.
/// ```
///
/// # Parameters
///
/// ```text
/// MAXREC is the maximum size of SPK record. See the SPICELIB
/// routine SPKPVN for details.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) Most types of errors in the input record cannot be diagnosed
/// by this routine. This routine assumes that the input record
/// is valid.
///
/// 2) If the subtype code in the input record is invalid, an error
/// is signaled by a routine in the call tree of this
/// routine.
/// ```
///
/// # Particulars
///
/// ```text
/// The exact format and structure of type 19 (ESOC/DDID piecewise
/// interpolation) SPK segments is described in the SPK Required
/// Reading.
/// ```
///
/// # Examples
///
/// ```text
/// The SPKEnn routines are almost always used in conjunction with
/// the corresponding SPKRnn routines, which read the records from
/// SPK files.
///
/// The data returned by the SPKRnn routine is in a raw form, taken
/// directly from the segment. As such, it will be not be directly
/// useful to a user unless they have a complete understanding of the
/// structure of the data type. Given that understanding, however,
/// the SPKRnn routines could be used to "dump" and check segment data
/// for a particular epoch before evaluating the record to obtain a
/// state vector, as in the example that follows.
///
///
/// C
/// C Get a segment applicable to a specified body and epoch.
/// C
/// CALL SPKSFS ( BODY, ET, HANDLE, DESCR, IDENT, FOUND )
///
/// C
/// C Look at parts of the descriptor.
/// C
/// CALL DAFUS ( DESCR, 2, 6, DCD, ICD )
///
/// CENTER = ICD( 2 )
/// REF = ICD( 3 )
/// TYPE = ICD( 4 )
///
/// IF ( TYPE .EQ. 19 ) THEN
///
/// CALL SPKR19 ( HANDLE, DESCR, ET, RECORD )
/// .
/// . Look at the RECORD data.
/// .
/// CALL SPKE19 ( ET, RECORD, STATE )
/// .
/// . Check out the evaluated state.
/// .
/// END IF
/// ```
///
/// # Restrictions
///
/// ```text
/// 1) This routine assumes that the input record is valid. Any
/// checking of the input data is assumed to have been performed
/// when the source SPK file was created.
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// B.V. Semenov (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 2.0.1, 12-AUG-2021 (JDR)
///
/// Edited the header to comply with NAIF standard.
///
/// - SPICELIB Version 2.0.0, 11-MAY-2015 (NJB)
///
/// Updated to support subtype 2. Now performs
/// computations in-line, rather than calling
/// SPKE18.
///
/// - SPICELIB Version 1.0.0, 14-MAR-2014 (NJB) (BVS)
/// ```
pub fn spke19(
ctx: &mut SpiceContext,
et: f64,
record: &mut [f64],
state: &mut [f64; 6],
) -> crate::Result<()> {
SPKE19(et, record, state, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure SPKE19 ( SPK, evaluate record, type 19 )
pub fn SPKE19(
ET: f64,
RECORD: &mut [f64],
STATE: &mut [f64],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let mut RECORD = DummyArrayMut::new(RECORD, 1..);
let mut STATE = DummyArrayMut::new(STATE, 1..=6);
let mut LOCREC = StackArray::<f64, 198>::new(1..=MAXREC);
let mut WORK = ActualArray2D::<f64>::new(1..=(MAXREC * 2), 1..=2);
let mut VBUFF = StackArray::<f64, 6>::new(1..=6);
let mut FROM: i32 = 0;
let mut N: i32 = 0;
let mut PACKSZ: i32 = 0;
let mut SUBTYP: i32 = 0;
let mut TO: i32 = 0;
let mut XSTART: i32 = 0;
let mut YSTART: i32 = 0;
//
// SPICELIB functions
//
//
// Local parameters
//
//
// Index of subtype code in record:
//
//
// Index of packet count in record:
//
//
// Index at which packets start:
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
}
CHKIN(b"SPKE19", ctx)?;
//
// Capture the subtype from the record and set the packet size
// accordingly.
//
SUBTYP = intrinsics::IDNINT(RECORD[SBTIDX]);
if (SUBTYP == S19TP0) {
PACKSZ = S19PS0;
} else if (SUBTYP == S19TP1) {
PACKSZ = S19PS1;
} else if (SUBTYP == S19TP2) {
PACKSZ = S19PS2;
} else {
SETMSG(
b"Unexpected SPK type 19 subtype found in type 19 record.",
ctx,
);
ERRINT(b"#", SUBTYP, ctx);
SIGERR(b"SPICE(INVALIDVALUE)", ctx)?;
CHKOUT(b"SPKE19", ctx)?;
return Ok(());
}
//
// Get the packet count.
//
N = intrinsics::IDNINT(RECORD[CNTIDX]);
if (SUBTYP == S19TP0) {
//
// We interpolate each state component in turn. Position and
// velocity are interpolated separately.
//
XSTART = (3 + (PACKSZ * N));
for I in 1..=3 {
for J in 1..=N {
//
// For the Jth input packet, copy the Ith position and
// velocity components into the local record buffer LOCREC.
//
FROM = ((2 + (PACKSZ * (J - 1))) + I);
TO = ((2 * J) - 1);
LOCREC[TO] = RECORD[FROM];
LOCREC[(TO + 1)] = RECORD[(FROM + 3)];
}
//
// Interpolate the Ith position and velocity components of the
// state. We'll keep the position and overwrite the velocity.
//
let [arg5, arg6] = STATE
.get_disjoint_mut([I, (I + 3)])
.expect("mutable array elements passed to function must have disjoint indexes");
HRMINT(
N,
RECORD.subarray(XSTART),
LOCREC.as_slice(),
ET,
WORK.as_slice_mut(),
arg5,
arg6,
ctx,
)?;
}
//
// Now interpolate velocity, using separate velocity data and
// acceleration.
//
for I in 1..=3 {
for J in 1..=N {
//
// For the Jth input packet, copy the Ith position and
// velocity components into the local record buffer LOCREC.
//
FROM = (((2 + (PACKSZ * (J - 1))) + (PACKSZ / 2)) + I);
TO = ((2 * J) - 1);
LOCREC[TO] = RECORD[FROM];
LOCREC[(TO + 1)] = RECORD[(FROM + 3)];
}
//
// Interpolate the Ith velocity and acceleration components of
// the state. We'll capture the result in a temporary buffer,
// then transfer the velocity to the output state array.
//
let [arg5, arg6] = VBUFF
.get_disjoint_mut([I, (I + 3)])
.expect("mutable array elements passed to function must have disjoint indexes");
HRMINT(
N,
RECORD.subarray(XSTART),
LOCREC.as_slice(),
ET,
WORK.as_slice_mut(),
arg5,
arg6,
ctx,
)?;
}
//
// Fill in the velocity in the output state using the results of
// interpolating velocity and acceleration.
//
VEQU(VBUFF.as_slice(), STATE.subarray_mut(4));
} else if (SUBTYP == S19TP1) {
//
// We perform Lagrange interpolation on each state component.
//
// We'll transpose the state information in the input record so
// that contiguous pieces of it can be shoved directly into the
// interpolation routine LGRINT.
//
XPSGIP(PACKSZ, N, RECORD.subarray_mut(PKTIDX));
//
// We interpolate each state component in turn.
//
XSTART = (3 + (N * PACKSZ));
for I in 1..=PACKSZ {
YSTART = (3 + (N * (I - 1)));
STATE[I] = LGRINT(
N,
RECORD.subarray(XSTART),
RECORD.subarray(YSTART),
LOCREC.as_slice_mut(),
ET,
ctx,
)?;
}
} else if (SUBTYP == S19TP2) {
//
// We perform Hermite interpolation on each position component
// and corresponding velocity component.
//
XSTART = (3 + (N * PACKSZ));
for I in 1..=3 {
for J in 1..=N {
//
// For the Jth input packet, copy the Ith position and
// velocity components into the local record buffer LOCREC.
//
FROM = ((2 + (PACKSZ * (J - 1))) + I);
TO = ((2 * J) - 1);
LOCREC[TO] = RECORD[FROM];
LOCREC[(TO + 1)] = RECORD[(FROM + 3)];
}
//
// Interpolate the Ith position and velocity components of the
// state.
//
let [arg5, arg6] = STATE
.get_disjoint_mut([I, (I + 3)])
.expect("mutable array elements passed to function must have disjoint indexes");
HRMINT(
N,
RECORD.subarray(XSTART),
LOCREC.as_slice(),
ET,
WORK.as_slice_mut(),
arg5,
arg6,
ctx,
)?;
}
} else {
//
// This is a backstop case.
//
SETMSG(
b"Unexpected SPK type 19 subtype found in type 19 record.",
ctx,
);
ERRINT(b"#", SUBTYP, ctx);
SIGERR(b"SPICE(INVALIDVALUE)", ctx)?;
CHKOUT(b"SPKE19", ctx)?;
return Ok(());
}
CHKOUT(b"SPKE19", ctx)?;
Ok(())
}