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
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
const MAXDEG: i32 = 27;
const ITRUE: i32 = 1;
const IFALSE: i32 = -1;
const S19TP0: i32 = 0;
const S19TP1: i32 = (S19TP0 + 1);
const S19TP2: i32 = (S19TP1 + 1);
const S19PS0: i32 = 12;
const S19PS1: i32 = 6;
const S19PS2: i32 = 6;
const S19NST: i32 = 3;
const S19MXZ: i32 = S19PS0;
const S19MNZ: i32 = S19PS1;
const MAXRSZ: i32 = (2 + ((MAXDEG + 1) * (S19PS1 + 1)));
const BUFSIZ: i32 = 100;
const CTRLSZ: i32 = 3;
const DIRSIZ: i32 = 100;
const FILSIZ: i32 = 255;
struct SaveVars {
SPK: Vec<u8>,
CONTRL: StackArray<f64, 3>,
DATA: StackArray<f64, 100>,
IV1BEG: f64,
IV1END: f64,
IVFBEG: f64,
IVFEND: f64,
IVLBEG: f64,
IVLEND: f64,
BEGIDX: i32,
BEPIDX: i32,
BUFBAS: i32,
CURIVL: i32,
EEPIDX: i32,
ENDIDX: i32,
I: i32,
ISEL: i32,
IVLBAS: i32,
L: i32,
MIN1SZ: i32,
MINBEP: i32,
MINFSZ: i32,
MINIB: i32,
MINIE: i32,
MINNDR: i32,
MINNPK: i32,
NDIR: i32,
NINTVL: i32,
NOIVL: i32,
NPAD: i32,
NPKT: i32,
NREAD: i32,
NSDIR: i32,
PKTSIZ: i32,
PKTSZS: StackArray<i32, 3>,
PTRBAS: i32,
REMAIN: i32,
SHIFT: i32,
START: i32,
SUBTYP: i32,
UB: i32,
WNDSIZ: i32,
FINAL: bool,
}
impl SaveInit for SaveVars {
fn new() -> Self {
let mut SPK = vec![b' '; FILSIZ as usize];
let mut CONTRL = StackArray::<f64, 3>::new(1..=CTRLSZ);
let mut DATA = StackArray::<f64, 100>::new(1..=BUFSIZ);
let mut IV1BEG: f64 = 0.0;
let mut IV1END: f64 = 0.0;
let mut IVFBEG: f64 = 0.0;
let mut IVFEND: f64 = 0.0;
let mut IVLBEG: f64 = 0.0;
let mut IVLEND: f64 = 0.0;
let mut BEGIDX: i32 = 0;
let mut BEPIDX: i32 = 0;
let mut BUFBAS: i32 = 0;
let mut CURIVL: i32 = 0;
let mut EEPIDX: i32 = 0;
let mut ENDIDX: i32 = 0;
let mut I: i32 = 0;
let mut ISEL: i32 = 0;
let mut IVLBAS: i32 = 0;
let mut L: i32 = 0;
let mut MIN1SZ: i32 = 0;
let mut MINBEP: i32 = 0;
let mut MINFSZ: i32 = 0;
let mut MINIB: i32 = 0;
let mut MINIE: i32 = 0;
let mut MINNDR: i32 = 0;
let mut MINNPK: i32 = 0;
let mut NDIR: i32 = 0;
let mut NINTVL: i32 = 0;
let mut NOIVL: i32 = 0;
let mut NPAD: i32 = 0;
let mut NPKT: i32 = 0;
let mut NREAD: i32 = 0;
let mut NSDIR: i32 = 0;
let mut PKTSIZ: i32 = 0;
let mut PKTSZS = StackArray::<i32, 3>::new(0..=(S19NST - 1));
let mut PTRBAS: i32 = 0;
let mut REMAIN: i32 = 0;
let mut SHIFT: i32 = 0;
let mut START: i32 = 0;
let mut SUBTYP: i32 = 0;
let mut UB: i32 = 0;
let mut WNDSIZ: i32 = 0;
let mut FINAL: bool = false;
{
use f2rust_std::data::Val;
let mut clist = [Val::I(S19PS0), Val::I(S19PS1), Val::I(S19PS2)].into_iter();
PKTSZS
.iter_mut()
.for_each(|n| *n = clist.next().unwrap().into_i32());
debug_assert!(clist.next().is_none(), "DATA not fully initialised");
}
Self {
SPK,
CONTRL,
DATA,
IV1BEG,
IV1END,
IVFBEG,
IVFEND,
IVLBEG,
IVLEND,
BEGIDX,
BEPIDX,
BUFBAS,
CURIVL,
EEPIDX,
ENDIDX,
I,
ISEL,
IVLBAS,
L,
MIN1SZ,
MINBEP,
MINFSZ,
MINIB,
MINIE,
MINNDR,
MINNPK,
NDIR,
NINTVL,
NOIVL,
NPAD,
NPKT,
NREAD,
NSDIR,
PKTSIZ,
PKTSZS,
PTRBAS,
REMAIN,
SHIFT,
START,
SUBTYP,
UB,
WNDSIZ,
FINAL,
}
}
}
/// S/P Kernel, subset, type 19
///
/// Extract a subset of the data in an SPK segment of type 19
/// into a new segment.
///
/// # Required Reading
///
/// * [SPK](crate::required_reading::spk)
/// * [DAF](crate::required_reading::daf)
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// HANDLE I Handle of file containing source segment.
/// BADDR I Beginning address in file of source segment.
/// EADDR I Ending address in file of source segment.
/// BEGIN I Beginning (initial epoch) of subset.
/// END I End (final epoch) of subset.
/// ```
///
/// # Detailed Input
///
/// ```text
/// HANDLE,
/// BADDR,
/// EADDR are the file handle assigned to an SPK file, and the
/// beginning and ending addresses of a segment within
/// that file. Together these identify an SPK segment
/// from which a subset is to be extracted.
///
/// The subset is written to a second SPK file which is
/// open for writing, and in which a new segment has been
/// started. See the $Particulars section below for
/// details.
///
/// BEGIN,
/// END are the initial and final epochs (ephemeris time)
/// of the subset.
///
/// The first epoch for which there will be ephemeris
/// data in the new segment will be the greatest time
/// in the source segment that is less than or equal
/// to BEGIN.
///
/// The last epoch for which there will be ephemeris
/// data in the new segment will be the smallest time
/// in the source segment that is greater than or equal
/// to END.
/// ```
///
/// # Detailed Output
///
/// ```text
/// None. See $Files section.
/// ```
///
/// # Exceptions
///
/// ```text
/// 1) This routine relies on the caller to ensure that the
/// interval [BEGIN, END] is contained in the coverage
/// interval of the source segment.
///
/// 2) If BEGIN > END, no data are written to the target file.
///
/// 3) If a unexpected SPK type 19 subtype is found in the input
/// segment, the error SPICE(INVALIDVALUE) is signaled.
///
/// 4) The input segment must have valid structure; this
/// routine may fail in unpredictable ways if not.
/// ```
///
/// # Files
///
/// ```text
/// Data are extracted from the file connected to the input
/// handle, and written to the current DAF open for writing.
///
/// The segment descriptor and summary must already have been written
/// prior to calling this routine. The segment must be ended
/// external to this routine.
/// ```
///
/// # Particulars
///
/// ```text
/// This routine is intended solely for use as a utility by the
/// routine SPKSUB.
///
/// It transfers a subset of a type 19 SPK data segment to
/// a properly initialized segment of a second SPK file.
///
/// The exact structure of a segment of data type 19 is described
/// in the section on type 19 in the SPK Required Reading.
/// ```
///
/// # Examples
///
/// ```text
/// This routine is intended only for use as a utility by SPKSUB.
/// To use this routine successfully, you must:
///
/// Open the SPK file from which to extract data.
/// Locate the segment from which data should be extracted.
///
/// Open the SPK file to which this data should be written.
/// Begin a new segment (array).
/// Write the summary information for the array.
///
/// Call this routine to extract the appropriate data from the
/// SPK open for read.
///
/// End the array to which this routine writes data.
///
/// Much of this procedure is carried out by the routine SPKSUB. The
/// examples of that routine illustrate more fully the process
/// described above.
/// ```
///
/// # Restrictions
///
/// ```text
/// 1) This routine relies on the input segment being correct;
/// very limited error checking is performed on the input
/// data.
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// B.V. Semenov (JPL)
/// W.L. Taber (JPL)
/// I.M. Underwood (JPL)
/// E.D. Wright (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 2.0.1, 03-JUN-2021 (JDR)
///
/// Edited the header to comply with NAIF standard. Removed
/// unnecessary $Revisions section.
///
/// - SPICELIB Version 2.0.0, 04-APR-2017 (NJB)
///
/// Typo in comment fixed.
///
/// 11-MAY-2015 (NJB)
///
/// Updated to support subtype 2.
///
/// - SPICELIB Version 1.0.0, 17-OCT-2011 (NJB) (BVS) (WLT) (IMU) (EDW)
/// ```
pub fn spks19(
ctx: &mut SpiceContext,
handle: i32,
baddr: i32,
eaddr: i32,
begin: f64,
end: f64,
) -> crate::Result<()> {
SPKS19(handle, baddr, eaddr, begin, end, ctx.raw_context())?;
ctx.handle_errors()?;
Ok(())
}
//$Procedure SPKS19 ( S/P Kernel, subset, type 19 )
pub fn SPKS19(
HANDLE: i32,
BADDR: i32,
EADDR: i32,
BEGIN: f64,
END: f64,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let save = ctx.get_vars::<SaveVars>();
let save = &mut *save.borrow_mut();
//
// SPICELIB functions
//
//
// Local parameters
//
//
// Mini-segment control area size:
//
//
// Local variables
//
//
// Saved variables
//
//
// Initial values
//
//
// Standard SPICE error handling.
//
if RETURN(ctx) {
return Ok(());
}
CHKIN(b"SPKS19", ctx)?;
//
// Terminology
// ===========
//
// - A point P is in the "interior" of a set S if P is
// an element of S and P is not contained in the boundary
// of S. If S is a discrete set of distinct times, then
// the interior points of S are greater than the earliest
// time in S and earlier than the latest time in S. If
// S is the closed interval [A, B], that is, if S is the set of
// points P such that
//
// A < P < B
// - -
//
// then the interior of S is the set of points P such that
//
// A < P < B
//
// - A subset S2 of a set S1 is in the "interior" of S1 if
// every point of S2 is contained in the interior of S1.
//
// - SPK type 19 interpolation intervals are often simply
// called "intervals."
//
// - The data set corresponding to a type 19 interpolation
// interval is called a "mini-segment."
//
// - "Padding" consists of a sequence of contiguous data packets
// and a corresponding sequence of epochs provided to enable
// correct interpolation near interval boundaries, where the
// epochs lie outside of the interval's coverage time range.
// Padding data and epochs are always drawn only from the same
// input mini-segment that provides data for the output
// mini-segment under construction.
//
// - A "base address" of a structure is the DAF address preceding
// the first address occupied by the structure.
//
//
// Algorithm
// =========
//
// The algorithm below transfers to the output segment sufficient
// data to cover the time range BEGIN : END, such that the output
// segment yields interpolation behavior identical to that of the
// selected portion of the input segment.
//
// No use is made of the selection order attribute other than to
// transfer it to the output segment. This simplifies the algorithm,
// at the expense of making the output segment larger than necessary
// by at most a small, bounded amount. Specifically, when either
// BEGIN or END coincides with an interior interval boundary, a
// small additional output interval is created so as to make that
// boundary an interior point of the output segment's coverage
// interval. This guarantees that the correct interval can be
// selected when a request time coincides with the boundary of
// interest.
//
// The overall approach is:
//
// 1) Obtain attribute information from the input segment.
//
// 2) Create a first output mini-segment. This mini-segment is
// created using data from the first input mini-segment
// having an end time greater than or equal to BEGIN.
//
// The first output mini-segment contains padding, if needed,
// on both the left and right sides. On the left side, given
// a nominal interpolation window width W (W must be even),
// the nominal pad size NPAD is (W/2) - 1. If I is the index
// of the last time tag (in the selected input mini-segment)
// less than or equal to BEGIN, the pad starts at I-NPAD or
// 1, whichever is greater.
//
// On the right side, if END is greater than or equal to the
// last epoch of the input mini-segment, all epochs and
// packets of the input mini-segment following the first ones
// selected are transferred to the output mini-segment.
//
// The first mini-segment requires padding on the right only
// if END precedes the end time of the input mini-segment. In
// this case the pad size is chosen so that the output
// mini-segment contains W/2 epochs greater than or equal to
// END, if possible. If I is the index of the first time tag
// in the mini-segment greater than or equal to END, then the
// pad ends at I + (W/2) - 1 or NPKT, whichever is smaller.
//
// Note that due to the asymmetry of the search techniques
// used (there are no SPICELIB right-to-left search routines
// analogous to LSTLTD and LSTLED), the implementation of the
// pad computation for the right side is not as similar to
// that for the left side as one might expect.
//
// The first output mini-segment and all subsequent output
// mini-segments have the structure of an SPK type 18
// segment. They consist of
//
// a) A sequence of data packets
//
// b) A sequence of epochs
//
// c) An epoch directory, if needed
//
// d) A control area, consisting of
//
// - A subtype code
// - An interpolation window size
// - A packet count
//
// 3) All input mini-segments whose interpolation intervals
// follow that of the first used mini-segment and whose stop
// times are less than or equal to END are copied whole
// to the output segment. We refer to this sequence of
// mini-segments as the "middle group." The middle group may
// be empty.
//
// 4) If necessary, a final output mini-segment is written. This
// mini-segment will be required unless either
//
// - The interval of the first mini-segment contains in
// its interior the interval BEG : END.
//
// - The middle group ends at the end of the input segment.
//
// Note that if the last interval of the middle group ends at
// END, but END is less than the final input interval's stop
// time, a final mini-segment is still needed to ensure
// correct interval selection. If there is no middle group
// and the first used interpolation interval ends at END, and
// if END is less than the final input interval's stop time,
// a final mini-segment is required as well.
//
// The interpolation interval of the final output
// mini-segment always starts at an input interval boundary.
// This interval has padding on the left only if the
// corresponding input interval has padding on the left; any
// existing left side padding from the input mini-segment is
// simply copied to the output mini-segment. On the right
// side, padding is created if it is necessary and possible
// to do so. When right side padding is used, the pad size
// and placement follow the same rules used for the right
// side padding of the first output mini-segment.
//
// 5) After all output mini-segments have been written, the
// following segment-level structures are written to the
// output segment:
//
// a) The output segment interpolation interval
// boundaries. This list includes the start time of
// each output interval and the stop time of the final
// output interval.
//
// b) The output segment interpolation interval boundary
// directory, if needed.
//
// c) The output segment's mini-segment begin and "end"
// pointers. This list consists of the segment
// base-relative first address of each mini-segment,
// plus the relative address following the final output
// mini-segment.
//
// d) The output segment control area. This consists of:
//
// - The interval selection order flag. This is copied
// from the input segment.
//
// - The output segment interval count
//
//
//
//
// See whether there's any work to do; return immediately if not.
//
if (BEGIN > END) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// We don't check BEGIN and END against the time bounds of the
// descriptor of the input file because, according to the
// SPK subsetting subsystem design, the calling routine SPKSUB
// has done this already. Note that the descriptor of the source
// segment is not even an input to this routine. If we wanted to,
// we could search the input DAF for a descriptor that mapped to
// the address range BADDR : EADDR. but we're not going to do
// that.
//
// Initialize the flag indicating the existence of the "final"
// output mini-segment.
//
save.FINAL = false;
//***********************************************************************
//
// Part 1: Obtain attributes of the input segment
//
//***********************************************************************
//
// Read the input segment structure control area.
//
DAFGDA(HANDLE, (EADDR - 1), EADDR, save.DATA.as_slice_mut(), ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Fetch the interval selection order flag and the
// number of interpolation intervals.
//
save.ISEL = intrinsics::IDNINT(save.DATA[1]);
save.NINTVL = intrinsics::IDNINT(save.DATA[2]);
//
// Compute the number of interval boundary directories. Recall that
// the final interval stop time must be accounted for, so the
// directory count is
//
// ( ( NINTVL + 1) - 1 ) / DIRSIZ
//
save.NDIR = (save.NINTVL / DIRSIZ);
//
// Find the base address IVLBAS of the interval start times. First
// set PTRBAS, which is the address preceding the interval pointers.
//
// The interval base precedes the interval bounds, the interval
// directories, the interval pointers, and the control area.
//
save.PTRBAS = (EADDR - ((2 + save.NINTVL) + 1));
save.IVLBAS = (save.PTRBAS - ((save.NDIR + save.NINTVL) + 1));
//***********************************************************************
//
// Part 2: Create the first output mini-segment
//
//***********************************************************************
//
// Search for the first interval that will contribute data to the
// output segment. We first find the last interval boundary that is
// strictly less than the epoch BEGIN. The final interval stop time
// need not be considered, since the segment covers the interval
// [BEGIN : END]. Note however there is a "corner case" in which
//
// BEGIN == END == <final interval stop time>
//
// Since we're only examining interval start times, the last one
// we may need to read is at index NINTVL.
//
save.NREAD = intrinsics::MIN0(&[BUFSIZ, save.NINTVL]);
save.BUFBAS = save.IVLBAS;
//
// NREAD is at least 1 here.
//
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.NREAD),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.REMAIN = (save.NINTVL - save.NREAD);
//
// The variable NREAD is the array index of the last
// item read into the buffer on the previous read
// operation. On the first pass NREAD is at least 1.
//
while ((save.REMAIN > 0) && (save.DATA[save.NREAD] < BEGIN)) {
save.BUFBAS = (save.BUFBAS + save.NREAD);
save.NREAD = intrinsics::MIN0(&[BUFSIZ, save.REMAIN]);
//
// NREAD is at least 1 here.
//
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.NREAD),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.REMAIN = (save.REMAIN - save.NREAD);
}
//
// Let I be the index of the last interval boundary time that
// precedes BEGIN. If there are no such boundary times, I will be
// zero. This latter case can happen only when the first interval
// start time is equal to BEGIN.
//
// At this point BUFBAS - IVLBAS is the number of boundaries we
// examined before the final call above to DAFGDA. All of those
// boundary times were strictly less than BEGIN.
//
save.I = ((save.BUFBAS - save.IVLBAS) + LSTLTD(BEGIN, save.NREAD, save.DATA.as_slice()));
//
// Let BEGIDX be the index of the last interval start time that
// precedes BEGIN, unless BEGIN coincides with the first interval
// start time; in this case, BEGIDX must be 1.
//
save.BEGIDX = intrinsics::MAX0(&[1, save.I]);
//
// In order to extract data from the mini-segment, we'll need its
// address range.
//
DAFGDA(
HANDLE,
(save.PTRBAS + save.BEGIDX),
((save.PTRBAS + save.BEGIDX) + 1),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Convert the segment-base-relative mini-segment begin and end
// pointers to absolute DAF addresses.
//
save.MINIB = ((BADDR - 1) + intrinsics::IDNINT(save.DATA[1]));
save.MINIE = (((BADDR - 1) + intrinsics::IDNINT(save.DATA[2])) - 1);
//
// Read the control area of the mini-segment.
//
save.BUFBAS = (save.MINIE - CTRLSZ);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + CTRLSZ),
save.CONTRL.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Fetch the control area parameters for the mini-segment.
//
save.SUBTYP = intrinsics::IDNINT(save.CONTRL[1]);
save.WNDSIZ = intrinsics::IDNINT(save.CONTRL[2]);
save.NPKT = intrinsics::IDNINT(save.CONTRL[3]);
//
// Set the packet size, which is a function of the subtype.
//
if ((save.SUBTYP < 0) || (save.SUBTYP >= S19NST)) {
SETMSG(
b"Unexpected SPK type 19 subtype # found in type 19 segment within mini-segment #.",
ctx,
);
ERRINT(b"#", save.SUBTYP, ctx);
ERRINT(b"#", save.BEGIDX, ctx);
SIGERR(b"SPICE(NOTSUPPORTED)", ctx)?;
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.PKTSIZ = save.PKTSZS[save.SUBTYP];
//
// Determine how much of the mini-segment we need to transfer. The
// first step is to find the last epoch less than or equal to BEGIN
// in the mini-segment's epoch list. Let MINBEP be the base address
// of the epoch list (that is, the start address minus 1).
//
save.MINBEP = ((save.MINIB - 1) + (save.NPKT * save.PKTSIZ));
//
// Read epochs until we find one greater than or equal to BEGIN.
//
// It's possible that only the last epoch of the input mini-segment
// satisfies this criterion, but at least one epoch must satisfy it.
//
save.NREAD = intrinsics::MIN0(&[BUFSIZ, save.NPKT]);
save.BUFBAS = save.MINBEP;
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.NREAD),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.REMAIN = (save.NPKT - save.NREAD);
//
// The variable NREAD is the array index of the last
// item read into the buffer on the previous read
// operation.
//
while ((save.REMAIN > 0) && (save.DATA[save.NREAD] < BEGIN)) {
//
// Advance the buffer base to account for the NREAD
// epochs fetched on the previous DAFGDA call.
//
save.BUFBAS = (save.BUFBAS + save.NREAD);
save.NREAD = intrinsics::MIN0(&[BUFSIZ, save.REMAIN]);
//
// Since REMAIN was positive at the beginning of this
// loop iteration, NREAD is positive here.
//
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.NREAD),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.REMAIN = (save.REMAIN - save.NREAD);
}
//
// At this point BUFBAS - MINBEP is the number of epochs in the
// input mini-segment we've examined before the final call above to
// DAFGDA. All of those epochs were strictly less than BEGIN.
//
// Let BEPIDX be the index of the last epoch that precedes or is
// equal to BEGIN. That epoch is contained in the last buffer we
// read.
//
save.BEPIDX = ((save.BUFBAS - save.MINBEP) + LSTLED(BEGIN, save.NREAD, save.DATA.as_slice()));
//
// BEPIDX is at least 1 and may be as large as NPKT.
//
// Compute the number of pad epochs we need to maintain proper
// interpolation behavior in the neighborhood of the epoch at
// index BEPIDX.
//
save.NPAD = ((save.WNDSIZ / 2) - 1);
//
// Shift BEPIDX by the pad amount, if possible. The minimum value
// of BEPIDX is 1.
//
save.BEPIDX = intrinsics::MAX0(&[1, (save.BEPIDX - save.NPAD)]);
//
// The output mini-segment can never have fewer than two epochs.
// When the window size is 2 and BEPIDX is equal to NPKT, we
// must extend the window on the left.
//
save.BEPIDX = intrinsics::MIN0(&[save.BEPIDX, (save.NPKT - 1)]);
//
// If the input interval end time is less than or equal to END, we
// need to use the rest of the data from this interval. Otherwise
// find out how much data from this interval we need to transfer.
//
save.BUFBAS = (save.IVLBAS + save.BEGIDX);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 1),
std::slice::from_mut(&mut save.IVLEND),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Let EEPIDX be the index of the last epoch we select from
// the current input mini-segment. We'll set EEPIDX below.
//
if (save.IVLEND <= END) {
//
// The requested subset coverage is either equal to or extends
// beyond the right boundary of this interval. We'll use all data
// from this interval.
//
save.EEPIDX = save.NPKT;
} else {
//
// IVLEND is strictly greater than END. This interval covers
//
// [BEGIN, END].
//
// Read epochs from this mini-segment until we find one greater
// than or equal to END. We have an error if we run out of
// epochs.
//
// The input mini-segment contains ( NPKT - BEPIDX + 1 ) epochs
// following and including the one at BEPIDX.
//
save.REMAIN = ((save.NPKT - save.BEPIDX) + 1);
//
// REMAIN is at least 2 at this point, since in this case,
// some epoch exceeds END, and that epoch must have index
// greater than BEPIDX.
//
save.NREAD = intrinsics::MIN0(&[BUFSIZ, save.REMAIN]);
//
// NREAD is at least 2.
//
if (save.NREAD < 2) {
//
// This code should not be reached.
//
DAFHFN(HANDLE, &mut save.SPK, ctx)?;
SETMSG(b"Input file: #. Segment address range: #:#. Structural error found: NREAD is #; end time of interval # is #.", ctx);
ERRCH(b"#", &save.SPK, ctx);
ERRINT(b"#", BADDR, ctx);
ERRINT(b"#", EADDR, ctx);
ERRINT(b"#", save.NREAD, ctx);
ERRINT(b"#", save.BEGIDX, ctx);
ERRDP(b"#", save.IVLEND, ctx);
SIGERR(b"SPICE(SPKSTRUCTUREERROR)", ctx)?;
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Set the buffer base address so that we start reading at
// address MINBEP + BEPIDX.
//
save.BUFBAS = ((save.MINBEP + save.BEPIDX) - 1);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.NREAD),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.REMAIN = (save.REMAIN - save.NREAD);
//
// NREAD is (still) at least 2.
//
while ((save.REMAIN > 0) && (save.DATA[save.NREAD] <= END)) {
save.BUFBAS = (save.BUFBAS + save.NREAD);
save.NREAD = intrinsics::MIN0(&[save.REMAIN, BUFSIZ]);
//
// NREAD is at least 1 since REMAIN was positive
// at the top of the loop.
//
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.NREAD),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.REMAIN = (save.REMAIN - save.NREAD);
}
//
// At this point BUFBAS - MINBEP is the number of epochs in the
// input mini-segment we've examined before the final call above
// to DAFGDA. If this set of epochs is non-empty, all of these
// epochs are less than or equal to END. Note that it's possible
// for END and BEGIN to be equal to the first epoch.
//
// Let EEPIDX be the index of the first epoch that is strictly
// greater than END. As asserted above, in this branch of the
// code, such an epoch must exist. That epoch is contained in the
// last buffer we read.
//
// EEPIDX exceeds by 1 the index of the last epoch less than or
// equal to END.
//
save.L = LSTLED(END, save.NREAD, save.DATA.as_slice());
save.EEPIDX = (((save.BUFBAS - save.MINBEP) + save.L) + 1);
//
// EEPIDX is at least 2 and is less than or equal to NPKT.
//
if ((save.EEPIDX < 2) || (save.EEPIDX > save.NPKT)) {
//
// This code should not be reached.
//
DAFHFN(HANDLE, &mut save.SPK, ctx)?;
SETMSG(b"Input file: #. Segment address range: #:#. Structural error found: last epoch is #; end time of interval # is #.", ctx);
ERRCH(b"#", &save.SPK, ctx);
ERRINT(b"#", BADDR, ctx);
ERRINT(b"#", EADDR, ctx);
ERRDP(b"#", save.DATA[save.NREAD], ctx);
ERRINT(b"#", save.BEGIDX, ctx);
ERRDP(b"#", save.IVLEND, ctx);
SIGERR(b"SPICE(SPKSTRUCTUREERROR)", ctx)?;
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Compute the number of pad epochs we need to maintain proper
// interpolation behavior in the neighborhood of the epoch at
// index EEPIDX.
//
if (save.DATA[save.L] == END) {
//
// The epochs at indices EEPIDX-1 and EEPIDX comprise the
// first two epochs of the right half of an interpolation
// window of size WNDSIZ. We need two fewer pad epochs to
// complete the right half of the window.
//
save.NPAD = ((save.WNDSIZ / 2) - 2);
} else {
//
// The epoch at EEPIDX is the first of the pad.
//
save.NPAD = ((save.WNDSIZ / 2) - 1);
}
//
// The maximum allowed value of EEPIDX is NPKT.
//
save.EEPIDX = intrinsics::MIN0(&[save.NPKT, (save.EEPIDX + save.NPAD)]);
}
//
// At this point BEPIDX and EEPIDX are both set.
//
// Look up the input interval's start time at index BEGIDX.
// We'll use this below when we compute the interval start
// time of the first output mini-segment.
//
DAFGDA(
HANDLE,
(save.IVLBAS + save.BEGIDX),
(save.IVLBAS + save.BEGIDX),
std::slice::from_mut(&mut save.IVLBEG),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
// We're ready to start transferring data to the output segment. The
// first mini-segment of the output segment will contain packets
// BEPIDX through EEPIDX of the input mini-segment at index BEGIDX.
//
{
let m1__: i32 = save.BEPIDX;
let m2__: i32 = save.EEPIDX;
let m3__: i32 = 1;
save.I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
save.BUFBAS = ((save.MINIB - 1) + ((save.I - 1) * save.PKTSIZ));
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.PKTSIZ),
save.DATA.as_slice_mut(),
ctx,
)?;
DAFADA(save.DATA.as_slice(), save.PKTSIZ, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.I += m3__;
}
}
//
// Now transfer the epochs at indices BEPIDX : EEPIDX.
//
// Inside this loop, determine the bounds of the first output
// interpolation interval. Each bound is either the corresponding
// bound of the input interval, or a boundary epoch (first or last)
// of the output epoch list, whichever is most restrictive.
//
{
let m1__: i32 = save.BEPIDX;
let m2__: i32 = save.EEPIDX;
let m3__: i32 = 1;
save.I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
save.BUFBAS = (((save.MINIB - 1) + (save.NPKT * save.PKTSIZ)) + (save.I - 1));
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 1),
save.DATA.as_slice_mut(),
ctx,
)?;
DAFADA(save.DATA.as_slice(), 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Let IV1BEG be the start time of the first output
// interpolation interval. Determine IV1BEG on the first loop
// pass. IVLBEG has already been set to the start time of the
// input interval at index BEGIDX.
//
if (save.I == save.BEPIDX) {
//
// The first output interval cannot start earlier than
// the interval from which its data are taken. It may
// start later.
//
save.IV1BEG = intrinsics::DMAX1(&[save.IVLBEG, save.DATA[1]]);
}
//
// Determine IV1END on the final loop pass.
//
if (save.I == save.EEPIDX) {
//
// The first output interval cannot end later than
// the interval from which its data are taken. It may
// end earlier.
//
save.IV1END = intrinsics::DMIN1(&[save.IVLEND, save.DATA[1]]);
}
save.I += m3__;
}
}
//
// Create the epoch directory for the first output mini-segment.
//
save.MINNPK = ((save.EEPIDX - save.BEPIDX) + 1);
save.MINNDR = ((save.MINNPK - 1) / DIRSIZ);
{
let m1__: i32 = 1;
let m2__: i32 = save.MINNDR;
let m3__: i32 = 1;
save.I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
//
// Set BUFBAS to the address that immediately precedes the
// element we're about to read. We must skip over the data
// packets and the first (BEPIDX-1) epochs before starting our
// count.
//
save.BUFBAS = (((((save.MINIB - 1) + (save.NPKT * save.PKTSIZ)) + (save.BEPIDX - 1))
+ (save.I * DIRSIZ))
- 1);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 1),
save.DATA.as_slice_mut(),
ctx,
)?;
DAFADA(save.DATA.as_slice(), 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.I += m3__;
}
}
//
// Finally, write out the control area for the first mini-segment.
//
DAFADA(&[(save.SUBTYP as f64)], 1, ctx)?;
DAFADA(&[(save.WNDSIZ as f64)], 1, ctx)?;
DAFADA(&[(save.MINNPK as f64)], 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Compute the size of the first output mini-segment; we'll need
// this later to compute the second mini-segment start pointer.
// The size is the sum of the sizes of the packet set, the
// epochs, the epoch directories, and the control area.
//
save.MIN1SZ = (((save.MINNPK * (save.PKTSIZ + 1)) + save.MINNDR) + CTRLSZ);
//***********************************************************************
//
// Part 3: Transfer the middle group of mini-segments to the
// output segment, if this group is non-empty
//
//***********************************************************************
//
// At this point, we might already be done with copying
// mini-segments. If the coverage interval of the mini-segment we
// just processed contains [BEGIN, END] in its interior, we're done.
// If there are no more input mini-segments, we're also done.
// Otherwise, we'll continue to transfer data from subsequent
// input mini-segments.
//
// At this point IVLEND is the end time of the first input
// interval. Note that this time may differ from IV1END, which
// is the end time of the first output interval.
//
if ((save.IVLEND > END) || (save.BEGIDX == save.NINTVL)) {
//
// We've transferred all the data we need. We don't need
// to obtain data from other mini-segments.
//
save.ENDIDX = save.BEGIDX;
//
// FINAL is already set to .FALSE.
//
} else {
//
// We need more data, and there are more data to be had.
//
// Things get a bit easier here: all mini-segments that follow
// the one we just wrote, and that have end times less than or
// equal to END, get copied without modification to the output
// file. Note that this sequence of mini-segments could be empty.
save.CURIVL = (save.BEGIDX + 1);
//
// Initialize the start time of the final output mini-segment.
// We'll update this if we produce more output mini-segments.
//
save.IVFBEG = save.IVLEND;
//
// Get the end time of the interval at index CURIVL.
//
save.BUFBAS = (save.IVLBAS + save.CURIVL);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 1),
std::slice::from_mut(&mut save.IVLEND),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// CURIVL is the index of the interval we're about to process,
// and if CURIVL is in range, IVLEND is its end time.
//
while ((save.IVLEND <= END) && (save.CURIVL <= save.NINTVL)) {
//
// Entering this loop means the "middle" component of the
// output segment is non-empty.
//
// Get the begin and end pointers for the current mini-segment.
//
save.BUFBAS = ((save.PTRBAS + save.CURIVL) - 1);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 2),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.MINIB = ((BADDR - 1) + intrinsics::IDNINT(save.DATA[1]));
save.MINIE = (((BADDR - 1) + intrinsics::IDNINT(save.DATA[2])) - 1);
//
// Transfer all data from DAF address MINIB through DAF
// address MINIE to the target SPK segment.
//
save.REMAIN = ((save.MINIE - save.MINIB) + 1);
save.BUFBAS = (save.MINIB - 1);
save.NREAD = intrinsics::MIN0(&[BUFSIZ, save.REMAIN]);
while (save.REMAIN > 0) {
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.NREAD),
save.DATA.as_slice_mut(),
ctx,
)?;
DAFADA(save.DATA.as_slice(), save.NREAD, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.REMAIN = (save.REMAIN - save.NREAD);
save.BUFBAS = (save.BUFBAS + save.NREAD);
save.NREAD = intrinsics::MIN0(&[BUFSIZ, save.REMAIN]);
}
//
// We've copied the mini-segment at index CURIVL.
//
// Save the end time of this mini-segment in case
// this one turns out NOT to be the last; in that
// case this is the final interval's start time.
//
save.IVFBEG = save.IVLEND;
//
// Get the end time of the next interval, if there
// is one.
//
save.CURIVL = (save.CURIVL + 1);
if (save.CURIVL <= save.NINTVL) {
save.BUFBAS = (save.IVLBAS + save.CURIVL);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 1),
std::slice::from_mut(&mut save.IVLEND),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
}
}
//
// We've transferred the middle group, if it exists, to the
// output segment.
//
// If the last mini-segment we transferred isn't the last of the
// input segment, we're going to copy at least a portion of the
// next mini-segment to the output file.
//
// At this point CURIVL is the index of the next interval to
// process, if any. If CURIVL is valid, IVLEND is the interval's
// end time.
//
//***********************************************************************
//
// Part 4: Create the final output mini-segment, if necessary
//
//***********************************************************************
if (save.CURIVL > save.NINTVL) {
//
// The coverage of the middle group extends to the end of
// the coverage of the input segment. There's no more data to
// transfer.
//
// FINAL is already set to .FALSE.
//
save.ENDIDX = save.NINTVL;
} else {
//
// We're going to create one last output mini-segment.
//
save.FINAL = true;
//
// The input segment contains at least one more interpolation
// interval, and the end time of this interval is greater than
// END. Note that if this interval's end time were equal to
// END, the interval would have been processed in the loop
// above.
//
save.ENDIDX = save.CURIVL;
//
// In order to extract data from the mini-segment, we'll need
// its address range.
//
DAFGDA(
HANDLE,
(save.PTRBAS + save.ENDIDX),
((save.PTRBAS + save.ENDIDX) + 1),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.MINIB = ((BADDR - 1) + intrinsics::IDNINT(save.DATA[1]));
save.MINIE = (((BADDR - 1) + intrinsics::IDNINT(save.DATA[2])) - 1);
//
// Read the control area of the mini-segment.
//
save.BUFBAS = (save.MINIE - CTRLSZ);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + CTRLSZ),
save.CONTRL.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Fetch the control area parameters for the mini-segment.
//
save.SUBTYP = intrinsics::IDNINT(save.CONTRL[1]);
save.WNDSIZ = intrinsics::IDNINT(save.CONTRL[2]);
save.NPKT = intrinsics::IDNINT(save.CONTRL[3]);
//
// Set the packet size, which is a function of the subtype.
//
if ((save.SUBTYP < 0) || (save.SUBTYP >= S19NST)) {
SETMSG(b"Unexpected SPK type 19 subtype # found in type 19 segment within mini-segment #.", ctx);
ERRINT(b"#", save.SUBTYP, ctx);
ERRINT(b"#", save.CURIVL, ctx);
SIGERR(b"SPICE(NOTSUPPORTED)", ctx)?;
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.PKTSIZ = save.PKTSZS[save.SUBTYP];
//
// Determine how much of the mini-segment we need to transfer.
// The first step is to find the last epoch less than or equal
// to END in the mini-segment's epoch list. Let MINBEP be the
// base address of the epoch list (that is, the start address
// minus 1).
//
save.MINBEP = ((save.MINIB - 1) + (save.NPKT * save.PKTSIZ));
//
// Read epochs until we find one strictly greater than END.
// The previous interval was the last one with an end time
// less than or equal to END, so the epoch we seek should
// exist. We have an error condition if it doesn't.
//
save.NREAD = intrinsics::MIN0(&[BUFSIZ, save.NPKT]);
save.BUFBAS = save.MINBEP;
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.NREAD),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.REMAIN = (save.NPKT - save.NREAD);
//
// The variable NREAD is the array index of the last item read
// into the buffer on the previous read operation.
//
while ((save.REMAIN > 0) && (save.DATA[save.NREAD] <= END)) {
save.BUFBAS = (save.BUFBAS + save.NREAD);
save.NREAD = intrinsics::MIN0(&[BUFSIZ, save.REMAIN]);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.NREAD),
save.DATA.as_slice_mut(),
ctx,
)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.REMAIN = (save.REMAIN - save.NREAD);
}
//
// At this point BUFBAS - MINBEP is the number of epochs in
// the input mini-segment we've examined before the final call
// above to DAFGDA. If this set of epochs is non-empty, all of
// these epochs are less than or equal to END. Note that it's
// possible for END and BEGIN to be equal to the first epoch.
//
// Let EEPIDX be the index of the first epoch that is strictly
// greater than END. As asserted above, in this branch of the
// code, such an epoch must exist. That epoch is contained in
// the last buffer we read.
//
// EEPIDX exceeds by 1 the index of the last epoch less than
// or equal to END.
//
save.L = LSTLED(END, save.NREAD, save.DATA.as_slice());
save.EEPIDX = (((save.BUFBAS - save.MINBEP) + save.L) + 1);
//
// EEPIDX is at least 2 and is less than or equal to NPKT.
//
if (save.EEPIDX < 2) {
//
// This code should not be reached, since getting here
// implies the first epoch of the interval is greater than
// END.
//
DAFHFN(HANDLE, &mut save.SPK, ctx)?;
SETMSG(b"Input file: #. Segment address range: #:#. Structural error found: no epochs in final input interval exceed END. Interval index is #; END is #.", ctx);
ERRCH(b"#", &save.SPK, ctx);
ERRINT(b"#", BADDR, ctx);
ERRINT(b"#", EADDR, ctx);
ERRINT(b"#", save.ENDIDX, ctx);
ERRDP(b"#", END, ctx);
SIGERR(b"SPICE(SPKSTRUCTUREERROR)", ctx)?;
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Compute the number of pad epochs we need to maintain proper
// interpolation behavior in the neighborhood of the epoch at
// index EEPIDX.
//
if (save.DATA[save.L] == END) {
//
// The epochs at indices EEPIDX-1 and EEPIDX comprise
// the first two epochs of the right half of an
// interpolation window of size WNDSIZ. We need two
// fewer pad epochs to complete the right half of the
// window.
//
save.NPAD = ((save.WNDSIZ / 2) - 2);
} else {
//
// The epoch at EEPIDX is the first of the pad.
//
save.NPAD = ((save.WNDSIZ / 2) - 1);
}
//
// Update the final epoch index to include the pad. The index
// cannot exceed the mini-segment's packet count.
//
save.EEPIDX = intrinsics::MIN0(&[save.NPKT, (save.EEPIDX + save.NPAD)]);
//
// EEPIDX must always exceed BEPIDX; no interpolation
// interval may have zero length.
//
// When BEGIN is equal to END, and both are equal to the
// first epoch, and the window size is 2, NPAD will be
// -1, and EEPIDX will be 1. We don't want to allow
// EEPIDX to be less than 2.
//
save.EEPIDX = intrinsics::MAX0(&[save.EEPIDX, 2]);
//
// EEPIDX should always be in range at this point.
//
if ((save.EEPIDX < 2) || (save.EEPIDX > save.NPKT)) {
//
// This code should not be reached, since getting here
// implies the first epoch of the interval is greater than
// END.
//
DAFHFN(HANDLE, &mut save.SPK, ctx)?;
SETMSG(b"Input file: #. Segment address range: #:#. BEPIDX = #; EEPIDX = #; NPKT = #.Interval index is #; END is #.", ctx);
ERRCH(b"#", &save.SPK, ctx);
ERRINT(b"#", BADDR, ctx);
ERRINT(b"#", EADDR, ctx);
ERRINT(b"#", save.BEPIDX, ctx);
ERRINT(b"#", save.EEPIDX, ctx);
ERRINT(b"#", save.NPKT, ctx);
ERRINT(b"#", save.ENDIDX, ctx);
ERRDP(b"#", END, ctx);
SIGERR(b"SPICE(SPKSTRUCTUREERROR)", ctx)?;
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Write the packets of the last mini-segment.
//
{
let m1__: i32 = 1;
let m2__: i32 = save.EEPIDX;
let m3__: i32 = 1;
save.I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
save.BUFBAS = ((save.MINIB - 1) + ((save.I - 1) * save.PKTSIZ));
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + save.PKTSIZ),
save.DATA.as_slice_mut(),
ctx,
)?;
DAFADA(save.DATA.as_slice(), save.PKTSIZ, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.I += m3__;
}
}
//
// Write the epochs of the last mini-segment. Save the
// final epoch; we'll need it later.
//
{
let m1__: i32 = 1;
let m2__: i32 = save.EEPIDX;
let m3__: i32 = 1;
save.I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
save.BUFBAS = (((save.MINIB - 1) + (save.NPKT * save.PKTSIZ)) + (save.I - 1));
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 1),
save.DATA.as_slice_mut(),
ctx,
)?;
DAFADA(save.DATA.as_slice(), 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
if (save.I == save.EEPIDX) {
//
// The current interval is the last of the output
// segment. The interval end must be greater than or
// equal to END. It's safe to simply choose the final
// epoch as the interval end.
//
save.IVFEND = save.DATA[1];
}
save.I += m3__;
}
}
//
// Create epoch directories for the last mini-segment.
//
save.MINNPK = save.EEPIDX;
save.MINNDR = ((save.MINNPK - 1) / DIRSIZ);
{
let m1__: i32 = 1;
let m2__: i32 = save.MINNDR;
let m3__: i32 = 1;
save.I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
save.BUFBAS =
((((save.MINIB - 1) + (save.NPKT * save.PKTSIZ)) + (save.I * DIRSIZ)) - 1);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 1),
save.DATA.as_slice_mut(),
ctx,
)?;
DAFADA(save.DATA.as_slice(), 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.I += m3__;
}
}
//
// Finally, write out the control area for the last
// mini-segment.
//
DAFADA(&[(save.SUBTYP as f64)], 1, ctx)?;
DAFADA(&[(save.WNDSIZ as f64)], 1, ctx)?;
DAFADA(&[(save.MINNPK as f64)], 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Compute the size in DAF addresses of the last mini-segment.
// This is the sum of the sizes of the packet space, the
// epochs, the directories, and the control area.
//
save.MINFSZ = (((save.MINNPK * (save.PKTSIZ + 1)) + save.MINNDR) + CTRLSZ);
}
//
// We're done with the final mini-segment.
//
}
//
// We've transferred all of the data we need from mini-segments at
// indices BEGIDX : ENDIDX.
//***********************************************************************
//
// Part 5: Create segment-level data structures in the output segment
//
//***********************************************************************
//
// Write out the interval bounds for the new segment.
//
// Let NOIVL be the number of intervals in the output subset
// segment.
//
save.NOIVL = ((save.ENDIDX - save.BEGIDX) + 1);
//
// The first interval start time is IV1BEG.
//
DAFADA(&[save.IV1BEG], 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
//
// Write the remaining interval boundaries.
//
if (save.NOIVL == 1) {
//
// The final interval boundary is the stop time of
// the first interval.
//
DAFADA(&[save.IV1END], 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
} else {
//
// There are multiple output mini-segments. There is either
// a non-empty middle group, a final mini-segment, or both.
//
// Set the upper bound of the interval boundary transfer loop.
//
if save.FINAL {
//
// We'll transfer all interval start times up to,
// but not including, the final one.
//
save.UB = (save.NOIVL - 1);
} else {
//
// There's no mini-segment following the middle group.
//
// Transfer all start times of the middle group, plus
// the end time of the last interval of the middle
// group.
//
save.UB = (save.NOIVL + 1);
}
//
// Transfer interval boundaries from the middle group.
//
{
let m1__: i32 = 2;
let m2__: i32 = save.UB;
let m3__: i32 = 1;
save.I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
save.BUFBAS = ((save.IVLBAS + (save.BEGIDX - 1)) + (save.I - 1));
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 1),
save.DATA.as_slice_mut(),
ctx,
)?;
DAFADA(save.DATA.subarray(1), 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.I += m3__;
}
}
//
// If the "final" mini-segment exists, we haven't
// transferred its interval boundaries. Do it now.
//
if save.FINAL {
//
// The start and end times of the last output interpolation
// interval are stored in IVFBEG and IVFEND.
//
// Note that IVFBEG was initialized after the first output
// mini-segment was written, and it was updated if necessary
// in the block of code that transferred the middle group.
//
DAFADA(&[save.IVFBEG], 1, ctx)?;
DAFADA(&[save.IVFEND], 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
}
}
//
// The interval boundaries have been written.
//
// Create an interval boundary directory for the new segment. Every
// boundary whose index relative to BEGIDX-1 is multiple of DIRSIZ
// becomes a directory entry, unless that entry has no successors.
// This implies that the interval bounds to be read belong to the
// range
//
// BEGIDX + 1 : ENDIDX - 1
//
// This implies that we can read all of the directory entries
// from the input segment; we won't use as directory entries
// the initial or final interval bounds of the output segment.
//
// Since the number of epoch boundaries is NOIVL + 1, the directory
// count is
//
// ( ( NOIVL + 1 ) - 1 ) / DIRSIZ
//
//
save.NSDIR = (save.NOIVL / DIRSIZ);
{
let m1__: i32 = 1;
let m2__: i32 = save.NSDIR;
let m3__: i32 = 1;
save.I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
//
// Look up the interval boundary at offset I*DIRSIZ from
// the boundary index BEGIDX-1.
//
save.BUFBAS = (((save.IVLBAS + (save.BEGIDX - 1)) + (save.I * DIRSIZ)) - 1);
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 1),
save.DATA.as_slice_mut(),
ctx,
)?;
//
// Write this directory entry to the output segment.
//
DAFADA(save.DATA.as_slice(), 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.I += m3__;
}
}
//
// Write out mini-segment pointers for the new segment.
//
// The first output mini-segment ranges from relative
// addresses 1 : MIN1SZ.
//
DAFADA(&[(1 as f64)], 1, ctx)?;
if (save.NOIVL == 1) {
//
// The next pointer indicates the first address after the
// mini-segment, whether or not there is another mini-segment.
//
// Note that MIN1SZ was initialized after the first output
// mini-segment was written.
//
DAFADA(&[((save.MIN1SZ + 1) as f64)], 1, ctx)?;
} else {
//
// There are multiple output mini-segments. There is either
// a non-empty middle group, a final mini-segment, or both.
//
// We can obtain from the input segment the sizes of the
// mini-segments that were copied whole.
//
save.START = (save.MIN1SZ + 1);
//
// Set the upper bound of the mini-segment pointer transfer loop.
//
if save.FINAL {
//
// We'll transfer all mini-segment start pointers up to and
// including the start pointer of the final output
//
save.UB = save.NOIVL;
} else {
//
// The middle group is non-empty, and there's no mini-segment
// following the middle group.
//
// Write all start pointers of the middle group, plus the end
// pointer of the last mini-segment of the middle group. The
// end pointer is the successor of the last DAF address
// occupied by the mini-segment.
//
save.UB = (save.NOIVL + 1);
}
//
// Write mini-segment pointers from the middle group.
//
// All of the middle group pointers of the output segment will be
// shifted relative to the corresponding pointers of the input
// segment. The shift reflects the sum of the sizes of the input
// mini-segments preceding the first one from which data were
// transferred, as well as the amount by which the first output
// mini-segment "shrank" relative to the mini-segment from which
// it was created. The shift equals the difference between the
// final address of the first output mini-segment and the final
// address of the input mini-segment at index BEGIDX.
//
{
let m1__: i32 = 2;
let m2__: i32 = save.UB;
let m3__: i32 = 1;
save.I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
//
// Look up the Ith start pointer.
//
save.BUFBAS = ((save.PTRBAS + (save.BEGIDX - 1)) + (save.I - 1));
DAFGDA(
HANDLE,
(save.BUFBAS + 1),
(save.BUFBAS + 1),
save.DATA.as_slice_mut(),
ctx,
)?;
//
// On the first pass, compute the pointer shift amount.
//
if (save.I == 2) {
save.SHIFT = ((save.MIN1SZ + 1) - intrinsics::IDNINT(save.DATA[1]));
}
save.START = (intrinsics::IDNINT(save.DATA[1]) + save.SHIFT);
DAFADA(&[(save.START as f64)], 1, ctx)?;
if FAILED(ctx) {
CHKOUT(b"SPKS19", ctx)?;
return Ok(());
}
save.I += m3__;
}
}
//
// If the "final" mini-segment exists, we haven't
// transferred its end pointer. Do it now.
//
if save.FINAL {
//
// MINFSZ is the size of the final output mini-segment.
//
// The end pointer of the last output mini-segment is
// START+MINFSZ. The end pointer is the successor of the last
// DAF address of the mini-segment.
//
// Write the pointer.
//
DAFADA(&[((save.START + save.MINFSZ) as f64)], 1, ctx)?;
}
}
//
// Write the interval count and selection flag to the
// new segment.
//
DAFADA(&[(save.ISEL as f64)], 1, ctx)?;
DAFADA(&[(save.NOIVL as f64)], 1, ctx)?;
CHKOUT(b"SPKS19", ctx)?;
Ok(())
}