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
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
//
// GENERATED FILE
//

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

const RECSYS: &[u8] = b"RECTANGULAR";
const LATSYS: &[u8] = b"LATITUDINAL";
const SPHSYS: &[u8] = b"SPHERICAL";
const RADSYS: &[u8] = b"RA/DEC";
const CYLSYS: &[u8] = b"CYLINDRICAL";
const GEOSYS: &[u8] = b"GEODETIC";
const PGRSYS: &[u8] = b"PLANETOGRAPHIC";
const XCRD: &[u8] = b"X";
const YCRD: &[u8] = b"Y";
const ZCRD: &[u8] = b"Z";
const RADCRD: &[u8] = b"RADIUS";
const LONCRD: &[u8] = b"LONGITUDE";
const LATCRD: &[u8] = b"LATITUDE";
const RACRD: &[u8] = b"RIGHT ASCENSION";
const DECCRD: &[u8] = b"DECLINATION";
const RNGCRD: &[u8] = b"RANGE";
const CLTCRD: &[u8] = b"COLATITUDE";
const ALTCRD: &[u8] = b"ALTITUDE";
const POSDEF: &[u8] = b"POSITION";
const SOBDEF: &[u8] = b"SUB-OBSERVER POINT";
const SINDEF: &[u8] = b"SURFACE INTERCEPT POINT";
const NWREL: i32 = 5;
const NWLONG: i32 = 7;
const EXWIDX: i32 = ((NWREL + NWLONG) + 1);
const MXBEGM: i32 = 55;
const MXENDM: i32 = 13;
const MXMSG: i32 = ((MXBEGM + MXENDM) + 10);
const LBCELL: i32 = -5;
const FPRINT: i32 = 32;
const LPRINT: i32 = 126;

struct SaveVars {
    COPYB: Vec<u8>,
    COPYE: Vec<u8>,
    REMAIN: f64,
    T0: f64,
}

impl SaveInit for SaveVars {
    fn new() -> Self {
        let mut COPYB = vec![b' '; MXBEGM as usize];
        let mut COPYE = vec![b' '; MXENDM as usize];
        let mut REMAIN: f64 = 0.0;
        let mut T0: f64 = 0.0;

        Self {
            COPYB,
            COPYE,
            REMAIN,
            T0,
        }
    }
}

/// GF, progress reporting package
///
/// The entry points contained under this routine provide users
/// information regarding the status of a GF search in progress.
///
/// # Required Reading
///
/// * [GF](crate::required_reading::gf)
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  LBCELL     P   The SPICE cell lower bound.
///  MXBEGM     P   Maximum progress report message prefix length.
///  MXENDM     P   Maximum progress report message suffix length.
///  WINDOW     I   A window over which a job is to be performed.
///  BEGMSS     I   Beginning of the text portion of the output message
///  ENDMSS     I   End of the text portion of the output message
///  IVBEG      I   Current confinement window interval start time.
///  IVEND      I   Current confinement window interval stop time.
///  TIME       I   Input to the reporting routine.
/// ```
///
/// # Detailed Input
///
/// ```text
///  See the individual entry points.
/// ```
///
/// # Detailed Output
///
/// ```text
///  See the individual entry points.
/// ```
///
/// # Parameters
///
/// ```text
///  LBCELL   is the SPICE cell lower bound.
///
///  MXBEGM,
///  MXENDM   are, respectively, the maximum lengths of the progress
///           report message prefix and suffix.
/// ```
///
/// # Exceptions
///
/// ```text
///  1)  See the individual entry points.
/// ```
///
/// # Particulars
///
/// ```text
///  This umbrella routine contains default progress reporting entry
///  points that display a report via console I/O. These routines may
///  be used by SPICE-based applications as inputs to mid-level GF
///  search routines. These routines may be useful even when progress
///  reporting is not desired, since the mid-level search routines
///  provide some capabilities that aren't supported by the top-level
///  GF routines.
///
///  Developers wishing to use their own GF progress reporting
///  routines must design them with the same interfaces and should
///  assign them the same progress reporting roles as the entry points
///  of these routines.
///
///  The entry points contained in this routine are written to
///  make reporting of work (such as searching for a geometric event)
///  over a particular window easy. This is an important feature for
///  interactive programs that may "go away" from the user's control
///  for a considerable length of time. It allows the user to see that
///  something is still going on (although maybe not too quickly).
///
///  The three entry points contained under this module are:
///
///     GFREPI  used to set up the reporting mechanism. It lets GFRPRT
///             know that some task is about to begin that involves
///             interaction with some window of times. It is used
///             only to set up and store the constants associated with
///             the reporting of the job in progress.
///
///     GFREPU  is used to notify the reporter that work has
///             progressed to a given point with respect to the start
///             of the confinement window.
///
///     GFREPF  is used to "finish" the reporting of work (set the
///             completion value to 100%.
///
///  The progress reporting utilities are called by GF search routines
///  as follows:
///
///     1) Given a window over which some work is to be performed,
///        CALL GFREPI with the appropriate inputs, to let the routine
///        know the intervals over which some work is to be done.
///
///     2) Each time some "good" amount of work has been done, call
///        GFREPU so that the total amount of work done can be updated
///        and can be reported.
///
///     3) When work is complete call GFREPF to "clean up" the end of
///        the progress report.
/// ```
///
/// # 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) This example shows how to call a mid-level GF search API that
///     requires as input progress reporting routines.
///
///     If custom progress reporting routines are available, they
///     can replace GFREPI, GFREPU, and GFREPF in any GF API calls.
///
///     The code example below is the first example in the header of
///     GFOCCE.
///
///
///     Conduct a search using the default GF progress reporting
///     capability.
///
///     The program will use console I/O to display a simple
///     ASCII-based progress report.
///
///     The program will find occultations of the Sun by the Moon as
///     seen from the center of the Earth over the month December,
///     2001.
///
///     We use light time corrections to model apparent positions of
///     Sun and Moon. Stellar aberration corrections are not specified
///     because they don't affect occultation computations.
///
///     Use the meta-kernel shown below to load the required SPICE
///     kernels.
///
///
///        KPL/MK
///
///        File name: gfrprt_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
///           ---------                     --------
///           de421.bsp                     Planetary ephemeris
///           pck00008.tpc                  Planet orientation and
///                                         radii
///           naif0009.tls                  Leapseconds
///
///
///        \begindata
///
///           KERNELS_TO_LOAD = ( 'de421.bsp',
///                               'pck00008.tpc',
///                               'naif0009.tls'  )
///
///        \begintext
///
///        End of meta-kernel
///
///
///     Example code begins here.
///
///
///           PROGRAM GFRPRT_EX1
///           IMPLICIT NONE
///
///     C
///     C     SPICELIB functions
///     C
///           INTEGER               WNCARD
///
///     C
///     C     SPICELIB default functions for
///     C
///     C        - Interrupt handling (no-op function):   GFBAIL
///     C        - Search refinement:                     GFREFN
///     C        - Progress report termination:           GFREPF
///     C        - Progress report initialization:        GFREPI
///     C        - Progress report update:                GFREPU
///     C        - Search step size "get" function:       GFSTEP
///     C
///           LOGICAL               GFBAIL
///           EXTERNAL              GFBAIL
///
///           EXTERNAL              GFREFN
///           EXTERNAL              GFREPI
///           EXTERNAL              GFREPU
///           EXTERNAL              GFREPF
///           EXTERNAL              GFSTEP
///
///     C
///     C     Local parameters
///     C
///           CHARACTER*(*)         TIMFMT
///           PARAMETER           ( TIMFMT =
///          .   'YYYY MON DD HR:MN:SC.###### ::TDB (TDB)' )
///
///           DOUBLE PRECISION      CNVTOL
///           PARAMETER           ( CNVTOL = 1.D-6 )
///
///           INTEGER               MAXWIN
///           PARAMETER           ( MAXWIN = 2 * 100 )
///
///           INTEGER               TIMLEN
///           PARAMETER           ( TIMLEN = 40 )
///
///           INTEGER               LBCELL
///           PARAMETER           ( LBCELL = -5 )
///
///     C
///     C     Local variables
///     C
///           CHARACTER*(TIMLEN)    WIN0
///           CHARACTER*(TIMLEN)    WIN1
///           CHARACTER*(TIMLEN)    BEGSTR
///           CHARACTER*(TIMLEN)    ENDSTR
///
///           DOUBLE PRECISION      CNFINE ( LBCELL : 2 )
///           DOUBLE PRECISION      ET0
///           DOUBLE PRECISION      ET1
///           DOUBLE PRECISION      LEFT
///           DOUBLE PRECISION      RESULT ( LBCELL : MAXWIN )
///           DOUBLE PRECISION      RIGHT
///
///           INTEGER               I
///
///           LOGICAL               BAIL
///           LOGICAL               RPT
///
///     C
///     C     Saved variables
///     C
///     C     The confinement and result windows CNFINE and RESULT are
///     C     saved because this practice helps to prevent stack
///     C     overflow.
///     C
///           SAVE                  CNFINE
///           SAVE                  RESULT
///
///     C
///     C     Load kernels.
///     C
///           CALL FURNSH ( 'gfrprt_ex1.tm' )
///
///     C
///     C     Initialize the confinement and result windows.
///     C
///           CALL SSIZED ( 2,      CNFINE )
///           CALL SSIZED ( MAXWIN, RESULT )
///
///     C
///     C     Obtain the TDB time bounds of the confinement
///     C     window, which is a single interval in this case.
///     C
///           WIN0 = '2001 DEC 01 00:00:00 TDB'
///           WIN1 = '2002 JAN 01 00:00:00 TDB'
///
///           CALL STR2ET ( WIN0, ET0 )
///           CALL STR2ET ( WIN1, ET1 )
///
///     C
///     C     Insert the time bounds into the confinement
///     C     window.
///     C
///           CALL WNINSD ( ET0, ET1, CNFINE )
///
///     C
///     C     Select a 20 second step. We'll ignore any occultations
///     C     lasting less than 20 seconds.
///     C
///           CALL GFSSTP ( 20.D0 )
///
///     C
///     C     Turn on progress reporting; turn off interrupt
///     C     handling.
///     C
///           RPT  = .TRUE.
///           BAIL = .FALSE.
///
///     C
///     C     Perform the search.
///     C
///           CALL GFOCCE ( 'ANY',
///          .              'MOON',   'ellipsoid',  'IAU_MOON',
///          .              'SUN',    'ellipsoid',  'IAU_SUN',
///          .              'LT',     'EARTH',      CNVTOL,
///          .              GFSTEP,   GFREFN,       RPT,
///          .              GFREPI,   GFREPU,       GFREPF,
///          .              BAIL,     GFBAIL,       CNFINE,  RESULT )
///
///
///           IF ( WNCARD(RESULT) .EQ. 0 ) THEN
///
///              WRITE (*,*) 'No occultation was found.'
///
///           ELSE
///
///              DO I = 1, WNCARD(RESULT)
///
///     C
///     C           Fetch and display each occultation interval.
///     C
///                 CALL WNFETD ( RESULT, I, LEFT, RIGHT )
///
///                 CALL TIMOUT ( LEFT,  TIMFMT, BEGSTR )
///                 CALL TIMOUT ( RIGHT, TIMFMT, ENDSTR )
///
///                 WRITE (*,*) 'Interval ', I
///                 WRITE (*,*) '   Start time: '//BEGSTR
///                 WRITE (*,*) '   Stop time:  '//ENDSTR
///
///              END DO
///
///           END IF
///
///           END
///
///
///     When this program was executed on a Mac/Intel/gfortran/64-bit
///     platform, the output was:
///
///
///     Occultation/transit search 100.00% done.
///
///      Interval            1
///         Start time: 2001 DEC 14 20:10:14.195952  (TDB)
///         Stop time:  2001 DEC 14 21:35:50.317994  (TDB)
///
///
///     Note that the progress report has the format shown below:
///
///        Occultation/transit search   6.02% done.
///
///     The completion percentage was updated approximately once per
///     second.
///
///
///  2) The following piece of code provides a more concrete example
///     of how these routines might be used. It is part of code that
///     performs a search for the time of an occultation of one body
///     by another. It is intended only for illustration and is not
///     recommended for use in code that has to do real work.
///
///     C
///     C     Prepare the progress reporter if appropriate.
///     C
///           IF ( RPT ) THEN
///              CALL UDREPI ( CNFINE, 'Occultation/transit search ',
///          .                         'done.'                      )
///           END IF
///
///     C
///     C     Cycle over the intervals in the confining window.
///     C
///           COUNT = WNCARD(CNFINE)
///
///           DO I = 1, COUNT
///     C
///     C        Retrieve the bounds for the Ith interval of the
///     C        confinement window. Search this interval for
///     C        occultation events. Union the result with the
///     C        contents of the RESULT window.
///     C
///              CALL WNFETD ( CNFINE, I, START, FINISH  )
///
///              CALL ZZGFSOLV ( ZZGFOCST, UDSTEP, UDREFN, BAIL,
///          .                   UDBAIL,   CSTEP,  STEP,   START,
///          .                   FINISH,   TOL,    RPT,    UDREPU,
///          .                   RESULT                          )
///
///
///              IF (  FAILED()  ) THEN
///                 CALL CHKOUT ( 'GFOCCE'  )
///                 RETURN
///              END IF
///
///              IF ( BAIL ) THEN
///     C
///     C           Interrupt handling is enabled.
///     C
///                 IF ( UDBAIL () ) THEN
///     C
///     C              An interrupt has been issued. Return now
///     C              regardless of whether the search has been
///     C              completed.
///     C
///                    CALL CHKOUT ( 'GFOCCE' )
///                    RETURN
///
///                 END IF
///
///              END IF
///
///           END DO
///
///     C
///     C     End the progress report.
///     C
///           IF ( RPT ) THEN
///              CALL UDREPF
///           END IF
///
///
///  3) For more concrete examples of how these routines are used in
///     SPICELIB, please refer to the actual code of any of the GF API
///     calls.
/// ```
///
/// # Author and Institution
///
/// ```text
///  N.J. Bachman       (JPL)
///  J. Diaz del Rio    (ODC Space)
///  L.S. Elson         (JPL)
///  B.V. Semenov       (JPL)
///  W.L. Taber         (JPL)
///  I.M. Underwood     (JPL)
/// ```
///
/// # Version
///
/// ```text
/// -    SPICELIB Version 1.0.2, 27-AUG-2021 (JDR)
///
///         Edited the header of all entry points and GFRPRT to comply with
///         NAIF standard.
///
///         Added complete example code to GFRPRT.
///
/// -    SPICELIB Version 1.0.1, 10-FEB-2014 (BVS)
///
///         Added declarations of IVBEG and IVEND to the $Declarations
///         section of the GFREPU header.
///
///         Corrected declaration of WINDOW in the $Declarations
///         section and added descriptions of LBCELL to the GFREPI
///         header.
///
/// -    SPICELIB Version 1.0.0, 06-MAR-2009 (NJB) (LSE) (WLT) (IMU)
/// ```
pub fn gfrprt(
    ctx: &mut SpiceContext,
    window: &[f64],
    begmss: &str,
    endmss: &str,
    ivbeg: f64,
    ivend: f64,
    time: f64,
) -> crate::Result<()> {
    GFRPRT(
        window,
        begmss.as_bytes(),
        endmss.as_bytes(),
        ivbeg,
        ivend,
        time,
        ctx.raw_context(),
    )?;
    ctx.handle_errors()?;
    Ok(())
}

//$Procedure GFRPRT ( GF, progress reporting package )
pub fn GFRPRT(
    WINDOW: &[f64],
    BEGMSS: &[u8],
    ENDMSS: &[u8],
    IVBEG: f64,
    IVEND: f64,
    TIME: f64,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    //
    // SPICELIB functions
    //

    //
    // Local parameters
    //

    //
    // Local variables
    //

    CHKIN(b"GFRPRT", ctx)?;
    SIGERR(b"SPICE(BOGUSENTRY)", ctx)?;
    CHKOUT(b"GFRPRT", ctx)?;
    Ok(())
}

/// GF, progress report initialization
///
/// Initialize a search progress report.
///
/// # Required Reading
///
/// * [GF](crate::required_reading::gf)
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  LBCELL     P   The SPICE cell lower bound.
///  MXBEGM     P   Maximum progress report message prefix length.
///  MXENDM     P   Maximum progress report message suffix length.
///  WINDOW     I   A window over which a job is to be performed.
///  BEGMSS     I   Beginning of the text portion of output message.
///  ENDMSS     I   End of the text portion of output message.
/// ```
///
/// # Detailed Input
///
/// ```text
///  WINDOW   is the name of a constraint window. This is the window
///           associated with some root finding activity. It is
///           used to determine how much total time is being searched
///           in order to find the events of interest.
///
///  BEGMSS   is the beginning of the progress report message written
///           to standard output by the GF subsystem. This output
///           message has the form
///
///              BEGMSS(1:LASTNB(BEGMSS)) // ' xxx.xx% ' // ENDMSS
///
///           The total length of BEGMSS must be less than MXBEGM
///           characters. All characters of BEGMSS must be printable.
///
///           For example, the progress report message created by the
///           SPICELIB routine GFOCCE at the completion of a search is
///
///              Occultation/transit search 100.00% done.
///
///           In this message, BEGMSS is
///
///              'Occultation/transit search'
///
///  ENDMSS   is the last portion of the output message written to
///           standard output by the GF subsystem.
///
///           The total length of ENDMSS must be less than MXENDM
///           characters. All characters of ENDMSS must be printable.
///
///           In the progress report message created by GFOCCE at the
///           completion of a search, ENDMSS is
///
///              'done.'
/// ```
///
/// # Parameters
///
/// ```text
///  LBCELL   is the SPICE cell lower bound.
///
///  MXBEGM,
///  MXENDM   are, respectively, the maximum lengths of the progress
///           report message prefix and suffix. See the INCLUDE file
///           zzgf.inc for details.
/// ```
///
/// # Exceptions
///
/// ```text
///  1)  If BEGMSS has length greater than MXBEGM characters, or if
///      ENDMSS has length greater than MXENDM characters, the error
///      SPICE(MESSAGETOOLONG) is signaled.
///
///  2)  If either BEGMSS or ENDMSS contains non-printing characters,
///      the error SPICE(NOTPRINTABLECHARS) is signaled.
/// ```
///
/// # Particulars
///
/// ```text
///  This entry point initializes the GF progress reporting system. It
///  is called by the GF root finding utilities once at the start of
///  each search pass. See the $Particulars section of the main
///  subroutine header for further details of its function.
/// ```
///
/// # Examples
///
/// ```text
///  See $Examples in GFRPRT.
/// ```
///
/// # Author and Institution
///
/// ```text
///  N.J. Bachman       (JPL)
///  J. Diaz del Rio    (ODC Space)
///  L.S. Elson         (JPL)
///  B.V. Semenov       (JPL)
///  W.L. Taber         (JPL)
///  I.M. Underwood     (JPL)
/// ```
///
/// # Version
///
/// ```text
/// -    SPICELIB Version 1.0.2, 27-AUG-2021 (JDR)
///
///         Edited the header to comply with NAIF standard.
///
///         Extended description of BEGMSS and ENDMSS arguments.
///
/// -    SPICELIB Version 1.0.1, 10-FEB-2014 (BVS)
///
///         Corrected declaration of WINDOW in the $Declarations
///         section. Added description of LBCELL to the $Declarations,
///         $Brief_I/O, and $Parameters sections.
///
/// -    SPICELIB Version 1.0.0, 21-FEB-2009 (NJB) (LSE) (WLT) (IMU)
/// ```
pub fn gfrepi(
    ctx: &mut SpiceContext,
    window: &[f64],
    begmss: &str,
    endmss: &str,
) -> crate::Result<()> {
    GFREPI(
        window,
        begmss.as_bytes(),
        endmss.as_bytes(),
        ctx.raw_context(),
    )?;
    ctx.handle_errors()?;
    Ok(())
}

//$Procedure GFREPI ( GF, progress report initialization )
pub fn GFREPI(
    WINDOW: &[f64],
    BEGMSS: &[u8],
    ENDMSS: &[u8],
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let WINDOW = DummyArray::new(WINDOW, LBCELL..);
    let mut AVE: f64 = 0.0;
    let mut MEASUR: f64 = 0.0;
    let mut STDDEV: f64 = 0.0;
    let mut CHRCOD: i32 = 0;
    let mut LONG: i32 = 0;
    let mut SHORT: i32 = 0;

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

    CHKIN(b"GFREPI", ctx)?;

    //
    // Check to see if either the message prefix or suffix
    // is too long.
    //
    if (LASTNB(BEGMSS) > MXBEGM) {
        SETMSG(
            b"Progress report prefix message contains # characters; limit is #.",
            ctx,
        );
        ERRINT(b"#", LASTNB(BEGMSS), ctx);
        ERRINT(b"#", MXBEGM, ctx);
        SIGERR(b"SPICE(MESSAGETOOLONG)", ctx)?;
        CHKOUT(b"GFREPI", ctx)?;
        return Ok(());
    }

    if (LASTNB(ENDMSS) > MXENDM) {
        SETMSG(
            b"Progress report suffix message contains # characters; limit is #.",
            ctx,
        );
        ERRINT(b"#", LASTNB(ENDMSS), ctx);
        ERRINT(b"#", MXENDM, ctx);
        SIGERR(b"SPICE(MESSAGETOOLONG)", ctx)?;
        CHKOUT(b"GFREPI", ctx)?;
        return Ok(());
    }

    //
    // Now check that all the characters in the message prefix and
    // suffix can be printed.
    //
    for I in 1..=LASTNB(BEGMSS) {
        CHRCOD = intrinsics::ICHAR(fstr::substr(BEGMSS, I..=I));

        if ((CHRCOD < FPRINT) || (CHRCOD > LPRINT)) {
            SETMSG(b"The progress report message prefix contains a nonprintable character; ASCII code is #.", ctx);
            ERRINT(b"#", CHRCOD, ctx);
            SIGERR(b"SPICE(NONPRINTABLECHARS)", ctx)?;
            CHKOUT(b"GFREPI", ctx)?;
            return Ok(());
        }
    }

    for I in 1..=LASTNB(ENDMSS) {
        CHRCOD = intrinsics::ICHAR(fstr::substr(ENDMSS, I..=I));

        if ((CHRCOD < FPRINT) || (CHRCOD > LPRINT)) {
            SETMSG(b"The progress report message suffix contains a nonprintable character; ASCII code is #.", ctx);
            ERRINT(b"#", CHRCOD, ctx);
            SIGERR(b"SPICE(NONPRINTABLECHARS)", ctx)?;
            CHKOUT(b"GFREPI", ctx)?;
            return Ok(());
        }
    }

    fstr::assign(&mut save.COPYB, BEGMSS);
    fstr::assign(&mut save.COPYE, ENDMSS);

    //
    // Find the length of the window. Use that to initialize the work
    // reporter.
    //
    WNSUMD(
        WINDOW.as_slice(),
        &mut MEASUR,
        &mut AVE,
        &mut STDDEV,
        &mut SHORT,
        &mut LONG,
        ctx,
    )?;
    ZZGFTSWK(MEASUR, 1.0, 4, BEGMSS, ENDMSS, ctx)?;

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

    //
    // Initialize the time to the start of the confinement window.
    // The remaining amount of work in the current interval is
    // the measure of the interval.
    //
    if (CARDD(WINDOW.as_slice(), ctx)? >= 2) {
        save.T0 = WINDOW[1];
        save.REMAIN = (WINDOW[2] - save.T0);
    } else {
        save.REMAIN = 0.0;
    }

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

/// GF, progress report update
///
/// Tell the progress reporting system how far a search has
/// progressed.
///
/// # Required Reading
///
/// * [GF](crate::required_reading::gf)
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  IVBEG      I   Start time of work interval.
///  IVEND      I   End time of work interval.
///  TIME       I   Current time being examined in the search process
/// ```
///
/// # Detailed Input
///
/// ```text
///  IVBEG,
///  IVEND    are the bounds of an interval that is contained in some
///           interval belonging to the confinement window. The
///           confinement window is associated with some root finding
///           activity. It is used to determine how much total time is
///           being searched in order to find the events of interest.
///
///           In order for a meaningful progress report to be
///           displayed, IVBEG and IVEND must satisfy the following
///           constraints:
///
///              - IVBEG must be less than or equal to IVEND.
///
///              - The interval [ IVBEG, IVEND ] must be contained in
///                some interval of the confinement window. It can be
///                a proper subset of the containing interval; that
///                is, it can be smaller than the interval of the
///                confinement window that contains it.
///
///              - Over a search pass, the sum of the differences
///
///                   IVEND - IVBEG
///
///                for all calls to this routine made during the pass
///                must equal the measure of the confinement window.
///
///
///  TIME     is the current time reached in the search for an event.
///           TIME must lie in the interval
///
///              IVBEG : IVEND
///
///           inclusive. The input values of TIME for a given interval
///           need not form an increasing sequence.
/// ```
///
/// # Detailed Output
///
/// ```text
///  None. This routine does perform console I/O when progress
///  reporting is enabled.
/// ```
///
/// # Exceptions
///
/// ```text
///  1)  If IVBEG and IVEND are in decreasing order, the error
///      SPICE(BADENDPOINTS) is signaled.
///
///  2)  If TIME is not in the closed interval [IVBEG, IVEND], the
///      error SPICE(VALUEOUTOFRANGE) is signaled.
///
///  3)  If an I/O error results from writing to standard output, the
///      error is signaled by a routine in the call tree of this
///      routine.
/// ```
///
/// # Particulars
///
/// ```text
///  This entry point is used to indicate the current progress of a
///  search. Using information recorded through the initialization
///  entry point of this routine, the progress reporting system
///  determines how much work has been completed and whether or not to
///  report it on the users screen.
/// ```
///
/// # Examples
///
/// ```text
///  See $Examples in GFRPRT.
/// ```
///
/// # Restrictions
///
/// ```text
///  1)  This routine has no way of enforcing that the input values of
///      IVBEG and IVEND are compatible with the input window passed to
///      GFREPI. Callers of this routine are responsible for ensuring
///      that this requirement is obeyed.
/// ```
///
/// # Author and Institution
///
/// ```text
///  N.J. Bachman       (JPL)
///  J. Diaz del Rio    (ODC Space)
///  L.S. Elson         (JPL)
///  B.V. Semenov       (JPL)
///  W.L. Taber         (JPL)
///  I.M. Underwood     (JPL)
/// ```
///
/// # Version
///
/// ```text
/// -    SPICELIB Version 1.0.2, 27-AUG-2021 (JDR)
///
///         Edited the header to comply with NAIF standard.
///
/// -    SPICELIB Version 1.0.1, 10-FEB-2014 (BVS)
///
///         Added declarations of IVBEG and IVEND to the $Declarations
///         section.
///
/// -    SPICELIB Version 1.0.0, 21-FEB-2009 (NJB) (LSE) (WLT) (IMU)
/// ```
pub fn gfrepu(ctx: &mut SpiceContext, ivbeg: f64, ivend: f64, time: f64) -> crate::Result<()> {
    GFREPU(ivbeg, ivend, time, ctx.raw_context())?;
    ctx.handle_errors()?;
    Ok(())
}

//$Procedure GFREPU ( GF, progress report update )
pub fn GFREPU(IVBEG: f64, IVEND: f64, TIME: f64, ctx: &mut Context) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut INCR: f64 = 0.0;

    if RETURN(ctx) {
        return Ok(());
    }

    CHKIN(b"GFREPU", ctx)?;

    //
    // Do a few error checks before getting started.
    //
    // We expect the endpoints of the current window to be in order.
    //
    if (IVEND < IVBEG) {
        SETMSG(
            b"Interval endpoints are #:#; endpoints must be in increasing order.",
            ctx,
        );
        ERRDP(b"#", IVBEG, ctx);
        ERRDP(b"#", IVEND, ctx);
        SIGERR(b"SPICE(BADENDPOINTS)", ctx)?;
        CHKOUT(b"GFREPU", ctx)?;
        return Ok(());
    }

    //
    // We expect TIME to be in the current interval of the confinement
    // window.
    //
    if ((TIME < IVBEG) || (TIME > IVEND)) {
        SETMSG(b"TIME should be in interval #:# but is #.", ctx);
        ERRDP(b"#", TIME, ctx);
        ERRDP(b"#", IVBEG, ctx);
        ERRDP(b"#", IVEND, ctx);
        SIGERR(b"SPICE(VALUEOUTOFRANGE)", ctx)?;
        CHKOUT(b"GFREPU", ctx)?;
        return Ok(());
    }

    //
    // The amount of work done is the difference between the current
    // time and the previous time T0, presuming both times are in
    // the current interval.  Note this work amount may be negative.
    //
    if ((save.T0 >= IVBEG) && (save.T0 <= IVEND)) {
        INCR = (TIME - save.T0);
    } else {
        //
        // T0 is in the previous interval.  The amount of work
        // done to complete processing of that interval is REMAIN.
        // The amount of work done in the current interval is
        // the difference of TIME and the left endpoint of the
        // interval.
        //
        INCR = ((save.REMAIN + TIME) - IVBEG);
    }

    //
    // The remaining work is the distance from TIME to the right
    // endpoint of the current interval.
    //
    save.REMAIN = (IVEND - TIME);

    //
    // Record the current time as T0.
    //
    save.T0 = TIME;

    //
    // Report the work increment.
    //
    ZZGFWKIN(INCR, ctx)?;

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

/// GF, progress report finalization
///
/// Finish a progress report.
///
/// # Required Reading
///
/// * [GF](crate::required_reading::gf)
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  None.
/// ```
///
/// # Detailed Output
///
/// ```text
///  None. This routine does perform console I/O when progress
///  reporting is enabled.
/// ```
///
/// # Exceptions
///
/// ```text
///  1)  If an I/O error results from writing to standard output, the
///      error is signaled by a routine in the call tree of this
///      routine.
/// ```
///
/// # Particulars
///
/// ```text
///  This entry point "finishes" a progress report, i.e. updates the
///  report to indicate the underlying task is 100% complete.
/// ```
///
/// # Examples
///
/// ```text
///  See $Examples in GFRPRT.
/// ```
///
/// # Author and Institution
///
/// ```text
///  N.J. Bachman       (JPL)
///  J. Diaz del Rio    (ODC Space)
///  L.S. Elson         (JPL)
///  W.L. Taber         (JPL)
///  I.M. Underwood     (JPL)
/// ```
///
/// # Version
///
/// ```text
/// -    SPICELIB Version 1.0.1, 07-APR-2021 (JDR)
///
///         Edited the header to comply with NAIF standard.
///
/// -    SPICELIB Version 1.0.0, 21-FEB-2009 (NJB) (LSE) (WLT) (IMU)
/// ```
pub fn gfrepf(ctx: &mut SpiceContext) -> crate::Result<()> {
    GFREPF(ctx.raw_context())?;
    ctx.handle_errors()?;
    Ok(())
}

//$Procedure GFREPF ( GF, progress report finalization )
pub fn GFREPF(ctx: &mut Context) -> f2rust_std::Result<()> {
    let save = ctx.get_vars::<SaveVars>();
    let save = &mut *save.borrow_mut();

    let mut BEGIN = [b' '; MXBEGM as usize];
    let mut END = [b' '; MXENDM as usize];
    let mut FREQ: f64 = 0.0;
    let mut INCR: f64 = 0.0;
    let mut TOTAL: f64 = 0.0;
    let mut STDOUT: i32 = 0;
    let mut TCHECK: i32 = 0;
    let mut UNIT: i32 = 0;

    if RETURN(ctx) {
        return Ok(());
    }

    CHKIN(b"GFREPF", ctx)?;

    ZZGFWKAD(0.0, 1, &save.COPYB, &save.COPYE, ctx);
    ZZGFWKIN(0.0, ctx)?;

    //
    // Determine whether progress report output is currently
    // being sent to standard output. Fetch the output unit.
    //
    ZZGFWKMO(
        &mut UNIT,
        &mut TOTAL,
        &mut FREQ,
        &mut TCHECK,
        &mut BEGIN,
        &mut END,
        &mut INCR,
        ctx,
    );

    STDIO(b"STDOUT", &mut STDOUT, ctx)?;

    if (UNIT != STDOUT) {
        //
        // We're not currently writing to standard output, so we're
        // done.
        //
        CHKOUT(b"GFREPF", ctx)?;
        return Ok(());
    }

    //
    // Emit a final blank line by moving the cursor down two
    // spaces.
    //
    // The set of actual arguments passed here is rather funky
    // and deserves some explanation:
    //
    //    The first argument, calling for a leading blank line, moves
    //    the cursor down so that the next blank line written won't
    //    overwrite the final status message. That blank line is
    //    followed with a cursor repositioning command that moves the
    //    cursor to the beginning of the line that was just written. The
    //    last argument, calling for another blank line, moves the
    //    cursor down again. The total cursor movement is down 2 lines.
    //    This results in one skipped line.
    //
    // We could accomplish the same results more simply if were
    // were to use I/O statements in this routine; however, in the
    // interest of minimizing the number of places where I/O is
    // performed, we rely on ZZGFDSPS to do that job.
    //
    ZZGFDSPS(1, b" ", b"A", 1, ctx)?;

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