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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
/// C-kernel, evaluate pointing record, data type 2
///
/// Evaluate a pointing record returned by CKR02 from a CK data type 2
/// segment. Return the C-matrix and angular velocity vector
/// associated with the time CLKOUT.
///
/// # Required Reading
///
/// * [CK](crate::required_reading::ck)
/// * [ROTATION](crate::required_reading::rotation)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// NEEDAV I .TRUE. if angular velocity is requested.
/// RECORD I Data type 2 pointing record.
/// CMAT O C-matrix.
/// AV O Angular velocity vector.
/// CLKOUT O SCLK associated with C-matrix.
/// ```
///
/// # Detailed Input
///
/// ```text
/// NEEDAV is .TRUE. if angular velocity is requested.
///
/// RECORD is a set of double precision numbers returned by CKR02
/// that contain sufficient information from a data type
/// 2 pointing segment to evaluate the C-matrix and the
/// angular velocity vector for a particular instance.
///
/// The contents of RECORD are as follows:
///
/// RECORD( 1 ) = start SCLKDP of interval
///
/// RECORD( 2 ) = SCLK for which pointing was found
///
/// RECORD( 3 ) = seconds / tick rate
///
/// RECORD( 4 ) = q0
/// RECORD( 5 ) = q1
/// RECORD( 6 ) = q2
/// RECORD( 7 ) = q3
///
/// RECORD( 8 ) = av1
/// RECORD( 9 ) = av2
/// RECORD( 10 ) = av3
///
/// The quantities q0 - q3 are the components of the
/// quaternion that represents the C - matrix associated
/// with the start of the interval. The quantities av1,
/// av2, and av3 are the components of the angular velocity
/// vector.
/// ```
///
/// # Detailed Output
///
/// ```text
/// CMAT is a rotation matrix that transforms the components
/// of a vector expressed in the inertial frame given in
/// the segment to components expressed in the instrument
/// fixed frame at the returned time.
///
/// Thus, if a vector v has components x, y, z in the
/// inertial frame, then v has components x', y', z' in the
/// instrument fixed frame where:
///
/// [ x' ] [ ] [ x ]
/// | y' | = | CMAT | | y |
/// [ z' ] [ ] [ z ]
///
/// If the x', y', z' components are known, use the
/// transpose of the C-matrix to determine x, y, z as
/// follows.
///
/// [ x ] [ ]T [ x' ]
/// | y | = | CMAT | | y' |
/// [ z ] [ ] [ z' ]
/// (Transpose of CMAT)
///
/// AV is the angular velocity vector. The angular velocity
/// contained in RECORD is returned only if NEEDAV is .TRUE.
///
/// The direction of the angular velocity vector gives
/// the right-handed axis about which the instrument fixed
/// reference frame is rotating. The magnitude of AV is
/// the magnitude of the instantaneous velocity of the
/// rotation, in radians per second.
///
/// The angular velocity vector is returned in component
/// form
///
/// AV = [ AV1 , AV2 , AV3 ]
///
/// which is in terms of the inertial coordinate frame
/// specified in the segment descriptor.
///
/// CLKOUT is the encoded SCLK associated with the returned
/// C-matrix and angular velocity vector.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) No checking is done to determine whether RECORD is valid.
/// ```
///
/// # Particulars
///
/// ```text
/// For a detailed description of the structure of a type 2 pointing
/// segment, see the CK Required Reading.
///
/// Pointing data in a type 2 segment consists of intervals during
/// which the orientation of the spacecraft structure can be described
/// by an initial C-matrix and a constant angular velocity vector.
/// From the information contained in the pointing record returned by
/// CKR02, this subroutine calculates and returns the C-matrix
/// associated with the time returned by CKR02. It also returns the
/// angular velocity vector contained in the pointing record.
/// ```
///
/// # Examples
///
/// ```text
/// A call to a CKEnn routine is almost always preceded by a call to
/// the corresponding CKRnn routine, which gets the logical record
/// that CKEnn evaluates.
///
/// The following code fragment searches through a file (represented
/// by HANDLE) for all segments applicable to the Voyager 2 wide angle
/// camera, for a particular spacecraft clock time, that are of data
/// types 1 or 2. It then evaluates the pointing for that epoch and
/// prints the result.
///
///
/// SC = -32
/// INST = -32002
/// C
/// C Load the Voyager 2 spacecraft clock kernel and the C-kernel.
/// C
/// CALL FURNSH ( 'VGR_SCLK.TSC' )
/// CALL DAFOPR ( 'VGR2_CK.BC', HANDLE )
///
/// C
/// C Get the spacecraft clock time. Must encode it for use
/// C in the C-kernel.
/// C
///
/// WRITE (*,*) 'Enter spacecraft clock time string:'
/// READ (*,FMT='(A)') SCLKCH
/// CALL SCENCD ( SC, SCLKCH, SCLKDP )
///
/// C
/// C Search from the beginning through all segments.
/// C
/// CALL DAFBFS ( HANDLE )
/// CALL DAFFNA ( SFND )
///
/// DO WHILE ( SFND )
///
/// CALL DAFGN ( IDENT )
/// CALL DAFGS ( DESCR )
/// CALL DAFUS ( DESCR, 2, 6, DCD, ICD )
///
/// IF ( INST .EQ. ICD( 1 ) .AND.
/// . SCLKDP + TOL .GE. DCD( 1 ) .AND.
/// . SCLKDP - TOL .LE. DCD( 2 ) ) THEN
///
/// DTYPE = ICD ( 3 )
///
/// IF ( DTYPE .EQ. 1 ) THEN
///
/// CALL CKR01 ( HANDLE, DESCR, SCLKDP, TOL, NEEDAV,
/// . RECORD, FOUND )
///
/// IF ( FOUND ) THEN
/// CALL CKE01 ( NEEDAV, RECORD, CMAT, AV, CLKOUT )
/// END IF
///
/// ELSE IF ( DTYPE .EQ. 2 ) THEN
///
/// CALL CKR02 ( HANDLE, DESCR, SCLKDP, TOL,
/// . RECORD, FOUND )
///
/// IF ( FOUND ) THEN
/// CALL CKE02 ( NEEDAV, RECORD, CMAT, AV, CLKOUT )
/// END IF
///
/// END IF
///
/// IF ( FOUND ) THEN
///
/// WRITE (*,*) 'Segment descriptor and identifier:'
/// WRITE (*,*) DCD, ICD
/// WRITE (*,*) IDENT
///
/// WRITE (*,*) 'C-matrix:'
/// WRITE (*,*) CMAT
///
/// END IF
///
/// END IF
///
/// CALL DAFFNA ( SFND )
///
/// END DO
/// ```
///
/// # Author and Institution
///
/// ```text
/// J. Diaz del Rio (ODC Space)
/// J.M. Lynch (JPL)
/// B.V. Semenov (JPL)
/// W.L. Taber (JPL)
/// E.D. Wright (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.0, 12-AUG-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
///
/// - SPICELIB Version 1.0.3, 31-JAN-2008 (BVS)
///
/// Removed non-standard end-of-declarations marker
/// 'C%&END_DECLARATIONS' from comments.
///
/// - SPICELIB Version 1.0.2, 22-AUG-2006 (EDW)
///
/// Replaced references to LDPOOL with references
/// to FURNSH.
///
/// - SPICELIB Version 1.0.1, 10-MAR-1992 (WLT)
///
/// Comment section for permuted index source lines was added
/// following the header.
///
/// - SPICELIB Version 1.0.0, 30-AUG-1991 (JML)
/// ```
pub fn cke02(
ctx: &mut SpiceContext,
needav: bool,
record: &[f64],
cmat: &mut [[f64; 3]; 3],
av: &mut [f64; 3],
clkout: &mut f64,
) -> crate::Result<()> {
CKE02(
needav,
record,
cmat.as_flattened_mut(),
av,
clkout,
ctx.raw_context(),
)?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure CKE02 ( C-kernel, evaluate pointing record, data type 2 )
pub fn CKE02(
NEEDAV: bool,
RECORD: &[f64],
CMAT: &mut [f64],
AV: &mut [f64],
CLKOUT: &mut f64,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let RECORD = DummyArray::new(RECORD, 1..);
let mut CMAT = DummyArrayMut2D::new(CMAT, 1..=3, 1..=3);
let mut AV = DummyArrayMut::new(AV, 1..=3);
let mut QUAT = StackArray::<f64, 4>::new(1..=4);
let mut CBASE = StackArray2D::<f64, 9>::new(1..=3, 1..=3);
let mut ROT = StackArray2D::<f64, 9>::new(1..=3, 1..=3);
let mut AVTEMP = StackArray::<f64, 3>::new(1..=3);
let mut ANGLE: f64 = 0.0;
let mut TIME: f64 = 0.0;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
} else {
CHKIN(b"CKE02", ctx)?;
}
//
// Copy the returned encoded SCLK time into CLKOUT.
//
*CLKOUT = RECORD[2];
// The quaternion stored in RECORD represents the C - matrix
// corresponding to the start time of the interval. The angular
// velocity vector is constant throughout the interval and gives
// the axis and rate by which the spacecraft is rotating.
//
// Copy the quaternion and the angular velocity from RECORD.
//
// RECORD ( 4 ) = q0
// RECORD ( 5 ) = q1
// RECORD ( 6 ) = q2
// RECORD ( 7 ) = q3
//
// RECORD ( 8 ) = av1
// RECORD ( 9 ) = av2
// RECORD ( 10 ) = av3
//
VEQUG(RECORD.subarray(4), 4, QUAT.as_slice_mut());
VEQU(RECORD.subarray(8), AVTEMP.as_slice_mut());
//
// Calculate the angle of the rotation.
//
// RECORD ( 1 ) = The start time of the interval.
// RECORD ( 2 ) = The time that pointing was returned for.
// RECORD ( 3 ) = The number of seconds per SCLK tick.
//
TIME = ((RECORD[2] - RECORD[1]) * RECORD[3]);
ANGLE = (TIME * VNORM(AVTEMP.as_slice()));
//
// Construct a matrix which rotates vectors by ANGLE radians about
// AVTEMP.
//
AXISAR(AVTEMP.as_slice(), ANGLE, ROT.as_slice_mut());
//
// Convert the quaternion to a C - matrix.
//
Q2M(QUAT.as_slice(), CBASE.as_slice_mut());
//
// Rotate each of the axis vectors of the spacecraft instrument frame
// by ANGLE radians about AVTEMP. (AVTEMP is given in the same
// inertial frame as the C - matrix.) The resulting matrix is the
// transpose of the requested C - matrix.
//
// [ ] [ ] T [ ] T
// [ ROT ] * [ CBASE ] = [ CMAT ]
// [ ] [ ] [ ]
//
// OR
//
// [ ] [ ] T [ ]
// [ CBASE ] * [ ROT ] = [ CMAT ]
// [ ] [ ] [ ]
//
MXMT(CBASE.as_slice(), ROT.as_slice(), CMAT.as_slice_mut());
//
// Return the angular velocity only if it is requested.
//
if NEEDAV {
VEQU(AVTEMP.as_slice(), AV.as_slice_mut());
}
CHKOUT(b"CKE02", ctx)?;
Ok(())
}