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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
const QSIZ: i32 = 4;
const QAVSIZ: i32 = 7;
const CK1DTP: i32 = 1;
const CK1RSZ: i32 = 8;
const CK2DTP: i32 = 2;
const CK2RSZ: i32 = 10;
const CK3DTP: i32 = 3;
const CK3RSZ: i32 = 17;
const CK4DTP: i32 = 4;
const CK4PCD: f64 = 128.0;
const CK4MXD: i32 = 18;
const CK4SFT: i32 = 10;
const CK4RSZ: i32 = (((CK4MXD + 1) * QAVSIZ) + CK4SFT);
const CK5DTP: i32 = 5;
const CK5MXD: i32 = 23;
const CK5MET: i32 = 4;
const CK5MXP: i32 = 14;
const CK5RSZ: i32 = (((CK5MXD + 1) * CK5MXP) + CK5MET);
const CK6DTP: i32 = 6;
const CK6MXD: i32 = 23;
const CK6MET: i32 = 4;
const CK6PS3: i32 = 7;
const CK6RSZ: i32 = (((CK6MXD + 1) * (CK6PS3 + 1)) + CK6MET);
const CKMRSZ: i32 = CK5RSZ;
const NDC: i32 = 2;
const NIC: i32 = 6;
const SHFTAD: i32 = (CK4SFT - 1);
/// C-kernel, get record, type 04
///
/// Return a specified pointing record from a CK type 04 segment.
/// The segment is identified by a CK file handle and segment
/// descriptor.
///
/// # Required Reading
///
/// * [CK](crate::required_reading::ck)
/// * [DAF](crate::required_reading::daf)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// HANDLE I The handle of the file containing the segment.
/// DESCR I The segment descriptor.
/// RECNO I The number of the pointing record to be returned.
/// RECORD O The pointing record.
/// ```
///
/// # Detailed Input
///
/// ```text
/// HANDLE is the handle of the binary CK file containing the
/// desired segment. The file should have been opened
/// for read or write access, either by CKLPF, DAFOPR,
/// or DAFOPW.
///
/// DESCR is the packed descriptor of the data type 4 segment.
///
/// RECNO is the number of the pointing record to be returned
/// from the data type 4 segment.
/// ```
///
/// # Detailed Output
///
/// ```text
/// RECORD is the pointing record indexed by RECNO in the
/// segment. The contents of the record are as follows:
///
/// ---------------------------------------------------
/// | The midpoint of the approximation interval |
/// ---------------------------------------------------
/// | The radius of the approximation interval |
/// ---------------------------------------------------
/// | Number of coefficients for q0 |
/// ---------------------------------------------------
/// | Number of coefficients for q1 |
/// ---------------------------------------------------
/// | Number of coefficients for q2 |
/// ---------------------------------------------------
/// | Number of coefficients for q3 |
/// ---------------------------------------------------
/// | Number of coefficients for AV1 |
/// ---------------------------------------------------
/// | Number of coefficients for AV2 |
/// ---------------------------------------------------
/// | Number of coefficients for AV3 |
/// ---------------------------------------------------
/// | q0 Cheby coefficients |
/// ---------------------------------------------------
/// | q1 Cheby coefficients |
/// ---------------------------------------------------
/// | q2 Cheby coefficients |
/// ---------------------------------------------------
/// | q3 Cheby coefficients |
/// ---------------------------------------------------
/// | AV1 Cheby coefficients (optional) |
/// ---------------------------------------------------
/// | AV2 Cheby coefficients (optional) |
/// ---------------------------------------------------
/// | AV3 Cheby coefficients (optional) |
/// ---------------------------------------------------
/// ```
///
/// # Parameters
///
/// ```text
/// See 'ckparam.inc'.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the segment is not of data type 4, the error
/// SPICE(CKWRONGDATATYPE) is signaled.
///
/// 2) If RECNO is less than one or greater than the number of
/// records in the specified segment, the error
/// SPICE(CKNONEXISTREC) is signaled.
///
/// 3) If the specified handle does not belong to any DAF file that
/// is currently known to be open, an error is signaled by a
/// routine in the call tree of this routine.
///
/// 4) If DESCR is not a valid descriptor of a segment in the CK
/// file specified by HANDLE, the results of this routine are
/// unpredictable.
/// ```
///
/// # Files
///
/// ```text
/// The file specified by HANDLE should be open for read or
/// write access.
/// ```
///
/// # Particulars
///
/// ```text
/// For a detailed description of the structure of a type 4 segment,
/// see the CK required reading.
///
/// This is a utility routine that may be used to read the individual
/// pointing records that make up a type 4 segment. It is normally
/// used in conjunction with CKNR04, which gives the number of
/// pointing records stored in a segment.
/// ```
///
/// # Examples
///
/// ```text
/// Suppose that DATA.BC is a CK file that contains segments of
/// data type 4. Then the following code fragment extracts the
/// data packets contained in the segment.
///
/// C
/// C CK parameters include file.
/// C
/// INCLUDE 'ckparam.inc'
///
/// C
/// C Local variables.
/// C
/// DOUBLE PRECISION DCD ( 2 )
/// DOUBLE PRECISION DESCR ( 5 )
/// DOUBLE PRECISION PKTDAT ( CK4RSZ )
///
/// INTEGER AVFLAG
/// INTEGER HANDLE
/// INTEGER I
/// INTEGER ICD ( 6 )
/// INTEGER K
/// INTEGER LASTAD
/// INTEGER NCOEF ( QAVSIZ )
/// INTEGER NREC
///
/// LOGICAL FOUND
/// C
/// C First load the file. (The file may also be opened by using
/// C CKLPF.)
/// C
/// CALL DAFOPR ( 'DATA.BC', HANDLE )
/// C
/// C Begin forward search. Find the first array.
/// C
/// CALL DAFBFS ( HANDLE )
/// CALL DAFFNA ( FOUND )
/// C
/// C Get segment descriptor.
/// C
/// CALL DAFGS ( DESCR )
/// C
/// C Unpack the segment descriptor into its double precision
/// C and integer components.
/// C
/// CALL DAFUS ( DESCR, 2, 6, DCD, ICD )
///
/// IF ( ICD( 3 ) .EQ. 4 ) THEN
/// C
/// C How many records does this segment contain?
/// C
/// CALL CKNR04 ( HANDLE, DESCR, NREC )
///
/// DO I = 1, NREC
/// C
/// C Get the data records stored in the segment.
/// C
/// CALL CKGR04 ( HANDLE, DESCR, I, PKTDAT )
/// C
/// C Print data packet contents. Print coverage interval
/// C midpoint & radii first.
/// C
/// WRITE (2,*) PKTDAT (1)
/// WRITE (2,*) PKTDAT (2)
/// C
/// C Decode numbers of coefficients.
/// C
/// CALL ZZCK4D2I ( PKTDAT(3), QAVSIZ, CK4PCD, NCOEF )
/// C
/// C Print number of coefficients for Q0, Q1, Q2 and Q3.
/// C
/// WRITE (2,FMT='(I2,6X,I2)') NCOEF( 1 ), NCOEF( 2 )
/// WRITE (2,FMT='(I2,6X,I2)') NCOEF( 3 ), NCOEF( 4 )
/// C
/// C Print number coefficients for AV1, AV2 and AV3.
/// C
/// WRITE (2,FMT='(I2,6X,I2)') NCOEF( 5 ), NCOEF( 6 )
/// WRITE (2,FMT='(I2,6X,I2)') NCOEF( 7 )
/// C
/// C Print Cheby coefficients.
/// C
/// LASTAD = 0
///
/// DO K = 1, QAVSIZ
/// LASTAD = LASTAD + NCOEF( K )
/// END DO
///
/// DO K = 4, LASTAD + 4
/// WRITE (2,*) PKTDAT (K)
/// END DO
///
/// END DO
///
/// END IF
/// ```
///
/// # Restrictions
///
/// ```text
/// 1) The binary CK file containing the segment whose descriptor
/// was passed to this routine must be opened for read or write
/// access by either CKLPF, DAFOPR, or DAFOPW.
/// ```
///
/// # Author and Institution
///
/// ```text
/// J. Diaz del Rio (ODC Space)
/// B.V. Semenov (JPL)
/// Y.K. Zaiko (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.0.2, 26-OCT-2021 (JDR)
///
/// Edited the header to comply with NAIF standard.
///
/// - SPICELIB Version 1.0.1, 18-APR-2014 (BVS)
///
/// Minor header edits.
///
/// - SPICELIB Version 1.0.0, 05-MAY-1999 (YKZ) (BVS)
/// ```
pub fn ckgr04(
ctx: &mut SpiceContext,
handle: i32,
descr: &[f64],
recno: i32,
record: &mut [f64],
) -> crate::Result<()> {
CKGR04(handle, descr, recno, record, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure CKGR04 ( C-kernel, get record, type 04 )
pub fn CKGR04(
HANDLE: i32,
DESCR: &[f64],
RECNO: i32,
RECORD: &mut [f64],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let DESCR = DummyArray::new(DESCR, 1..);
let mut RECORD = DummyArrayMut::new(RECORD, 1..);
let mut DCD = StackArray::<f64, 2>::new(1..=NDC);
let mut ENDS = StackArray::<i32, 1>::new(1..=1);
let mut ICD = StackArray::<i32, 6>::new(1..=NIC);
let mut NREC: i32 = 0;
let mut NUMALL: i32 = 0;
let mut NUMCFT = StackArray::<i32, 7>::new(1..=QAVSIZ);
//
// SPICELIB functions
//
//
// Local parameters
//
//
// Length (in DPs) of non-coefficient front part of RECORD when
// it contains decoded numbers of coefficients. It is one less
// than the length of the same part in a record exchanged between
// CKR04 and CKE04 because it doesn't contain time at which
// pointing has to be evaluated.
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
} else {
CHKIN(b"CKGR04", ctx)?;
}
//
// Unpack descriptor and check segment data type. Signal an error
// if it's not 4.
//
DAFUS(
DESCR.as_slice(),
NDC,
NIC,
DCD.as_slice_mut(),
ICD.as_slice_mut(),
);
if (ICD[3] != CK4DTP) {
SETMSG(
b"Data type of the segment should be 4: Passed descriptor shows type = #.",
ctx,
);
ERRINT(b"#", ICD[3], ctx);
SIGERR(b"SPICE(CKWRONGDATATYPE)", ctx)?;
CHKOUT(b"CKGR04", ctx)?;
return Ok(());
}
//
// If a request was made for a data record which doesn't
// exist, then signal an error and leave.
//
CKNR04(HANDLE, DESCR.as_slice(), &mut NREC, ctx)?;
if ((RECNO < 1) || (RECNO > NREC)) {
SETMSG(
b"Requested record number (#) does not exist. There are # records in the segment.",
ctx,
);
ERRINT(b"#", RECNO, ctx);
ERRINT(b"#", NREC, ctx);
SIGERR(b"SPICE(CKNONEXISTREC)", ctx)?;
CHKOUT(b"CKGR04", ctx)?;
return Ok(());
}
//
// Get the data record indexed by RECNO.
//
SGFPKT(
HANDLE,
DESCR.as_slice(),
RECNO,
RECNO,
RECORD.as_slice_mut(),
ENDS.as_slice_mut(),
ctx,
)?;
//
// Decode 7 numbers of coefficients from double precision value.
//
ZZCK4D2I(&mut RECORD[3], QAVSIZ, CK4PCD, NUMCFT.as_slice_mut());
//
// Compute total number of coefficients in the fetched packet.
//
NUMALL = 0;
for K in 1..=QAVSIZ {
NUMALL = (NUMALL + NUMCFT[K]);
}
//
// Move polynomial coefficients to the right to free space for
// decoded numbers of coefficients and insert these numbers
// starting from the third position.
//
for K in intrinsics::range(NUMALL, 1, -1) {
RECORD[(K + SHFTAD)] = RECORD[(K + 3)];
}
for K in 1..=7 {
RECORD[(K + 2)] = NUMCFT[K] as f64;
}
//
// All done.
//
CHKOUT(b"CKGR04", ctx)?;
Ok(())
}