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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
const ND: i32 = 2;
const NI: i32 = 6;
const NDESCR: i32 = (ND + ((NI + 1) / 2));
/// CK type 04: End a segment
///
/// End the type 04 CK segment currently being written to the DAF
/// file associated with HANDLE. See also CKW04B and CKW04E.
///
/// # 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 an CK file open for writing.
/// ENDTIM I The segment coverage end encoded SCLK time.
/// ```
///
/// # Detailed Input
///
/// ```text
/// HANDLE is the file handle of an CK file that has been
/// opened for writing, and to which a type 4 CK segment
/// is being written.
///
/// ENDTIM is the encoded SCLK time for the end of the segment
/// coverage.
/// ```
///
/// # Detailed Output
///
/// ```text
/// None.
///
/// The type 04 segment in the DAF file associated with HANDLE will be
/// ended, making the addition of the data to the file permanent.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If an error occurs while reading or writing the file
/// indicated by HANDLE, the error is signaled by a routine in
/// the call tree of this routine.
/// ```
///
/// # Files
///
/// ```text
/// See the argument HANDLE.
/// ```
///
/// # Particulars
///
/// ```text
/// This routine ends a type 4 CK segment which is being written to
/// the DAF file associated with HANDLE. Ending the DAF segment is a
/// necessary step in the process of making the data a permanent part
/// of the DAF file.
///
/// This routine is one of a set of three routines for creating and
/// adding data to type 4 CK segments. These routines are:
///
/// CKW04B: Begin a type 4 CK segment. This routine must be
/// called before any data may be added to a type 4
/// segment.
///
/// CKW04A: Add data to a type 4 CK segment. This routine may be
/// called any number of times after a call to CKW04B to
/// add type 4 records to the CK segment that was
/// started.
///
/// CKW04E: End a type 4 CK segment. This routine is called to
/// make the type 4 segment a permanent addition to the
/// DAF file. Once this routine is called, no further type
/// 4 records may be added to the segment. A new segment
/// must be started.
///
/// A type 4 CK segment consists of coefficient sets for variable
/// order Chebyshev polynomials over consecutive time intervals of
/// a variable length. The gaps between intervals are allowed.
/// The Chebyshev polynomials represent individual quaternion
/// components q0, q1, q2 and q3 and individual angular velocities
/// AV1, AV2 and AV3 if they are included with the data.
///
/// The pointing data supplied to the type 4 CK writer (CKW04A)
/// is packed into an array as a sequence of records,
///
/// ----------------------------------------------------
/// | Record 1 | Record 2 | .. | Record N-1 | Record N |
/// ----------------------------------------------------
///
/// with each record in data packets has the following format.
///
/// ----------------------------------------------------
/// | 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) |
/// ----------------------------------------------------
/// ```
///
/// # Examples
///
/// ```text
/// Assume that we have:
///
/// HANDLE is the handle of an CK file opened with write
/// access.
///
/// SEGID is a character string of no more than 40 characters
/// which provides a pedigree for the data in the CK
/// segment we will create.
///
/// INST is the SPICE ID code for the instrument whose
/// pointing data is to be placed into the file.
///
/// AVFLAG angular rates flag.
///
/// REFFRM is the name of the SPICE reference frame for the
/// pointing data.
///
/// BEGTIM is the starting encoded SCLK time for which the
/// segment is valid.
///
/// ENDTIM is the ending encoded SCLK time for which the segment
/// is valid.
///
/// N is the number of type 4 records that we want to
/// put into a segment in an CK file.
///
/// NPKTS is integer array which contains the lengths of
/// variable size data packets
///
/// RECRDS contains N type 4 records packaged for the CK
/// file.
///
/// SCSTRT contains the initial encoded SC time for each of
/// the records contained in RECRDS, where
///
/// SCSTRT(I) < SCSTRT(I+1), I = 1, N-1
///
/// SCSTRT(1) <= FIRST, SCSTRT(N) < LAST
///
/// Then the following code fragment demonstrates how to create
/// a type 4 CK segment if all of the data for the segment is
/// available at one time.
///
/// C
/// C Begin the segment.
/// C
/// CALL CKW04B ( HANDLE, BEGTIM, INST, REF, AVFLAG, SEGID )
/// C
/// C Add the data to the segment all at once.
/// C
/// CALL CKW04A ( HANDLE, N, NPKTS, RECRDS, SCSTRT )
/// C
/// C End the segment, making the segment a permanent
/// C addition to the CK file.
/// C
/// CALL CKW04E ( HANDLE, ENDTIM )
/// ```
///
/// # Restrictions
///
/// ```text
/// 1) The type 4 CK segment being closed must have been started by
/// the routine CKW04B, the routine which begins a type 4 CK
/// segment.
/// ```
///
/// # 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, 02-JUN-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 ckw04e(ctx: &mut SpiceContext, handle: i32, endtim: f64) -> crate::Result<()> {
CKW04E(handle, endtim, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure CKW04E ( CK type 04: End a segment )
pub fn CKW04E(HANDLE: i32, ENDTIM: f64, ctx: &mut Context) -> f2rust_std::Result<()> {
let mut DCD = StackArray::<f64, 2>::new(1..=ND);
let mut DESCR = StackArray::<f64, 5>::new(1..=NDESCR);
let mut ICD = StackArray::<i32, 6>::new(1..=NI);
let mut FOUND: bool = false;
//
// SPICELIB functions.
//
//
// Local parameters.
//
//
// DAF ND and NI values for CK files and length of a DAF descriptor.
//
//
// Local variables.
//
//
// Standard SPICELIB error handling.
//
if RETURN(ctx) {
return Ok(());
} else {
CHKIN(b"CKW04E", ctx)?;
}
//
// This is simple, just call the routine which ends a generic
// segment.
//
SGWES(HANDLE, ctx)?;
if FAILED(ctx) {
CHKOUT(b"CKW04E", ctx)?;
return Ok(());
}
//
// Now update the descriptor with the end time. Locate the segment
// with a backward search.
//
DAFBBS(HANDLE, ctx)?;
DAFFPA(&mut FOUND, ctx)?;
if !FOUND {
//
// We have a bug.
//
SETMSG(b"The segment which was just written could not be found by a DAF search. This indicates a serious error. Contact NAIF.", ctx);
SIGERR(b"SPICE(BUG)", ctx)?;
CHKOUT(b"CKW04E", ctx)?;
return Ok(());
}
//
// Get the descriptor, set the end time, and update the descriptor
// in the file.
//
DAFGS(DESCR.as_slice_mut(), ctx)?;
DAFUS(
DESCR.as_slice(),
ND,
NI,
DCD.as_slice_mut(),
ICD.as_slice_mut(),
);
DCD[2] = ENDTIM;
DAFPS(ND, NI, DCD.as_slice(), ICD.as_slice(), DESCR.as_slice_mut());
DAFRS(DESCR.as_slice(), ctx)?;
//
// All done.
//
CHKOUT(b"CKW04E", ctx)?;
Ok(())
}