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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
pub const MAXDEG: i32 = 27;
const SIDLEN: i32 = 40;
const FPRINT: i32 = 32;
const LPRINT: i32 = 126;
const DSCSIZ: i32 = 5;
const DTYPE: i32 = 13;
const DIRSIZ: i32 = 100;
const STATSZ: i32 = 6;
/// Write SPK segment, type 13
///
/// Write a type 13 segment to an SPK file.
///
/// # Required Reading
///
/// * [NAIF_IDS](crate::required_reading::naif_ids)
/// * [SPC](crate::required_reading::spc)
/// * [SPK](crate::required_reading::spk)
/// * [TIME](crate::required_reading::time)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// HANDLE I Handle of an SPK file open for writing.
/// BODY I NAIF code for an ephemeris object.
/// CENTER I NAIF code for center of motion of BODY.
/// FRAME I Reference frame name.
/// FIRST I Start time of interval covered by segment.
/// LAST I End time of interval covered by segment.
/// SEGID I Segment identifier.
/// DEGREE I Degree of interpolating polynomials.
/// N I Number of states.
/// STATES I Array of states.
/// EPOCHS I Array of epochs corresponding to states.
/// MAXDEG P Maximum allowed degree of interpolating polynomial.
/// ```
///
/// # Detailed Input
///
/// ```text
/// HANDLE is the file handle of an SPK file that has been
/// opened for writing.
///
/// BODY is the NAIF integer code for an ephemeris object
/// whose state relative to another body is described
/// by the segment to be created.
///
/// CENTER is the NAIF integer code for the center of motion
/// of the object identified by BODY.
///
/// FRAME is the NAIF name for a reference frame
/// relative to which the state information for BODY
/// is specified.
///
/// FIRST,
/// LAST are, respectively, the start and stop times of
/// the time interval over which the segment defines
/// the state of BODY.
///
/// SEGID is the segment identifier. An SPK segment
/// identifier may contain up to 40 characters.
///
/// DEGREE is the degree of the Hermite polynomials used to
/// interpolate the states. All components of the
/// state vectors are interpolated by polynomials of
/// fixed degree.
///
/// N is the number of states in the input state vector
/// array.
///
/// STATES contains a time-ordered array of geometric states
/// ( x, y, z, dx/dt, dy/dt, dz/dt, in kilometers and
/// kilometers per second ) of BODY relative to CENTER,
/// specified relative to FRAME.
///
/// EPOCHS is an array of epochs corresponding to the members
/// of the state array. The epochs are specified as
/// seconds past J2000, TDB.
/// ```
///
/// # Detailed Output
///
/// ```text
/// None. See $Particulars for a description of the effect of this
/// routine.
/// ```
///
/// # Parameters
///
/// ```text
/// MAXDEG is the maximum allowed degree of the interpolating
/// polynomial. If the value of MAXDEG is increased,
/// the SPICELIB routine SPKPV must be changed
/// accordingly. In particular, the size of the
/// record passed to SPKRnn and SPKEnn must be
/// increased, and comments describing the record size
/// must be changed.
/// ```
///
/// # Exceptions
///
/// ```text
/// If any of the following exceptions occur, this routine will return
/// without creating a new segment.
///
/// 1) If FRAME is not a recognized name, the error
/// SPICE(INVALIDREFFRAME) is signaled.
///
/// 2) If the last non-blank character of SEGID occurs past index 40,
/// the error SPICE(SEGIDTOOLONG) is signaled.
///
/// 3) If SEGID contains any nonprintable characters, the error
/// SPICE(NONPRINTABLECHARS) is signaled.
///
/// 4) If DEGREE is not at least 1 or is greater than MAXDEG, the
/// error SPICE(INVALIDDEGREE) is signaled.
///
/// 5) If DEGREE is not odd, the error SPICE(INVALIDDEGREE) is
/// signaled.
///
/// 6) If the number of states N is not at least (DEGREE+1)/2,
/// the error SPICE(TOOFEWSTATES) is signaled.
///
/// 7) If FIRST is greater than or equal to LAST, the error
/// SPICE(BADDESCRTIMES) is signaled.
///
/// 8) If the elements of the array EPOCHS are not in strictly
/// increasing order, the error SPICE(TIMESOUTOFORDER) is
/// signaled.
///
/// 9) If the first epoch, EPOCHS(1), is greater than FIRST, the
/// error SPICE(BADDESCRTIMES) is signaled.
///
/// 10) If the last epoch, EPOCHS(N), is less than LAST, the error
/// SPICE(BADDESCRTIMES) is signaled.
/// ```
///
/// # Files
///
/// ```text
/// A new type 13 SPK segment is written to the SPK file attached
/// to HANDLE.
/// ```
///
/// # Particulars
///
/// ```text
/// This routine writes an SPK type 13 data segment to the open SPK
/// file according to the format described in the type 13 section of
/// the SPK Required Reading. The SPK file must have been opened with
/// write access.
/// ```
///
/// # 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) Suppose that you have a time-ordered array of geometric states
/// of a new object that follows Phobos, with a delay of 1 hour,
/// in its orbit around Mars and are prepared to produce a segment
/// of type 13 in an SPK file. Create a new SPK file with this
/// segment. Use an existing SPK to create the input data for the
/// SPK segment.
///
/// Use the meta-kernel shown below to load the required SPICE
/// kernels.
///
///
/// KPL/MK
///
/// File: spkw13_ex1.tm
///
/// This meta-kernel is intended to support operation of SPICE
/// example programs. The kernels shown here should not be
/// assumed to contain adequate or correct versions of data
/// required by SPICE-based user applications.
///
/// In order for an application to use this meta-kernel, the
/// kernels referenced here must be present in the user's
/// current working directory.
///
/// The names and contents of the kernels referenced
/// by this meta-kernel are as follows:
///
/// File name Contents
/// --------- --------
/// mar097.bsp Mars satellite ephemeris
/// naif0012.tls Leapseconds
///
/// \begindata
///
/// KERNELS_TO_LOAD = ( 'mar097.bsp',
/// 'naif0012.tls' )
///
/// \begintext
///
/// End of meta-kernel
///
///
/// Example code begins here.
///
///
/// PROGRAM SPKW13_EX1
/// IMPLICIT NONE
///
/// C
/// C SPICELIB functions
/// C
/// DOUBLE PRECISION HALFPI
///
/// C
/// C Local parameters.
/// C
/// CHARACTER*(*) SPKNAM
/// PARAMETER ( SPKNAM = 'spkw13_ex1.bsp' )
///
/// INTEGER DEGREE
/// PARAMETER ( DEGREE = 3 )
///
/// INTEGER MARS
/// PARAMETER ( MARS = 499 )
///
/// INTEGER NAMLEN
/// PARAMETER ( NAMLEN = 255 )
///
/// INTEGER NEPOCS
/// PARAMETER ( NEPOCS = 800 )
///
/// INTEGER NOBJ
/// PARAMETER ( NOBJ = 403 )
///
/// C
/// C Local variables.
/// C
/// CHARACTER*(NAMLEN) IFNAME
/// CHARACTER*(NAMLEN) SEGID
///
/// DOUBLE PRECISION DELTA
/// DOUBLE PRECISION ET
/// DOUBLE PRECISION EPOCHS ( NEPOCS )
/// DOUBLE PRECISION LT
/// DOUBLE PRECISION STATE ( 6 )
/// DOUBLE PRECISION STATES ( 6, NEPOCS )
/// DOUBLE PRECISION STEP
/// DOUBLE PRECISION TIME
///
/// INTEGER I
/// INTEGER HANDLE
///
/// C
/// C Load the input SPK file.
/// C
/// CALL FURNSH ( 'spkw13_ex1.tm' )
///
/// C
/// C Convert the input UTC to ephemeris time
/// C
/// CALL STR2ET ( '2018 Apr 03 08:35', ET )
///
/// C
/// C Create the time-ordered array of geometric states,
/// C at unequal time steps.
/// C
/// TIME = ET
/// STEP = 60.D0
/// DELTA = 10.D0
///
/// DO I=1, NEPOCS
///
/// CALL SPKEZR ( 'PHOBOS', TIME, 'J2000', 'NONE',
/// . 'MARS', STATES(1,I), LT )
///
/// EPOCHS(I) = TIME + 3600.D0
/// TIME = TIME + STEP +
/// . SIN( HALFPI() * I / 2.D0 ) * DELTA
///
/// END DO
///
/// C
/// C Open a new SPK file, with 5000 characters reserved
/// C for comments.
/// C
/// IFNAME = 'Test SPK type 13 internal filename.'
/// CALL SPKOPN ( SPKNAM, IFNAME, 5000, HANDLE )
///
/// C
/// C Create a segment identifier.
/// C
/// SEGID = 'MY_SAMPLE_SPK_TYPE_13_SEGMENT'
///
///
/// C
/// C Write the segment.
/// C
/// CALL SPKW13 ( HANDLE, NOBJ, MARS, 'J2000',
/// . EPOCHS(1), EPOCHS(NEPOCS), SEGID, DEGREE,
/// . NEPOCS, STATES, EPOCHS )
///
/// C
/// C Close the new SPK file.
/// C
/// CALL SPKCLS ( HANDLE )
///
/// C
/// C Compute the state of Phobos as seen from Mars,
/// C 12 hours after the input UTC time.
/// C
/// ET = ET + 43200.0D0
/// CALL SPKEZR ( 'PHOBOS', ET, 'J2000', 'NONE', 'MARS',
/// . STATE, LT )
///
/// WRITE (*,'(A)') 'Phobos as seen from Mars'
/// WRITE (*,'(A,F20.6)') ' Epoch (s):', ET
/// WRITE (*,'(A,3F14.6)') ' Position (km):',
/// . (STATE(I), I=1,3)
/// WRITE (*,'(A,3F14.6)') ' Velocity (km/s):',
/// . (STATE(I), I=4,6)
/// WRITE (*,*)
///
/// C
/// C Load the newly created kernel, and compute the state
/// C of the new object as seen from Mars, 13 hours after
/// C the input UTC time.
/// C
/// CALL FURNSH ( SPKNAM )
/// ET = ET + 3600.0D0
///
/// CALL SPKEZR ( '403', ET, 'J2000', 'NONE', 'MARS',
/// . STATE, LT )
///
/// WRITE (*,'(A)') 'Object 403 as seen from Mars'
/// WRITE (*,'(A,F20.6)') ' Epoch (s):', ET
/// WRITE (*,'(A,3F14.6)') ' Position (km):',
/// . (STATE(I), I=1,3)
/// WRITE (*,'(A,3F14.6)') ' Velocity (km/s):',
/// . (STATE(I), I=4,6)
///
/// END
///
///
/// When this program was executed on a Mac/Intel/gfortran/64-bit
/// platform, the output was:
///
///
/// Phobos as seen from Mars
/// Epoch (s): 576059769.185657
/// Position (km): -7327.262770 2414.326550 5207.106376
/// Velocity (km/s): -0.942893 -1.894731 -0.396715
///
/// Object 403 as seen from Mars
/// Epoch (s): 576063369.185657
/// Position (km): -7327.262770 2414.326550 5207.106376
/// Velocity (km/s): -0.942893 -1.894731 -0.396715
///
///
/// Note that after run completion, a new SPK file exists in
/// the output directory.
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 2.0.1, 05-JUL-2021 (JDR)
///
/// Edited the header to comply with NAIF standard. Removed
/// unnecessary $Revisions section.
///
/// - SPICELIB Version 2.0.0, 23-DEC-2013 (NJB)
///
/// Increased MAXDEG to 27 for compatibility
/// with SPK type 21. Deleted declaration of
/// unused parameter TYPIDX.
///
/// - SPICELIB Version 1.0.0, 20-MAR-2000 (NJB)
/// ```
pub fn spkw13(
ctx: &mut SpiceContext,
handle: i32,
body: i32,
center: i32,
frame: &str,
first: f64,
last: f64,
segid: &str,
degree: i32,
n: i32,
states: &[[f64; 6]],
epochs: &[f64],
) -> crate::Result<()> {
SPKW13(
handle,
body,
center,
frame.as_bytes(),
first,
last,
segid.as_bytes(),
degree,
n,
states.as_flattened(),
epochs,
ctx.raw_context(),
)?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure SPKW13 ( Write SPK segment, type 13 )
pub fn SPKW13(
HANDLE: i32,
BODY: i32,
CENTER: i32,
FRAME: &[u8],
FIRST: f64,
LAST: f64,
SEGID: &[u8],
DEGREE: i32,
N: i32,
STATES: &[f64],
EPOCHS: &[f64],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let STATES = DummyArray2D::new(STATES, 1..=6, 1..);
let EPOCHS = DummyArray::new(EPOCHS, 1..);
let mut DESCR = StackArray::<f64, 5>::new(1..=DSCSIZ);
let mut MAXTIM: f64 = 0.0;
let mut CHRCOD: i32 = 0;
let mut REFCOD: i32 = 0;
let mut WINSIZ: i32 = 0;
//
// SPICELIB functions
//
//
// Local parameters
//
//
// Local variables
//
//
// Local variables
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
} else {
CHKIN(b"SPKW13", ctx)?;
}
//
// Set the window size corresponding to the input degree. This
// size will be used in various places below.
//
WINSIZ = ((DEGREE + 1) / 2);
//
// Get the NAIF integer code for the reference frame.
//
NAMFRM(FRAME, &mut REFCOD, ctx)?;
if (REFCOD == 0) {
SETMSG(b"The reference frame # is not supported.", ctx);
ERRCH(b"#", FRAME, ctx);
SIGERR(b"SPICE(INVALIDREFFRAME)", ctx)?;
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
}
//
// Check to see if the segment identifier is too long.
//
if (LASTNB(SEGID) > SIDLEN) {
SETMSG(b"Segment identifier contains more than 40 characters.", ctx);
SIGERR(b"SPICE(SEGIDTOOLONG)", ctx)?;
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
}
//
// Now check that all the characters in the segment identifier
// can be printed.
//
for I in 1..=LASTNB(SEGID) {
CHRCOD = intrinsics::ICHAR(fstr::substr(SEGID, I..=I));
if ((CHRCOD < FPRINT) || (CHRCOD > LPRINT)) {
SETMSG(
b"The segment identifier contains nonprintable characters",
ctx,
);
SIGERR(b"SPICE(NONPRINTABLECHARS)", ctx)?;
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
}
}
//
// Make sure that the degree of the interpolating polynomials is
// in range.
//
if ((DEGREE < 1) || (DEGREE > MAXDEG)) {
SETMSG(
b"The interpolating polynomials have degree #; the valid degree range is [1, #]",
ctx,
);
ERRINT(b"#", DEGREE, ctx);
ERRINT(b"#", MAXDEG, ctx);
SIGERR(b"SPICE(INVALIDDEGREE)", ctx)?;
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
}
//
// Make sure that the degree of the interpolating polynomials is odd.
//
if EVEN(DEGREE) {
SETMSG(b"The interpolating polynomials have degree #; for SPK type 13, the degree must be odd.", ctx);
ERRINT(b"#", DEGREE, ctx);
SIGERR(b"SPICE(INVALIDDEGREE)", ctx)?;
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
}
//
// Make sure that the number of states is sufficient to define a
// polynomial whose degree is DEGREE.
//
if (N < WINSIZ) {
SETMSG(b"At least # states are required to define a Hermite polynomial of degree #. Number of states supplied: #", ctx);
ERRINT(b"#", WINSIZ, ctx);
ERRINT(b"#", DEGREE, ctx);
ERRINT(b"#", N, ctx);
SIGERR(b"SPICE(TOOFEWSTATES)", ctx)?;
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
}
//
// The segment stop time should be greater then the begin time.
//
if (FIRST >= LAST) {
SETMSG(
b"The segment start time: # is greater then the segment end time: #",
ctx,
);
ERRDP(b"#", FIRST, ctx);
ERRDP(b"#", LAST, ctx);
SIGERR(b"SPICE(BADDESCRTIMES)", ctx)?;
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
}
//
// Make sure the epochs form a strictly increasing sequence.
//
MAXTIM = EPOCHS[1];
for I in 2..=N {
if (EPOCHS[I] <= MAXTIM) {
SETMSG(
b"EPOCH # having index # is not greater than its predecessor #.",
ctx,
);
ERRDP(b"#", EPOCHS[I], ctx);
ERRINT(b"#", I, ctx);
ERRDP(b"#", EPOCHS[(I - 1)], ctx);
SIGERR(b"SPICE(TIMESOUTOFORDER)", ctx)?;
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
} else {
MAXTIM = EPOCHS[I];
}
}
//
// Make sure that the span of the input epochs includes the interval
// defined by the segment descriptor.
//
if (EPOCHS[1] > FIRST) {
SETMSG(b"Segment start time # precedes first epoch #.", ctx);
ERRDP(b"#", FIRST, ctx);
ERRDP(b"#", EPOCHS[1], ctx);
SIGERR(b"SPICE(BADDESCRTIMES)", ctx)?;
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
} else if (EPOCHS[N] < LAST) {
SETMSG(b"Segment end time # follows last epoch #.", ctx);
ERRDP(b"#", LAST, ctx);
ERRDP(b"#", EPOCHS[N], ctx);
SIGERR(b"SPICE(BADDESCRTIMES)", ctx)?;
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
}
//
// If we made it this far, we're ready to start writing the segment.
//
//
// Create the segment descriptor.
//
SPKPDS(
BODY,
CENTER,
FRAME,
DTYPE,
FIRST,
LAST,
DESCR.as_slice_mut(),
ctx,
)?;
//
// Begin a new segment.
//
DAFBNA(HANDLE, DESCR.as_slice(), SEGID, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKW13", ctx)?;
return Ok(());
}
//
// The type 13 segment structure is eloquently described by this
// diagram from the SPK Required Reading:
//
// +-----------------------+
// | State 1 |
// +-----------------------+
// | State 2 |
// +-----------------------+
// .
// .
// .
// +-----------------------+
// | State N |
// +-----------------------+
// | Epoch 1 |
// +-----------------------+
// | Epoch 2 |
// +-----------------------+
// .
// .
// .
// +-----------------------+
// | Epoch N |
// +-----------------------+
// | Epoch 100 | (First directory)
// +-----------------------+
// .
// .
// .
// +-----------------------+
// | Epoch ((N-1)/100)*100 | (Last directory)
// +-----------------------+
// | Window size - 1 |
// +-----------------------+
// | Number of states |
// +-----------------------+
//
//
DAFADA(STATES.as_slice(), (N * STATSZ), ctx)?;
DAFADA(EPOCHS.as_slice(), N, ctx)?;
for I in 1..=((N - 1) / DIRSIZ) {
DAFADA(EPOCHS.subarray((DIRSIZ * I)), 1, ctx)?;
}
DAFADA(&[((WINSIZ - 1) as f64)], 1, ctx)?;
DAFADA(&[(N as f64)], 1, ctx)?;
//
// As long as nothing went wrong, end the segment.
//
if !FAILED(ctx) {
DAFENA(ctx)?;
}
CHKOUT(b"SPKW13", ctx)?;
Ok(())
}