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
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
const TYPE: &[u8] = b"CK";
const ND: i32 = 2;
const NI: i32 = 6;
const MXCREC: i32 = 1000;
/// CK, open new file.
///
/// Open a new CK file, returning the handle of the opened file.
///
/// # Required Reading
///
/// * [CK](crate::required_reading::ck)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// FNAME I The name of the CK file to be opened.
/// IFNAME I The internal filename for the CK.
/// NCOMCH I The number of characters to reserve for comments.
/// HANDLE O The handle of the opened CK file.
/// ```
///
/// # Detailed Input
///
/// ```text
/// FNAME is the name of the CK file to be opened.
///
/// IFNAME is the internal filename for the CK file that is being
/// created. The internal filename may be up to 60
/// characters long. If you do not have any conventions
/// for tagging your files, an internal filename of
/// 'CK_file' is perfectly acceptable. You may also leave
/// it blank if you like.
///
/// NCOMCH is the space, measured in characters, to be initially
/// set aside for the comment area when a new CK file
/// is opened. The amount of space actually set aside may
/// be greater than the amount requested, due to the
/// manner in which comment records are allocated in an CK
/// file. However, the amount of space set aside for
/// comments will always be at least the amount that was
/// requested.
///
/// The value of NCOMCH should be greater than or equal to
/// zero, i.e., 0 <= NCOMCH. A negative value, should one
/// occur, will be assumed to be zero.
/// ```
///
/// # Detailed Output
///
/// ```text
/// HANDLE is the handle of the opened CK file. If an error
/// occurs the value of this variable will not represent a
/// valid handle.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) If the value of NCOMCH is negative, a value of zero (0) will
/// be used for the number of comment characters to be set aside
/// for comments.
///
/// 2) If an error occurs while attempting to open a CK file the
/// value of HANDLE will not represent a valid file handle.
/// ```
///
/// # Files
///
/// ```text
/// See FNAME and HANDLE.
/// ```
///
/// # Particulars
///
/// ```text
/// Open a new CK file, reserving room for comments if requested.
///
/// A CKCLS call should balance every CKOPN call.
/// ```
///
/// # Examples
///
/// ```text
/// The numerical results shown for this example may differ across
/// platforms. The results depend on the SPICE kernels used as
/// input, the compiler and supporting libraries, and the machine
/// specific arithmetic implementation.
///
/// 1) Create a CK type 3 segment; fill with data for a simple time
/// dependent rotation and angular velocity, and reserve room in
/// the CK comments area for 5000 characters.
///
/// Example code begins here.
///
///
/// PROGRAM CKOPN_EX1
/// IMPLICIT NONE
///
/// C
/// C Local parameters.
/// C
/// CHARACTER*(*) CK3
/// PARAMETER ( CK3 = 'ckopn_ex1.bc' )
///
/// DOUBLE PRECISION SPTICK
/// PARAMETER ( SPTICK = 0.001D0 )
///
/// INTEGER INST
/// PARAMETER ( INST = -77703 )
///
/// INTEGER MAXREC
/// PARAMETER ( MAXREC = 201 )
///
/// INTEGER NAMLEN
/// PARAMETER ( NAMLEN = 42 )
///
/// C
/// C Local variables.
/// C
/// CHARACTER*(NAMLEN) REF
/// CHARACTER*(NAMLEN) IFNAME
/// CHARACTER*(NAMLEN) SEGID
///
/// DOUBLE PRECISION AVVS ( 3,MAXREC )
/// DOUBLE PRECISION BEGTIM
/// DOUBLE PRECISION ENDTIM
/// DOUBLE PRECISION QUATS ( 0:3,MAXREC )
/// DOUBLE PRECISION RATE
/// DOUBLE PRECISION RWMAT ( 3, 3 )
/// DOUBLE PRECISION SPACES
/// DOUBLE PRECISION SCLKDP ( MAXREC )
/// DOUBLE PRECISION STARTS ( MAXREC/2)
/// DOUBLE PRECISION STICKS
/// DOUBLE PRECISION THETA
/// DOUBLE PRECISION WMAT ( 3, 3 )
/// DOUBLE PRECISION WQUAT ( 0:3 )
///
/// INTEGER HANDLE
/// INTEGER I
/// INTEGER NCOMCH
/// INTEGER NINTS
///
/// LOGICAL AVFLAG
///
/// C
/// C NCOMCH is the number of characters to reserve for the
/// C kernel's comment area. This example doesn't write
/// C comments, but it reserves room for 5000 characters.
/// C
/// NCOMCH = 5000
///
/// C
/// C The base reference from for the rotation data.
/// C
/// REF = 'J2000'
///
/// C
/// C Time spacing in encoded ticks and in seconds
/// C
/// STICKS = 10.D0
/// SPACES = STICKS * SPTICK
///
/// C
/// C Declare an angular rate in radians per sec.
/// C
/// RATE = 1.D-2
///
/// C
/// C Internal file name and segment ID.
/// C
/// SEGID = 'Test type 3 CK segment'
/// IFNAME = 'Test CK type 3 segment created by CKW03'
///
///
/// C
/// C Open a new kernel.
/// C
/// CALL CKOPN ( CK3, IFNAME, NCOMCH, HANDLE )
///
/// C
/// C Create a 3x3 double precision identity matrix.
/// C
/// CALL IDENT ( WMAT )
///
/// C
/// C Convert the matrix to quaternion.
/// C
/// CALL M2Q ( WMAT, WQUAT )
///
/// C
/// C Copy the work quaternion to the first row of
/// C QUATS.
/// C
/// CALL MOVED ( WQUAT, 4, QUATS(0,1) )
///
/// C
/// C Create an angular velocity vector. This vector is in the
/// C REF reference frame and indicates a constant rotation
/// C about the Z axis.
/// C
/// CALL VPACK ( 0.D0, 0.D0, RATE, AVVS(1,1) )
///
/// C
/// C Set the initial value of the encoded ticks.
/// C
/// SCLKDP(1) = 1000.D0
///
/// C
/// C Fill the rest of the AVVS and QUATS matrices
/// C with simple data.
/// C
/// DO I = 2, MAXREC
///
/// C
/// C Create the corresponding encoded tick value in
/// C increments of STICKS with an initial value of
/// C 1000.0 ticks.
/// C
/// SCLKDP(I) = 1000.D0 + (I-1) * STICKS
///
/// C
/// C Create the transformation matrix for a rotation of
/// C THETA about the Z axis. Calculate THETA from the
/// C constant angular rate RATE at increments of SPACES.
/// C
/// THETA = (I-1) * RATE * SPACES
/// CALL ROTMAT ( WMAT, THETA, 3, RWMAT )
///
/// C
/// C Convert the RWMAT matrix to SPICE type quaternion.
/// C
/// CALL M2Q ( RWMAT, WQUAT )
///
/// C
/// C Store the quaternion in the QUATS matrix.
/// C Store angular velocity in AVVS.
/// C
/// CALL MOVED ( WQUAT, 4, QUATS(0,I) )
/// CALL VPACK ( 0.D0, 0.D0, RATE, AVVS(1,I) )
///
/// END DO
///
/// C
/// C Create an array start times for the interpolation
/// C intervals. The end time for a particular interval is
/// C determined as the time of the final data value prior in
/// C time to the next start time.
/// C
/// NINTS = MAXREC/2
/// DO I = 1, NINTS
///
/// STARTS(I) = SCLKDP(I*2 - 1)
///
/// END DO
///
/// C
/// C Set the segment boundaries equal to the first and last
/// C time for the data arrays.
/// C
/// BEGTIM = SCLKDP(1)
/// ENDTIM = SCLKDP(MAXREC)
///
/// C
/// C This segment contains angular velocity.
/// C
/// AVFLAG = .TRUE.
///
/// C
/// C All information ready to write. Write to a CK type 3
/// C segment to the file indicated by HANDLE.
/// C
/// CALL CKW03 ( HANDLE, BEGTIM, ENDTIM, INST, REF,
/// . AVFLAG, SEGID, MAXREC, SCLKDP, QUATS,
/// . AVVS, NINTS, STARTS )
///
/// C
/// C SAFELY close the file.
/// C
/// CALL CKCLS ( HANDLE )
///
/// END
///
///
/// When this program is executed, no output is presented on
/// screen. After run completion, a new CK file exists in the
/// output directory.
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// K.R. Gehringer (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 2.1.0, 02-JUL-2021 (JDR)
///
/// Changed the output argument name NAME to FNAME for consistency
/// with other routines.
///
/// Edited the header to comply with NAIF standard. Added
/// complete code example based on existing fragment.
///
/// Extended $Parameters section.
///
/// - SPICELIB Version 2.0.0, 09-NOV-2006 (NJB)
///
/// Routine has been upgraded to support comment
/// area allocation using NCOMCH.
///
/// - SPICELIB Version 1.0.0, 26-JAN-1995 (KRG)
/// ```
pub fn ckopn(
ctx: &mut SpiceContext,
fname: &str,
ifname: &str,
ncomch: i32,
handle: &mut i32,
) -> crate::Result<()> {
CKOPN(
fname.as_bytes(),
ifname.as_bytes(),
ncomch,
handle,
ctx.raw_context(),
)?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure CKOPN ( CK, open new file. )
pub fn CKOPN(
FNAME: &[u8],
IFNAME: &[u8],
NCOMCH: i32,
HANDLE: &mut i32,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let mut NCOMR: i32 = 0;
//
// SPICELIB functions
//
//
// Local parameters
//
//
// DAF ND and NI values for CK files.
//
//
// Length of a DAF comment record, in characters.
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
}
CHKIN(b"CKOPN", ctx)?;
//
// Compute the number of comment records that we want to allocate, if
// the number of comment characters requested is greater than zero,
// we always allocate an extra record to account for the end of line
// marks in the comment area.
//
if (NCOMCH > 0) {
NCOMR = (((NCOMCH - 1) / MXCREC) + 1);
} else {
NCOMR = 0;
}
//
// Just do it. All of the error handling is taken care of for us.
//
DAFONW(FNAME, TYPE, ND, NI, IFNAME, NCOMR, HANDLE, ctx)?;
if FAILED(ctx) {
//
// If we failed, make sure that HANDLE does not contain a value
// that represents a valid DAF file handle.
//
*HANDLE = 0;
}
CHKOUT(b"CKOPN", ctx)?;
Ok(())
}