File size: 80,039 Bytes
5ab75a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function [Pc, out] = Pc3D_Hall(r1,v1,C1,r2,v2,C2,HBR,params)
% Pc3D_Hall - Calculate the Hall (2021) approximation for the probability
%             of collision between two satellites, given input states and
%             covariances at the nominal time of closest approach (TCA).
%
% Syntax: [Pc, out] = Pc3D_Hall(r1,v1,C1,r2,v2,C2,HBR);
%         [Pc, out] = Pc3D_Hall(r1,v1,C1,r2,v2,C2,HBR,params);
%
% =========================================================================
%
% Copyright (c) 2023-2025 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Description:
%
%   This function uses the Hall (2021) 3D-Nc method to calculate the
%   probability of collision (Pc) between two space objects for a single
%   conjunction, given input states and covariances at the nominal TCA.
%
%   The 3D-Nc method locally approximates both satellite trajectories 
%   using two-body equations of motion, and therefore accounts for their
%   curvature. This distingushes the 3D-Nc method of calculating Pc values
%   from the more widely used 2D-Pc methods of Foster and Estes (1992) or,
%   equivalently, Akella and Alfriend (2000), which locally approximate the
%   trajecories as linear.
%
%   For single, well-isolated conjunctions the expected number of
%   collisions (Nc) equals the collision probability (Pc), as explained by
%   Hall (2021).
%
%   This function estimates the collision rate, Ncdot, over the time span
%   during which it contributes appreciably to the expected number of
%   collisions, Nc (which equals Pc for a single conjunction, as explained
%   in detail by Hall 2021). The ephemeris time points for this time span
%   Ta <= t < Tb are iteratively refined so that the integral of Ncdot over
%   time meets a set of internally-specified accuracy/convergence criteria.
%
%   At each time point during the calculation, the peak overlap position
%   (POP) of the primary and secondary rel. position PDFs must be
%   iteratively estimated. If this process converges for all time points
%   for which Ncdot contributes appreciably to Nc, then the overall Pc
%   estimation process is deemed to converge.
%
%   All input/output units are meters/seconds/radians, unless otherwise
%   noted.
%
% =========================================================================
%
% Input:
%
%    r1 - Primary object's ECI position vector (m)             [3x1 or 1x3]
%
%    v1 - Primary object's ECI velocity vector (m/s)           [3x1 or 1x3]
%
%    C1 - Primary object's ECI covariance matrix (m position units)   [6x6]
%
%    r2 - Secondary object's ECI position vector (m)           [3x1 or 1x3]
%
%    v2 - Secondary object's ECI velocity vector (m/s)       [3x1] or [1x3]
%
%    C2 - Secondary object's ECI covariance matrix (m position units) [6x6]  
%
%    HBR - Combined primary+secondary hard-body radii.   [1x1, 1x2, or 2x1]
%          If two elemens are passed in, it is assumed
%          each element separately represents each
%          object's individual HBR. In this case, the HBRs
%          are added together to create a single unified
%          combined HBR.
%
%    params - (Optional) Auxilliary input parameter structrure, described
%             in detail in function default_params_Pc3D_Hall.m.
%
% =========================================================================
%
% Output:
%
%   Pc - The estimated conjunction-integrated Pc value (i.e., Nc value).
%        A NaN Pc indicates POP convergence failed at one (or more) of the
%        ephemeris time points.
%
%   out - An auxilliary output structure which contains a large number of
%         quantities from the Hall (2021) 3D-Nc method calculation, within
%         the following fields (listed roughly in decreasing order of
%         importance):
%
%     converged = Convergence flag.
%
%     HBR = The combined primary+secondary HBR used for the calculation.
%
%     Nc = The estimated expected number of collisions for the conjunction
%          (for converged estimates, this equals Pc).
%
%     Neph = Number of eph. time points created in the refinement process.
%
%     nrefine = Number of refinements used to find the eph. time points.
%
%     Tmin = Begin time of the ephemeris time points relative to TCA (s).
%
%     Tmax = End time of the ephemeris time points relative to TCA (s).
%
%     TaConj = Begin time (Ta) of effective conj. duration from TCA (s).
%
%     TbConj = End time (Ta) of effective conj. duration from TCA (s).
%              Note: Ta and Tb are estimated in a way to be comparable to a
%                    C12b duration using gamma = 1e-6.
%
%     Tmin_limit = Begin time (TA) of the encounter segment rel. to TCA (s)
%
%     Tmax_limit = End time (TB) of the encounter segment rel. to TCA (s).
%
%     TpeakConj = Estimated time of peak collision rate rel. to TCA (s).
%
%     Teph = Refinement ephemeris time points relative to TCA (s). [1xNeph]
%
%     Ncdot = Collision rate (dNc/dt) at ephemeris times.          [1xNeph]
%
%     Nccum = Cumulative collision number at ephemeris times.      [1xNeph]
%
%     MD2eff = Effective Maha.dist. at the eph. times (HBR-center) [1xNeph]
%
%     MS2eff = Effective Maha.dist. at the eph. times              [1xNeph]
%              (HBR-surface).
%
%     MD2min = Min MD2eff for all of the eph. times.
%
%     MS2min = Min MDSeff for all of the eph. times.
%
%     Ncmaxima = Number of Ncdot maxima found for all of the eph. times.
%
%     Ncminima = Number of Ncdot mimima found for all of the eph. times.
%
%     NcdotDurCut = Cutoff factor for Ncdot span defining eff. duration.
%
%     NccumDurCut = Cutoff factor for Nccum span defining eff. duration.
%
%     Ncmaxcut = Number of Ncdot maxima found above cutoff for times.
%
%     Xmean10/Xmean20 = Pos/vel state vector at initial time (TCA)    [6x1]
%                       for pri/sec
%
%     Pmean10/Pmean20 = Pos/vel covariance at initial time (TCA) for  [6x1]
%                       pri/sec
%
%     Emean10/Emean20 = Equinoctial element vector at initial time    [6x1]
%                       for pri/sec
%
%     Jmean10/Jmean20 = Jacobian matrix dX(t0)/dE(t0) for pri/sec     [6x6]
%
%     Kmean10/Kmean20 = Inverse of Jmean10/Jmean20                    [6x6]
%
%     Qmean10/Qmean20 = Equinoctial covariance at initial time (TCA)  [6x6]
%                       for pri/sec
%
%     Xmean1T/Xmean2T = Pos/vel state vectors at ephemeris times   [6xNeph]
%                       for pri/sec
%
%     Jmean1T/Jmean2T = Jacobian matrices dX(t)/dE(t0) for       [6x6xNeph]
%                       pri/sec
%
%     POPconv = Convergence flags for PeakOverlapPos function.     [1xNeph]
%
%     POPiter = Iteration numbers for PeakOverlapPos function.     [1xNeph]
%
%     POPfail = Failure indicators for PeakOverlapPos function.    [1xNeph]
%
%     xs1/xs2 = Expansion-center pri/sec states from               [6xNeph]
%               PeakOverlapPos.
%
%     Js1/Js2 = Expansion-center pri/sec Jacobians from          [6x6xNeph]
%               PeakOverlapPos.
%
%     Ncdot_SmallHBR = Coll rate at eph. times in small-HBR limit. [1xNeph]
%
%     Nccum_SmallHBR = Cum. collision number in small-HBR limit.   [1xNeph]
%
%     Nc_SmallHBR = Cum.coll.num. at final eph.time in small-HBR limit.
%
%     period1/period2 = Orbital period of primary/secondary (s).
%
%     Texpand = Expansion factor used to generate the ephemeris.
%
%     tau0 = C12b conjunction duration begin time (for the specified gamma)
%            relative to TCA.
%
%     tau1 = C12b conjunction duration end time (for the specified gamma)
%            relative to TCA.
%
%     taum, dtau = The midpoint and span of the C12b conjunction duration.
%
%     tau0_gam1, tau1_gam1 = Conj. duration bounds for gamma = 1 relative
%                            to TCA.
%
%     covXcorr_corrections_applied = true if covariance cross correlation
%                                    corrections were applied
%
%     params = A copy of the parameters structure used for the calculation.
%
% =========================================================================
% 
% References:
%
%    Hall, D. T. "Expected Collision Rates for Tracked Satellites"
%    Journal of Spacecraft and Rockets, Vol.58. No.3, pp.715-728, 2021.
%
%    Foster, J. L., and Estes, H. S., "A Parametric Analysis of Orbital
%    Debris Collision Probability and Maneuver Rate for Space Vehicles,"
%    NASA/JSC-25898, Aug. 1992.
%
%    Akella, M. R., and Alfriend, K. T., "The Probability of Collision
%    Between Space Objects," Journal of Guidance, Control, and Dynamics,
%    Vol. 23, No. 5, pp. 769-772, 2000.
%
%    Hall, D. T., et. al., "High-fidelity Collision Probabilities 
%    Estimated Using Brute Force Monte Carlo Simulations," 
%    AAS 18-244, 2018
%
%    Casali, S. J., et. al. (2018) "Effect of Cross-Correlation of
%    Orbital Error on Probability of Collision Determination" AAS 18-272.
%
%    Coppola, V. T. (2012a) "Including Velocity Uncertainty in the
%    Probability of Collision Between Space Objects" AAS 12-247.
%
%    Coppola, V. T. (2012b) "Evaluating the Short Encounter Assumption
%    of the Probability of Collision Formula" AAS 12-248.
%
% =========================================================================
%
% Dependencies:
%
%  default_params_Pc3D_Hall.m
%  conj_bounds_Coppola.m
%  orbit_period.m
%  convert_cartesian_to_equinoctial.m
%  jacobian_E0_to_Xt.m 
%  jacobian_equinoctial_to_cartesian.m
%  cov_make_symmetric.m
%  jacobian_E0_to_Xt.m
%  PeakOverlapPos.m
%  CovRemEigValClip.m
%  getLebedevSphere.m
%  multiprod.m
%  multitransp.m
%
% =========================================================================
%
% Subfunctions:
%
%  Ncdot_integrand
%  Ncdot_quad2D_integrand
%  add_eph_times
%
% =========================================================================
%
% Disclaimer:
%
%    No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY
%    WARRANTY OF ANY KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY,
%    INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE
%    WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
%    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR FREEDOM FROM
%    INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR
%    FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO
%    THE SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN ANY MANNER,
%    CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT
%    OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY
%    OTHER APPLICATIONS RESULTING FROM USE OF THE SUBJECT SOFTWARE.
%    FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND LIABILITIES
%    REGARDING THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE,
%    AND DISTRIBUTES IT "AS IS."
%
%    Waiver and Indemnity:  RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS
%    AGAINST THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND
%    SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT.  IF RECIPIENT'S USE OF
%    THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES,
%    EXPENSES OR LOSSES ARISING FROM SUCH USE, INCLUDING ANY DAMAGES FROM
%    PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S USE OF THE SUBJECT
%    SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS THE UNITED
%    STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY
%    PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW.  RECIPIENT'S SOLE
%    REMEDY FOR ANY SUCH MATTER SHALL BE THE IMMEDIATE, UNILATERAL
%    TERMINATION OF THIS AGREEMENT.
%
% =========================================================================
%
% Initial version: Jan 2020;  Latest update: Aug 2025
%
% ----------------- BEGIN CODE -----------------

persistent pathsAdded
if isempty(pathsAdded)
    [p,~,~] = fileparts(mfilename('fullpath'));
    s = what(fullfile(p,'Pc3D_Hall_Utils')); addpath(s.path);
    s = what(fullfile(p,'Utils')); addpath(s.path);
    s = what(fullfile(p,'../Utils/OrbitTransformations')); addpath(s.path);
    s = what(fullfile(p,'../Utils/AugmentedMath')); addpath(s.path);
    pathsAdded = true;
end

% Initializations and defaults
Nargin = nargin;

% Non-verbose processing by default
if (Nargin < 8) || isempty(params)
    params.verbose = false;
end

% Set any parameters that remain to be defined to their defaults, but leave
% any that are already defined unchanged. Also calculate the Lebedev
% quadrature unit-vectors and weights, but only as required.
params = default_params_Pc3D_Hall(params);

% Debug plotting flag (used only for revising the conjunction duration
% refinement and other algorithms)
if ~isfield(params,'debug_plotting') || isempty(params.debug_plotting)
    params.debug_plotting = 0;
end
refinement_debug_plotting = params.debug_plotting;
if refinement_debug_plotting; params.verbose = true; end

% Copy parameters to the output structure
out.params = params;

% Ensure primary/secondary state vectors are column vectors
r1 = reshape(r1,3,1); v1 = reshape(v1,3,1);
r2 = reshape(r2,3,1); v2 = reshape(v2,3,1);

% Ensure primary/secondary covariances are 6x6
if ~isequal(size(C1),[6 6])
    error('Pc3D_Hall:badCovariance','C1 covariance must be a 6x6 matrix');
end
if ~isequal(size(C2),[6 6])
    error('Pc3D_Hall:badCovariance','C2 covariance must be a 6x6 matrix');
end

% Process the input HBR

N_HBR = numel(HBR);

if (N_HBR == 2)
    % Process individual primary and secondary HBR values
    %  HBR(1) = Primary   HBR
    %  HBR(2) = Secondary HBR
    if (min([HBR(1) HBR(2)]) < 0)
       error('Pc3D_Hall:InvalidHBR', 'Both HBR values must be nonnegative.');
    end  
    HBR = HBR(1)+HBR(2);
elseif (N_HBR ~= 1)
    error('Pc3D_Hall:InvalidHBR', 'Input HBR must have one or two elements.');
end

if (HBR <= 0)
    error('Pc3D_Hall:InvalidHBR', 'Combined HBR value must be positive.');
end
out.HBR = HBR;

% Return 1 for Pc if HBR is infinite
if isinf(HBR)
    Pc = 1;
    return;
end

% Initialize other misc variables required for the Nc3D calculation
H = HBR/1e3; H2 = H^2; % HBR in km
Lclip = (params.Fclip*H)^2;
twopi = 2*pi; twopicubed = twopi^3;
% I6x6 = eye(6,6); 

% MD2 value that is sufficiently large so that Ncdot can be assumed
% to be zero. Specifically, this is the smallest value for
% which isequal(exp(-MD2/2),0) returns a true value, which
% is MD2cut = 1491 to the nearest integer.
MD2cut = 1491;

% Ncdot value that is sufficiently tiny so that Ncdot can be assumed
% to be zero, which is a bit larger than realmin('double') = 2.2251e-308,
% and used to add stability to the refinement algorithm
Ncdottiny = 1e-300;

% Retrograde orbit processing
% Reorient to handle conjunctions involving retrograde orbits, which occur
% for the Alfano (2009) test cases, but have never been encountered with
% actual CARA conjunctions. This eliminates the occurence of retrograde
% orbits by reorienting the (x,y,z) axes of the reference frame.
if params.RetrogradeReorientation > 0
    [r1,v1,C1,r2,v2,C2,RRout] = ...
        RetrogradeReorientation(r1,v1,C1,r2,v2,C2,params);
    out.RetrogradeReorientation = RRout.Reoriented;
else
    out.RetrogradeReorientation = false;
end

% Mean equinoctial matrices at nominal TCA for primary and secondary
[out.Xmean10,out.Pmean10,out.Emean10,out.Jmean10,out.Kmean10, ...
    out.Qmean10,out.Qmean10RemStat,out.Qmean10Raw,            ...
    out.Qmean10Rem,C1Rem] = EquinoctialMatrices(r1,v1,C1,     ...
    params.remediate_NPD_TCA_eq_covariances);
[out.Xmean20,out.Pmean20,out.Emean20,out.Jmean20,out.Kmean20, ...
    out.Qmean20,out.Qmean20RemStat,out.Qmean20Raw,            ...
    out.Qmean20Rem,C2Rem] = EquinoctialMatrices(r2,v2,C2,     ...
    params.remediate_NPD_TCA_eq_covariances);

% Return unconverged if any equinoctial elements are undefined
if any(isnan(out.Emean10)) || any(isnan(out.Emean20))
    Pc = NaN;
    out.converged = false;
    return;
end

% Get covariance cross correlation parameters
if params.apply_covXcorr_corrections
    [XCprocessing,sigp,Gp,sigs,Gs] = get_covXcorr_parameters(params);
    if XCprocessing
        % Product of DCP sigma values
        sigpXsigs = sigp*sigs;
        % DCP 6x1 sensitivity vectors for TCA pos/vel states (km units)
        Gp = Gp' / 1e3; % Primary

        Gs = Gs' / 1e3; % Secondary
        % Reorient the 6x1 sensitivity vectors, if required
        if out.RetrogradeReorientation
            Gp = RRout.M6 * Gp;
            Gs = RRout.M6 * Gs;
        end
        % DCP 6x1 sensitivity vectors for TCA equinoctial states
        GEp = out.Kmean10*Gp;
        GEs = out.Kmean20*Gs;
    end
else
    XCprocessing = false;
end
out.covXcorr_corrections_applied = XCprocessing;

% Construct the relative state and covariance at nominal TCA
r = r2-r1; v = v2-v1;

% Apply covariance cross correlation correction to rel. PV state covariance
if XCprocessing
    % See C18 eq 11
    C = C1Rem + C2Rem - sigpXsigs * (Gs*Gp'+Gp*Gs');
else
    C = C1Rem + C2Rem;
end

% Calculate the linear-trajectory conjunction bounds (tau0,tau1)
% for the specified value(s) of gamma, as described in C12b.
[out.tau0,out.tau1,out.tau0_gam1,out.tau1_gam1] = ...
    conj_bounds_Coppola(params.gamma,HBR,r,v,C,params.verbose);

% Check for good values of (tau0,tau1), because many important 
% quantities will be derived from these.  Some of these can occur when the
% rel.pos. covariance matrix C(1:3,1:3) is not positive definite 
if isinf(out.tau0) || isinf(out.tau1)
    warning('Pc3D_Hall:InvalidTimeBounds', 'Coppola conjunction time bound(s) have infinite value(s)');
elseif isnan(out.tau0) || isnan(out.tau1)
    error('Pc3D_Hall:InvalidTimeBounds', 'Coppola conjunction time bound(s) have NaN value(s).');
elseif (imag(out.tau0) ~= 0) || (imag(out.tau1) ~= 0)
    error('Pc3D_Hall:InvalidTimeBounds', 'Coppola conjunction time bound(s) have imaginary component(s).');
elseif (out.tau0 >= out.tau1)
    error('Pc3D_Hall:InvalidTimeBounds', 'Coppola conjunction time bounds span a nonpositive interval.');
end

% Define the linear-trajectory conjunction midpoint and duration
% (i.e.,tau middle and width)
out.taum = (out.tau1+out.tau0)/2;
out.dtau = (out.tau1-out.tau0);

% Calculate the orbital periods, and min orbital period
out.period1 = orbit_period(r1,v1,params.GM);
out.period2 = orbit_period(r2,v2,params.GM);
period0 = min(out.period1,out.period2);
half_period0 = 0.5*period0;

% Initialize parameters for PeakOverlapPos function
PAR.verbose = params.verbose > 1;
PAR.Fclip   = params.Fclip;
PAR.maxiter = params.POPmaxiter;

% Create the the expanded ephemeris conjunction duration time bounds

NTexpand = numel(params.Texpand);

switch NTexpand
    
    case 0 % No value indicates numerical search for conjunction duration
        
        bad_Texpand = false;
        
        % If either limit is empty then estimate
        % the undefined ones using two-body motion

        Tmin_limit = params.Tmin_limit;
        Tmax_limit = params.Tmax_limit;
        
        if isempty(Tmin_limit) || isempty(Tmax_limit)
            
            % Estimate conjunction segment bounds using two-body motion
        
            Ns = 51;
            Ts = min(1.10*max(out.period1,out.period2), ...
                     2.20*min(out.period1,out.period2));
            Ts = linspace(-Ts,Ts,Ns);

            dr2 = delta_r2_equin(Ts, ...
                out.Emean10(1),out.Emean10(2),out.Emean10(3), ...
                out.Emean10(4),out.Emean10(5),out.Emean10(6), ...
                out.Emean20(1),out.Emean20(2),out.Emean20(3),...
                out.Emean20(4),out.Emean20(5),out.Emean20(6), ...
                params.GM);
            
            if refinement_debug_plotting > 0
                figure;
                plot(Ts,sqrt(dr2),'--+b');
            end

            % Find maxima in two-body primary-to-secondary mean distance
            [~,imax] = extrema(dr2,false,true);
            
            % Pick maxima bracketing the TCA or the Coppola midpoint
            Ia = max(imax(Ts(imax) < min(0,out.taum)));
            Ib = min(imax(Ts(imax) > max(0,out.taum)));

            if isempty(Ia) || isempty(Ib) || (Ia == 1) || (Ib == Ns)
                
                % If two-body solution fails, use half the min period
                Tmin_limit = -half_period0;
                Tmax_limit =  half_period0;
                % warning('Failed to find the |r2-r1| maxima bracketing TCA (1)');
                
            else
                
                % Refine the bracketing two-body maxima

                dr2fun = @(ttt)delta_r2_equin(ttt, ...
                    out.Emean10(1),out.Emean10(2),out.Emean10(3), ...
                    out.Emean10(4),out.Emean10(5),out.Emean10(6), ...
                    out.Emean20(1),out.Emean20(2),out.Emean20(3),...
                    out.Emean20(4),out.Emean20(5),out.Emean20(6), ...
                    params.GM);

                Ia = Ia-1;
                Ib = Ib+1;
                if (Ia < 1) || (Ib > Ns)
                    error('Failed to find the |r2-r1| maxima bracketing TCA');
                end
                
                tolX = period0/100;

                if refinement_debug_plotting > 0
                    [xmnma,ymnma,xmxma,ymxma,converged,nbisect,x,y,imnma,imxma] = ...
                        refine_bounded_extrema(dr2fun,Ts(Ia:Ib),dr2(Ia:Ib),[],100,2, ...
                        tolX,NaN,false,true,true); %#ok<ASGLU>
                    hold on;
                    plot(x,sqrt(y),':xr');
                    hold off;
                else
                    [xmnma,ymnma,xmxma,ymxma,converged,nbisect,x,y,imnma,imxma] = ...
                        refine_bounded_extrema(dr2fun,Ts(Ia:Ib),dr2(Ia:Ib),[],100,2, ...
                        tolX,NaN,false,false,false); %#ok<ASGLU>
                end
                
                if (numel(xmxma) < 2)
                    % If two-body refinement fails, use half the min period
                    Tmin_limit = -half_period0;
                    Tmax_limit =  half_period0;
                    % warning('Failed to find dr2 maxima bracketing TCA (2)');
                else
                    % Use limiting delta-r maximima for conjunction segment
                    % bounds
                    Tmin_limit = min(xmxma);
                    Tmax_limit = max(xmxma);
                end
                
                % If a usable input limit exists, then try to use it
                if ~isempty(params.Tmin_limit)
                    Tmin_limit = params.Tmin_limit;
                end
                if ~isempty(params.Tmax_limit)
                    Tmax_limit = params.Tmax_limit;
                end
                if (Tmin_limit >= Tmax_limit)
                    error('Pc3D_Hall:InvalidTimeLimits', 'Calculated min/max conjunction segment time limits invalid');
                end

            end
            
        end

        % Define the conjunction segment time limits
        if ~isempty(params.Tmin_limit)
            Tmin_limit = params.Tmin_limit;
        end
        if ~isempty(params.Tmax_limit)
            Tmax_limit = params.Tmax_limit;
        end
        out.Tmin_limit = Tmin_limit;
        out.Tmax_limit = Tmax_limit;
        
        % Define the initial duration bounds
        if isempty(params.Tmin_initial)
            % If empty, then use Coppola duration bound
            Tmin_initial = out.tau0;
        elseif (params.Tmin_initial == -Inf)
            % If infinite, then use conjunction segment bound
            Tmin_initial = Tmin_limit;
        else
            % Use the input initial limit
            Tmin_initial = params.Tmin_initial;
        end
        if isempty(params.Tmax_initial)
            % If empty, then use Coppola duration bound
            Tmax_initial = out.tau1;
        elseif (params.Tmax_initial == Inf)
            % If infinite, then use conjunction segment bound
            Tmax_initial = Tmax_limit;
        else
            % Use the input initial limit
            Tmax_initial = params.Tmax_initial;
        end
        out.Tmin_initial = Tmin_initial;
        out.Tmax_initial = Tmax_initial;

        % Initialize ephemeris number and time bounds
        out.Neph = params.Neph;        
        % Use clipped time bounds
        out.Tmin = Tmin_initial;
        out.Tmax = Tmax_initial;
        if (out.Tmax <= Tmin_limit) || (out.Tmin >= Tmax_limit)
            out.Tmin = Tmin_limit;
            out.Tmax = Tmax_limit;
        else
            out.Tmin = max(out.Tmin,Tmin_limit);
            out.Tmax = min(out.Tmax,Tmax_limit);
        end
        % % Use expanded time bounds
        % out.Tmin = min(Tmin_initial,Tmin_limit);
        % out.Tmax = max(Tmax_initial,Tmax_limit);
        
        % Flag to see if time bounds coincide with Coppola limits
        starting_at_Coppola_limits = ...
            (out.Tmin == out.tau0) & (out.Tmax == out.tau1);
        
        % Set up parameters for ephemeris refinement, used for infrequent
        % conjunctions with Ncdot peak not bounded by Coppola duration 

        % Max number of refinement steps
        Nrefinemax = 250;
        
        % Time step for Ncdot peak searching
        dTeph0 = (out.Tmax-out.Tmin)/2;
        Nrefine0 = Nrefinemax/3;
        dTeph0 = max([dTeph0 abs(Tmin_limit)/Nrefine0 abs(Tmax_limit)/Nrefine0]);
        
        Ncdottol     = 2e-1; % Tolerance for Ncdot peak searching
        Ncsumtol     = 2e-2; % Tolerance for Nc integration sum components
        Ncdotgam     = 1e-6; % Gamma for Ncdot span
        NcdotgamNeph = 51;   % Number of eph points for Ncdotgam span

        NcdotgamNeph = max(NcdotgamNeph,round((params.Neph-1)/2));
        Ncdotgam = max(Ncdotgam,params.gamma);
        x = sqrt(2)*erfcinv(Ncdotgam);
        Ncdotred = exp(-x^2/2)/sqrt(2*pi);
        
    case 1 % Single scalar indicates expansion factor
        
        % Expand (tau0,tau1) to create the the ephemeris time bounds as
        % specified by the Texpand parameter.
        
        if (params.Texpand <= 0) || ...
           isinf(params.Texpand) || ...
           isnan(params.Texpand)
            
            bad_Texpand = true;
            
        else
            
            bad_Texpand = false;
            
            % Ephemeris half width initially set to conjunction duration
            % half width
            
            taue = out.dtau/2;

            % Expanded eph. duration
            
            taue = params.Texpand*taue;
            
            % Define the time integration limits, which are also the
            % ephemeris begin and end times
            
            out.Tmin = out.taum-taue;   % Eph. begin time
            out.Tmax = out.taum+taue;   % Eph. end   time
            
            % Restrict eph. using half of the min. orbital period
            
            Tmin_limit = -half_period0;
            Tmax_limit =  half_period0;
            if params.Torblimit
                out.Tmin = max(out.Tmin,Tmin_limit);
                out.Tmax = min(out.Tmax,Tmax_limit);
            end

            % Define the number of ephemeris points

            if (params.Texpand <= 1)
                
                % No expansion specified
                
                out.Neph = params.Neph;
                
            else
                
                % Boost number of ephemeris points for half-widths longer
                % than a fraction of the min. orbital period

                boost = max(1,taue/half_period0) * params.Texpand;
                Neph_expand = ceil(boost*(params.Neph-1)+1);
                
                % Ensure an odd number of eph. points
                
                out.Neph = max(params.Neph,Neph_expand);
                if (mod(out.Neph,2) == 0)
                    out.Neph = out.Neph+1;
                end
                
            end
            
        end
        
    case 2 % Two-vector indicates explicit (Tmin,Tmax) bounds.
        
        if any(isinf(params.Texpand))  || ...
           any(isnan(params.Texpand))
       
            bad_Texpand = true;
            
        else
            
            % Adopt the specified (Tmin,Tmax) and Neph parameters.
            
            out.Tmin = min(params.Texpand);
            out.Tmax = max(params.Texpand);

            if (out.Tmin >= out.Tmax)
                bad_Texpand = true;
            else
                bad_Texpand = false;
            end

            Tmin_limit = -half_period0;
            Tmax_limit =  half_period0;
            
            if params.Neph < 0
                % Forced number of ephemeris points
                out.Neph = -params.Neph;
            else
                % Expanded number of ephemeris points
                Neph_expand = ...
                    ceil(((out.Tmax-out.Tmin)/out.dtau)*(params.Neph-1)+1);
                out.Neph = max(params.Neph,Neph_expand);
                if (mod(out.Neph,2) == 0)
                    out.Neph = out.Neph+1;
                end
            end

        end
        
    otherwise
        
        bad_Texpand = true;
    
end

if bad_Texpand
    error('Pc3D_Hall:InvalidTExpand', 'Invalid Texpand parameter');
end
out.Texpand = params.Texpand;

% Generate the ephemeris times
out.Teph = linspace(out.Tmin,out.Tmax,out.Neph);

% Calc pos/vel mean states and associated Jacobians at all ephemeris times
[out.Jmean1T,out.Xmean1T] = jacobian_E0_to_Xt(out.Teph,out.Emean10);
[out.Jmean2T,out.Xmean2T] = jacobian_E0_to_Xt(out.Teph,out.Emean20);

% Initialize output arrays
out.Ncdot = NaN(1,out.Neph);
out.LebNumb = NaN(1,out.Neph);
out.Ncdot_SmallHBR = NaN(1,out.Neph);
out.MD2eff = NaN(1,out.Neph);
out.MS2eff = NaN(1,out.Neph);
out.POPconv = true(1,out.Neph);
out.POPiter = zeros(1,out.Neph);
out.POPfail = zeros(1,out.Neph);
out.xs1  = NaN(6,out.Neph); 
out.Es01 = NaN(6,out.Neph);
out.Js1  = NaN(6,6,out.Neph); 
out.xs2  = NaN(6,out.Neph);
out.Es02 = NaN(6,out.Neph);
out.Js2  = NaN(6,6,out.Neph);
out.xu   = NaN(6,out.Neph); 
out.Ps   = NaN(6,6,out.Neph);

% Set up conjunction duration ephemeris refining

refine_ephemeris = (NTexpand == 0);
nrefine = 0; still_refining = true; need_eph_calc = true(1,out.Neph);

while still_refining
    
    if params.verbose
        disp(['Neph = ' num2str(out.Neph) ...
              ' nrefine = ' num2str(nrefine) ...
              ' need = ' num2str(sum(need_eph_calc))]);
    end

    % Loop to process all ephemeris time points

    for neph=1:out.Neph

        % Calculate the peak overlap position (POP), and associated quantities
        
        if need_eph_calc(neph)
            
            % Set flag indicating eph point has been calcualated
            need_eph_calc(neph) = false;
            
            % Calculate the POP
            PAR.verbose = false;
            [converged,~,~,~,POP] = PeakOverlapPos(out.Teph(neph), ...
                out.Xmean1T(:,neph),out.Jmean1T(:,:,neph),0,out.Emean10,out.Qmean10, ...
                out.Xmean2T(:,neph),out.Jmean2T(:,:,neph),0,out.Emean20,out.Qmean20, ...
                H,PAR);
            % if ~converged
            %     keyboard;
            % end

            % Store the convergence, iteration and failure indicators for output
            out.POPconv(neph) = converged;
            out.POPiter(neph) = POP.iteration;
            out.POPfail(neph) = POP.failure;
            
            % If the POP function converged, then calculate Nc values

            if converged

                % Store the expansion-center EQ & PV states and Jacobians
                out.xs1(:,neph)   = POP.xs1;
                out.Es01(:,neph)  = POP.Es01;
                out.Js1(:,:,neph) = POP.Js1;
                out.xs2(:,neph)   = POP.xs2;
                out.Es02(:,neph)  = POP.Es02;
                out.Js2(:,:,neph) = POP.Js2;

                % Calculate pos/vel covariances
                Ps1 = cov_make_symmetric(POP.Js1 * out.Qmean10 * POP.Js1');

                Ps2 = cov_make_symmetric(POP.Js2 * out.Qmean20 * POP.Js2');

                % Retrieve the mode points for the two pos/vel PDFs
                r1u = POP.xu1(1:3); v1u = POP.xu1(4:6);
                r2u = POP.xu2(1:3); v2u = POP.xu2(4:6);

                % Calculate the relative offset pos and vel mode values
                ru = r2u-r1u; vu = v2u-v1u;

                % Construct the joint pos/vel covariance
                if XCprocessing
                    % DCP 6x1 sensitivity vectors for cartesian pos/vel
                    % state at the current eph time - see Hall (2021) eq 57
                    GCp = POP.Js1*GEp;
                    GCs = POP.Js2*GEs;
                    Ps = Ps1 + Ps2 - sigpXsigs * (GCs*GCp'+GCp*GCs');
                else
                    Ps = Ps1 + Ps2;
                end
                
                % Calculate the relative pos/vel coviariance, extract the
                % submatrices, and calculate related quantities
                As = Ps(1:3,1:3); Bs = Ps(4:6,1:3); Cs = Ps(4:6,4:6);
                [~,~,~,~,~,Asdet,Asinv] = CovRemEigValClip(As,Lclip);
                Ns0 = nthroot(twopicubed*Asdet,-2);
                bs = Bs*Asinv; Csp = Cs-bs*Bs'; 

                

                % Store xu and Ps for output

                out.xu(:,neph) = [ru; vu];

                out.Ps(:,:,neph) = Ps;



                % Calculate the approximate Ncdot value, valid in the small HBR

                % limit (i.e., for HBR << all-other-relevant-length-scales)

                Asinv_ru = Asinv * ru;

                Ms = ru' * Asinv_ru;
                Ns = Ns0 * exp(-0.5*Ms);
                out.Ncdot_SmallHBR(neph) = H2 * pi * Ns * norm(vu);
                out.MD2eff(neph) = Ms;
                out.MS2eff(neph) = Ms-2*H*norm(Asinv_ru);
                
                % If MS2 is sufficiently small, then Ncdot can be assumed
                % to be zero, otherwise calculate Ncdot using unit-sphere
                % integration. (Note: MS2 is the first-order approximation
                % of the minimum MD^2 over the HBR-sphere surface)
                
                if out.MS2eff(neph) > MD2cut
                    
                    out.Ncdot(neph) = 0;
                    
                else

                    % Calculate the Ncdot value using numerical
                    % integration, over the unit sphere, which should match
                    % Ncdot_SmallHBR in the limit of HBR << min(sigma)

                    if params.use_Lebedev

                        % Use Lebedev for integration over the unit sphere
                        Pint = Ncdot_integrand(params.vec_Lebedev,ru,vu,Asinv, ...
                            H,bs,Csp,-log(Ns0),false);
                        szPint = size(Pint);
                        if (szPint(1) ~= 1); Pint = Pint'; end

                        out.Ncdot(neph) = H2 * Pint * params.wgt_Lebedev;

                        

                        % Calc number of Lebedev quad points contributing

                        % signficantly to the Ncdot sum. If too low,

                        % then the unit sphere integration can be

                        % inaccurate.

                        MaxPint = max(Pint);

                        out.LebNumb(neph) = sum(Pint > 1e-3*MaxPint);



                    else



                        % Define the anonymous function for the quad2d integrand

                        fun = @(ph,u)Ncdot_quad2d_integrand(ph,u, ...

                            ru,vu,Asinv,H,bs,Csp,-log(Ns0),false);

                        

                        % Perform the 2-D integration

                        % Pint = integral2(fun,0,twopi,-1,1);



                        % Perform the quad2d integration

                        [Pint,~] = quad2d(fun,0,twopi,-1,1, ...

                            'AbsTol',params.AbsTol,'RelTol',params.RelTol, ...

                            'MaxFunEvals',params.MaxFunEvals);

                        % % [integ, integ_unc] = quad2d(fun,0,twopi,-1,1, ...

                        % %     'AbsTol',0,'RelTol',params.RelTol,'MaxFunEvals',190, ...

                        % %     'FailurePlot',true);



                        % Ncdot value

                        out.Ncdot(neph) = H2 * Pint;

                        

                    end

                    

                    % Clip tiny Ncdot values for convergence stability

                    if out.Ncdot(neph) <= Ncdottiny

                        out.Ncdot(neph) = 0;

                    end

                    

                end



            end

            

        end



    end

    

    % Refine the ephemeris time points, if required

   

    if refine_ephemeris

        

        % Check if any of the PeakOverlapPos runs converged

        

        if max(out.POPconv) == 0

            

            % No converged PeakOverlapPos instances

            

            if (out.Tmin <= Tmin_limit) && (out.Tmax >= Tmax_limit)

                % Stop refining if time has been expanded to full encounter

                % segment limits, otherwise increase the time resolution

                % by increasing the number of ephemeris points

                if out.Neph <= 8*params.Neph

                    out.Neph = 2*out.Neph;

                else

                    still_refining = false;

                end

            else

                if isempty(params.Tmin_initial) || isempty(params.Tmax_initial)

                    % Using Coppola limits as initial bounds

                    if nrefine == 0

                        % Expand ephemeris to span full STEVI

                        stevi = max([abs(out.tau0) abs(out.tau1) out.dtau]);

                        out.Tmin = max(-stevi,Tmin_limit);

                        out.Tmax = min( stevi,Tmax_limit);

                    else

                        % Expand ephemeris to span full encounter segment

                        % limits

                        out.Tmin = Tmin_limit;

                        out.Tmax = Tmax_limit;

                    end

                else

                    % Expand ephemeris to span full encounter segment

                    % limits

                    out.Tmin = Tmin_limit;

                    out.Tmax = Tmax_limit;

                end

            end

                

            % Generate a new list of ephemeris times, and reinitialize the

            % associated arrays

            if still_refining

                out.Teph = linspace(out.Tmin,out.Tmax,out.Neph);

                need_eph_calc = true(1,out.Neph);

                % Calc pos/vel mean states and associated Jacobians at all ephemeris times

                [out.Jmean1T,out.Xmean1T] = jacobian_E0_to_Xt(out.Teph,out.Emean10);

                [out.Jmean2T,out.Xmean2T] = jacobian_E0_to_Xt(out.Teph,out.Emean20);

                % Initialize output arrays

                out.Ncdot = NaN(1,out.Neph);

                out.LebNumb = NaN(1,out.Neph);

                out.Ncdot_SmallHBR = NaN(1,out.Neph);

                out.MD2eff = NaN(1,out.Neph);

                out.MS2eff = NaN(1,out.Neph);

                out.POPconv = true(1,out.Neph);

                out.POPiter = zeros(1,out.Neph);

                out.POPfail = zeros(1,out.Neph);

                out.xs1  = NaN(6,out.Neph); 

                out.Es01 = NaN(6,out.Neph);

                out.Js1  = NaN(6,6,out.Neph); 

                out.xs2  = NaN(6,out.Neph);

                out.Es02 = NaN(6,out.Neph);

                out.Js2  = NaN(6,6,out.Neph);

                out.xu   = NaN(6,out.Neph); 

                out.Ps   = NaN(6,6,out.Neph);

            end

            

        else



            % Find the maximum Ncdot value for the current refinement of

            % the ephemeris

            [Ncdotmax,nmax] = max(out.Ncdot);



            % Cutoff for Ncdot reduction sought by refinements

            Ncdotcut = Ncdotmax*Ncdotred;



            % If Ncdotmax is zero, then use the time of the min Maha dist

            % as a proxy

            if (Ncdotmax == 0)

                [MD2min,nmax] = min(out.MD2eff);

            end



            if refinement_debug_plotting > 0

                if nrefine == 0

                    figure;

                end

                subplot(3,1,1);

                plot(out.Teph,out.MD2eff,'+-b');

                hold on;

                plot(out.Teph,out.MS2eff,'x:m');

                hold off

                if min(out.MS2eff) < 1500

                    ylim([min(out.MS2eff) min(1500,max(out.MS2eff))]);

                end

                ylabel('M_D^2');

                subplot(3,1,2);

                if any(out.Ncdot > 0)

                    semilogy(out.Teph,out.Ncdot,'*-b');

                else

                    plot(out.Teph,out.Ncdot,'*-b');

                end

                ylabel('dNc/dt');

                drawnow;

                if refinement_debug_plotting > 1

                    keyboard;

                end

            end



            % Ephemeris refinement logic



            if params.AccelerateRefinement      && ...

               (nrefine == 0)                   && ...

               starting_at_Coppola_limits       && ...

               (Ncdotmax > 0)                   && ...

               (out.Ncdot(1) < Ncdotcut)        && ...

               (out.Ncdot(out.Neph) < Ncdotcut) && ...

               all(out.POPconv > 0)

                % Most isolated conjunctions without Tmin_initial or

                % Tmax_initial conjunction time boundary limit parameters

                % specified on input will use this branch, if allowed, and

                % will need no further ephemeris refinement

                still_refining = false;

            elseif (nmax == 1) && (out.Teph(1) > Tmin_limit)

                % Shift lower if possible

                Tnew = max(out.Teph(1)-dTeph0,Tmin_limit);

                [out,need_eph_calc] = add_eph_times(Tnew,out,need_eph_calc);

            elseif (nmax == out.Neph) && (out.Teph(out.Neph) < Tmax_limit)

                % Shift higher if possible

                Tnew = min(out.Teph(out.Neph)+dTeph0,Tmax_limit);

                [out,need_eph_calc] = add_eph_times(Tnew,out,need_eph_calc);

            elseif (out.Ncdot(1) > Ncdotcut) && (out.Teph(1) > Tmin_limit)

                % Shift lower if possible

                Tnew = max(out.Teph(1)-dTeph0,Tmin_limit);

                [out,need_eph_calc] = add_eph_times(Tnew,out,need_eph_calc);

            elseif (out.Ncdot(out.Neph) > Ncdotcut) && (out.Teph(out.Neph) < Tmax_limit)

                % Shift higher if possible

                Tnew = min(out.Teph(out.Neph)+dTeph0,Tmax_limit);

                [out,need_eph_calc] = add_eph_times(Tnew,out,need_eph_calc);

            else

                % Timestep bisection refinements

                nmd = max(2,min(out.Neph-1,nmax));

                nlo = nmd-1; nhi = nmd+1;

                if (Ncdotmax == 0)

                    % Check if bisection near Maha dist minimum is required

                    if (out.MD2eff(nlo) > out.MD2eff(nhi))

                        nbs = nlo; nas = nhi;

                    else

                        nbs = nhi; nas = nlo;

                    end

                    dTbs = abs((out.Teph(nmd)-out.Teph(nbs)));

                    dTas = abs((out.Teph(nmd)-out.Teph(nas)));

                    if dTas > 3*dTbs

                        % Bisect large gap on opposite side of MD2min

                        Tnew = (out.Teph(nmd)+out.Teph(nas))/2;

                        [out,need_eph_calc] = add_eph_times(Tnew,out,need_eph_calc);

                    else

                        % Check if MD2 is converged to within relative and

                        % absolute tolerance

                        dMD2 = abs(out.MD2eff(nbs)-out.MD2eff(nmd));

                        if ((dMD2 < 1e-2) || (dMD2 < 1e-2*MD2min)) && ...

                           (dTbs < 1e-1*(out.Tmax-out.Tmin))

                            % Stop refining for small MD2 differences

                            still_refining = false;

                        else

                            % Bisect near peak

                            Tnew = (out.Teph(nmd)+out.Teph(nbs))/2;

                            [out,need_eph_calc] = add_eph_times(Tnew,out,need_eph_calc);

                        end

                    end

                else

                    

                    % Test new method for refining Ncdot peaks

                    new_method = true;

                    if new_method

                        

                        % Find all peaks in Ncdot that still need refining

                        dNcd = diff(out.Ncdot);

                        dNcm = [Inf dNcd];

                        dNcp = [-dNcd Inf];

                        ipeak = (out.Ncdot > 0) & (dNcm > 0) & (dNcp > 0);

                        % Bisect peaks that need refinement

                        ipeak = find(ipeak); Npeak = numel(ipeak);

                        % Limit Ncdot peaks to top four, to prevent noisy

                        % Ncdot curves from generating many insignificant 

                        % peaks

                        if Npeak > 4

                            [~,ipsrt] = sort(out.Ncdot(ipeak),'descend');

                            ipeak = ipeak(ipsrt);

                            ipeak = ipeak(1:4);

                            Npeak = 4;

                        end

                        % Generate new ephemeris times near peaks using

                        % bisection method

                        Tnew = [];

                        for npeak=1:Npeak

                            ipk = ipeak(npeak); ipkm1 = ipk-1; ipkp1 = ipk+1;

                            dNctol = Ncdottol*out.Ncdot(ipk);

                            % Check if interval preceding the peak needs

                            % bisection

                            if ipk > 1

                                ibs = ipkm1;

                                dNcbs = out.Ncdot(ipk)-out.Ncdot(ibs);

                                if dNcbs > dNctol

                                    % Bisect interval if above Ncdot

                                    % tolerance

                                    Tnew = cat(2,Tnew,(out.Teph(ipk)+out.Teph(ibs))/2);

                                elseif ipk < out.Neph && ...

                                    (out.Teph(ipk)-out.Teph(ipkm1)) >= 2*(out.Teph(ipkp1)-out.Teph(ipk))

                                    % Bisect interval if too long with

                                    % respect to interval on other side

                                    Tnew = cat(2,Tnew,(out.Teph(ipk)+out.Teph(ibs))/2);

                                end

                            end

                            % Check if interval following the peak needs

                            % bisection

                            if ipk < out.Neph

                                ibs = ipkp1;

                                dNcbs = out.Ncdot(ipk)-out.Ncdot(ibs);

                                if dNcbs > dNctol

                                    % Bisect interval if above Ncdot

                                    % tolerance

                                    Tnew = cat(2,Tnew,(out.Teph(ipk)+out.Teph(ibs))/2);

                                elseif ipk > 1 && ...

                                    (out.Teph(ipkp1)-out.Teph(ipk)) >= 2*(out.Teph(ipk)-out.Teph(ipkm1))

                                    % Bisect interval if too long with

                                    % respect to interval on other side

                                   Tnew = cat(2,Tnew,(out.Teph(ipk)+out.Teph(ibs))/2);

                                end

                            end

                        end

                        

                    else

                        

                        % Check if bisection near Ncdot peak is required

                        if (out.Ncdot(nlo) < out.Ncdot(nhi))

                            nbs = nlo;

                        else

                            nbs = nhi;

                        end

                        if abs(out.Ncdot(nmd)-out.Ncdot(nbs)) > Ncdottol*Ncdotmax

                            % Bisect near Ncdot peak

                            Tnew = (out.Teph(nmd)+out.Teph(nbs))/2;

                        else

                            Tnew = [];

                        end

                        

                    end



                    % If there are new ephemeris times, add them.

                    % Otherwise check other refinement requirements

                    if ~isempty(Tnew)

                        [out,need_eph_calc] = add_eph_times(Tnew,out,need_eph_calc);

                    else

                        % Check if bisection is needed due to large trapezoidal sum

                        % components in integration

                        df = diff(out.Teph);

                        dt = [df(1) df(1:out.Neph-2)+df(2:out.Neph-1) df(out.Neph-1)]/2;

                        Ncdt = out.Ncdot;

                        Ncdt(isnan(Ncdt)) = 0;

                        st = dt.*Ncdt;

                        sf = sum(st);

                        % Make debugging plot

                        if refinement_debug_plotting

                            subplot(3,1,3);

                            plot(out.Teph,st./sum(st),'+:');

                            ylabel('{\Delta}t dNc/dt');

                            title(['Nc = ' num2str(sf)]);

                            drawnow;

                        end

                        % Check for integration steps with large

                        % trapezoidal summation components

                        ndx = st > Ncsumtol*sf;

                        if any(ndx)

                            % Bisect the integration steps with large

                            % trapezoidal components

                            ndx = find(ndx); Nndx = numel(ndx);

                            Tnew = [];

                            for nn=1:Nndx

                                n = ndx(nn);

                                if (n == 1)

                                    if (out.Teph(1) > Tmin_limit)

                                        Tnew = cat(2,Tnew,out.Teph(n)-dTeph0);

                                    end

                                    Tnew = cat(2,Tnew,0.5*(out.Teph(n+1)+out.Teph(n)));

                                elseif (n == out.Neph)

                                    if (out.Teph(out.Neph) < Tmax_limit)

                                        Tnew = cat(2,Tnew,out.Teph(n)+dTeph0);

                                    end

                                    Tnew = cat(2,Tnew,0.5*(out.Teph(n-1)+out.Teph(n)));

                                else

                                    Tnew = cat(2,Tnew,0.5*(out.Teph(n-1)+out.Teph(n)));

                                    Tnew = cat(2,Tnew,0.5*(out.Teph(n+1)+out.Teph(n)));

                                end

                            end

                            [out,need_eph_calc] = add_eph_times(Tnew,out,need_eph_calc);

                        else

                            % Bisect integration steps above Ncdot cutoff

                            idx = find(out.Ncdot > Ncdotcut);

                            if isempty(idx)

                                % If none is above cutoff or zero, then 

                                % add no new integration times

                                Tnew = [];

                            else

                                % Calculate new integration times by

                                % bisection

                                if idx(1) > 1; idx = cat(2,idx(1)-1,idx); end

                                if idx(end) < out.Neph; idx = cat(2,idx,idx(end)+1); end

                                Nidx = numel(idx);

                                dtmax = (max(out.Teph(idx))-min(out.Teph(idx)))/NcdotgamNeph;

                                Tnew = [];

                                for nn=1:Nidx

                                    n = idx(nn);

                                    if (n > 1)

                                        if (out.Teph(n)-out.Teph(n-1)) > dtmax

                                            Tnew = cat(2,Tnew,(out.Teph(n)+out.Teph(n-1))/2);

                                        end

                                    end

                                    if (n < out.Neph)

                                        if (out.Teph(n+1)-out.Teph(n)) > dtmax

                                            Tnew = cat(2,Tnew,(out.Teph(n+1)+out.Teph(n))/2);

                                        end

                                    end

                                end

                            end

                            % Continue refining the integration sum if

                            % there are any new integration times

                            if isempty(Tnew)

                                still_refining = false;

                            else

                                [out,need_eph_calc] = add_eph_times(Tnew,out,need_eph_calc);

                            end

                        end



                    end

                end

            end



        end



        % Increment refinement counter

        if still_refining

            nrefine = nrefine+1;

        end



        % Stop refining if too many refinements have been performed

        if nrefine > Nrefinemax

            still_refining = false;

        end

        

    else

        

        % Not refining ephemeris

        still_refining = false;

        

    end

    

end



% If any conjunction duration refinements occurred, then reset the min/max

% ephemeris time limits

if nrefine > 0

    out.Tmin = min(out.Teph);

    out.Tmax = max(out.Teph);

end



% Output the min. effective MD^2 value and MS^2 value

% (MD^2 is measured from HBR-center; MS^2 from HBR surface)

out.MD2min = min(out.MD2eff);

out.MS2min = min(out.MS2eff);



% Output the cumulative and final collision numbers, calculated using

%  trapezoidal quadrature time-integration



NcdtSH = out.Ncdot_SmallHBR;

NcdtSH(isnan(NcdtSH)) = 0;

out.Nccum_SmallHBR = cumtrapz(out.Teph,NcdtSH);

out.Nc_SmallHBR = out.Nccum_SmallHBR(end);

if out.Nccum_SmallHBR <= params.Pc_tiny; out.Nccum_SmallHBR = 0; end



Ncdt = out.Ncdot;

Ncdt(isnan(Ncdt)) = 0;

out.Nccum = cumtrapz(out.Teph,Ncdt);

out.Nc = out.Nccum(end);

if out.Nc <= params.Pc_tiny; out.Nc = 0; end



% Convergence analysis



POPconv = out.POPconv > 0;

if all(POPconv)

    % All POP estimates converged

    Ncdotmax = max(out.Ncdot);

    % Check if the maximum Ncdot is finite for convergence evaluation

    if isinf(Ncdotmax)

        out.converged = false;

        out.Ncdotcut = NaN;

    else

        out.converged = true;

        out.Ncdotcut = Ncdotmax*Ncdotred;

    end

elseif ~any(POPconv)

    % No POP estimates converged

    out.converged = false;

    out.Ncdotcut = NaN;

else

    % Some POP estimtes converged and some did not

    % Find the maximum Ncdot value

    Ncdotmax = max(out.Ncdot);

    % Check for other convergence conditions

    if isinf(Ncdotmax)

        % Check if the maximum Ncdot is finite for convergence evaluation    

        out.converged = false;

        out.Ncdotcut = NaN;

    elseif any(out.POPfail >= 100)

        % Not converged because an undefined equinoctial orbit was

        % encountered

        out.converged = false;

        out.Ncdotcut = NaN;

    else

        % Cutoff for Ncdot reduction sought by integration step refinements

        out.Ncdotcut = Ncdotmax*Ncdotred;

        if Ncdotmax == 0

            if out.MS2min > MD2cut

                % Mark as converged zero values with all MS2eff values 

                % greater than limit

                out.converged = true;

            elseif (~isempty(params.Tmin_limit) || ~isempty(params.Tmax_limit))

                % If all POP convergences correspond to zero Ncdot values, and

                % if either of the time limits were specified on input, then

                % mark this special case as converged.

                out.converged = true;

            else

                out.converged = false;

            end

        else

            % Points at or above cutoff

            idx = find(out.Ncdot >= out.Ncdotcut);

            % If any of the points above cutoff are adjacent to and

            % unconverged POP point, then mark Pc calcualtion as

            % unconverged

            Nidx = numel(idx); out.converged = true;

            for nidx=1:Nidx

                iii = idx(nidx);

                if iii > 1 && (POPconv(iii-1) == 0)

                    out.converged = false;

                    break;

                end

                if iii < out.Neph && (POPconv(iii+1) == 0)

                    out.converged = false;

                    break;

                end

            end

        end

    end

end



% Calculate the average effective number of Lebedev quad points that

% contributed to the unit sphere sums

if out.converged && out.Nc > 0 && params.use_Lebedev

    LebNumb = out.LebNumb; LebNumb(isnan(LebNumb)) = 0;

    AvgLebNumb = cumtrapz(out.Teph,Ncdt.*LebNumb);

    out.AvgLebNumb = AvgLebNumb(end)/out.Nc;

    % if out.AvgLebNumb < max(1,0.0125*(params.deg_Lebedev/2))

    %     % Mark as unconverged because too few Lebedev

    %     % quadrature points contributed, meaning that the

    %     % unit-sphere integrand function was too sharply peaked

    %     out.converged = false;

    % elseif out.AvgLebNumb < max(1,0.025*(params.deg_Lebedev/2))

    %     warning('Pc3D could be inaccurate because sharply peaked unit-sphere integrands were detected');

    % end

end



% Add refinement status to convergence flag

if refine_ephemeris

    out.converged = out.converged & (nrefine < Nrefinemax);

end

out.nrefine = nrefine;



% Define output Pc value

if out.converged

    Pc = out.Nc;

    if isnan(Pc)

        error('Supposedly converged calculation yields Pc = NaN');

    else

        Pc = min(1,Pc); % Account for quadrature errors in large-HBR limit

    end

else

    % Use NaN to mark non-convergence on output

    Pc = NaN;

end



% Define the conjunction duration cutoff values

out.NcdotDurCut = Ncdotred; % Cutoff for Ncdot span defining eff. duration

out.NccumDurCut = Ncdotgam; % Cutoff for Nccum span defining eff. duration



% Calculate the number of interior Ncdot extrema

if ~out.converged

    

    % No extrema calculated for unconverged cases

    out.Ncmaxcut = NaN; out.Ncmaxima = NaN; out.Ncminima = NaN;

    out.Ncvalmaxima = NaN; out.NcvalNearTCA = NaN;

    % No conjunction duration limits for unconverged cases

    out.TaConj = NaN; out.TbConj = NaN; out.TpeakConj = NaN;

    

else



    % Count extrema for converged cases using small-HBR limit approximation

    % which is less noisy

    if out.Nc_SmallHBR == 0

        % Construct surrogate Ncdot curve using relative MD if required

        NcdtSH = exp(-0.5*(out.MD2eff-out.MD2min));

        NcdtSH(isnan(NcdtSH)) = 0;       

    end    

    % Calculate the minima and maxima

    [Ncmaxima,imax,~,~] = extrema(NcdtSH,true);

    out.Ncmaxima = numel(Ncmaxima);

    [~,~,Ncminima,imin] = extrema(NcdtSH,false);

    out.Ncminima = numel(Ncminima);

    

    % % Count extrema for converged cases

    % if out.Nc == 0

    %     % Construct surrogate Ncdot curve using relative MD if required

    %     Ncdt = exp(-0.5*(out.MD2eff-out.MD2min));

    %     Ncdt(isnan(Ncdt)) = 0;       

    % end    

    % % Calculate the minima and maxima

    % [Ncmaxima,imax,~,~] = extrema(Ncdt,true);

    % out.Ncmaxima = numel(Ncmaxima);

    % [~,~,Ncminima,imin] = extrema(Ncdt,false);

    % out.Ncminima = numel(Ncminima);

    

    % Calculate the Nc values corresponding to each maximum

    if out.Ncmaxima > 0

        if out.Nc == 0

            % Zero Ncdot means that the Nc values for the tallest peak and

            % the near-TCA peak are both also zero

            out.Ncvalmaxima = 0; out.NcvalNearTCA = 0;

        else

            % Calculate Nc values for tallest peak and near-TCA peak

            out.Ncvalmaxima = NaN(out.Ncmaxima,1);

            for iii=1:out.Ncmaxima

                jmin = imax(iii);

                for jjj=imax(iii):-1:1

                    if jjj == 1

                        jmin = jjj;

                        break;

                    elseif ~isempty(imin) && min(abs(jjj-imin)) == 0

                        jmin = jjj;

                        break;

                    end

                end

                jmax = imax(iii);

                for jjj=imax(iii):out.Neph

                    if jjj == out.Neph

                        jmax = jjj;

                        break;

                    elseif min(abs(jjj-imin)) == 0

                        jmax = max(1,jjj-1);

                        break;

                    end

                end

                if jmin == jmax

                    if jmin == 1

                        dTephA = 0.5*out.Teph(2);

                    else

                        dTephA = 0.5*(out.Teph(jmin)-out.Teph(jmin-1));

                    end

                    if jmax == out.Neph

                        dTephB = 0.5*(out.Teph(jmax)-out.Teph(jmax-1));

                    else

                        dTephB = 0.5*(out.Teph(jmax+1)-out.Teph(jmax));

                    end

                    Ncvalcum = out.Teph(jmin)*(dTephA+dTephB);

                else

                    Ncvalcum = cumtrapz(out.Teph(jmin:jmax),Ncdt(jmin:jmax));

                end

                out.Ncvalmaxima(iii) = Ncvalcum(end);

                % disp(['jmin = ' num2str(jmin) ...

                %      ' jcnt = ' num2str(imax(iii)) ...

                %      ' jmax = ' num2str(jmax) ...

                %      ' Tcnt = ' num2str(out.Teph(imax(iii))) ...

                %      ' Nc = ' num2str(out.Ncvalmaxima(iii)) ...

                %      ' (' num2str(100*out.Ncvalmaxima(iii)/out.Nc) '%)']);
            end
            % Find the Nc value for the maximum closest to TCA
            [~,jjj] = min(abs(out.Teph(imax)));
            out.NcvalNearTCA = out.Ncvalmaxima(jjj);
        end
    else
        out.Ncvalmaxima = NaN; out.NcvalNearTCA = NaN;
    end

    % Calculate the time that Ncdot peaks
    if out.Ncmaxima == 0
        out.TpeakConj = NaN;
    else
        [~,imax] = max(Ncdt);
        out.TpeakConj = out.Teph(imax);
    end
    
    % Calculate Ncdot cutoff
    Ncdtcut = max(Ncdt)*out.NcdotDurCut;
    % Number of maxima at or above cutoff
    out.Ncmaxcut = sum(Ncmaxima >= Ncdtcut);
    % Points at or above Ncdot cutoff
    idx = find(Ncdt >= Ncdtcut);
    out.TaConj = min(out.Teph(idx));
    if isempty(out.TaConj); out.TaConj = NaN; end
    out.TbConj = max(out.Teph(idx));
    if isempty(out.TbConj); out.TbConj = NaN; end
    if out.Nc > 0
        % Calculate swath with Nccum in the cutoff range
        Nccm = out.Nccum/out.Nc;
        ndx = Nccm > 0.5;
        Nccm(ndx) = 1-Nccm(ndx);
        ndx = find(Nccm > out.NccumDurCut);
        Nndx = numel(ndx);
        if (ndx(1) > 1); ndx(1) = ndx(1)-1; end
        if (ndx(Nndx) < out.Neph); ndx(Nndx) = ndx(Nndx)+1; end
        out.TaConj = min(out.TaConj,min(out.Teph(ndx)));
        out.TbConj = max(out.TbConj,max(out.Teph(ndx)));
    end    
    
end

% Conjunction duration fractions
out.TaFrac = out.TaConj/Tmin_limit;
out.TbFrac = out.TbConj/Tmax_limit;

if refinement_debug_plotting
    figure;
    xrng = plot_range(out.Teph,0.05);
    subplot(3,1,1);
    plot(out.Teph,out.Nccum,'-');
    xlim(xrng);
    title(['Nc = ' num2str(out.Nc) '  Ne = ' num2str(out.Neph) '  Nr = ' num2str(out.nrefine)]);
    subplot(3,1,2);
    plot(out.Teph,out.Ncdot,'+:');
    xlim(xrng);
    subplot(3,1,3);
    semilogy(out.Teph,out.Ncdot,'+:');
    xlim(xrng);
    keyboard;
end

return
end

% =========================================================================

function integrand = Ncdot_integrand(rht,mur,muv,Ainv,R,Q1,Q2,logZ,slow_method)

% Calculate the integrand for the Ncdot unit-sphere integral.

% Initialize output

sz = size(rht);
Nsz = numel(sz);

if (Nsz == 2)
    sz(1) = 1;
else
    sz = sz(2:end);
end

integrand = NaN(sz);

% Slow vs fast method

if slow_method

    % Number of elements in (ph,u) arrays

    N = prod(sz);

    % Check if velocity dispersion will be zero. Here 
    %   Q2 = C - B * Ainv * B'.
    
    zero_sig2 = (max(abs(Q2(:))) == 0);

    % Perform calculate for each of the elements
    
    sqrt2 = sqrt(2);
    sqrtpi = sqrt(pi);
    sqrt2pi = sqrt2*sqrtpi;
    
    num_nonpos_sig2 = 0;

    for n=1:N

        % Calculate rhat

        rhat = rht(:,n);
        
        % Calculate difference from center
        
        dr = R*rhat-mur;
        
        % Calculate the nu(rhat,t) factor.
        
        if zero_sig2
            
            % Limiting nu factor  as sigma -> 0+ (C12a eq 39)
            
            nu = max(0, -muv' * rhat);

            

        else



            % Positive sigma C12a eqs 31, 36 & 37. Here

            %   Q1 = B * Ainv;

            %   Q2 = C - B * Ainv * B'
        
            sig2 = rhat' * Q2 * rhat;



            % Handle nonpositive and positive sig2 values

            

            if (sig2 <= 0)

                % Limiting nu factor  as sigma -> 0+ (C12a eq 39)

                num_nonpos_sig2 = num_nonpos_sig2+1;

                nu = max(0, -muv' * rhat);
            else
                sig = sqrt(sig2);
                nu0 = rhat' * (muv + Q1 * dr);

                nus = nu0 / sig / sqrt2;

                H = exp(-nus^2)-sqrtpi*nus*erfc(nus);

                nu = sig * H / sqrt2pi;

            end



        end

        

        % Calculate the Mahalanobis distance squared



        MD2 = dr' * Ainv * dr;

        % Calculate the integrand, which is the MVN N3 function 

        neglogN3 = logZ + 0.5*MD2;
        integrand(n) = nu * exp(-neglogN3);

        % disp(['n, ph, u, i = ' num2str(n) ' ' num2str(ph(n)) ...
        %       ' ' num2str(u(n)) ' ' num2str(integrand(n))]);
          
        if isnan(integrand(n)) || isinf(integrand(n)) || (integrand(n) < 0)
            error('Bad Ncdot integrand');
        end
        
    end
    
    if (num_nonpos_sig2 > 0)
        warning([num2str(num_nonpos_sig2) ' of ' ...
            num2str(N) ...
            ' velocity sigma^2 factors are not positive.']);
    end
    
else
    
    % Allocate space ultimately for difference-from-center vectors,
    % i.e. dr = r-mur.  NOTE: initially this will be used to hold rhat
    % vectors, and later changed to dr vectors.
    
    sznew = cat(2,3,cat(2,1,sz));
    rhat = zeros(sznew);
    
    szmat = cat(2,1,cat(2,1,sz));    
    
    % Expand the rhat vector array
    
    rhat(1,1,:,:) = rht(1,:,:);
    rhat(2,1,:,:) = rht(2,:,:);
    rhat(3,1,:,:) = rht(3,:,:);
    
    % Calculate the dr = r-mur vectors
    
    dr = R*rhat - repmat(mur,szmat);
    
    % Check if velocity dispersion will be zero. Here 
    %   Q2 = C - B * Ainv * B'.
    
    zero_sig2 = (max(abs(Q2(:))) == 0);
    
    % Calculate the nu(rhat,t) factor.

    if zero_sig2

        % Limiting nu factor  as sigma -> 0+ (C12a eq 39)

        nu = -squeeze(multiprod(multitransp(repmat(muv,szmat)),rhat));
        
        % Ensure nu is not negative

        nu(nu < 0) = 0;

    else
        
        % Use the rhat vectors to calculate the nu(rhat,t) factors, 
        % using C12a eqs 31, 36 and 37.  Here
        %
        %   Q1 = B * Ainv;
        %   Q2 = C - B * Ainv * B'.

        % This segmented calculation:
        %
        % q = repmat(Q2,szmat);
        % product = multiprod(q,rhat);
        % sig2 = squeeze(multiprod(multitransp(rhat),product));
        %
        % replaced with the following:
        
        sig2 = squeeze(multiprod( ...
            multitransp(rhat),multiprod(repmat(Q2,szmat),rhat)));
        
        % Calculate nu values.  First find any nonpositive sigma^2 values,
        % and mark the associated sigma values with NaNs.

        nonpos_sig2 = (sig2(:) <= 0);        
        num_nonpos_sig2 = sum(nonpos_sig2);
        any_nonpos_sig2 = (num_nonpos_sig2 > 0);
        
        if any_nonpos_sig2
            sig = NaN(size(sig2));
            pos_sig2 = ~nonpos_sig2;
            sig(pos_sig2) = sqrt(sig2(pos_sig2));
            % warning([num2str(num_nonpos_sig2) ' of ' ...
            %     num2str(numel(sig2(:))) ...
            %     ' velocity sigma^2 factors are not positive.']);
        else
            sig = sqrt(sig2);
        end

        % This segmented calculation:
        %
        % q = repmat(Q1,szmat);        
        % product = repmat(muv,szmat) + multiprod(q,dr);
        % nu0 = squeeze(multiprod(multitransp(rhat),product));
        %
        % replaced with the following:
        
        nu0 = squeeze(multiprod(multitransp(rhat),repmat(muv,szmat) ...
            + multiprod(repmat(Q1,szmat),dr)));
        
        % Use nu0 to calculate nu
        
        sqrt2 = sqrt(2);
        sqrtpi = sqrt(pi);
        sqrt2pi = sqrt2*sqrtpi;
        
        nus = nu0 ./ (sig * sqrt2);
        H = exp(-nus.^2) - sqrtpi * (nus .* erfc(nus) );
        nu = sig .* H / sqrt2pi;

        % Account for the nonpositive sigma^2 values
        
        if any_nonpos_sig2
            
            if (Nsz == 2)
                
                nu_nonpos_sig2  = -muv' * rht(:,nonpos_sig2);

                nu_nonpos_sig2(nu_nonpos_sig2 < 0) = 0;

                nu(nonpos_sig2) = nu_nonpos_sig2;

                

            else

                

                % Size of nu array



                sznu = size(nu);



                % Find the single-index indices for the nonpositive sigma^2

                % values



                ndx_nonpos_sig2 = find(nonpos_sig2);



                % Loop over indices and fix the bad nu values resulting from

                % nonpositive sigma^2 values



                for nn=1:num_nonpos_sig2



                    % Get subscripts mapping into nu array



                    kk = ndx_nonpos_sig2(nn);

                    [ii,jj] = ind2sub(sznu,kk);



                    % Limiting nu factor  as sigma -> 0+ (C12a eq 39)                



                    rh = rht(:,ii,jj);



                    nu(ii,jj) = max(0, -muv' * rh);

                end
                
            end

        end
        
    end
    
    % Calculate the Mahalanobis distance squared
    
    % This segmented calculation:
    %
    % Ai = repmat(Ainv,szmat);
    % product = multiprod(Ai,dr);    
    % MD2 = squeeze(multiprod(multitransp(dr),product));
    %
    % replaced with the following:
    
    MD2 = squeeze(multiprod(multitransp(dr),multiprod(repmat(Ainv,szmat),dr)));
    
    % Calculate the integrand, which is the product of the MVN N3 function
    % and the nur(r,t) factor (C12a eq 36a).
    
    neglogN3 = logZ + 0.5*MD2;
    integrand = nu .* exp(-neglogN3);
    
end

return
end

% =========================================================================

function integrand = Ncdot_quad2d_integrand(ph,u,mur,muv,Ainv,R,Q1,Q2,logZ,slow_method)

% Calculate the integrand for the Ncdot unit-sphere integral, assuming that 
% (ph,u) are 2D matrices of equal dimension, as would be required for use
% with Matlab's quad2d function.

% Get the size of the (ph,u) arrays

sz = size(ph);

% Allocate space for rhat vectors

sznew = cat(2,3,sz);
rhat = zeros(sznew);

% Complement of u

up = real(sqrt(1-u.^2));

% Calculte r0-hat vectors

rhat(1,:,:) = cos(ph) .* up;
rhat(2,:,:) = sin(ph) .* up;
rhat(3,:,:) = u;

% Calculate integrand

integrand = Ncdot_integrand(rhat,mur,muv,Ainv,R,Q1,Q2,logZ,slow_method);

return
end

% =========================================================================

function [out,need_eph_calc] = add_eph_times(Tnew,out,need_eph_calc)

% Add new ephemeris times and associated quantities

% Ensure there are no repeated values in new eph times
Tnew = unique(Tnew);

% Return for no new ephemeris times
if isempty(Tnew)
    return;
end

% Check to see if the new times are already in the ephemeris
ndx = ismember(Tnew,out.Teph);
if any(ndx)
    Tnew = Tnew(~ndx);
end

% Return for no new ephemeris times
if isempty(Tnew)
    return;
end

% Add new times to ephemeris
out.Teph = cat(2,out.Teph,Tnew);

% Calculate new PV states and Jacobians
[J1new,X1new] = jacobian_E0_to_Xt(Tnew,out.Emean10);
[J2new,X2new] = jacobian_E0_to_Xt(Tnew,out.Emean20);
out.Xmean1T = cat(2,out.Xmean1T,X1new);
out.Jmean1T = cat(3,out.Jmean1T,J1new);
out.Xmean2T = cat(2,out.Xmean2T,X2new);
out.Jmean2T = cat(3,out.Jmean2T,J2new);

% Add flags indicating new ephemeris calculations are required
Snew = size(Tnew); NaNnew = NaN(Snew);
need_eph_calc = cat(2,need_eph_calc,true(Snew));

% Add buffer space for new MD2eff and Ncdot values
out.Ncdot = cat(2,out.Ncdot,NaNnew);
out.LebNumb = cat(2,out.LebNumb,NaNnew);
out.Ncdot_SmallHBR = cat(2,out.Ncdot_SmallHBR,NaNnew);
out.MD2eff = cat(2,out.MD2eff,NaNnew);
out.MS2eff = cat(2,out.MS2eff,NaNnew);
out.POPconv = cat(2,out.POPconv,false(Snew));
out.POPiter = cat(2,out.POPiter,NaNnew);
out.POPfail = cat(2,out.POPfail,NaNnew);
NaNvec = NaN(size(X1new));
NaNmat = NaN(size(J1new));
out.xs1 = cat(2,out.xs1,NaNvec);
out.Es01 = cat(2,out.Es01,NaNvec);
out.Js1 = cat(3,out.Js1,NaNmat);
out.xs2 = cat(2,out.xs2,NaNvec);
out.Es02 = cat(2,out.Es02,NaNvec);
out.Js2 = cat(3,out.Js2,NaNmat);
out.xu = cat(2,out.xu,NaNvec);
out.Ps = cat(3,out.Ps,NaNmat);

% Sort new ephemeris into increasing order
[out.Teph,nsrt] = sort(out.Teph);
need_eph_calc = need_eph_calc(nsrt);
out.Xmean1T = out.Xmean1T(:,nsrt);
out.Jmean1T = out.Jmean1T(:,:,nsrt);
out.Xmean2T = out.Xmean2T(:,nsrt);
out.Jmean2T = out.Jmean2T(:,:,nsrt);
out.Ncdot = out.Ncdot(nsrt);
out.LebNumb = out.LebNumb(nsrt);
out.Ncdot_SmallHBR = out.Ncdot_SmallHBR(nsrt);
out.MD2eff = out.MD2eff(nsrt);
out.MS2eff = out.MS2eff(nsrt);
out.POPconv = out.POPconv(nsrt);
out.POPiter = out.POPconv(nsrt);
out.POPfail = out.POPfail(nsrt);
out.xs1 = out.xs1(:,nsrt);
out.Es01 = out.Es01(:,nsrt);
out.Js1 = out.Js1(:,:,nsrt);
out.xs2 = out.xs2(:,nsrt);
out.Es02 = out.Es02(:,nsrt);
out.Js2 = out.Js2(:,:,nsrt);
out.xu = out.xu(:,nsrt);
out.Ps = out.Ps(:,:,nsrt);

% New numer of ephermis points
out.Neph = numel(out.Teph);

return;
end

% ----------------- END OF CODE ------------------
%
% Please record any changes to the software in the change history 
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer      |    Date     |     Description
% ---------------------------------------------------
% D.Hall         | 2020-JAN-10 | Initial Development.
% D.Hall         | 2020-MAR-18 | Incorporated try-catch structures;
%                                added comments regarding default params.
% D.Hall         | 2020-JUN-16 | Modified to allow maxiter = 1 to calculate
%                                the original Coppola (2012) formulation.
% D.Hall         | 2020-JUL-26 | Expanded the list and explanation of the
%                                quantities contained in the auxiliary
%                                output structure 'out'.
% D.Hall         | 2020-SEP-21 | Added covariance cross correlation
%                                correction processing
% D.Hall         | 2021-APR-16 | Added Ncdotcut limit clipping to increase
%                                refinement convergence stability, as
%                                discovered during cross correlation
%                                correction testing
% D.Hall         | 2022-JAN-24 | Implemented a means to check if a
%                                sufficient fraction of Lebedev quadrature
%                                points contribute to result. Large-HBR
%                                or small-covariance cases can lead to an
%                                insufficient fraction, which can cause
%                                inaccurate unit-sphere integrals. For
%                                these rare cases, the code now returns NaN
%                                as a Pc result, indicating an unconverged
%                                result.
% L. Baars       | 2022-SEP-28 | Added paths for newly updated SDK
%                                structure. Fixed expected outputs for
%                                3D-Pc calculated values after double
%                                checking the outputs against original
%                                baseline.
% L. Baars       | 02-27-2023  | Fixed relative pathing issue in addpath
%                                calls.
% D. Hall        | 07-27-2023  | Added retrograde orbit processing
% E. White       | 08-07-2023  | Added compliant documentation
% D. Hall        | 02-22-2024  | Added non-convergence for peak overlap
%                                point (POP) failure flags of 100 or
%                                greater, indicating that an undefined
%                                equnioctial orbit was encountered during
%                                the POP calculation process.
% L. Baars       | 03-05-2024  | Modified fractional exponent to use more
%                                stable nthroot() function.
% D. Hall        | 06-28-2024  | Modified code to fix small bug in output
%                                TpeakConj parameter; previously it was
%                                incorrectly calculated using the small-HBR
%                                limit collision rate curve, rather than
%                                the actual curve.
% L. Baars       | 08-20-2024  | Added disclaimer and updated copyright
%                                dates.

% =========================================================================
%
% Copyright (c) 2023-2025 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================