rsspice 0.1.0

Pure Rust port of the SPICE Toolkit for space geometry
Documentation
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
//
// GENERATED FILE
//

use super::*;
use crate::SpiceContext;
use f2rust_std::*;

pub const LBCELL: i32 = -5;
const KWINTV: &[u8] = b"INTERVAL";
const KWSEG: &[u8] = b"SEGMENT";
const LNSIZE: i32 = 80;
const ND: i32 = 2;
const NI: i32 = 6;

/// CK coverage
///
/// Find the coverage window for a specified object in a specified CK
/// file.
///
/// # Required Reading
///
/// * [CELLS](crate::required_reading::cells)
/// * [DAF](crate::required_reading::daf)
/// * [CK](crate::required_reading::ck)
/// * [TIME](crate::required_reading::time)
/// * [WINDOWS](crate::required_reading::windows)
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  CKFNM      I   Name of CK file.
///  IDCODE     I   ID code of object.
///  NEEDAV     I   Flag indicating whether angular velocity is needed.
///  LEVEL      I   Coverage level: 'SEGMENT' OR 'INTERVAL'.
///  TOL        I   Tolerance in ticks.
///  TIMSYS     I   Time system used to represent coverage.
///  COVER     I-O  Window giving coverage for IDCODE.
/// ```
///
/// # Detailed Input
///
/// ```text
///  CKFNM    is the name of a C-kernel.
///
///  IDCODE   is the integer ID code of an object, normally a
///           spacecraft structure or instrument, for which pointing
///           data are expected to exist in the specified CK file.
///
///  NEEDAV   is a logical variable indicating whether only segments
///           having angular velocity are to be considered when
///           determining coverage. When NEEDAV is .TRUE., segments
///           without angular velocity don't contribute to the coverage
///           window; when NEEDAV is .FALSE., all segments for IDCODE
///           may contribute to the coverage window.
///
///  LEVEL    is the level (granularity) at which the coverage is
///           examined. Allowed values and corresponding meanings are:
///
///              'SEGMENT'    The output coverage window contains
///                           intervals defined by the start and stop
///                           times of segments for the object
///                           designated by IDCODE.
///
///              'INTERVAL'   The output coverage window contains
///                           interpolation intervals of segments for
///                           the object designated by IDCODE. For type
///                           1 segments, which don't have
///                           interpolation intervals, each epoch
///                           associated with a pointing instance is
///                           treated as a singleton interval; these
///                           intervals are added to the coverage
///                           window.
///
///                           All interpolation intervals are
///                           considered to lie within the segment
///                           bounds for the purpose of this summary:
///                           if an interpolation interval extends
///                           beyond the segment coverage interval,
///                           only its intersection with the segment
///                           coverage interval is considered to
///                           contribute to the total coverage.
///
///  TOL      is a tolerance value expressed in ticks of the spacecraft
///           clock associated with IDCODE. Before each interval is
///           inserted into the coverage window, the interval is
///           intersected with the segment coverage interval, then if
///           the intersection is non-empty, it is expanded by TOL: the
///           left endpoint of the intersection interval is reduced by
///           TOL and the right endpoint is increased by TOL. Adjusted
///           interval endpoints, when expressed as encoded SCLK, never
///           are less than zero ticks. Any intervals that overlap as a
///           result of the expansion are merged.
///
///           The coverage window returned when TOL > 0 indicates the
///           coverage provided by the file to the CK readers CKGPAV
///           and CKGP when that value of TOL is passed to them as an
///           input.
///
///  TIMSYS   is a string indicating the time system used in the output
///           coverage window. TIMSYS may have the values:
///
///               'SCLK'    Elements of COVER are expressed in encoded
///                         SCLK ("ticks"), where the clock is
///                         associated with the object designated by
///                         IDCODE.
///
///               'TDB'     Elements of COVER are expressed as seconds
///                         past J2000 TDB.
///
///
///  COVER    is an initialized SPICE window data structure. COVER
///           optionally may contain coverage data on input; on output,
///           the data already present in COVER will be combined with
///           coverage found for the object designated by IDCODE in the
///           file CKFNM.
///
///           If COVER contains no data on input, its size and
///           cardinality still must be initialized.
/// ```
///
/// # Detailed Output
///
/// ```text
///  COVER    is a SPICE window data structure which represents the
///           merged coverage for IDCODE. When the coverage level is
///           'INTERVAL', this is the set of time intervals for which
///           data for IDCODE are present in the file CKFNM, merged
///           with the set of time intervals present in COVER on input.
///           The merged coverage is represented as the union of one or
///           more disjoint time intervals. The window COVER contains
///           the pairs of endpoints of these intervals.
///
///           When the coverage level is 'SEGMENT', COVER is computed
///           in a manner similar to that described above, but the
///           coverage intervals used in the computation are those of
///           segments rather than interpolation intervals within
///           segments.
///
///           When TOL is > 0, the intervals comprising the coverage
///           window for IDCODE are expanded by TOL and any intervals
///           overlapping as a result are merged. The resulting window
///           is returned in COVER. The expanded window in no case
///           extends beyond the segment bounds in either direction by
///           more than TOL.
///
///           The interval endpoints contained in COVER are encoded
///           spacecraft clock times if TIMSYS is 'SCLK'; otherwise the
///           times are converted from encoded spacecraft clock to
///           seconds past J2000 TDB.
///
///           See the $Examples section below for a complete example
///           program showing how to retrieve the endpoints from COVER.
/// ```
///
/// # Exceptions
///
/// ```text
///  1)  If the input file has transfer format, the error
///      SPICE(INVALIDFORMAT) is signaled.
///
///  2)  If the input file is not a transfer file but has architecture
///      other than DAF, the error SPICE(INVALIDARCHTYPE) is signaled.
///
///  3)  If the input file is a binary DAF file of type other than CK,
///      the error SPICE(INVALIDFILETYPE) is signaled.
///
///  4)  If the CK file cannot be opened or read, an error is signaled
///      by a routine in the call tree of this routine. The output
///      window will not be modified.
///
///  5)  If the size of the output window argument COVER is
///      insufficient to contain the actual number of intervals in the
///      coverage window for IDCODE, an error is signaled by a routine
///      in the call tree of this routine.
///
///  6)  If TOL is negative, the error SPICE(VALUEOUTOFRANGE) is
///      signaled.
///
///  7)  If LEVEL is not recognized, the error SPICE(INVALIDOPTION)
///      is signaled.
///
///  8)  If TIMSYS is not recognized, the error SPICE(NOTSUPPORTED)
///      is signaled.
///
///  9)  If a time conversion error occurs, the error is signaled by a
///      routine in the call tree of this routine.
///
///  10) If the output time system is TDB, the CK subsystem must be
///      able to map IDCODE to the ID code of the associated spacecraft
///      clock. If this mapping cannot be performed, an error is
///      signaled by a routine in the call tree of this routine.
///
///  11) If the input CK type is not one of the supported CK types, the
///      error SPICE(NOTSUPPORTED) is signaled. This problem may
///      indicate the version of the SPICE Toolkit being used is
///      outdated and a new version is required.
/// ```
///
/// # Files
///
/// ```text
///  This routine reads a C-kernel.
///
///  If the output time system is 'TDB', then a leapseconds kernel
///  and an SCLK kernel for the spacecraft clock associated with
///  IDCODE must be loaded before this routine is called.
///
///  If the ID code of the clock associated with IDCODE is not
///  equal to
///
///     IDCODE / 1000
///
///  then the kernel variable
///
///     CK_<IDCODE>_SCLK
///
///  must be present in the kernel pool to identify the clock
///  associated with IDCODE. This variable must contain the ID code
///  to be used for conversion between SCLK and TDB. Normally this
///  variable is provided in a text kernel loaded via FURNSH.
/// ```
///
/// # Particulars
///
/// ```text
///  This routine provides an API via which applications can determine
///  the coverage a specified CK file provides for a specified
///  object.
/// ```
///
/// # Examples
///
/// ```text
///  The numerical results shown for these examples 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) Display the interval-level coverage for each object in a
///     specified CK file. Use tolerance of zero ticks. Do not
///     request angular velocity. Express the results in the TDB time
///     system.
///
///     Find the set of objects in the file. Loop over the contents
///     of the ID code set: find the coverage for each item in the
///     set and display the coverage.
///
///
///     Example code begins here.
///
///
///           PROGRAM CKCOV_EX1
///           IMPLICIT NONE
///
///     C
///     C     SPICELIB functions
///     C
///           INTEGER               WNCARD
///           INTEGER               CARDI
///     C
///     C     Local parameters
///     C
///     C
///     C     Declare the coverage window.  Make enough room
///     C     for MAXIV intervals.
///     C
///           INTEGER               FILSIZ
///           PARAMETER           ( FILSIZ = 255 )
///
///           INTEGER               LBCELL
///           PARAMETER           ( LBCELL = -5 )
///
///           INTEGER               MAXIV
///           PARAMETER           ( MAXIV  = 100000 )
///
///           INTEGER               WINSIZ
///           PARAMETER           ( WINSIZ = 2 * MAXIV )
///
///           INTEGER               TIMLEN
///           PARAMETER           ( TIMLEN = 50 )
///
///           INTEGER               MAXOBJ
///           PARAMETER           ( MAXOBJ = 1000 )
///
///     C
///     C     Local variables
///     C
///           CHARACTER*(FILSIZ)    CKFNM
///           CHARACTER*(FILSIZ)    LSK
///           CHARACTER*(FILSIZ)    SCLK
///           CHARACTER*(TIMLEN)    TIMSTR
///
///           DOUBLE PRECISION      B
///           DOUBLE PRECISION      COVER ( LBCELL : WINSIZ )
///           DOUBLE PRECISION      E
///
///           INTEGER               I
///           INTEGER               IDS   ( LBCELL : MAXOBJ )
///           INTEGER               J
///           INTEGER               NIV
///
///     C
///     C     Load a leapseconds kernel and SCLK kernel for output
///     C     time conversion.  Note that we assume a single spacecraft
///     C     clock is associated with all of the objects in the CK.
///     C
///           CALL PROMPT ( 'Name of leapseconds kernel > ', LSK  )
///           CALL FURNSH ( LSK )
///
///           CALL PROMPT ( 'Name of SCLK kernel        > ', SCLK )
///           CALL FURNSH ( SCLK )
///
///     C
///     C     Get name of CK file.
///     C
///           CALL PROMPT ( 'Name of CK file            > ', CKFNM )
///
///     C
///     C     Initialize the set IDS.
///     C
///           CALL SSIZEI ( MAXOBJ, IDS )
///
///     C
///     C     Initialize the window COVER.
///     C
///           CALL SSIZED ( WINSIZ, COVER )
///
///     C
///     C     Find the set of objects in the CK file.
///     C
///           CALL CKOBJ ( CKFNM, IDS )
///
///     C
///     C     We want to display the coverage for each object.  Loop
///     C     over the contents of the ID code set, find the coverage
///     C     for each item in the set, and display the coverage.
///     C
///           DO I = 1, CARDI( IDS )
///     C
///     C        Find the coverage window for the current
///     C        object. Empty the coverage window each time
///     C        so we don't include data for the previous object.
///     C
///              CALL SCARDD ( 0,   COVER )
///              CALL CKCOV  ( CKFNM,       IDS(I),  .FALSE.,
///          .                 'INTERVAL',  0.D0,    'TDB',    COVER )
///
///     C
///     C        Get the number of intervals in the coverage
///     C        window.
///     C
///              NIV = WNCARD( COVER )
///
///     C
///     C        Display a simple banner.
///     C
///              WRITE (*,*) '========================================'
///              WRITE (*,*) 'Coverage for object ', IDS(I)
///
///     C
///     C        Convert the coverage interval start and stop
///     C        times to TDB calendar strings.
///     C
///              DO J = 1, NIV
///     C
///     C           Get the endpoints of the Jth interval.
///     C
///                 CALL WNFETD ( COVER, J, B, E )
///     C
///     C           Convert the endpoints to TDB calendar
///     C           format time strings and display them.
///     C
///                 CALL TIMOUT ( B,
///          .                    'YYYY MON DD HR:MN:SC.###### ' //
///          .                    '(TDB) ::TDB',
///          .                    TIMSTR                           )
///                 WRITE (*,*) ' '
///                 WRITE (*,*) 'Interval: ', J
///                 WRITE (*,*) 'Start:    ', TIMSTR
///
///                 CALL TIMOUT ( E,
///          .                    'YYYY MON DD HR:MN:SC.###### ' //
///          .                    '(TDB) ::TDB',
///          .                    TIMSTR                          )
///                 WRITE (*,*) 'Stop:     ', TIMSTR
///                 WRITE (*,*) ' '
///
///              END DO
///
///              WRITE (*,*) '========================================'
///
///           END DO
///
///           END
///
///
///     When this program was executed on a Mac/Intel/gfortran/64-bit
///     platform, using the LSK file named naif0010.tls, the SCLK file
///     named cas00145.tsc and the CK file named 08052_08057ra.bc, the
///     output was:
///
///
///     Name of leapseconds kernel > naif0010.tls
///     Name of SCLK kernel        > cas00145.tsc
///     Name of CK file            > 08052_08057ra.bc
///      ========================================
///      Coverage for object       -82000
///
///      Interval:            1
///      Start:    2008 FEB 21 00:01:07.771186 (TDB)
///      Stop:     2008 FEB 23 22:53:30.001738 (TDB)
///
///
///      Interval:            2
///      Start:    2008 FEB 23 22:58:13.999732 (TDB)
///      Stop:     2008 FEB 24 02:22:25.913175 (TDB)
///
///
///      Interval:            3
///      Start:    2008 FEB 24 02:27:49.910886 (TDB)
///      Stop:     2008 FEB 24 19:46:33.470587 (TDB)
///
///
///      Interval:            4
///      Start:    2008 FEB 24 19:49:33.469315 (TDB)
///      Stop:     2008 FEB 25 04:25:21.250677 (TDB)
///
///
///      Interval:            5
///      Start:    2008 FEB 25 04:29:33.248897 (TDB)
///      Stop:     2008 FEB 25 15:23:44.971594 (TDB)
///
///
///      Interval:            6
///      Start:    2008 FEB 25 15:24:12.971396 (TDB)
///      Stop:     2008 FEB 25 20:25:04.843864 (TDB)
///
///
///      Interval:            7
///      Start:    2008 FEB 25 20:25:48.843553 (TDB)
///      Stop:     2008 FEB 26 00:01:04.752306 (TDB)
///
///      ========================================
///
///
///  2) Find the segment-level coverage for the object designated by
///     IDCODE provided by the set of CK files loaded via a
///     metakernel. (The metakernel must also specify leapseconds and
///     SCLK kernels.) Use tolerance of zero ticks. Do not request
///     angular velocity. Express the results in the TDB time system.
///
///     Use the meta-kernel shown below to load the required SPICE
///     kernels.
///
///
///        KPL/MK
///
///        File name: ckcov_ex2.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
///          ---------                      --------
///          naif0010.tls                   Leapseconds
///          cas00145.tsc                   Cassini SCLK
///          08052_08057ra.bc               Orientation for Cassini
///
///        \begindata
///
///          KERNELS_TO_LOAD = ( 'naif0010.tls'
///                              'cas00145.tsc'
///                              '08052_08057ra.bc')
///
///        \begintext
///
///        End of meta-kernel
///
///
///     Example code begins here.
///
///
///           PROGRAM CKCOV_EX2
///           IMPLICIT NONE
///     C
///     C     SPICELIB functions
///     C
///           INTEGER               WNCARD
///
///     C
///     C     Local parameters
///     C
///           INTEGER               LBCELL
///           PARAMETER           ( LBCELL = -5 )
///
///           INTEGER               FILSIZ
///           PARAMETER           ( FILSIZ = 255 )
///
///           INTEGER               LNSIZE
///           PARAMETER           ( LNSIZE = 80 )
///
///           INTEGER               MAXCOV
///           PARAMETER           ( MAXCOV = 100000 )
///
///           INTEGER               TIMLEN
///           PARAMETER           ( TIMLEN = 50 )
///
///     C
///     C     Local variables
///     C
///           CHARACTER*(FILSIZ)    FILE
///           CHARACTER*(LNSIZE)    IDCH
///           CHARACTER*(FILSIZ)    META
///           CHARACTER*(FILSIZ)    SOURCE
///           CHARACTER*(TIMLEN)    TIMSTR
///           CHARACTER*(LNSIZE)    TYPE
///
///           DOUBLE PRECISION      B
///           DOUBLE PRECISION      COVER  ( LBCELL : 2*MAXCOV )
///           DOUBLE PRECISION      E
///
///           INTEGER               COUNT
///           INTEGER               HANDLE
///           INTEGER               I
///           INTEGER               IDCODE
///           INTEGER               NIV
///
///           LOGICAL               FOUND
///
///     C
///     C     Prompt for the metakernel name; load the metakernel.
///     C     The metakernel lists the CK files whose coverage
///     C     for IDCODE we'd like to determine.  The metakernel
///     C     must also specify a leapseconds kernel and an SCLK
///     C     kernel for the clock associated with IDCODE.
///     C
///           CALL PROMPT ( 'Enter name of metakernel > ', META )
///
///           CALL FURNSH ( META )
///
///     C
///     C     Get the ID code of interest.
///     C
///           CALL PROMPT ( 'Enter ID code            > ', IDCH )
///
///           CALL PRSINT ( IDCH,  IDCODE )
///
///     C
///     C     Initialize the coverage window.
///     C
///           CALL SSIZED ( MAXCOV, COVER )
///
///     C
///     C     Find out how many kernels are loaded.  Loop over the
///     C     kernels:  for each loaded CK file, add its coverage
///     C     for IDCODE, if any, to the coverage window.
///     C
///           CALL KTOTAL ( 'CK', COUNT )
///
///           DO I = 1, COUNT
///
///              CALL KDATA ( I,       'CK',    FILE,  TYPE,
///          .                SOURCE,  HANDLE,  FOUND       )
///
///              CALL CKCOV ( FILE,      IDCODE,  .FALSE.,
///          .                'SEGMENT', 0.D0,    'TDB',    COVER )
///
///           END DO
///
///     C
///     C     Display results.
///     C
///     C     Get the number of intervals in the coverage
///     C     window.
///     C
///           NIV = WNCARD( COVER )
///
///     C
///     C     Display a simple banner.
///     C
///           WRITE (*,*) ' '
///           WRITE (*,*) 'Coverage for object ', IDCODE
///
///     C
///     C     Convert the coverage interval start and stop
///     C     times to TDB calendar strings.
///     C
///           DO I = 1, NIV
///     C
///     C        Get the endpoints of the Ith interval.
///     C
///              CALL WNFETD ( COVER, I, B, E )
///     C
///     C        Convert the endpoints to TDB calendar
///     C        format time strings and display them.
///     C
///              CALL TIMOUT ( B,
///          .                 'YYYY MON DD HR:MN:SC.###### ' //
///          .                 '(TDB) ::TDB',
///          .                 TIMSTR                           )
///              WRITE (*,*) ' '
///              WRITE (*,*) 'Interval: ', I
///              WRITE (*,*) 'Start:    ', TIMSTR
///
///              CALL TIMOUT ( E,
///          .                 'YYYY MON DD HR:MN:SC.###### ' //
///          .                 '(TDB) ::TDB',
///          .                 TIMSTR                           )
///              WRITE (*,*) 'Stop:     ', TIMSTR
///              WRITE (*,*) ' '
///
///           END DO
///
///           END
///
///
///     When this program was executed on a Mac/Intel/gfortran/64-bit
///     platform, using the meta-kernel file named ckcov_ex2.tm and
///     the NAIF ID "-82000" (Cassini spacecraft bus), the output was:
///
///
///     Enter name of metakernel > ckcov_ex2.tm
///     Enter ID code            > -82000
///
///      Coverage for object       -82000
///
///      Interval:            1
///      Start:    2008 FEB 21 00:01:07.771186 (TDB)
///      Stop:     2008 FEB 26 00:01:04.752306 (TDB)
/// ```
///
/// # Restrictions
///
/// ```text
///  1)  When this routine is used to accumulate coverage for IDCODE
///      provided by multiple CK files, the inputs NEEDAV, LEVEL, TOL,
///      and TIMSYS  must have the same values for all files in order
///      for the result to be meaningful.
/// ```
///
/// # Author and Institution
///
/// ```text
///  N.J. Bachman       (JPL)
///  J. Diaz del Rio    (ODC Space)
///  B.V. Semenov       (JPL)
/// ```
///
/// # Version
///
/// ```text
/// -    SPICELIB Version 2.1.0, 08-OCT-2021 (JDR)
///
///         Bug fix: added call to FAILED after call to GETFAT.
///
///         Changed input argument name "CK" to "CKFNM" for consistency
///         with other routines.
///
///         Edited the header to comply with NAIF standard. Added solutions
///         using CASSINI data. Fixed a bug on Example #2. Added entry #11
///         in $Exceptions section and corrected short error messages in
///         entry #2 and #3.
///
/// -    SPICELIB Version 2.0.0, 05-JAN-2014 (NJB) (BVS)
///
///         Updated index entries.
///
///         Last update was 05-JAN-2014 (NJB) (BVS)
///
///            Updated to support type 6.
///
/// -    SPICELIB Version 1.0.1, 30-NOV-2007 (NJB)
///
///         Corrected bug in first program in header $Examples section:
///         program now empties the coverage window prior to collecting
///         data for the current object. Updated examples to use WNCARD
///         rather than CARDD.
///
/// -    SPICELIB Version 1.0.0, 07-JAN-2005 (NJB)
/// ```
pub fn ckcov(
    ctx: &mut SpiceContext,
    ckfnm: &str,
    idcode: i32,
    needav: bool,
    level: &str,
    tol: f64,
    timsys: &str,
    cover: &mut [f64],
) -> crate::Result<()> {
    CKCOV(
        ckfnm.as_bytes(),
        idcode,
        needav,
        level.as_bytes(),
        tol,
        timsys.as_bytes(),
        cover,
        ctx.raw_context(),
    )?;
    ctx.handle_errors()?;
    Ok(())
}

//$Procedure CKCOV ( CK coverage )
pub fn CKCOV(
    CKFNM: &[u8],
    IDCODE: i32,
    NEEDAV: bool,
    LEVEL: &[u8],
    TOL: f64,
    TIMSYS: &[u8],
    COVER: &mut [f64],
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let mut COVER = DummyArrayMut::new(COVER, LBCELL..);
    let mut ARCH = [b' '; LNSIZE as usize];
    let mut KERTYP = [b' '; LNSIZE as usize];
    let mut DC = StackArray::<f64, 2>::new(1..=ND);
    let mut DCTOL = StackArray::<f64, 2>::new(1..=ND);
    let mut DESCR = StackArray::<f64, 5>::new(1..=(ND + ((NI + 1) / 2)));
    let mut ET: f64 = 0.0;
    let mut CLKID: i32 = 0;
    let mut DTYPE: i32 = 0;
    let mut HANDLE: i32 = 0;
    let mut IC = StackArray::<i32, 6>::new(1..=NI);
    let mut SEGBEG: i32 = 0;
    let mut SEGEND: i32 = 0;
    let mut AVOK: bool = false;
    let mut FOUND: bool = false;
    let mut ISTDB: bool = false;
    let mut SEGLVL: bool = false;

    //
    // SPICELIB functions
    //

    //
    // Local parameters
    //

    //
    // Local variables
    //

    //
    // Standard SPICE error handling.
    //
    if RETURN(ctx) {
        return Ok(());
    }

    CHKIN(b"CKCOV", ctx)?;

    //
    // Check tolerance value.
    //
    if (TOL < 0.0) {
        SETMSG(b"Tolerance must be non-negative; actual value was #.", ctx);
        ERRDP(b"#", TOL, ctx);
        SIGERR(b"SPICE(VALUEOUTOFRANGE)", ctx)?;
        CHKOUT(b"CKCOV", ctx)?;
        return Ok(());
    }

    //
    // Use a logical flag to indicate whether this is a segment-level
    // coverage description.
    //
    SEGLVL = EQSTR(LEVEL, KWSEG);

    //
    // Check coverage level keyword.
    //
    if !(SEGLVL || EQSTR(LEVEL, KWINTV)) {
        SETMSG(
            b"Allowed values of LEVEL are # and #; actual value was #.",
            ctx,
        );
        ERRCH(b"#", KWSEG, ctx);
        ERRCH(b"#", KWINTV, ctx);
        ERRCH(b"#", LEVEL, ctx);
        SIGERR(b"SPICE(INVALIDOPTION)", ctx)?;
        CHKOUT(b"CKCOV", ctx)?;
        return Ok(());
    }

    //
    // See whether GETFAT thinks we've got a CK file.
    //
    GETFAT(CKFNM, &mut ARCH, &mut KERTYP, ctx)?;

    if FAILED(ctx) {
        CHKOUT(b"CKCOV", ctx)?;
        return Ok(());
    }

    if fstr::eq(&ARCH, b"XFR") {
        SETMSG(b"Input file # has architecture #. The file must be a binary CK file to be readable by this routine.  If the input file is an CK file in transfer format, run TOBIN on the file to convert it to binary format.", ctx);
        ERRCH(b"#", CKFNM, ctx);
        ERRCH(b"#", &ARCH, ctx);
        SIGERR(b"SPICE(INVALIDFORMAT)", ctx)?;
        CHKOUT(b"CKCOV", ctx)?;
        return Ok(());
    } else if fstr::ne(&ARCH, b"DAF") {
        SETMSG(b"Input file # has architecture #. The file must be a binary CK file to be readable by this routine.  Binary CK files have DAF architecture.  If you expected the file to be a binary CK file, the problem may be due to the file being an old non-native file lacking binary file format information. It\'s also possible the file has been corrupted.", ctx);
        ERRCH(b"#", CKFNM, ctx);
        ERRCH(b"#", &ARCH, ctx);
        SIGERR(b"SPICE(INVALIDARCHTYPE)", ctx)?;
        CHKOUT(b"CKCOV", ctx)?;
        return Ok(());
    } else if fstr::ne(&KERTYP, b"CK") {
        SETMSG(b"Input file # has file type #. The file must be a binary CK file to be readable by this routine. If you expected the file to be a binary CK file, the problem may be due to the file being an old non-native file lacking binary file format information. It\'s also possible the file has been corrupted.", ctx);
        ERRCH(b"#", CKFNM, ctx);
        ERRCH(b"#", &KERTYP, ctx);
        SIGERR(b"SPICE(INVALIDFILETYPE)", ctx)?;
        CHKOUT(b"CKCOV", ctx)?;
        return Ok(());
    }

    //
    // Set a logical flag indicating whether the time system is SCLK.
    //
    ISTDB = EQSTR(TIMSYS, b"TDB");

    //
    // Check time system.
    //
    if !ISTDB {
        if !EQSTR(TIMSYS, b"SCLK") {
            SETMSG(
                b"Time system spec TIMSYS was #; allowed values are SCLK and TDB.",
                ctx,
            );
            ERRCH(b"#", TIMSYS, ctx);
            SIGERR(b"SPICE(NOTSUPPORTED)", ctx)?;
            CHKOUT(b"CKCOV", ctx)?;
            return Ok(());
        }
    }

    //
    // If the output time system is TDB, find the clock ID associated
    // with IDCODE.
    //
    if ISTDB {
        CKMETA(IDCODE, b"SCLK", &mut CLKID, ctx)?;

        if FAILED(ctx) {
            CHKOUT(b"CKCOV", ctx)?;
            return Ok(());
        }
    }

    //
    // Open the file for reading.
    //
    DAFOPR(CKFNM, &mut HANDLE, ctx)?;

    if FAILED(ctx) {
        CHKOUT(b"CKCOV", ctx)?;
        return Ok(());
    }

    //
    // We will examine each segment descriptor in the file, and
    // we'll update our coverage bounds according to the data found
    // in these descriptors.
    //
    // If TOL > 0, we'll apply TOL after we've found the coverage
    // for the zero-tolerance case.
    //
    // If the time system is TDB, we'll convert the times to TDB
    // at the end of this routine.

    //
    // Start a forward search.
    //
    DAFBFS(HANDLE, ctx)?;

    //
    // Find the next DAF array.
    //
    DAFFNA(&mut FOUND, ctx)?;

    while FOUND {
        //
        // Note:  we check FAILED() at the bottom of this loop; this
        // routine returns if FAILED() returns .TRUE. at that point.
        //
        // Fetch and unpack the segment descriptor.
        //
        DAFGS(DESCR.as_slice_mut(), ctx)?;
        DAFUS(
            DESCR.as_slice(),
            ND,
            NI,
            DC.as_slice_mut(),
            IC.as_slice_mut(),
        );

        //
        // Let AVOK indicate whether the segment satisfies the
        // angular velocity restriction.
        //
        AVOK = ((IC[4] == 1) || !NEEDAV);

        if ((IC[1] == IDCODE) && AVOK) {
            //
            // This segment is for the body of interest.  If angular
            // velocity is needed, this segment has it.
            //
            if SEGLVL {
                //
                // This is a segment-level summary.
                //
                // Insert the coverage bounds into the coverage window.
                // Adjust the interval using the tolerance.
                //
                DCTOL[1] = intrinsics::DMAX1(&[(DC[1] - TOL), 0.0]);
                DCTOL[2] = (DC[2] + TOL);

                //
                // Convert the time to TDB if necessary.
                //
                if ISTDB {
                    //
                    // Convert the time bounds to TDB before inserting
                    // into the window.
                    //
                    for I in 1..=2 {
                        SCT2E(CLKID, DCTOL[I], &mut ET, ctx)?;
                        DCTOL[I] = ET;
                    }
                }

                if (DCTOL[1] <= DCTOL[2]) {
                    WNINSD(DCTOL[1], DCTOL[2], COVER.as_slice_mut(), ctx)?;
                }
            } else {
                //
                // We're looking for an interval-level coverage window.
                // This information must be retrieved in a
                // data-type-dependent fashion.  The coverage routines
                // we'll call will, if necessary, adjust intervals by TOL
                // and convert interval times to TDB.
                //
                DTYPE = IC[3];
                SEGBEG = IC[5];
                SEGEND = IC[6];

                if (DTYPE == 1) {
                    ZZCKCV01(
                        HANDLE,
                        SEGBEG,
                        SEGEND,
                        CLKID,
                        TOL,
                        TIMSYS,
                        COVER.as_slice_mut(),
                        ctx,
                    )?;
                } else if (DTYPE == 2) {
                    ZZCKCV02(
                        HANDLE,
                        SEGBEG,
                        SEGEND,
                        CLKID,
                        TOL,
                        TIMSYS,
                        COVER.as_slice_mut(),
                        ctx,
                    )?;
                } else if (DTYPE == 3) {
                    ZZCKCV03(
                        HANDLE,
                        SEGBEG,
                        SEGEND,
                        CLKID,
                        TOL,
                        TIMSYS,
                        COVER.as_slice_mut(),
                        ctx,
                    )?;
                } else if (DTYPE == 4) {
                    ZZCKCV04(
                        HANDLE,
                        SEGBEG,
                        SEGEND,
                        CLKID,
                        TOL,
                        TIMSYS,
                        COVER.as_slice_mut(),
                        ctx,
                    )?;
                } else if (DTYPE == 5) {
                    ZZCKCV05(
                        HANDLE,
                        SEGBEG,
                        SEGEND,
                        CLKID,
                        DC.as_slice(),
                        TOL,
                        TIMSYS,
                        COVER.as_slice_mut(),
                        ctx,
                    )?;
                } else if (DTYPE == 6) {
                    ZZCKCV06(
                        HANDLE,
                        SEGBEG,
                        SEGEND,
                        CLKID,
                        DC.as_slice(),
                        TOL,
                        TIMSYS,
                        COVER.as_slice_mut(),
                        ctx,
                    )?;
                } else {
                    SETMSG(b"Supported CK data types are 1, 2, 3, 4, 5.  Data type of segment: #. This problem may indicate that you need to update your SPICE Toolkit.", ctx);
                    ERRINT(b"#", DTYPE, ctx);
                    SIGERR(b"SPICE(NOTSUPPORTED)", ctx)?;
                    CHKOUT(b"CKCOV", ctx)?;
                    return Ok(());
                }
            }
        }

        DAFFNA(&mut FOUND, ctx)?;

        if FAILED(ctx) {
            CHKOUT(b"CKCOV", ctx)?;
            return Ok(());
        }
    }

    //
    // COVER now represents the coverage of the entire file at the
    // granularity indicated by LEVEL, combined with the coverage
    // contained in COVER on input.
    //
    // Release the file.
    //
    DAFCLS(HANDLE, ctx)?;

    CHKOUT(b"CKCOV", ctx)?;
    Ok(())
}