seiza-cli 0.18.2

Command-line interface for seiza: star detection, plate solving, and dataset management
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
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
use anyhow::{Context, Result};
use clap::{Args, Parser, Subcommand, ValueEnum};
use seiza::blind::BlindIndex;
use seiza::catalog::{StarCatalog, TileCatalog};
use seiza::data_paths;
use seiza::minor_bodies::MinorBodyCatalog;
use seiza::objects::{ObjectCatalog, ObjectKind, ObjectQuery, ObjectSort, SkyRegion};
use seiza::solve::{SolveHint, solve};
use seiza::star_ids::{StarIdentifierCatalog, StarLookupMatch};
use seiza::{DetectBackend, DetectConfig, DetectedStar};
use seiza_satellites::{
    CacheState, CelesTrakSource, ExposureProvenance, ObserverLocation, OrbitalCatalogSource,
    SatelliteCatalog, SatelliteTrack, SingleExposure, TrackOptions, UtcTimestamp,
};
use std::path::PathBuf;

mod astap;
mod background;
mod build_data;
mod color;
mod common;
mod deconvolution;
mod master;
mod preview;
mod provenance;
mod setup;
mod solve_field;
mod stack;
mod stretch_command;
mod worker;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum AstronomyImageFormat {
    Fits,
    Xisf,
}

fn astronomy_image_format(path: &std::path::Path) -> Option<AstronomyImageFormat> {
    if seiza_xisf::is_xisf_path(path) {
        return Some(AstronomyImageFormat::Xisf);
    }
    path.extension()
        .and_then(|extension| extension.to_str())
        .is_some_and(|extension| {
            extension.eq_ignore_ascii_case("fits")
                || extension.eq_ignore_ascii_case("fit")
                || extension.eq_ignore_ascii_case("fts")
        })
        .then_some(AstronomyImageFormat::Fits)
}

fn is_fits_path(path: &std::path::Path) -> bool {
    astronomy_image_format(path) == Some(AstronomyImageFormat::Fits)
}

fn is_astronomy_image_path(path: &std::path::Path) -> bool {
    astronomy_image_format(path).is_some()
}

fn open_astronomy_image(path: &std::path::Path) -> Result<seiza_fits::FitsImage> {
    match astronomy_image_format(path) {
        Some(AstronomyImageFormat::Fits) => seiza_fits::FitsImage::open(path)
            .map_err(|error| anyhow::anyhow!("{}: {error}", path.display())),
        Some(AstronomyImageFormat::Xisf) => {
            seiza_xisf::open(path).map_err(|error| anyhow::anyhow!("{}: {error}", path.display()))
        }
        None => anyhow::bail!("{} is not a FITS or XISF path", path.display()),
    }
}

fn read_astronomy_headers(
    path: &std::path::Path,
) -> Result<Vec<(String, seiza_fits::HeaderValue)>> {
    match astronomy_image_format(path) {
        Some(AstronomyImageFormat::Fits) => seiza_fits::read_header(path)
            .map_err(|error| anyhow::anyhow!("{}: {error}", path.display())),
        Some(AstronomyImageFormat::Xisf) => seiza_xisf::read_header(path)
            .map_err(|error| anyhow::anyhow!("{}: {error}", path.display())),
        None => anyhow::bail!("{} is not a FITS or XISF path", path.display()),
    }
}

pub(crate) enum LoadedImage {
    Dynamic(image::DynamicImage),
    LinearF32 {
        luma: Vec<f32>,
        width: u32,
        height: u32,
    },
}

impl LoadedImage {
    pub(crate) fn dimensions(&self) -> (u32, u32) {
        match self {
            Self::Dynamic(image) => (image.width(), image.height()),
            Self::LinearF32 { width, height, .. } => (*width, *height),
        }
    }

    pub(crate) fn detect_stars(&self, config: &DetectConfig) -> Vec<DetectedStar> {
        match self {
            Self::Dynamic(image) => seiza::detect_stars(image, config),
            Self::LinearF32 {
                luma,
                width,
                height,
            } => seiza::detect_stars_luma_f32(luma, *width, *height, config),
        }
    }

    fn is_converted_8bit_color(&self) -> bool {
        matches!(
            self,
            Self::Dynamic(
                image::DynamicImage::ImageLumaA8(_)
                    | image::DynamicImage::ImageRgb8(_)
                    | image::DynamicImage::ImageRgba8(_)
            )
        )
    }

    fn to_rgb8(&self) -> image::RgbImage {
        match self {
            Self::Dynamic(image) => image.to_rgb8(),
            Self::LinearF32 {
                luma,
                width,
                height,
            } => {
                let mut rgb = Vec::with_capacity(luma.len() * 3);
                for &value in luma {
                    let value = (value.clamp(0.0, 1.0) * 255.0).round() as u8;
                    rgb.extend_from_slice(&[value; 3]);
                }
                image::RgbImage::from_raw(*width, *height, rgb)
                    .expect("linear luma dimensions were validated when loaded")
            }
        }
    }

    fn to_luma8(&self) -> image::GrayImage {
        match self {
            Self::Dynamic(image) => image.to_luma8(),
            Self::LinearF32 {
                luma,
                width,
                height,
            } => {
                let luma = luma
                    .iter()
                    .map(|value| (value.clamp(0.0, 1.0) * 255.0).round() as u8)
                    .collect();
                image::GrayImage::from_raw(*width, *height, luma)
                    .expect("linear luma dimensions were validated when loaded")
            }
        }
    }
}

/// Open an image in the representation selected for detection. FITS and XISF
/// use an MTF-compressed u8 buffer for Auto/U8 and native-precision linear f32
/// for F32, which also lets a compatibility retry genuinely reload the source.
pub(crate) fn load_image(path: &std::path::Path, backend: DetectBackend) -> Result<LoadedImage> {
    if is_astronomy_image_path(path) {
        let image = open_astronomy_image(path)?;
        let width = u32::try_from(image.width)
            .map_err(|_| anyhow::anyhow!("image width exceeds supported dimensions"))?;
        let height = u32::try_from(image.height)
            .map_err(|_| anyhow::anyhow!("image height exceeds supported dimensions"))?;
        if backend == DetectBackend::F32 {
            return Ok(LoadedImage::LinearF32 {
                luma: image.to_luma_f32(),
                width,
                height,
            });
        }
        let stretched = image.stretch_to_u8(&seiza_fits::StretchParams::default());
        let buffer = image::GrayImage::from_raw(width, height, stretched)
            .ok_or_else(|| anyhow::anyhow!("image dimensions mismatch"))?;
        return Ok(LoadedImage::Dynamic(image::DynamicImage::ImageLuma8(
            buffer,
        )));
    }
    image::open(path)
        .map(LoadedImage::Dynamic)
        .with_context(|| format!("failed to open {}", path.display()))
}

/// Auto FITS/XISF begins with its compact MTF/u8 representation, while
/// converted 8-bit color can change sub-level ordering relative to f32 luma.
/// Either may benefit from a compatibility retry after a solve miss.
fn auto_can_retry_f32(path: &std::path::Path, image: &LoadedImage, backend: DetectBackend) -> bool {
    backend == DetectBackend::Auto
        && (is_astronomy_image_path(path) || image.is_converted_8bit_color())
}

fn redetect_f32(
    path: &std::path::Path,
    image: &LoadedImage,
    config: &DetectConfig,
) -> Result<Vec<DetectedStar>> {
    let config = DetectConfig {
        backend: DetectBackend::F32,
        ..config.clone()
    };
    if is_astronomy_image_path(path) {
        return Ok(load_image(path, DetectBackend::F32)?.detect_stars(&config));
    }
    Ok(image.detect_stars(&config))
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum DetectionFallback {
    None,
    #[default]
    F32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DetectionPass {
    Primary,
    F32Fallback,
}

/// Common detection and retry state for every hinted or blind solve
/// invocation. The f32 representation is loaded lazily after a solve miss and
/// cached when a command tries more than one solving strategy.
pub(crate) struct SolveInvocation<'a> {
    path: &'a std::path::Path,
    image: &'a LoadedImage,
    config: DetectConfig,
    fallback: DetectionFallback,
    primary_stars: Vec<DetectedStar>,
    f32_stars: Option<Vec<DetectedStar>>,
    active_f32: bool,
}

impl<'a> SolveInvocation<'a> {
    pub(crate) fn new(
        path: &'a std::path::Path,
        image: &'a LoadedImage,
        config: DetectConfig,
        fallback: DetectionFallback,
    ) -> Self {
        let primary_stars = image.detect_stars(&config);
        Self {
            path,
            image,
            config,
            fallback,
            primary_stars,
            f32_stars: None,
            active_f32: false,
        }
    }

    pub(crate) fn stars(&self) -> &[DetectedStar] {
        if self.active_f32 {
            self.f32_stars
                .as_deref()
                .expect("active f32 solve has f32 detections")
        } else {
            &self.primary_stars
        }
    }

    pub(crate) fn solve<T>(
        &mut self,
        mut solver: impl FnMut(&[DetectedStar]) -> std::result::Result<T, seiza::Error>,
    ) -> Result<T> {
        self.solve_with_pass(|stars, _| solver(stars))
    }

    pub(crate) fn solve_with_pass<T>(
        &mut self,
        mut solver: impl FnMut(&[DetectedStar], DetectionPass) -> std::result::Result<T, seiza::Error>,
    ) -> Result<T> {
        match solver(&self.primary_stars, DetectionPass::Primary) {
            Ok(solution) => Ok(solution),
            Err(seiza::Error::Solve(_))
                if self.fallback == DetectionFallback::F32
                    && auto_can_retry_f32(self.path, self.image, self.config.backend) =>
            {
                eprintln!("u8 detection did not solve; retrying with f32 detection");
                if self.f32_stars.is_none() {
                    self.f32_stars = Some(redetect_f32(self.path, self.image, &self.config)?);
                }
                let result = solver(
                    self.f32_stars.as_deref().expect("f32 stars were populated"),
                    DetectionPass::F32Fallback,
                );
                if result.is_ok() {
                    self.active_f32 = true;
                }
                result.map_err(anyhow::Error::from)
            }
            Err(error) => Err(anyhow::Error::from(error)),
        }
    }
}

fn blind_params_for_detection_pass(
    params: &seiza::blind::BlindParams,
    can_retry_f32: bool,
    fallback_hypotheses: usize,
    pass: DetectionPass,
) -> seiza::blind::BlindParams {
    let mut attempt = params.clone();
    if can_retry_f32 && pass == DetectionPass::Primary && fallback_hypotheses > 0 {
        attempt.max_hypotheses = attempt.max_hypotheses.min(fallback_hypotheses);
    }
    attempt
}

/// RA/Dec hint from FITS-compatible headers (N.I.N.A. writes RA/DEC in degrees).
fn fits_hint(path: &std::path::Path) -> Option<(f64, f64)> {
    let image = open_astronomy_image(path).ok()?;
    let ra = image
        .header_f64("RA")
        .or_else(|| image.header_f64("OBJCTRA"))?;
    let dec = image
        .header_f64("DEC")
        .or_else(|| image.header_f64("OBJCTDEC"))?;
    Some((ra, dec))
}

#[derive(Parser)]
#[command(name = "seiza", about = "Star detection and plate solving", version)]
struct Cli {
    /// Detector numeric representation. Auto uses compact u8 for decoded
    /// 8-bit images and FITS, and f32 for other higher-precision inputs.
    #[arg(long, value_enum, default_value = "auto", global = true)]
    detection_backend: DetectionBackendArg,
    /// Retry a failed Auto/u8 solve using f32 detection. FITS is reloaded from
    /// its native-precision linear samples.
    #[arg(long, value_enum, default_value = "f32", global = true)]
    detection_fallback: DetectionFallback,
    /// Blind hypotheses to verify with Auto/u8 before an available f32 retry.
    /// Zero gives u8 the full --max-hypotheses budget.
    #[arg(long, default_value_t = 64, global = true)]
    detection_fallback_hypotheses: usize,
    #[command(subcommand)]
    command: Command,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum DetectionBackendArg {
    Auto,
    U8,
    F32,
}

impl From<DetectionBackendArg> for DetectBackend {
    fn from(value: DetectionBackendArg) -> Self {
        match value {
            DetectionBackendArg::Auto => Self::Auto,
            DetectionBackendArg::U8 => Self::U8,
            DetectionBackendArg::F32 => Self::F32,
        }
    }
}

#[derive(Subcommand)]
enum Command {
    /// Guided installation of published star and object catalogs
    Setup(setup::SetupArgs),
    /// Detect stars in an image and print their positions
    Detect {
        /// Image file (PNG, JPEG, TIFF)
        image: PathBuf,
        /// Detection threshold in noise sigmas
        #[arg(long, default_value_t = 4.0)]
        sigma: f32,
        /// Maximum number of stars to report
        #[arg(long, default_value_t = 100)]
        max_stars: usize,
        /// Write a copy of the image with detections circled
        #[arg(long)]
        annotate: Option<PathBuf>,
    },
    /// Plate-solve an image against a star catalog
    Solve {
        /// Image file (PNG, JPEG, TIFF)
        image: PathBuf,
        /// Star tile file or catalog directory (default: standard catalog
        /// locations)
        #[arg(long)]
        data: Option<PathBuf>,
        /// Approximate field center RA, degrees (default: FITS RA header)
        #[arg(long, allow_negative_numbers = true)]
        ra: Option<f64>,
        /// Approximate field center Dec, degrees (default: FITS DEC header)
        #[arg(long, allow_negative_numbers = true)]
        dec: Option<f64>,
        /// Search radius around the hinted center, degrees
        #[arg(long, default_value_t = 2.0)]
        radius: f64,
        /// Approximate pixel scale, arcseconds per pixel
        #[arg(long)]
        scale: f64,
        /// Allowed relative scale error
        #[arg(long, default_value_t = 0.2)]
        scale_tolerance: f64,
        /// SIP distortion polynomial order to fit (0 = linear solution,
        /// 2-5 = fit when enough matched stars support it)
        #[arg(long, default_value_t = 0, value_parser = clap::value_parser!(u8).range(0..=5))]
        sip_order: u8,
        /// Detection threshold in noise sigmas
        #[arg(long, default_value_t = 4.0)]
        sigma: f32,
        /// Ignore detections within this many pixels of the image edges
        /// (captions and watermarks)
        #[arg(long, default_value_t = 0)]
        ignore_border: u32,
        /// Write a copy with detections (green) and catalog stars projected
        /// through the solution (red)
        #[arg(long)]
        annotate: Option<PathBuf>,
        /// Object catalog file: list objects in the field and draw them
        /// (cyan) on the annotated copy
        #[arg(long)]
        objects: Option<PathBuf>,
        /// Minor-body element file (comets + asteroids); positions are
        /// propagated to the acquisition time
        #[arg(long)]
        minor_bodies: Option<PathBuf>,
        /// Offline satellite OMM JSON or TLE file. Predicts tracks only for a
        /// single exposure and does not claim the trail was detected
        #[arg(long, conflicts_with = "satellites_celestrak")]
        satellites: Option<PathBuf>,
        /// Fetch and cache CelesTrak's current active-satellite OMM set
        #[arg(long, conflicts_with = "satellites")]
        satellites_celestrak: bool,
        /// CelesTrak cache directory (default: platform Seiza cache)
        #[arg(long, requires = "satellites_celestrak")]
        satellite_cache: Option<PathBuf>,
        /// UTC start of this single exposure (default: FITS DATE-BEG or
        /// DATE-OBS). Also supplies the minor-body acquisition time
        #[arg(long)]
        time: Option<String>,
        /// Shutter-open duration for this single exposure in seconds
        /// (default: FITS DATE-END or XPOSURE/EXPTIME/EXPOSURE)
        #[arg(long, value_parser = clap::value_parser!(f64))]
        exposure_seconds: Option<f64>,
        /// Observer geodetic latitude, degrees north
        #[arg(long, allow_negative_numbers = true, requires = "observer_lon")]
        observer_lat: Option<f64>,
        /// Observer geodetic longitude, degrees east
        #[arg(long, allow_negative_numbers = true, requires = "observer_lat")]
        observer_lon: Option<f64>,
        /// Observer height above the reference ellipsoid, meters
        #[arg(long, allow_negative_numbers = true)]
        observer_alt_m: Option<f64>,
        /// Fine satellite track sampling interval in seconds
        #[arg(long, default_value_t = 1.0, value_parser = clap::value_parser!(f64))]
        satellite_sample_seconds: f64,
        /// Maximum absolute age of orbital elements at exposure midpoint,
        /// hours. Prevents current elements being applied to old images
        #[arg(long, default_value_t = 168.0, value_parser = clap::value_parser!(f64))]
        satellite_max_element_age_hours: f64,
    },
    /// Download ready-to-use catalogs (recommended) or advanced source data
    #[command(
        after_help = "RECOMMENDED:\n  seiza download-data prebuilt --output <directory>\n      Download the complete ready-to-use, SHA-256-verified catalog bundle.\n\n  seiza setup\n      Choose a smaller use-case-based preset interactively.\n\nRun `seiza download-data prebuilt --help` to select individual ready-to-use files.\nThe other subcommands fetch upstream source data for custom catalog builds and are not needed to use Seiza."
    )]
    DownloadData {
        #[command(subcommand)]
        source: DownloadSource,
    },
    /// Build catalog data bundles from primary sources
    BuildData {
        #[command(subcommand)]
        source: BuildDataSource,
    },
    /// Build a reusable, memory-mapped blind pattern index
    BuildBlindIndex {
        /// Star tile file used to build the index
        #[arg(long)]
        data: PathBuf,
        /// Output blind index (use blind-gaia16.idx for the hosted G<=16 set)
        #[arg(long)]
        output: PathBuf,
        /// Catalog magnitude limit used to build the blind pattern index
        #[arg(long, default_value_t = 16.0)]
        index_mag_limit: f32,
        /// Longest allowed catalog pattern edge, degrees
        #[arg(long, default_value_t = 6.0)]
        max_pattern_deg: f64,
    },
    /// Plate-solve with no position hint (wide fields, pixel-scale range)
    SolveBlind {
        /// Image file (PNG, JPEG, TIFF, FITS, XISF)
        image: PathBuf,
        /// Star tile file or catalog directory (default: standard catalog
        /// locations)
        #[arg(long)]
        data: Option<PathBuf>,
        /// Prebuilt blind pattern index file or directory (default: found
        /// in the standard locations; built in memory when absent)
        #[arg(long)]
        index: Option<PathBuf>,
        /// Minimum plausible pixel scale, arcseconds per pixel
        #[arg(long, default_value_t = 0.1)]
        min_scale: f64,
        /// Maximum plausible pixel scale, arcseconds per pixel
        #[arg(long, default_value_t = 20.0)]
        max_scale: f64,
        /// Catalog magnitude limit used to build the blind pattern index.
        /// Use 16 with the deep Gaia catalog for small, fine-scale fields.
        #[arg(long, default_value_t = 12.7)]
        index_mag_limit: f32,
        /// Field hypotheses to verify before giving up
        #[arg(long, default_value_t = 400)]
        max_hypotheses: usize,
        /// Field hypotheses to pre-rank by coarse catalog projection;
        /// bounds the cost of an image that never solves
        #[arg(long, default_value_t = 20_000)]
        max_coarse_hypotheses: usize,
        /// SIP distortion polynomial order to fit on the accepted solution
        /// (0 = linear solution, 2-5 = fit when enough matched stars
        /// support it)
        #[arg(long, default_value_t = 0, value_parser = clap::value_parser!(u8).range(0..=5))]
        sip_order: u8,
        /// Detection threshold in noise sigmas
        #[arg(long, default_value_t = 4.0)]
        sigma: f32,
        /// Ignore detections within this many pixels of the image edges
        #[arg(long, default_value_t = 0)]
        ignore_border: u32,
    },
    /// Install the astrometry.net drop-in layout for Siril: copies of this
    /// binary named solve-field and bin/bash(.exe), plus the tmp directory
    /// Windows Siril writes its launch script into
    InstallSolveField {
        /// Directory to point Siril's astrometry.net preference at
        #[arg(long)]
        dir: PathBuf,
    },
    /// Serve versioned JSON-RPC plate-solve requests over stdin/stdout
    Worker {
        /// Star tile file or catalog directory kept open for the lifetime
        /// of the worker (default: standard catalog locations)
        #[arg(long, conflicts_with = "server")]
        data: Option<PathBuf>,
        /// Optional prebuilt blind pattern index file or directory kept
        /// open by the worker
        #[arg(long, conflicts_with = "server")]
        index: Option<PathBuf>,
        /// Remote seiza-server base URL; local images are converted according to --server-upload
        #[arg(long)]
        server: Option<String>,
        /// Remote bearer token; defaults to SEIZA_SERVER_TOKEN when set
        #[arg(long, requires = "server")]
        server_token: Option<String>,
        /// Remote upload representation (PNG is usually smaller and lossless for star detection)
        #[arg(long, value_enum, requires = "server")]
        server_upload: Option<worker::ServerUploadFormat>,
        /// Maximum remote upload, queue, and solve time in seconds (default: 300)
        #[arg(
            long,
            value_name = "SECONDS",
            requires = "server",
            value_parser = clap::value_parser!(u64).range(1..)
        )]
        server_timeout: Option<u64>,
    },
    /// Inspect a FITS or XISF file: headers, statistics, optional stretched PNG
    FitsInfo {
        /// FITS or XISF file
        image: PathBuf,
        /// Write an autostretched 8-bit preview
        #[arg(long)]
        stretch: Option<PathBuf>,
    },
    /// Apply an explicit display-stretch model to a linear FITS image
    Stretch(stretch_command::StretchArgs),
    /// Estimate and remove a smooth background gradient from linear FITS
    Background(background::BackgroundArgs),
    /// Experimentally restore mild blur in a linear FITS using a measured PSF
    Deconvolve(deconvolution::DeconvolutionArgs),
    /// Register and incrementally stack linear FITS light frames
    Stack(stack::StackArgs),
    /// Register and compose mono stacks into RGB, LRGB, or narrowband color
    Color(color::ColorArgs),
    /// Build sigma-clipped bias, dark, and flat masters from raw FITS frames
    Master(master::MasterArgs),
    /// Query a star tile file: list stars around a sky position
    Cone {
        /// Star tile file built by build-data
        #[arg(long)]
        data: PathBuf,
        #[arg(long, allow_negative_numbers = true)]
        ra: f64,
        #[arg(long, allow_negative_numbers = true)]
        dec: f64,
        /// Search radius in degrees
        #[arg(long, default_value_t = 1.0)]
        radius: f64,
        #[arg(long, default_value_t = 25)]
        limit: usize,
    },
    /// Query astronomical catalogs without plate-solving an image
    Catalog {
        #[command(subcommand)]
        query: CatalogCommand,
    },
}

#[derive(Subcommand)]
enum CatalogCommand {
    /// Resolve an object name, alias, or stable ID from objects.bin
    Object {
        #[command(flatten)]
        args: CatalogObjectArgs,
    },
    /// List named and deep-sky objects in a known sky region
    Objects {
        #[command(flatten)]
        args: CatalogObjectsArgs,
    },
    /// Resolve an exact stellar catalog designation without a network query
    Star {
        #[command(flatten)]
        args: CatalogStarArgs,
    },
    /// Exhaustively validate a catalog or index file
    Validate {
        /// Catalog, sidecar, or blind-index file; the format is auto-detected
        #[arg(long)]
        data: PathBuf,
    },
}

#[derive(Args)]
struct CatalogObjectArgs {
    /// Object catalog file or catalog directory (default: standard
    /// catalog locations)
    #[arg(long)]
    data: Option<PathBuf>,
    /// Object designation, common name, alias, or stable ID
    query: String,
    /// Return prefix completions instead of an exact lookup
    #[arg(long)]
    prefix: bool,
    /// Include every contributing source record, selection, and geometry
    #[arg(long)]
    all_sources: bool,
    /// Maximum prefix completions; ignored for exact lookup
    #[arg(long, default_value_t = 25)]
    limit: usize,
    #[arg(long, value_enum, default_value_t = CatalogOutputFormat::Table)]
    format: CatalogOutputFormat,
}

#[derive(Args)]
struct CatalogStarArgs {
    /// Star identifier sidecar file or catalog directory (default:
    /// standard catalog locations)
    #[arg(long)]
    data: Option<PathBuf>,
    /// Catalog designation or stellar name, for example HIP 32349 or RR Lyr
    query: String,
    /// Return textual-name prefix completions instead of an exact lookup
    #[arg(long)]
    prefix: bool,
    /// Maximum prefix completions; ignored for exact lookup
    #[arg(long, default_value_t = 25)]
    limit: usize,
    #[arg(long, value_enum, default_value_t = CatalogOutputFormat::Table)]
    format: CatalogOutputFormat,
}

#[derive(Args)]
struct CatalogObjectsArgs {
    /// Object catalog file or catalog directory (default: standard
    /// catalog locations)
    #[arg(long)]
    data: Option<PathBuf>,
    /// Cone center RA, ICRS degrees; use with --dec and --radius
    #[arg(long, allow_negative_numbers = true)]
    ra: Option<f64>,
    /// Cone center Dec, ICRS degrees; use with --ra and --radius
    #[arg(long, allow_negative_numbers = true)]
    dec: Option<f64>,
    /// Cone radius in degrees; use with --ra and --dec
    #[arg(long)]
    radius: Option<f64>,
    /// Convex image-footprint vertex as RA,DEC in boundary order; repeat 3+
    /// times. Conflicts with the cone arguments
    #[arg(
        long,
        value_name = "RA,DEC",
        allow_hyphen_values = true,
        value_parser = parse_sky_coordinate
    )]
    corner: Vec<SkyCoordinate>,
    /// Include only these object kinds; repeat or comma-separate values
    #[arg(long, value_delimiter = ',', value_parser = parse_object_kind)]
    kind: Vec<ObjectKind>,
    /// Faintest integrated visual magnitude to include; unknowns are excluded
    #[arg(long, allow_negative_numbers = true)]
    max_mag: Option<f32>,
    /// Minimum catalog major axis in arcminutes; unknowns are excluded
    #[arg(long)]
    min_size: Option<f32>,
    /// Require a common/popular name in addition to a catalog designation
    #[arg(long)]
    common_name_only: bool,
    /// Exclude objects whose extent overlaps the region but center is outside
    #[arg(long)]
    center_only: bool,
    /// Maximum results; zero means unlimited
    #[arg(long, default_value_t = 25)]
    limit: usize,
    #[arg(long, value_enum, default_value_t = CatalogSortArg::Prominence)]
    sort: CatalogSortArg,
    #[arg(long, value_enum, default_value_t = CatalogOutputFormat::Table)]
    format: CatalogOutputFormat,
}

#[derive(Debug, Clone, Copy)]
struct SkyCoordinate {
    ra: f64,
    dec: f64,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum CatalogSortArg {
    Prominence,
    Size,
    Magnitude,
    Distance,
    Name,
}

impl From<CatalogSortArg> for ObjectSort {
    fn from(value: CatalogSortArg) -> Self {
        match value {
            CatalogSortArg::Prominence => Self::Prominence,
            CatalogSortArg::Size => Self::Size,
            CatalogSortArg::Magnitude => Self::Magnitude,
            CatalogSortArg::Distance => Self::Distance,
            CatalogSortArg::Name => Self::Name,
        }
    }
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum CatalogOutputFormat {
    Table,
    Json,
    Csv,
}

#[derive(Subcommand)]
enum DownloadSource {
    /// Ready-to-use catalog bundle (recommended; SHA-256 verified)
    Prebuilt {
        /// Directory to download into
        #[arg(long)]
        output: PathBuf,
        /// Specific files (default: the complete standard bundle). Optional
        /// extras hosted outside the bundle, such as stars-deep-gaia20.bin, are
        /// fetched only when named here.
        #[arg(long)]
        file: Vec<String>,
    },
    /// Historical satellite TLE snapshots from the Seiza mirror or IAU fallback
    SatelliteHistory {
        /// UTC epoch(s) to prewarm, normally exposure midpoints. Nearby epochs
        /// reuse one validated observing-night snapshot.
        #[arg(long, required = true, num_args = 1.., value_name = "RFC3339")]
        epoch: Vec<String>,
        /// Shared satellite cache directory (default: platform Seiza cache)
        #[arg(long)]
        cache: Option<PathBuf>,
        /// Fetch each exact epoch from the public origin, bypassing the Seiza
        /// mirror and nearby-cache reuse. Required when publishing the mirror.
        #[arg(long)]
        origin: bool,
    },
    /// Advanced source: Tycho-2 distribution files from CDS (~150 MB)
    Tycho2 {
        /// Directory to download into
        #[arg(long)]
        output: PathBuf,
    },
    /// Advanced source: bright, variable, double, and IAU star identities
    StarIdentifiers {
        /// Directory to download into
        #[arg(long)]
        output: PathBuf,
    },
    /// Advanced source: the OpenNGC deep-sky object catalog (~4 MB)
    Openngc {
        /// Directory to download into
        #[arg(long)]
        output: PathBuf,
    },
    /// Advanced source: a pinned Seiza catalog-curation GitHub snapshot
    Curation {
        /// Directory to download into; must be empty or the same pinned snapshot
        #[arg(long)]
        output: PathBuf,
        /// GitHub owner/repository
        #[arg(long, default_value = "theatrus/seiza-catalog-curation")]
        repository: String,
        /// Exact Git commit (7-40 hexadecimal characters)
        #[arg(long)]
        commit: String,
    },
    /// Advanced source: OpenNGC, Sh2, Barnard, UGC, LDN, vdB,
    /// LBN, Cederblad, IAU + Bright Star Catalogue star names
    Objects {
        /// Directory to download into
        #[arg(long)]
        output: PathBuf,
    },
    /// Advanced source: Gaia DR3 via ESA TAP (resumable; can take hours)
    Gaia {
        /// Directory to download into
        #[arg(long)]
        output: PathBuf,
        /// Magnitude limit for the download
        #[arg(long, default_value_t = 15.0, allow_negative_numbers = true)]
        max_mag: f32,
        /// Sky chunks (multiples of 192; 768 = HEALPix level 3). Deeper
        /// magnitude limits need more chunks to stay under the TAP row cap
        #[arg(
            long,
            default_value_t = 768,
            value_parser = clap::value_parser!(u64).range(1..)
        )]
        chunks: u64,
    },
    /// Advanced source: Rochester active supernova/transient list
    Transients {
        /// Directory to download into
        #[arg(long)]
        output: PathBuf,
    },
    /// Advanced source: MPC comet and numbered-asteroid elements
    Mpc {
        /// Directory to download into
        #[arg(long)]
        output: PathBuf,
    },
}

#[derive(Subcommand)]
enum BuildDataSource {
    /// An ASTAP .1476 star database directory (e.g. D80, Gaia DR3)
    Astap {
        /// Directory containing *.1476 files
        #[arg(long)]
        input: PathBuf,
        /// Output tile file
        #[arg(long)]
        output: PathBuf,
        /// Epoch of the source positions, Julian year (D80 is epoch 2025)
        #[arg(long, default_value_t = 2025.0)]
        epoch: f64,
        /// Drop stars fainter than this magnitude
        #[arg(long, default_value_t = 21.0)]
        max_mag: f32,
        /// Declination bands (tile granularity); 180 = 1° tiles
        #[arg(long, default_value_t = 180)]
        bands: u32,
    },
    /// Tycho-2 (CDS I/259): the ~2.5M star lite tier
    Tycho2 {
        /// Directory containing tyc2.dat.NN[.gz]
        #[arg(long)]
        input: PathBuf,
        /// Output tile file
        #[arg(long)]
        output: PathBuf,
        /// Optional exact TYC/HIP lookup sidecar
        #[arg(long)]
        identifier_index: Option<PathBuf>,
        /// Optional directory from download-data star-identifiers
        #[arg(long, requires = "identifier_index")]
        identifier_sources: Option<PathBuf>,
        /// Epoch to apply proper motions to, Julian year
        #[arg(long, default_value_t = 2025.5)]
        epoch: f64,
        /// Drop stars fainter than this magnitude
        #[arg(long, default_value_t = 13.0)]
        max_mag: f32,
    },
    /// Object catalog from the downloaded object sources
    Objects {
        /// Directory containing the source files
        #[arg(long)]
        input: PathBuf,
        /// Pinned seiza-catalog-curation checkout; no network access is used
        #[arg(long)]
        curation_dir: Option<PathBuf>,
        /// Output object catalog file
        #[arg(long)]
        output: PathBuf,
        /// Optional deterministic provenance manifest with source-file hashes
        #[arg(long)]
        source_manifest: Option<PathBuf>,
    },
    /// Star tiles from Gaia DR3 TAP chunks (download-data gaia)
    Gaia {
        /// Directory containing gaia-*.csv
        #[arg(long)]
        input: PathBuf,
        /// Output tile file
        #[arg(long)]
        output: PathBuf,
        /// Epoch to apply proper motions to, Julian year
        #[arg(long, default_value_t = 2025.5)]
        epoch: f64,
        /// Drop stars fainter than this magnitude
        #[arg(long, default_value_t = 15.0)]
        max_mag: f32,
        /// Declination bands (tile granularity); 180 = 1° tiles
        #[arg(long, default_value_t = 180)]
        bands: u32,
    },
    /// Transient catalog from the downloaded Rochester active list
    Transients {
        /// Directory containing snactive.html
        #[arg(long)]
        input: PathBuf,
        /// Output transient catalog file
        #[arg(long)]
        output: PathBuf,
    },
    /// Comets + bright numbered asteroids from MPC element sets
    MinorBodies {
        /// Directory containing CometEls.txt and MPCORB.DAT.gz
        #[arg(long)]
        input: PathBuf,
        /// Output element file
        #[arg(long)]
        output: PathBuf,
        /// Drop asteroids with absolute magnitude H above this
        #[arg(long, default_value_t = 16.0)]
        max_h: f32,
    },
    /// Bundle manifest and optional upload-ready artifacts for hosting
    Manifest {
        /// Directory containing new or replacement .bin and .idx data files
        #[arg(long)]
        dir: PathBuf,
        /// Existing complete v2 or v4 manifest to roll forward. Files in
        /// `dir` replace matching entries; all other entries are retained.
        #[arg(long)]
        base_manifest: Option<PathBuf>,
        /// Version string and output layout: catalog-bundle-v2-* writes the
        /// flat compatibility bundle; catalog-bundle-v4-* writes immutable
        /// content-addressed artifact keys
        #[arg(long)]
        version: String,
        /// Output manifest path
        #[arg(long)]
        output: PathBuf,
        /// Stage both uncompressed and zstd artifacts below this directory.
        /// V4 only; the resulting content-addressed tree can be uploaded as
        /// one compatibility-safe publication step.
        #[arg(long)]
        artifact_dir: Option<PathBuf>,
        /// Zstd compression level (1-22). Defaults to maximum compression
        /// when --artifact-dir is supplied.
        #[arg(
            long,
            requires = "artifact_dir",
            value_parser = clap::value_parser!(i32).range(1..=22)
        )]
        zstd_level: Option<i32>,
    },
    /// Rolling satellite mirror manifest and upload-ready immutable TLEs
    SatelliteManifest {
        /// Shared orbital cache populated by download-data satellite-history
        #[arg(long)]
        cache: PathBuf,
        /// Previously published manifest to roll forward
        #[arg(long)]
        base_manifest: Option<PathBuf>,
        /// New manifest pointer; upload this to manifest.json last
        #[arg(long)]
        output: PathBuf,
        /// Stage content-addressed artifacts for S3 sync
        #[arg(long)]
        artifact_dir: PathBuf,
    },
}

/// Falling through every standard location earns the CLI flag hint the
/// library error cannot give.
fn with_data_flag_hint<T>(result: Result<T, data_paths::DataPathError>) -> Result<T> {
    result.map_err(|error| match error {
        data_paths::DataPathError::NoDefault { kind } => anyhow::anyhow!(
            "no {kind} found; pass --data (a file or a directory), run `seiza setup`, \
             or run: seiza download-data prebuilt --output <dir> (https://downloads.seiza.fyi)"
        ),
        other => anyhow::Error::new(other),
    })
}

fn main() -> Result<()> {
    // Compatibility modes come first, before clap sees the command line.
    // A copy of the binary named solve-field or bin/bash (or a
    // solve-field-shaped command line) routes to the astrometry.net mode
    // Siril drives; a copy named astap, or an ASTAP-style command line,
    // routes to the ASTAP mode N.I.N.A. drives.
    let raw: Vec<String> = std::env::args().skip(1).collect();
    let program = std::env::args().next().unwrap_or_default();
    if solve_field::invoked_as_bash(&program) {
        return solve_field::run_as_bash(&raw);
    }
    if solve_field::invoked_as_solve_field(&program) || solve_field::looks_like_solve_field(&raw) {
        return solve_field::run(&raw);
    }
    if astap::invoked_as_astap(&program) || astap::looks_like_astap(&raw) {
        return astap::run(&raw);
    }

    let cli = Cli::parse();
    let detection_backend = cli.detection_backend.into();
    let detection_fallback = cli.detection_fallback;
    let detection_fallback_hypotheses = cli.detection_fallback_hypotheses;
    match cli.command {
        Command::Setup(args) => setup::run(args),
        Command::InstallSolveField { dir } => solve_field::install_layout(&dir),
        Command::Detect {
            image,
            sigma,
            max_stars,
            annotate,
        } => detect(
            &image,
            sigma,
            max_stars,
            detection_backend,
            annotate.as_deref(),
        ),
        Command::Solve {
            image,
            data,
            ra,
            dec,
            radius,
            scale,
            scale_tolerance,
            sip_order,
            sigma,
            ignore_border,
            annotate,
            objects,
            minor_bodies,
            satellites,
            satellites_celestrak,
            satellite_cache,
            time,
            exposure_seconds,
            observer_lat,
            observer_lon,
            observer_alt_m,
            satellite_sample_seconds,
            satellite_max_element_age_hours,
        } => {
            let hint = match (ra, dec) {
                (Some(ra), Some(dec)) => (ra, dec),
                _ => fits_hint(&image).ok_or_else(|| {
                    anyhow::anyhow!("--ra/--dec required (no RA/DEC headers found in the image)")
                })?,
            };
            let acquisition_jd = resolve_acquisition_jd(&image, time.as_deref())?;
            let satellite_input = match (satellites, satellites_celestrak) {
                (Some(path), false) => Some(SatelliteInput::File(path)),
                (None, true) => Some(SatelliteInput::CelesTrak { satellite_cache }),
                (None, false) => None,
                (Some(_), true) => unreachable!("clap rejects conflicting satellite sources"),
            };
            let satellite_request = satellite_input
                .map(|input| {
                    if !satellite_sample_seconds.is_finite() || satellite_sample_seconds <= 0.0 {
                        anyhow::bail!(
                            "--satellite-sample-seconds must be a positive finite number"
                        );
                    }
                    if !satellite_max_element_age_hours.is_finite()
                        || satellite_max_element_age_hours <= 0.0
                    {
                        anyhow::bail!(
                            "--satellite-max-element-age-hours must be a positive finite number"
                        );
                    }
                    let exposure = resolve_single_exposure(
                        &image,
                        time.as_deref(),
                        exposure_seconds,
                        observer_lat,
                        observer_lon,
                        observer_alt_m,
                    )?;
                    Ok::<_, anyhow::Error>(SatelliteSolveRequest {
                        input,
                        exposure,
                        sample_interval_seconds: satellite_sample_seconds,
                        maximum_element_age_seconds: satellite_max_element_age_hours * 3600.0,
                    })
                })
                .transpose()?;
            let data = with_data_flag_hint(data_paths::star_data(data.as_deref()))?;
            let objects = objects
                .map(|path| data_paths::objects(Some(&path)))
                .transpose()?;
            let minor_bodies = minor_bodies
                .map(|path| data_paths::minor_bodies(Some(&path)))
                .transpose()?;
            solve_command(
                &image,
                &data,
                hint,
                radius,
                scale,
                scale_tolerance,
                sip_order,
                sigma,
                ignore_border,
                detection_backend,
                detection_fallback,
                annotate.as_deref(),
                objects.as_deref(),
                minor_bodies.as_deref(),
                acquisition_jd,
                satellite_request,
            )
        }
        Command::DownloadData { source } => run_download_command(source),
        Command::BuildData { source } => match source {
            BuildDataSource::Astap {
                input,
                output,
                epoch,
                max_mag,
                bands,
            } => build_data::build_astap(&input, &output, epoch, max_mag, bands),
            BuildDataSource::Tycho2 {
                input,
                output,
                identifier_index,
                identifier_sources,
                epoch,
                max_mag,
            } => build_data::build_tycho2(
                &input,
                &output,
                identifier_index.as_deref(),
                identifier_sources.as_deref(),
                epoch,
                max_mag,
            ),
            BuildDataSource::Objects {
                input,
                curation_dir,
                output,
                source_manifest,
            } => build_data::build_objects(
                &input,
                &output,
                source_manifest.as_deref(),
                curation_dir.as_deref(),
            ),
            BuildDataSource::Gaia {
                input,
                output,
                epoch,
                max_mag,
                bands,
            } => build_data::build_gaia(&input, &output, epoch, max_mag, bands),
            BuildDataSource::Transients { input, output } => {
                build_data::build_transients(&input, &output)
            }
            BuildDataSource::MinorBodies {
                input,
                output,
                max_h,
            } => build_data::build_minor_bodies(&input, &output, max_h),
            BuildDataSource::Manifest {
                dir,
                base_manifest,
                version,
                output,
                artifact_dir,
                zstd_level,
            } => build_data::build_manifest(
                &dir,
                base_manifest.as_deref(),
                &version,
                &output,
                artifact_dir.as_deref(),
                zstd_level.unwrap_or(22),
            ),
            BuildDataSource::SatelliteManifest {
                cache,
                base_manifest,
                output,
                artifact_dir,
            } => build_data::build_satellite_manifest(
                &cache,
                base_manifest.as_deref(),
                &output,
                &artifact_dir,
            ),
        },
        Command::BuildBlindIndex {
            data,
            output,
            index_mag_limit,
            max_pattern_deg,
        } => build_blind_index_command(&data, &output, index_mag_limit, max_pattern_deg),
        Command::SolveBlind {
            image,
            data,
            index,
            min_scale,
            max_scale,
            index_mag_limit,
            max_hypotheses,
            max_coarse_hypotheses,
            sip_order,
            sigma,
            ignore_border,
        } => {
            let data = with_data_flag_hint(data_paths::star_data(data.as_deref()))?;
            let index = data_paths::blind_index(index.as_deref())?;
            solve_blind_command(
                &image,
                &data,
                SolveBlindOptions {
                    index_path: index.as_deref(),
                    min_scale,
                    max_scale,
                    index_mag_limit,
                    max_hypotheses,
                    max_coarse_hypotheses,
                    sip_order,
                    sigma,
                    ignore_border,
                    detection_backend,
                    detection_fallback,
                    detection_fallback_hypotheses,
                },
            )
        }
        Command::Worker {
            data,
            index,
            server,
            server_token,
            server_upload,
            server_timeout,
        } => {
            let server_token = server_token.or_else(|| std::env::var("SEIZA_SERVER_TOKEN").ok());
            let data = match &server {
                Some(_) => None,
                None => Some(with_data_flag_hint(data_paths::star_data(data.as_deref()))?),
            };
            let index = index
                .map(|path| data_paths::blind_index(Some(&path)))
                .transpose()?
                .flatten();
            worker::run(worker::WorkerOptions {
                data_path: data.as_deref(),
                index_path: index.as_deref(),
                server: server.as_deref(),
                server_token: server_token.as_deref(),
                server_upload: server_upload.unwrap_or(worker::ServerUploadFormat::Png),
                server_timeout: std::time::Duration::from_secs(server_timeout.unwrap_or(300)),
                detection_backend,
                detection_fallback,
                detection_fallback_hypotheses,
            })
        }
        Command::FitsInfo { image, stretch } => fits_info(&image, stretch.as_deref()),
        Command::Stretch(options) => stretch_command::run(options),
        Command::Background(options) => background::run(options),
        Command::Deconvolve(options) => deconvolution::run(options),
        Command::Stack(options) => stack::run(options),
        Command::Color(options) => color::run(options),
        Command::Master(options) => master::run(options),
        Command::Cone {
            data,
            ra,
            dec,
            radius,
            limit,
        } => cone(&data, ra, dec, radius, limit),
        Command::Catalog { query } => match query {
            CatalogCommand::Object { args } => catalog_object(args),
            CatalogCommand::Objects { args } => catalog_objects(args),
            CatalogCommand::Star { args } => catalog_star(args),
            CatalogCommand::Validate { data } => catalog_validate(&data),
        },
    }
}

fn run_download_command(source: DownloadSource) -> Result<()> {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .context("failed to start the download runtime")?
        .block_on(download_command(source))
}

pub(crate) fn run_prebuilt_download(output: PathBuf, file: Vec<String>) -> Result<()> {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .context("failed to start the download runtime")?
        .block_on(download_prebuilt(output, file))
}

async fn download_prebuilt(output: PathBuf, file: Vec<String>) -> Result<()> {
    let selection = seiza_download::CatalogSet::from_names(file)?;
    let manager = seiza_download::CatalogManager::builder()
        .policy(seiza_download::CachePolicy::ForceRefresh)
        .build()?;
    let bundle = manager
        .ensure_with(&selection, report_download_event)
        .await?;
    let paths = bundle
        .materialize_with(&output, report_download_event)
        .await?;
    println!(
        "{} dataset(s) from {} ready in {}",
        paths.len(),
        bundle.version,
        output.display()
    );
    Ok(())
}

async fn download_command(source: DownloadSource) -> Result<()> {
    match source {
        DownloadSource::Prebuilt { output, file } => {
            download_prebuilt(output, file).await?;
        }
        DownloadSource::SatelliteHistory {
            epoch,
            cache,
            origin,
        } => {
            download_satellite_history(epoch, cache, origin).await?;
        }
        source => download_source(source).await?,
    }
    Ok(())
}

async fn download_satellite_history(
    epoch: Vec<String>,
    cache: Option<PathBuf>,
    origin: bool,
) -> Result<()> {
    let mut epochs = epoch
        .into_iter()
        .map(|value| {
            UtcTimestamp::parse(&value)
                .with_context(|| format!("invalid satellite history epoch {value:?}"))
        })
        .collect::<Result<Vec<_>>>()?;
    epochs.sort_by(|left, right| left.unix_seconds().total_cmp(&right.unix_seconds()));
    epochs.dedup_by(|left, right| left.unix_seconds() == right.unix_seconds());

    let source = match cache {
        Some(cache) => OrbitalCatalogSource::new(cache)?,
        None => OrbitalCatalogSource::platform_default()?,
    };
    for epoch in epochs {
        let load = if origin {
            source.fetch_historical_origin(epoch).await?
        } else {
            source.prewarm_historical(epoch).await?
        };
        let state = match load.state {
            CacheState::Downloaded => "downloaded",
            CacheState::Cached => "cached",
            CacheState::Fresh => "fresh cache",
            CacheState::StaleFallback => "stale cache fallback",
        };
        println!(
            "satellite history for {}: {} records ({}, {state}, {})",
            load.snapshot
                .query_time
                .expect("historical prewarming always records its query epoch")
                .to_rfc3339(),
            load.catalog.len(),
            match load.snapshot.provider {
                seiza_satellites::OrbitalCatalogProvider::CelesTrakActive => "CelesTrak active",
                seiza_satellites::OrbitalCatalogProvider::SeizaMirror => "Seiza mirror",
                seiza_satellites::OrbitalCatalogProvider::IauSatChecker => "IAU SatChecker",
            },
            load.cache_path.display()
        );
    }
    Ok(())
}

async fn download_source(source: DownloadSource) -> Result<()> {
    let downloader = seiza_sources::SourceDownloader::with_reporter(report_source_event)?;
    match source {
        DownloadSource::Tycho2 { output } => downloader.download_tycho2(output).await,
        DownloadSource::StarIdentifiers { output } => {
            downloader.download_star_identifiers(output).await
        }
        DownloadSource::Openngc { output } => downloader.download_openngc(output).await,
        DownloadSource::Curation {
            output,
            repository,
            commit,
        } => {
            downloader
                .download_curation(&repository, &commit, output)
                .await
        }
        DownloadSource::Objects { output } => downloader.download_objects(output).await,
        DownloadSource::Gaia {
            output,
            max_mag,
            chunks,
        } => downloader.download_gaia(output, max_mag, chunks).await,
        DownloadSource::Transients { output } => downloader.download_transients(output).await,
        DownloadSource::Mpc { output } => downloader.download_mpc(output).await,
        DownloadSource::Prebuilt { .. } | DownloadSource::SatelliteHistory { .. } => {
            unreachable!("handled by download_command")
        }
    }?;
    Ok(())
}

fn report_download_event(event: seiza_download::DownloadEvent) {
    use seiza_download::DownloadEvent;
    use std::io::{IsTerminal, Write};
    use std::sync::Mutex;
    use std::time::Instant;

    struct ActiveDownload {
        name: String,
        started: Instant,
        downloaded: u64,
        written: u64,
    }
    // Downloads are sequential; remember the active one so progress and
    // completion lines can report elapsed-time transfer speed.
    static ACTIVE: Mutex<Option<ActiveDownload>> = Mutex::new(None);
    let mib = |bytes: u64| bytes as f64 / 1_048_576.0;

    match event {
        DownloadEvent::FetchingManifest { url } => println!("  fetching {url}"),
        DownloadEvent::UsingCachedManifest { version, stale } => {
            let qualifier = if stale { "stale " } else { "" };
            println!("  using {qualifier}cached manifest {version}");
        }
        DownloadEvent::CacheHit { name, .. } => println!("  {name} already cached"),
        DownloadEvent::DownloadStarted { name, bytes } => {
            *ACTIVE.lock().unwrap() = Some(ActiveDownload {
                name: name.clone(),
                started: Instant::now(),
                downloaded: 0,
                written: 0,
            });
            println!("  downloading {name} ({:.1} MiB)", mib(bytes))
        }
        DownloadEvent::DownloadProgress {
            name,
            downloaded,
            total,
            written,
        } => {
            let mut active = ACTIVE.lock().unwrap();
            let elapsed = match active.as_mut() {
                Some(active) if active.name == name => {
                    active.downloaded = downloaded;
                    active.written = written;
                    active.started.elapsed().as_secs_f64()
                }
                _ => 0.0,
            };
            drop(active);
            if !std::io::stdout().is_terminal() {
                return;
            }
            let percent = downloaded.saturating_mul(100) / total.max(1);
            let speed = if elapsed > 0.0 {
                mib(downloaded) / elapsed
            } else {
                0.0
            };
            let unpacked = if written > downloaded {
                format!(", unpacked {:.1} MiB", mib(written))
            } else {
                String::new()
            };
            print!(
                "\r  {name}: {percent:>3}% ({:.1}/{:.1} MiB, {speed:.1} MiB/s{unpacked})   ",
                mib(downloaded),
                mib(total),
            );
            let _ = std::io::stdout().flush();
        }
        DownloadEvent::DownloadComplete { name, .. } => {
            let active = ACTIVE.lock().unwrap().take();
            match active.filter(|active| active.name == name && active.downloaded > 0) {
                Some(active) => {
                    let elapsed = active
                        .started
                        .elapsed()
                        .as_secs_f64()
                        .max(f64::MIN_POSITIVE);
                    let speed = mib(active.downloaded) / elapsed;
                    let unpacked = if active.written > active.downloaded {
                        format!(", unpacked to {:.1} MiB", mib(active.written))
                    } else {
                        String::new()
                    };
                    println!(
                        "\r  cached {name}: {:.1} MiB in {elapsed:.1}s ({speed:.1} MiB/s{unpacked})      ",
                        mib(active.downloaded),
                    );
                }
                None => println!("\r  cached {name}                         "),
            }
        }
        DownloadEvent::Verifying { name } => println!("  verifying {name}"),
        DownloadEvent::Installing { name, .. } => println!("  installing {name}"),
        DownloadEvent::InstallComplete { .. } => {}
    }
}

fn report_source_event(event: seiza_sources::SourceEvent) {
    use seiza_sources::SourceEvent;
    match event {
        SourceEvent::AlreadyPresent { path } => println!("  {} already present", path.display()),
        SourceEvent::Fetching { url, .. } => println!("  fetching {url}"),
        SourceEvent::Progress { .. } => {}
        SourceEvent::Retry {
            label,
            attempt,
            delay,
            error,
        } => eprintln!(
            "  {label} attempt {attempt} failed: {error}; retrying in {}s",
            delay.as_secs()
        ),
        SourceEvent::GaiaChunkComplete {
            chunk,
            rows,
            completed,
            total,
        } => println!("  chunk {chunk:04}: {rows} rows ({completed}/{total})"),
        SourceEvent::Ready { source, directory } => {
            println!("{source} ready in {}", directory.display())
        }
    }
}

fn catalog_object(args: CatalogObjectArgs) -> Result<()> {
    let data = with_data_flag_hint(data_paths::objects(args.data.as_deref()))?;
    let catalog =
        ObjectCatalog::open(&data).with_context(|| format!("failed to open {}", data.display()))?;
    let matches = if args.prefix {
        catalog.search_names(&args.query, args.limit)?
    } else {
        catalog.lookup_name(&args.query)?
    };
    if args.all_sources {
        return catalog_object_all_sources(&catalog, &matches, args.format);
    }

    match args.format {
        CatalogOutputFormat::Table => {
            println!(
                "{} object name match{}:",
                matches.len(),
                if matches.len() == 1 { "" } else { "es" }
            );
            println!(
                "{:<24} {:<18} {:<28} {:>10} {:>10}  id",
                "matched", "kind", "object", "ra", "dec"
            );
            for item in &matches {
                println!(
                    "{:<24} {:<18} {:<28} {:>10.5} {:>10.5}  {}",
                    item.matched_name,
                    item.object.kind.as_str(),
                    item.object.name,
                    item.object.ra,
                    item.object.dec,
                    item.object.metadata.id,
                );
            }
        }
        CatalogOutputFormat::Json => {
            let values = matches
                .iter()
                .map(|item| {
                    let object = &item.object;
                    serde_json::json!({
                        "matched_name": item.matched_name,
                        "kind": object.kind.as_str(),
                        "name": object.name,
                        "common_name": object.common_name,
                        "id": object.metadata.id,
                        "source": object.metadata.source,
                        "aliases": object.metadata.aliases,
                        "parent_ids": object.metadata.parent_ids,
                        "alternate_ids": object.metadata.alternate_ids,
                        "alternate_sources": object.metadata.alternate_sources,
                        "ra_deg": object.ra,
                        "dec_deg": object.dec,
                        "mag": object.mag,
                        "major_arcmin": object.major_arcmin,
                        "minor_arcmin": object.minor_arcmin,
                        "position_angle_deg": object.position_angle_deg,
                    })
                })
                .collect::<Vec<_>>();
            println!("{}", serde_json::to_string_pretty(&values)?);
        }
        CatalogOutputFormat::Csv => {
            println!(
                "matched_name,kind,name,common_name,ra_deg,dec_deg,mag,major_arcmin,id,source"
            );
            for item in &matches {
                let object = &item.object;
                println!(
                    "{},{},{},{},{:.8},{:.8},{},{},{},{}",
                    csv_field(&item.matched_name),
                    object.kind.as_str(),
                    csv_field(&object.name),
                    csv_field(&object.common_name),
                    object.ra,
                    object.dec,
                    csv_optional(object.mag),
                    csv_optional(object.major_arcmin),
                    csv_field(&object.metadata.id),
                    csv_field(&object.metadata.source),
                );
            }
        }
    }
    Ok(())
}

fn catalog_object_all_sources(
    catalog: &ObjectCatalog,
    matches: &[seiza::objects::ObjectNameMatch],
    format: CatalogOutputFormat,
) -> Result<()> {
    match format {
        CatalogOutputFormat::Json => {
            let values = matches
                .iter()
                .map(|item| {
                    let details = catalog.object_details(&item.object.metadata.id)?;
                    Ok(serde_json::json!({
                        "matched_name": item.matched_name,
                        "canonical": item.object,
                        "details": details,
                    }))
                })
                .collect::<Result<Vec<_>>>()?;
            println!("{}", serde_json::to_string_pretty(&values)?);
        }
        CatalogOutputFormat::Csv => {
            println!(
                "canonical_id,record_id,source,name,ra_deg,dec_deg,mag,major_arcmin,minor_arcmin,position_angle_deg"
            );
            for item in matches {
                for record in catalog.catalog_records(&item.object.metadata.id)? {
                    println!(
                        "{},{},{},{},{:.8},{:.8},{},{},{},{}",
                        csv_field(&item.object.metadata.id),
                        csv_field(&record.id),
                        csv_field(&record.source),
                        csv_field(&record.object.name),
                        record.object.ra,
                        record.object.dec,
                        csv_optional(record.object.mag),
                        csv_optional(record.object.major_arcmin),
                        csv_optional(record.object.minor_arcmin),
                        csv_optional(record.object.position_angle_deg),
                    );
                }
            }
        }
        CatalogOutputFormat::Table => {
            println!(
                "{} object name match{} (format v{}):",
                matches.len(),
                if matches.len() == 1 { "" } else { "es" },
                catalog.format_version(),
            );
            for item in matches {
                println!(
                    "\n{} [{}]  {}  {:.5}, {:+.5}",
                    item.object.name,
                    item.object.metadata.id,
                    item.object.kind.as_str(),
                    item.object.ra,
                    item.object.dec,
                );
                let Some(details) = catalog.object_details(&item.object.metadata.id)? else {
                    println!("  no source detail section");
                    continue;
                };
                println!("  selections:");
                for selection in &details.selections {
                    println!(
                        "    {:?}: source={} geometry={}{}",
                        selection.facet,
                        selection.source_record_id.as_deref().unwrap_or("-"),
                        selection.geometry_id.as_deref().unwrap_or("-"),
                        if selection.reason.is_empty() {
                            String::new()
                        } else {
                            format!(" ({})", selection.reason)
                        },
                    );
                }
                println!("  source records:");
                for record in &details.source_records {
                    println!(
                        "    {}  {}  {}  {:.5}, {:+.5}  size={}x{} pa={}",
                        record.id,
                        record.source,
                        record.object.name,
                        record.object.ra,
                        record.object.dec,
                        display_optional(record.object.major_arcmin),
                        display_optional(record.object.minor_arcmin),
                        display_optional(record.object.position_angle_deg),
                    );
                }
                println!("  geometries:");
                for geometry in &details.geometries {
                    let description = match &geometry.data {
                        seiza::objects::GeometryData::Point { .. } => "point".to_string(),
                        seiza::objects::GeometryData::Ellipse {
                            major_arcmin,
                            minor_arcmin,
                            position_angle_deg,
                            ..
                        } => format!(
                            "ellipse {}x{} arcmin pa={}",
                            major_arcmin,
                            display_optional(*minor_arcmin),
                            display_optional(*position_angle_deg),
                        ),
                        seiza::objects::GeometryData::OutlineSet { level, contours } => format!(
                            "outline-set {} contours, {} vertices, {}",
                            contours.len(),
                            contours
                                .iter()
                                .map(|contour| contour.vertices.len())
                                .sum::<usize>(),
                            level.as_deref().unwrap_or("no level"),
                        ),
                    };
                    println!(
                        "    {}  {:?}/{:?}  {}",
                        geometry.id, geometry.role, geometry.quality, description
                    );
                }
            }
        }
    }
    Ok(())
}

fn display_optional(value: Option<f32>) -> String {
    value.map_or_else(|| "-".into(), |value| format!("{value}"))
}

fn fits_info(path: &std::path::Path, stretch: Option<&std::path::Path>) -> Result<()> {
    let started = std::time::Instant::now();
    let image = open_astronomy_image(path)?;
    let load_time = started.elapsed();
    println!(
        "{}: {}x{} ({:?}-type pixels), loaded in {:.0}ms",
        path.display(),
        image.width,
        image.height,
        match image.pixels {
            seiza_fits::Pixels::U8(_) => "u8",
            seiza_fits::Pixels::U16(_) => "u16",
            seiza_fits::Pixels::I32(_) => "i32",
            seiza_fits::Pixels::F32(_) => "f32",
            seiza_fits::Pixels::F64(_) => "f64",
        },
        load_time.as_secs_f64() * 1000.0
    );
    for key in [
        "OBJECT", "FILTER", "EXPOSURE", "EXPTIME", "GAIN", "CCD-TEMP", "RA", "DEC", "INSTRUME",
        "TELESCOP", "DATE-OBS", "BAYERPAT",
    ] {
        if let Some(value) = image.header(key) {
            println!("  {key:<10} {value:?}");
        }
    }
    let started = std::time::Instant::now();
    let stats = image.statistics();
    println!(
        "  stats: median {} mad {:.0} mean {:.0} range {}..{} ({:.0}ms)",
        stats.median,
        stats.mad,
        stats.mean,
        stats.min,
        stats.max,
        started.elapsed().as_secs_f64() * 1000.0
    );
    if let Some(out) = stretch {
        let started = std::time::Instant::now();
        let data = image.stretch_to_u8(&seiza_fits::StretchParams::default());
        let elapsed = started.elapsed();
        image::GrayImage::from_raw(image.width as u32, image.height as u32, data)
            .ok_or_else(|| anyhow::anyhow!("dimension mismatch"))?
            .save(out)?;
        println!(
            "  stretched preview written to {} ({:.0}ms stretch)",
            out.display(),
            elapsed.as_secs_f64() * 1000.0
        );
    }
    Ok(())
}

fn detect(
    path: &std::path::Path,
    sigma: f32,
    max_stars: usize,
    detection_backend: DetectBackend,
    annotate: Option<&std::path::Path>,
) -> Result<()> {
    let img = load_image(path, detection_backend)?;
    let config = DetectConfig {
        backend: detection_backend,
        sigma,
        max_stars,
        ..Default::default()
    };
    let stars = img.detect_stars(&config);

    println!("{} stars detected in {}", stars.len(), path.display());
    println!(
        "{:>10} {:>10} {:>12} {:>8} {:>6}",
        "x", "y", "flux", "peak", "area"
    );
    for star in &stars {
        println!(
            "{:>10.2} {:>10.2} {:>12.1} {:>8.3} {:>6}",
            star.x, star.y, star.flux, star.peak, star.area
        );
    }

    if let Some(out) = annotate {
        let mut canvas = img.to_rgb8();
        for star in &stars {
            let radius = (star.area as f32).sqrt().max(6.0) as i32 + 4;
            imageproc::drawing::draw_hollow_circle_mut(
                &mut canvas,
                (star.x.round() as i32, star.y.round() as i32),
                radius,
                image::Rgb([64, 255, 64]),
            );
        }
        canvas
            .save(out)
            .with_context(|| format!("failed to write {}", out.display()))?;
        println!("annotated image written to {}", out.display());
    }

    Ok(())
}

fn cone(data: &std::path::Path, ra: f64, dec: f64, radius: f64, limit: usize) -> Result<()> {
    let catalog =
        TileCatalog::open(data).with_context(|| format!("failed to open {}", data.display()))?;
    println!(
        "{} stars in catalog, epoch {}",
        catalog.star_count(),
        catalog.epoch()
    );
    let stars = catalog.cone_search(ra, dec, radius, limit);
    println!("{} stars within {radius}° of ({ra}, {dec}):", stars.len());
    println!("{:>12} {:>12} {:>7}", "ra", "dec", "mag");
    for star in stars {
        println!("{:>12.6} {:>12.6} {:>7.3}", star.ra, star.dec, star.mag);
    }
    Ok(())
}

fn parse_sky_coordinate(value: &str) -> std::result::Result<SkyCoordinate, String> {
    let (ra, dec) = value
        .split_once(',')
        .ok_or_else(|| "expected RA,DEC in decimal degrees".to_string())?;
    let ra = ra
        .trim()
        .parse::<f64>()
        .map_err(|_| format!("invalid RA in {value:?}"))?;
    let dec = dec
        .trim()
        .parse::<f64>()
        .map_err(|_| format!("invalid Dec in {value:?}"))?;
    Ok(SkyCoordinate { ra, dec })
}

fn parse_object_kind(value: &str) -> std::result::Result<ObjectKind, String> {
    match value.trim().to_ascii_lowercase().as_str() {
        "galaxy" => Ok(ObjectKind::Galaxy),
        "open-cluster" => Ok(ObjectKind::OpenCluster),
        "globular-cluster" => Ok(ObjectKind::GlobularCluster),
        "nebula" => Ok(ObjectKind::Nebula),
        "planetary-nebula" => Ok(ObjectKind::PlanetaryNebula),
        "hii" | "hii-region" => Ok(ObjectKind::HiiRegion),
        "snr" | "supernova-remnant" => Ok(ObjectKind::SupernovaRemnant),
        "dark-nebula" => Ok(ObjectKind::DarkNebula),
        "cluster-nebula" => Ok(ObjectKind::ClusterWithNebula),
        "star" => Ok(ObjectKind::Star),
        "double-star" => Ok(ObjectKind::DoubleStar),
        "association" => Ok(ObjectKind::Association),
        "other" => Ok(ObjectKind::Other),
        "transient" => Ok(ObjectKind::Transient),
        _ => Err(format!(
            "unknown kind {value:?}; expected galaxy, open-cluster, globular-cluster, \
             nebula, planetary-nebula, hii-region, supernova-remnant, dark-nebula, \
             cluster-nebula, star, double-star, association, other, or transient"
        )),
    }
}

fn catalog_objects(args: CatalogObjectsArgs) -> Result<()> {
    if args.max_mag.is_some_and(|value| !value.is_finite()) {
        anyhow::bail!("--max-mag must be finite");
    }
    if args
        .min_size
        .is_some_and(|value| !value.is_finite() || value < 0.0)
    {
        anyhow::bail!("--min-size must be finite and non-negative");
    }

    let cone_requested = args.ra.is_some() || args.dec.is_some() || args.radius.is_some();
    let region = if !args.corner.is_empty() {
        if cone_requested {
            anyhow::bail!("--corner conflicts with --ra, --dec, and --radius");
        }
        SkyRegion::Polygon {
            vertices: args
                .corner
                .iter()
                .map(|corner| (corner.ra, corner.dec))
                .collect(),
        }
    } else {
        let (Some(ra), Some(dec), Some(radius_deg)) = (args.ra, args.dec, args.radius) else {
            anyhow::bail!(
                "specify either --ra/--dec/--radius or at least three ordered --corner RA,DEC values"
            );
        };
        SkyRegion::Cone {
            center: (ra, dec),
            radius_deg,
        }
    };

    let data = with_data_flag_hint(data_paths::objects(args.data.as_deref()))?;
    let catalog =
        ObjectCatalog::open(&data).with_context(|| format!("failed to open {}", data.display()))?;
    let query = ObjectQuery {
        kinds: args.kind,
        max_mag: args.max_mag,
        min_major_arcmin: args.min_size,
        common_name_only: args.common_name_only,
        include_extent_overlaps: !args.center_only,
        limit: (args.limit > 0).then_some(args.limit),
        sort: args.sort.into(),
    };
    let hits = catalog
        .query_region(&region, &query)
        .map_err(|error| anyhow::anyhow!(error))?;

    match args.format {
        CatalogOutputFormat::Table => {
            println!(
                "{} of {} catalog objects matched:",
                hits.len(),
                catalog.len()
            );
            println!(
                "{:>6} {:<7} {:<20} {:<30} {:>10} {:>10} {:>7} {:>8}",
                "score", "match", "kind", "object", "ra", "dec", "mag", "size'"
            );
            for hit in &hits {
                let object = &hit.object;
                let name = if object.common_name.is_empty() {
                    object.name.clone()
                } else {
                    format!("{} ({})", object.name, object.common_name)
                };
                let mag = object
                    .mag
                    .map(|value| format!("{value:.2}"))
                    .unwrap_or_else(|| "-".to_string());
                let size = object
                    .major_arcmin
                    .map(|value| format!("{value:.1}"))
                    .unwrap_or_else(|| "-".to_string());
                println!(
                    "{:>5.1}% {:<7} {:<20} {:<30} {:>10.5} {:>10.5} {:>7} {:>8}",
                    hit.predicted_prominence * 100.0,
                    if hit.extent_only { "extent" } else { "center" },
                    object.kind.as_str(),
                    name,
                    object.ra,
                    object.dec,
                    mag,
                    size
                );
            }
        }
        CatalogOutputFormat::Json => {
            let region_json = match &region {
                SkyRegion::Cone { center, radius_deg } => serde_json::json!({
                    "type": "cone",
                    "center": { "ra_deg": center.0, "dec_deg": center.1 },
                    "radius_deg": radius_deg,
                }),
                SkyRegion::Polygon { vertices } => serde_json::json!({
                    "type": "polygon",
                    "vertices": vertices.iter().map(|&(ra, dec)| {
                        serde_json::json!({ "ra_deg": ra, "dec_deg": dec })
                    }).collect::<Vec<_>>(),
                }),
            };
            let objects = hits
                .iter()
                .map(|hit| {
                    let object = &hit.object;
                    serde_json::json!({
                        "kind": object.kind.as_str(),
                        "name": object.name,
                        "common_name": object.common_name,
                        "id": object.metadata.id,
                        "source": object.metadata.source,
                        "aliases": object.metadata.aliases,
                        "parent_ids": object.metadata.parent_ids,
                        "alternate_ids": object.metadata.alternate_ids,
                        "alternate_sources": object.metadata.alternate_sources,
                        "ra_deg": object.ra,
                        "dec_deg": object.dec,
                        "mag": object.mag,
                        "major_arcmin": object.major_arcmin,
                        "minor_arcmin": object.minor_arcmin,
                        "position_angle_deg": object.position_angle_deg,
                        "center_inside": hit.center_inside,
                        "extent_only": hit.extent_only,
                        "distance_from_center_deg": hit.distance_from_center_deg,
                        "predicted_prominence": hit.predicted_prominence,
                    })
                })
                .collect::<Vec<_>>();
            println!(
                "{}",
                serde_json::to_string_pretty(&serde_json::json!({
                    "catalog": data.display().to_string(),
                    "catalog_objects": catalog.len(),
                    "region": region_json,
                    "returned": objects.len(),
                    "objects": objects,
                }))?
            );
        }
        CatalogOutputFormat::Csv => {
            println!(
                "kind,name,common_name,ra_deg,dec_deg,mag,major_arcmin,minor_arcmin,position_angle_deg,match,distance_from_center_deg,predicted_prominence,id,source,aliases,parent_ids,alternate_ids,alternate_sources"
            );
            for hit in &hits {
                let object = &hit.object;
                println!(
                    "{},{},{},{:.8},{:.8},{},{},{},{},{},{:.8},{:.8},{},{},{},{},{},{}",
                    object.kind.as_str(),
                    csv_field(&object.name),
                    csv_field(&object.common_name),
                    object.ra,
                    object.dec,
                    csv_optional(object.mag),
                    csv_optional(object.major_arcmin),
                    csv_optional(object.minor_arcmin),
                    csv_optional(object.position_angle_deg),
                    if hit.extent_only { "extent" } else { "center" },
                    hit.distance_from_center_deg,
                    hit.predicted_prominence,
                    csv_field(&object.metadata.id),
                    csv_field(&object.metadata.source),
                    csv_field(&object.metadata.aliases.join("|")),
                    csv_field(&object.metadata.parent_ids.join("|")),
                    csv_field(&object.metadata.alternate_ids.join("|")),
                    csv_field(&object.metadata.alternate_sources.join("|")),
                );
            }
        }
    }
    Ok(())
}

fn catalog_star(args: CatalogStarArgs) -> Result<()> {
    let data = with_data_flag_hint(data_paths::star_identifiers(args.data.as_deref()))?;
    let catalog = StarIdentifierCatalog::open(&data)
        .with_context(|| format!("failed to open {}", data.display()))?;
    let matches = if args.prefix {
        catalog
            .search_names(&args.query, args.limit)?
            .into_iter()
            .map(StarLookupMatch::Name)
            .collect::<Vec<_>>()
    } else {
        catalog.lookup_query(&args.query)?
    };
    let rows = matches.iter().map(catalog_star_row).collect::<Vec<_>>();

    match args.format {
        CatalogOutputFormat::Table => {
            println!(
                "{} {} match{} for {:?} in {} (epoch J{}):",
                rows.len(),
                if args.prefix { "prefix" } else { "exact" },
                if rows.len() == 1 { "" } else { "es" },
                args.query,
                catalog.attribution(),
                catalog.epoch()
            );
            if !rows.is_empty() {
                println!(
                    "{:<22} {:<18} {:<17} {:<10} {:>11} {:>11} {:>7}  stable ID",
                    "designation", "catalog", "kind", "detail", "ra", "dec", "mag"
                );
                for row in &rows {
                    println!(
                        "{:<22} {:<18} {:<17} {:<10} {:>11.6} {:>11.6} {:>7}  {}",
                        row.designation,
                        row.catalog,
                        row.kind,
                        row.detail,
                        row.ra,
                        row.dec,
                        row.mag
                            .map(|value| format!("{value:.3}"))
                            .unwrap_or_else(|| "-".to_string()),
                        row.stable_id,
                    );
                }
            }
        }
        CatalogOutputFormat::Json => {
            let matches = rows
                .iter()
                .map(|row| {
                    serde_json::json!({
                        "designation": row.designation,
                        "stable_id": row.stable_id,
                        "catalog": row.catalog,
                        "kind": row.kind,
                        "detail": row.detail,
                        "ra_deg": row.ra,
                        "dec_deg": row.dec,
                        "mag": row.mag,
                    })
                })
                .collect::<Vec<_>>();
            println!(
                "{}",
                serde_json::to_string_pretty(&serde_json::json!({
                    "query": args.query,
                    "mode": if args.prefix { "prefix" } else { "exact" },
                    "source": catalog.attribution(),
                    "epoch": catalog.epoch(),
                    "numeric_entries": catalog.numeric_len(),
                    "name_entries": catalog.name_len(),
                    "returned": matches.len(),
                    "matches": matches,
                }))?
            );
        }
        CatalogOutputFormat::Csv => {
            println!("designation,stable_id,catalog,kind,detail,ra_deg,dec_deg,mag,epoch,source");
            for row in &rows {
                println!(
                    "{},{},{},{},{},{:.8},{:.8},{},{},{}",
                    csv_field(&row.designation),
                    csv_field(&row.stable_id),
                    csv_field(&row.catalog),
                    csv_field(&row.kind),
                    csv_field(&row.detail),
                    row.ra,
                    row.dec,
                    row.mag
                        .map(|value| format!("{value:.3}"))
                        .unwrap_or_default(),
                    catalog.epoch(),
                    csv_field(catalog.attribution()),
                );
            }
        }
    }
    Ok(())
}

fn catalog_validate(path: &std::path::Path) -> Result<()> {
    use std::io::Read;

    let mut file =
        std::fs::File::open(path).with_context(|| format!("failed to open {}", path.display()))?;
    let mut magic = [0u8; 8];
    file.read_exact(&mut magic)
        .with_context(|| format!("failed to read catalog header from {}", path.display()))?;

    let summary = match &magic {
        b"SEIZASI1" => {
            let catalog = StarIdentifierCatalog::open(path)?;
            catalog.validate()?;
            format!(
                "stellar identifier sidecar: {} numeric identifiers, {} names",
                catalog.numeric_len(),
                catalog.name_len()
            )
        }
        b"SEIZAST1" | b"SEIZAST2" => {
            let catalog = TileCatalog::open(path)?;
            catalog.validate()?;
            format!("star tile catalog: {} stars", catalog.star_count())
        }
        b"SEIZABI1" => {
            let index = BlindIndex::open(path)?;
            index.validate()?;
            format!("blind-pattern index: {} patterns", index.pattern_count())
        }
        b"SEIZAOB1" | b"SEIZAOB3" | b"SEIZAOB\0" => {
            let catalog = ObjectCatalog::open(path)?;
            catalog.validate()?;
            format!(
                "object catalog v{}: {} objects",
                catalog.format_version(),
                catalog.len()
            )
        }
        b"SEIZAMB1" => {
            let catalog = MinorBodyCatalog::open(path)?;
            catalog.validate()?;
            format!("minor-body catalog: {} bodies", catalog.len())
        }
        _ => anyhow::bail!("{} is not a recognized seiza catalog", path.display()),
    };
    println!("{}: valid {summary}", path.display());
    Ok(())
}

struct CatalogStarRow {
    designation: String,
    stable_id: String,
    catalog: String,
    kind: String,
    detail: String,
    ra: f64,
    dec: f64,
    mag: Option<f32>,
}

fn catalog_star_row(value: &StarLookupMatch<'_>) -> CatalogStarRow {
    match value {
        StarLookupMatch::Identifier(star) => CatalogStarRow {
            designation: star.identifier.to_string(),
            stable_id: star.identifier.stable_id(),
            catalog: star.identifier.namespace().as_str().to_string(),
            kind: "catalog-identifier".to_string(),
            detail: String::new(),
            ra: star.ra,
            dec: star.dec,
            mag: Some(star.mag),
        },
        StarLookupMatch::Name(star) => CatalogStarRow {
            designation: star.designation.to_string(),
            stable_id: star.stable_id.to_string(),
            catalog: star.catalog.as_str().to_string(),
            kind: star.kind.as_str().to_string(),
            detail: star.detail.to_string(),
            ra: star.ra,
            dec: star.dec,
            mag: star.mag,
        },
    }
}

fn csv_field(value: &str) -> String {
    if value.contains([',', '"', '\n', '\r']) {
        format!("\"{}\"", value.replace('"', "\"\""))
    } else {
        value.to_string()
    }
}

fn csv_optional(value: Option<f32>) -> String {
    value.map(|value| value.to_string()).unwrap_or_default()
}

#[allow(clippy::too_many_arguments)]
/// Acquisition time as a JD: an explicit ISO 8601 argument wins, else a
/// FITS-compatible DATE-OBS header. `None` when neither is available.
fn resolve_acquisition_jd(image: &std::path::Path, time: Option<&str>) -> Result<Option<f64>> {
    let text = match time {
        Some(text) => Some(text.to_string()),
        None => {
            if is_astronomy_image_path(image) {
                read_astronomy_headers(image).ok().and_then(|headers| {
                    headers
                        .iter()
                        .find(|(k, _)| k == "DATE-OBS")
                        .and_then(|(_, v)| v.as_str().map(str::to_string))
                })
            } else {
                None
            }
        }
    };
    let Some(text) = text else { return Ok(None) };
    parse_iso_jd(&text)
        .map(Some)
        .ok_or_else(|| anyhow::anyhow!("cannot parse acquisition time {text:?} as ISO 8601"))
}

/// "2025-10-12T08:30:00(.frac)(Z)" to a Julian date.
fn parse_iso_jd(text: &str) -> Option<f64> {
    let text = text.trim().trim_end_matches('Z');
    let (date, clock) = match text.split_once('T') {
        Some((d, t)) => (d, t),
        None => (text, "0:0:0"),
    };
    let mut date_parts = date.split('-');
    let year: i32 = date_parts.next()?.parse().ok()?;
    let month: u32 = date_parts.next()?.parse().ok()?;
    let day: u32 = date_parts.next()?.parse().ok()?;
    let mut clock_parts = clock.split(':');
    let hour: f64 = clock_parts.next()?.parse().ok()?;
    let minute: f64 = clock_parts.next().unwrap_or("0").parse().ok()?;
    let second: f64 = clock_parts.next().unwrap_or("0").parse().ok()?;
    let day_fraction = day as f64 + (hour + minute / 60.0 + second / 3600.0) / 24.0;
    Some(seiza::minor_bodies::julian_date(year, month, day_fraction))
}

#[derive(Debug)]
enum SatelliteInput {
    File(PathBuf),
    CelesTrak { satellite_cache: Option<PathBuf> },
}

#[derive(Debug)]
struct SatelliteSolveRequest {
    input: SatelliteInput,
    exposure: SingleExposure,
    sample_interval_seconds: f64,
    maximum_element_age_seconds: f64,
}

fn resolve_single_exposure(
    image: &std::path::Path,
    explicit_start: Option<&str>,
    explicit_duration: Option<f64>,
    explicit_latitude: Option<f64>,
    explicit_longitude: Option<f64>,
    explicit_altitude: Option<f64>,
) -> Result<SingleExposure> {
    let headers = if is_astronomy_image_path(image) {
        read_astronomy_headers(image).with_context(|| {
            format!("failed to read FITS/XISF metadata from {}", image.display())
        })?
    } else {
        Vec::new()
    };
    resolve_single_exposure_from_headers(
        &headers,
        explicit_start,
        explicit_duration,
        explicit_latitude,
        explicit_longitude,
        explicit_altitude,
    )
}

fn resolve_single_exposure_from_headers(
    headers: &[(String, seiza_fits::HeaderValue)],
    explicit_start: Option<&str>,
    explicit_duration: Option<f64>,
    explicit_latitude: Option<f64>,
    explicit_longitude: Option<f64>,
    explicit_altitude: Option<f64>,
) -> Result<SingleExposure> {
    if explicit_start.is_none() {
        if let Some(timesys) = header_text(headers, "TIMESYS")
            && !timesys.eq_ignore_ascii_case("UTC")
        {
            anyhow::bail!(
                "satellite tracks currently require UTC timestamps; FITS TIMESYS is {timesys:?}"
            );
        }
        if let Some(reference) = header_text(headers, "TREFPOS")
            && !reference.to_ascii_uppercase().starts_with("TOP")
        {
            anyhow::bail!(
                "satellite tracks require topocentric acquisition times; FITS TREFPOS is {reference:?}"
            );
        }
    }

    let observer = match (explicit_latitude, explicit_longitude) {
        (Some(latitude), Some(longitude)) => {
            ObserverLocation::geodetic(latitude, longitude, explicit_altitude.unwrap_or(0.0))?
        }
        (Some(_), None) | (None, Some(_)) => {
            anyhow::bail!("--observer-lat and --observer-lon must be supplied together")
        }
        (None, None) if explicit_altitude.is_some() => {
            anyhow::bail!("--observer-alt-m requires --observer-lat and --observer-lon")
        }
        (None, None) => {
            let itrf = (
                header_number(headers, "OBSGEO-X"),
                header_number(headers, "OBSGEO-Y"),
                header_number(headers, "OBSGEO-Z"),
            );
            let geodetic = (
                header_number(headers, "OBSGEO-B"),
                header_number(headers, "OBSGEO-L"),
                header_number(headers, "OBSGEO-H"),
            );
            let site_alias = (
                header_number(headers, "SITELAT"),
                header_number(headers, "SITELONG"),
                header_number(headers, "SITEALT").unwrap_or(0.0),
            );
            match (itrf, geodetic, site_alias) {
                ((Some(x), Some(y), Some(z)), _, _) => ObserverLocation::itrf_meters(x, y, z)?,
                (_, (Some(latitude), Some(longitude), Some(altitude)), _) => {
                    ObserverLocation::geodetic(latitude, longitude, altitude)?
                }
                (_, _, (Some(latitude), Some(longitude), altitude)) => {
                    ObserverLocation::geodetic(latitude, longitude, altitude)?
                }
                _ => anyhow::bail!(
                    "satellite tracks require an observer location; pass --observer-lat/--observer-lon or provide FITS OBSGEO-X/Y/Z, OBSGEO-B/L/H, or SITELAT/SITELONG"
                ),
            }
        }
    };

    let date_beg = header_text(headers, "DATE-BEG");
    let date_end = header_text(headers, "DATE-END");
    let date_avg = header_text(headers, "DATE-AVG");
    let date_obs = header_text(headers, "DATE-OBS");
    let header_duration = ["XPOSURE", "EXPTIME", "EXPOSURE"]
        .into_iter()
        .find_map(|key| header_number(headers, key));

    if let Some(start) = explicit_start {
        let start = UtcTimestamp::parse(start)?;
        let duration = explicit_duration.or(header_duration).or_else(|| {
            let begin = UtcTimestamp::parse(date_beg?).ok()?;
            let end = UtcTimestamp::parse(date_end?).ok()?;
            Some(end.seconds_since(begin))
        });
        let duration = duration.ok_or_else(|| {
            anyhow::anyhow!(
                "satellite tracks require one shutter-open duration; pass --exposure-seconds or provide FITS DATE-BEG/DATE-END or XPOSURE/EXPTIME"
            )
        })?;
        return Ok(SingleExposure::from_start_and_duration(
            start,
            duration,
            observer,
            ExposureProvenance::Explicit,
        )?);
    }

    if let Some(duration) = explicit_duration {
        if let Some(start) = date_beg.or(date_obs) {
            return Ok(SingleExposure::from_start_and_duration(
                UtcTimestamp::parse(start)?,
                duration,
                observer,
                ExposureProvenance::Explicit,
            )?);
        }
        if let Some(midpoint) = date_avg {
            return Ok(SingleExposure::from_midpoint_and_duration(
                UtcTimestamp::parse(midpoint)?,
                duration,
                observer,
                ExposureProvenance::FitsDateAvgAndExposure,
            )?);
        }
        if let Some(end) = date_end {
            return Ok(SingleExposure::from_end_and_duration(
                UtcTimestamp::parse(end)?,
                duration,
                observer,
                ExposureProvenance::FitsEndAndExposure,
            )?);
        }
        anyhow::bail!(
            "--exposure-seconds also requires --time or a FITS DATE-BEG/DATE-OBS, DATE-AVG, or DATE-END timestamp"
        );
    }

    if let (Some(start), Some(end)) = (date_beg, date_end) {
        return Ok(SingleExposure::new(
            UtcTimestamp::parse(start)?,
            UtcTimestamp::parse(end)?,
            observer,
            ExposureProvenance::FitsBounds,
        )?);
    }

    if let (Some(midpoint), Some(duration)) = (date_avg, header_duration) {
        return Ok(SingleExposure::from_midpoint_and_duration(
            UtcTimestamp::parse(midpoint)?,
            duration,
            observer,
            ExposureProvenance::FitsDateAvgAndExposure,
        )?);
    }

    let duration = header_duration.ok_or_else(|| {
        anyhow::anyhow!(
            "satellite tracks require one shutter-open duration; pass --exposure-seconds or provide FITS XPOSURE/EXPTIME/EXPOSURE (stack integration totals are not accepted)"
        )
    })?;
    if let Some(start) = date_obs.or(date_beg) {
        return Ok(SingleExposure::from_start_and_duration(
            UtcTimestamp::parse(start)?,
            duration,
            observer,
            ExposureProvenance::FitsDateObsAndExposure,
        )?);
    }
    if let Some(end) = date_end {
        return Ok(SingleExposure::from_end_and_duration(
            UtcTimestamp::parse(end)?,
            duration,
            observer,
            ExposureProvenance::FitsEndAndExposure,
        )?);
    }
    anyhow::bail!(
        "satellite tracks require one exposure timestamp; pass --time or provide FITS DATE-BEG/DATE-OBS, DATE-AVG, or DATE-END"
    )
}

fn header_text<'a>(headers: &'a [(String, seiza_fits::HeaderValue)], key: &str) -> Option<&'a str> {
    headers
        .iter()
        .find(|(name, _)| name == key)
        .and_then(|(_, value)| value.as_str())
}

fn header_number(headers: &[(String, seiza_fits::HeaderValue)], key: &str) -> Option<f64> {
    headers
        .iter()
        .find(|(name, _)| name == key)
        .and_then(|(_, value)| value.as_f64())
}

fn load_satellite_catalog(input: SatelliteInput) -> Result<SatelliteCatalog> {
    match input {
        SatelliteInput::File(path) => SatelliteCatalog::open(&path)
            .with_context(|| format!("failed to open satellite elements from {}", path.display())),
        SatelliteInput::CelesTrak { satellite_cache } => {
            let source = match satellite_cache {
                Some(cache) => CelesTrakSource::new(cache)?,
                None => CelesTrakSource::platform_default()?,
            };
            let runtime = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .context("failed to initialize satellite download runtime")?;
            let load = runtime.block_on(source.load_active())?;
            let state = match load.state {
                CacheState::Fresh => "fresh cache",
                CacheState::Downloaded => "downloaded",
                CacheState::StaleFallback => "stale cache fallback",
                CacheState::Cached => "cache-only",
            };
            println!(
                "satellite elements: {} records ({state}, {})",
                load.catalog.len(),
                load.cache_path.display()
            );
            if let Some(warning) = load.warning {
                eprintln!("warning: {warning}");
            }
            Ok(load.catalog)
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn solve_command(
    path: &std::path::Path,
    data: &std::path::Path,
    center: (f64, f64),
    radius: f64,
    scale: f64,
    scale_tolerance: f64,
    sip_order: u8,
    sigma: f32,
    ignore_border: u32,
    detection_backend: DetectBackend,
    detection_fallback: DetectionFallback,
    annotate: Option<&std::path::Path>,
    objects: Option<&std::path::Path>,
    minor_bodies: Option<&std::path::Path>,
    acquisition_jd: Option<f64>,
    satellite_request: Option<SatelliteSolveRequest>,
) -> Result<()> {
    let img = load_image(path, detection_backend)?;
    let dims = img.dimensions();

    let config = DetectConfig {
        backend: detection_backend,
        sigma,
        ignore_border,
        max_stars: 200,
        ..Default::default()
    };
    let mut invocation = SolveInvocation::new(path, &img, config, detection_fallback);
    println!(
        "{} stars detected in {}x{} image",
        invocation.stars().len(),
        dims.0,
        dims.1
    );

    let catalog =
        TileCatalog::open(data).with_context(|| format!("failed to open {}", data.display()))?;
    let hint = SolveHint {
        center,
        radius_deg: radius,
        scale_arcsec_px: scale,
        scale_tolerance,
        sip_order,
    };
    let started = std::time::Instant::now();
    let solution = invocation.solve(|stars| solve(stars, &catalog, &hint, dims))?;
    let elapsed = started.elapsed();

    let wcs = &solution.wcs;
    let (ra, dec) = wcs.pixel_to_world(dims.0 as f64 / 2.0, dims.1 as f64 / 2.0);
    let det = wcs.cd[0][0] * wcs.cd[1][1] - wcs.cd[0][1] * wcs.cd[1][0];
    // Position angle of north, measured in the image
    let north = wcs.world_to_pixel(wcs.crval.0, wcs.crval.1 + 0.01);
    let rotation = north
        .map(|(x, y)| (x - wcs.crpix.0).atan2(-(y - wcs.crpix.1)).to_degrees())
        .unwrap_or(f64::NAN);

    println!("Solved in {:.2}s:", elapsed.as_secs_f64());
    println!(
        "  center     : {} {}  ({ra:.5}°, {dec:.5}°)",
        hms(ra),
        dms(dec)
    );
    println!("  pixel scale: {:.4}\"/px", wcs.scale_arcsec_per_px());
    println!("  rotation   : {rotation:.2}° (north angle in image)");
    println!(
        "  parity     : {}",
        if det > 0.0 { "normal" } else { "mirrored" }
    );
    println!(
        "  quality    : {} stars matched, RMS {:.3}\"",
        solution.matched_stars, solution.rms_arcsec
    );
    if let Some(sip) = &wcs.sip {
        println!(
            "  distortion : SIP order {} (forward and inverse)",
            sip.order
        );
    }
    let footprint = wcs.footprint(dims.0, dims.1);
    println!(
        "  footprint  : {:.4},{:.4} / {:.4},{:.4} / {:.4},{:.4} / {:.4},{:.4}",
        footprint[0].0,
        footprint[0].1,
        footprint[1].0,
        footprint[1].1,
        footprint[2].0,
        footprint[2].1,
        footprint[3].0,
        footprint[3].1
    );

    let placed = match objects {
        Some(path) => {
            let object_catalog = seiza::objects::ObjectCatalog::open(path)
                .with_context(|| format!("failed to open {}", path.display()))?;
            let placed = object_catalog
                .objects_in_footprint(wcs, dims)
                .map_err(|error| anyhow::anyhow!(error))?;
            println!("{} catalog objects in the field:", placed.len());
            for p in &placed {
                let size = match p.object.major_arcmin {
                    Some(major) => format!("{major:.1}'"),
                    None => "-".to_string(),
                };
                let common = if p.object.common_name.is_empty() {
                    String::new()
                } else {
                    format!(" ({})", p.object.common_name)
                };
                println!(
                    "  {:<12} {:>18} {:>8} at ({:.0}, {:.0}){common}",
                    p.object.kind.as_str(),
                    p.object.name,
                    size,
                    p.x,
                    p.y
                );
            }
            placed
        }
        None => Vec::new(),
    };

    let moving = if let Some(path) = minor_bodies {
        match acquisition_jd {
            Some(jd) => {
                let catalog = seiza::minor_bodies::MinorBodyCatalog::open(path)
                    .with_context(|| format!("failed to open {}", path.display()))?;
                let moving = catalog.objects_in_footprint(wcs, dims, jd, 20.0);
                println!("{} minor bodies in the field at JD {jd:.4}:", moving.len());
                for m in &moving {
                    let kind = match m.body.kind {
                        seiza::minor_bodies::MinorBodyKind::Comet => "comet",
                        seiza::minor_bodies::MinorBodyKind::Asteroid => "asteroid",
                    };
                    let rate = match m.motion_arcsec_per_hour {
                        Some(rate) => format!("{rate:.1}\"/hr"),
                        None => "-".to_string(),
                    };
                    println!(
                        "  {:<9} {:<32} V~{:>4.1} at ({:.0}, {:.0})  {:.3} AU  {rate}",
                        kind, m.body.name, m.mag, m.x, m.y, m.delta_au
                    );
                }
                moving
            }
            None => {
                println!(
                    "minor bodies skipped: no acquisition time (pass --time or use a FITS with DATE-OBS)"
                );
                Vec::new()
            }
        }
    } else {
        Vec::new()
    };

    let satellite_tracks = if let Some(request) = satellite_request {
        let satellite_catalog = load_satellite_catalog(request.input)?;
        if satellite_catalog.retrieved_at().is_none() {
            println!(
                "satellite elements: {} records ({})",
                satellite_catalog.len(),
                satellite_catalog.source()
            );
        }
        println!(
            "single exposure: {} to {} ({:.3}s, {:?})",
            request.exposure.start_utc.to_rfc3339(),
            request.exposure.end_utc.to_rfc3339(),
            request.exposure.duration_seconds(),
            request.exposure.provenance
        );
        let result = satellite_catalog.tracks_in_footprint(
            wcs,
            dims,
            &request.exposure,
            &TrackOptions {
                sample_interval_seconds: request.sample_interval_seconds,
                maximum_element_age_seconds: Some(request.maximum_element_age_seconds),
                ..TrackOptions::default()
            },
        )?;
        println!(
            "{} predicted satellite tracks in the field ({} elements considered):",
            result.tracks.len(),
            result.elements_considered
        );
        let mut undersampled = 0usize;
        for track in &result.tracks {
            let minimum_range = track
                .samples
                .iter()
                .map(|sample| sample.range_km)
                .fold(f64::INFINITY, f64::min);
            let illumination = match track.maximum_sunlight_fraction() {
                value if value <= 0.01 => "eclipsed",
                value if value >= 0.99 => "sunlit",
                _ => "partly sunlit",
            };
            let rate = track
                .maximum_apparent_rate_arcsec_per_second()
                .map(|rate| format!("{:>6.0}\"/s", rate))
                .unwrap_or_else(|| "      —".into());
            println!(
                "  {:<36} max el {:>5.1}°  {:>7.0} km  {rate}  {:<13} elements {:+.1}h",
                track.identity.display_label(),
                track.maximum_elevation_deg(),
                minimum_range,
                illumination,
                track.element_age_seconds / 3600.0
            );
            if let Some(pixel_rate) = track.maximum_pixel_rate_px_per_second()
                && pixel_rate > 0.0
                && track.clipped_length_px() / pixel_rate < 2.0 * track.sample_interval_seconds
            {
                undersampled += 1;
            }
        }
        if undersampled != 0 {
            eprintln!(
                "warning: {undersampled} track(s) cross the field in under two sample intervals; pass a smaller --satellite-sample-seconds for time-resolved samples"
            );
        }
        if result.propagation_failures != 0 {
            println!(
                "  {} element records could not be propagated",
                result.propagation_failures
            );
        }
        if result.stale_elements != 0 {
            eprintln!(
                "warning: {} element records were outside the {:.1}h maximum age",
                result.stale_elements,
                request.maximum_element_age_seconds / 3600.0
            );
            if result.stale_elements == result.elements_considered {
                eprintln!(
                    "warning: no element set is close enough to this exposure; use historical OMM/TLE data rather than current CelesTrak elements"
                );
            }
        }
        result.tracks
    } else {
        Vec::new()
    };

    if let Some(out) = annotate {
        let mut canvas = img.to_rgb8();
        for star in invocation.stars() {
            imageproc::drawing::draw_hollow_circle_mut(
                &mut canvas,
                (star.x.round() as i32, star.y.round() as i32),
                10,
                image::Rgb([64, 255, 64]),
            );
        }
        let fov = (dims.0 as f64).hypot(dims.1 as f64) / 2.0 * wcs.scale_arcsec_per_px() / 3600.0;
        for cat_star in catalog.cone_search(ra, dec, fov, 300) {
            if let Some((x, y)) = wcs.world_to_pixel(cat_star.ra, cat_star.dec)
                && x >= 0.0
                && y >= 0.0
                && x < dims.0 as f64
                && y < dims.1 as f64
            {
                imageproc::drawing::draw_hollow_circle_mut(
                    &mut canvas,
                    (x.round() as i32, y.round() as i32),
                    6,
                    image::Rgb([255, 64, 64]),
                );
            }
        }
        for p in &placed {
            // An asymmetric extent with an unknown position angle must not be
            // drawn at a guessed orientation; fall back to the conservative
            // major-axis circle.
            let (semi_minor_px, angle_deg) = match p.angle_deg {
                Some(angle) => (p.semi_minor_px, angle),
                None => (p.semi_major_px, 0.0),
            };
            draw_rotated_ellipse(
                &mut canvas,
                (p.x, p.y),
                p.semi_major_px.max(12.0),
                semi_minor_px.max(12.0),
                angle_deg,
                image::Rgb([64, 220, 255]),
            );
        }
        for m in &moving {
            let center = (m.x, m.y);
            imageproc::drawing::draw_hollow_circle_mut(
                &mut canvas,
                (m.x.round() as i32, m.y.round() as i32),
                8,
                image::Rgb([255, 200, 64]),
            );
            // Arrow along the characteristic direction, one hour of
            // apparent motion long, clamped so slow movers stay legible
            // and near-Earth flybys don't shoot off the frame.
            if let (Some(pa), Some(rate)) = (m.direction_pa_deg, m.motion_arcsec_per_hour) {
                let max_px = (dims.0 as f64).hypot(dims.1 as f64) * 0.06;
                let length = (rate / wcs.scale_arcsec_per_px()).clamp(14.0, max_px);
                draw_motion_arrow(&mut canvas, center, (m.ra, m.dec), wcs, pa, length);
            }
        }
        for track in &satellite_tracks {
            draw_satellite_track(&mut canvas, track);
        }
        canvas
            .save(out)
            .with_context(|| format!("failed to write {}", out.display()))?;
        println!("annotated image written to {}", out.display());
    }
    Ok(())
}

fn draw_satellite_track(canvas: &mut image::RgbImage, track: &SatelliteTrack) {
    let color = image::Rgb([255, 72, 220]);
    for segment in &track.clipped_segments {
        imageproc::drawing::draw_line_segment_mut(
            canvas,
            (segment.start.x as f32, segment.start.y as f32),
            (segment.end.x as f32, segment.end.y as f32),
            color,
        );
    }
    let Some(first) = track.clipped_segments.first() else {
        return;
    };
    let last = track
        .clipped_segments
        .last()
        .expect("a first satellite segment implies a last segment");
    imageproc::drawing::draw_hollow_circle_mut(
        canvas,
        (first.start.x.round() as i32, first.start.y.round() as i32),
        4,
        color,
    );
    draw_pixel_arrowhead(canvas, last.start, last.end, color);

    let label = track.identity.display_label();
    let label = label.chars().take(28).collect::<String>();
    let width = (label.chars().count() * 8 * 2) as i32;
    let x = (first.start.x.round() as i32 + 7).clamp(1, (canvas.width() as i32 - width - 1).max(1));
    let y = (first.start.y.round() as i32 + 7).clamp(1, (canvas.height() as i32 - 17).max(1));
    draw_bitmap_text(canvas, x + 1, y + 1, &label, image::Rgb([0, 0, 0]), 2);
    draw_bitmap_text(canvas, x, y, &label, color, 2);
}

fn draw_pixel_arrowhead(
    canvas: &mut image::RgbImage,
    from: seiza_satellites::PixelPoint,
    tip: seiza_satellites::PixelPoint,
    color: image::Rgb<u8>,
) {
    let dx = tip.x - from.x;
    let dy = tip.y - from.y;
    if dx.hypot(dy) < 1.0e-6 {
        return;
    }
    let angle = dy.atan2(dx);
    for offset in [-150.0_f64, 150.0] {
        let wing = angle + offset.to_radians();
        imageproc::drawing::draw_line_segment_mut(
            canvas,
            (tip.x as f32, tip.y as f32),
            (
                (tip.x + wing.cos() * 7.0) as f32,
                (tip.y + wing.sin() * 7.0) as f32,
            ),
            color,
        );
    }
}

fn draw_bitmap_text(
    canvas: &mut image::RgbImage,
    x: i32,
    y: i32,
    text: &str,
    color: image::Rgb<u8>,
    scale: i32,
) {
    use font8x8::UnicodeFonts;

    for (character_index, character) in text.chars().enumerate() {
        let character = if character.is_ascii() { character } else { '?' };
        let Some(glyph) = font8x8::BASIC_FONTS.get(character) else {
            continue;
        };
        let origin_x = x + character_index as i32 * 8 * scale;
        for (row, bits) in glyph.into_iter().enumerate() {
            for column in 0..8 {
                if bits & (1 << column) == 0 {
                    continue;
                }
                for offset_y in 0..scale {
                    for offset_x in 0..scale {
                        let pixel_x = origin_x + column * scale + offset_x;
                        let pixel_y = y + row as i32 * scale + offset_y;
                        if pixel_x >= 0
                            && pixel_y >= 0
                            && pixel_x < canvas.width() as i32
                            && pixel_y < canvas.height() as i32
                        {
                            canvas.put_pixel(pixel_x as u32, pixel_y as u32, color);
                        }
                    }
                }
            }
        }
    }
}

/// Draw an arrow from a body's pixel position along a sky position angle
/// (degrees east of north). `length_px` is the *displayed* shaft length —
/// the caller derives it from the apparent rate and clamps it; the sky
/// PA is converted to a pixel direction through the WCS so flips and
/// rotation come out right.
fn draw_motion_arrow(
    canvas: &mut image::RgbImage,
    from: (f64, f64),
    sky: (f64, f64),
    wcs: &seiza::wcs::Wcs,
    pa_deg: f64,
    length_px: f64,
) {
    // Step a small angle along the position angle on the sphere and
    // project it: the great-circle destination formula, with the step
    // small enough that the pixel direction is exact at any latitude.
    let (sin_pa, cos_pa) = pa_deg.to_radians().sin_cos();
    let dec1 = sky.1.to_radians();
    let delta = (30.0_f64 / 3600.0).to_radians();
    let dec2 = (dec1.sin() * delta.cos() + dec1.cos() * delta.sin() * cos_pa).asin();
    let ra2 = sky.0.to_radians()
        + (sin_pa * delta.sin() * dec1.cos()).atan2(delta.cos() - dec1.sin() * dec2.sin());
    let Some((px, py)) = wcs.world_to_pixel(ra2.to_degrees(), dec2.to_degrees()) else {
        return;
    };
    let (dx, dy) = (px - from.0, py - from.1);
    let norm = dx.hypot(dy);
    if norm < 1e-9 {
        return;
    }
    let (ux, uy) = (dx / norm, dy / norm);
    let tip = (from.0 + ux * length_px, from.1 + uy * length_px);
    let color = image::Rgb([255, 200, 64]);
    imageproc::drawing::draw_line_segment_mut(
        canvas,
        (from.0 as f32, from.1 as f32),
        (tip.0 as f32, tip.1 as f32),
        color,
    );
    let head = 6.0_f64.min(length_px * 0.4);
    for sign in [1.0_f64, -1.0] {
        let angle = uy.atan2(ux) + sign * 150.0_f64.to_radians();
        imageproc::drawing::draw_line_segment_mut(
            canvas,
            (tip.0 as f32, tip.1 as f32),
            (
                (tip.0 + angle.cos() * head) as f32,
                (tip.1 + angle.sin() * head) as f32,
            ),
            color,
        );
    }
}

fn draw_rotated_ellipse(
    canvas: &mut image::RgbImage,
    center: (f64, f64),
    semi_major: f64,
    semi_minor: f64,
    angle_deg: f64,
    color: image::Rgb<u8>,
) {
    let (sin_r, cos_r) = angle_deg.to_radians().sin_cos();
    let segments = 72;
    let point = |i: usize| -> (f32, f32) {
        let t = i as f64 / segments as f64 * std::f64::consts::TAU;
        let (lx, ly) = (semi_major * t.cos(), semi_minor * t.sin());
        (
            (center.0 + lx * cos_r - ly * sin_r) as f32,
            (center.1 + lx * sin_r + ly * cos_r) as f32,
        )
    };
    for i in 0..segments {
        imageproc::drawing::draw_line_segment_mut(canvas, point(i), point(i + 1), color);
    }
}

fn hms(ra: f64) -> String {
    let hours = ra.rem_euclid(360.0) / 15.0;
    let h = hours.floor();
    let m = ((hours - h) * 60.0).floor();
    let sec = (hours - h - m / 60.0) * 3600.0;
    format!("{:02}h {:02}m {:05.2}s", h as u32, m as u32, sec)
}

fn dms(dec: f64) -> String {
    let sign = if dec < 0.0 { '-' } else { '+' };
    let a = dec.abs();
    let d = a.floor();
    let m = ((a - d) * 60.0).floor();
    let sec = (a - d - m / 60.0) * 3600.0;
    format!("{sign}{:02}° {:02}′ {:04.1}″", d as u32, m as u32, sec)
}

struct SolveBlindOptions<'a> {
    index_path: Option<&'a std::path::Path>,
    min_scale: f64,
    max_scale: f64,
    index_mag_limit: f32,
    max_hypotheses: usize,
    max_coarse_hypotheses: usize,
    sip_order: u8,
    sigma: f32,
    ignore_border: u32,
    detection_backend: DetectBackend,
    detection_fallback: DetectionFallback,
    detection_fallback_hypotheses: usize,
}

fn solve_blind_command(
    path: &std::path::Path,
    data: &std::path::Path,
    options: SolveBlindOptions<'_>,
) -> Result<()> {
    use seiza::blind::{BlindIndex, BlindParams, solve_blind};

    let img = load_image(path, options.detection_backend)?;
    let dims = img.dimensions();
    let config = DetectConfig {
        backend: options.detection_backend,
        sigma: options.sigma,
        ignore_border: options.ignore_border,
        max_stars: 600,
        ..Default::default()
    };
    let can_retry_f32 = options.detection_fallback == DetectionFallback::F32
        && auto_can_retry_f32(path, &img, options.detection_backend);
    let mut invocation = SolveInvocation::new(path, &img, config, options.detection_fallback);
    println!(
        "{} stars detected in {}x{} image",
        invocation.stars().len(),
        dims.0,
        dims.1
    );

    let catalog =
        TileCatalog::open(data).with_context(|| format!("failed to open {}", data.display()))?;
    let mut params = BlindParams {
        min_scale_arcsec_px: options.min_scale,
        max_scale_arcsec_px: options.max_scale,
        index_mag_limit: options.index_mag_limit,
        max_hypotheses: options.max_hypotheses,
        max_coarse_hypotheses: options.max_coarse_hypotheses,
        sip_order: options.sip_order,
        ..Default::default()
    };
    let started = std::time::Instant::now();
    let index = if let Some(path) = options.index_path {
        let index = BlindIndex::open(path)
            .map_err(anyhow::Error::from)
            .with_context(|| format!("failed to open {}", path.display()))?;
        params.index_mag_limit = index.index_mag_limit();
        params.max_pattern_deg = index.max_pattern_deg();
        warn_on_index_catalog_mismatch(&index, &catalog);
        println!(
            "pattern index: {} patterns mapped from {} in {:.2}s (G<={:.1})",
            index.pattern_count(),
            path.display(),
            started.elapsed().as_secs_f64(),
            index.index_mag_limit()
        );
        index
    } else {
        let index = BlindIndex::build(&catalog, &params);
        println!(
            "pattern index: {} patterns built in {:.2}s",
            index.pattern_count(),
            started.elapsed().as_secs_f64()
        );
        index
    };

    let started = std::time::Instant::now();
    let solution = invocation.solve_with_pass(|stars, pass| {
        let attempt_params = blind_params_for_detection_pass(
            &params,
            can_retry_f32,
            options.detection_fallback_hypotheses,
            pass,
        );
        solve_blind(stars, &catalog, &index, &attempt_params, dims)
    })?;
    let wcs = &solution.wcs;
    let (ra, dec) = wcs.pixel_to_world(dims.0 as f64 / 2.0, dims.1 as f64 / 2.0);
    println!("Blind-solved in {:.2}s:", started.elapsed().as_secs_f64());
    println!(
        "  center     : {} {}  ({ra:.5}°, {dec:.5}°)",
        hms(ra),
        dms(dec)
    );
    println!("  pixel scale: {:.4}\"/px", wcs.scale_arcsec_per_px());
    println!(
        "  quality    : {} stars matched, RMS {:.3}\"",
        solution.matched_stars, solution.rms_arcsec
    );
    Ok(())
}

/// A blind index only produces hypotheses its build catalog supports; a
/// much shallower runtime catalog cannot verify the deep tiers and every
/// solve pays the full failure path with no diagnostic.
fn warn_on_index_catalog_mismatch(
    index: &seiza::blind::BlindIndex,
    catalog: &seiza::catalog::TileCatalog,
) {
    let built_from = index.source_star_count();
    let runtime = catalog.star_count();
    if built_from > 0 && runtime > 0 && built_from.max(runtime) > 2 * built_from.min(runtime) {
        eprintln!(
            "warning: blind index was built from a {built_from}-star catalog but solving \
             against {runtime} stars; deep-tier hypotheses may never verify"
        );
    }
}

fn build_blind_index_command(
    data: &std::path::Path,
    output: &std::path::Path,
    index_mag_limit: f32,
    max_pattern_deg: f64,
) -> Result<()> {
    use seiza::blind::{BlindIndex, BlindParams};

    let catalog =
        TileCatalog::open(data).with_context(|| format!("failed to open {}", data.display()))?;
    let params = BlindParams {
        index_mag_limit,
        max_pattern_deg,
        ..Default::default()
    };
    let started = std::time::Instant::now();
    let index = BlindIndex::build(&catalog, &params);
    println!(
        "built {} G<={index_mag_limit:.1} patterns in {:.2}s",
        index.pattern_count(),
        started.elapsed().as_secs_f64()
    );
    let started = std::time::Instant::now();
    index
        .write_to(output)
        .with_context(|| format!("failed to write {}", output.display()))?;
    println!(
        "wrote {} in {:.2}s",
        output.display(),
        started.elapsed().as_secs_f64()
    );
    Ok(())
}

#[cfg(test)]
mod cli_tests {
    use super::*;

    #[test]
    fn stack_requires_multiple_lights_and_linear_output() {
        assert!(
            Cli::try_parse_from(["seiza", "stack", "one.fits", "--output", "stack.fits"]).is_err()
        );
        let cli = Cli::try_parse_from([
            "seiza",
            "stack",
            "one.fits",
            "two.fits",
            "--output",
            "stack.fits",
            "--preview",
            "stack.png",
            "--report",
            "stack-report.json",
            "--normalization",
            "local",
            "--max-registration-drift",
            "512",
            "--max-registration-drift-fraction",
            "0.2",
        ])
        .unwrap();
        assert!(matches!(cli.command, Command::Stack(_)));
    }

    #[test]
    fn color_commands_make_the_composition_mode_explicit() {
        let lrgb = Cli::try_parse_from([
            "seiza",
            "color",
            "lrgb",
            "--luminance",
            "l.fits",
            "--red",
            "r.fits",
            "--green",
            "g.fits",
            "--blue",
            "b.fits",
            "--output",
            "lrgb.fits",
            "--preview",
            "lrgb.png",
        ])
        .unwrap();
        assert!(matches!(lrgb.command, Command::Color(_)));

        let narrowband = Cli::try_parse_from([
            "seiza",
            "color",
            "narrowband",
            "--ha",
            "ha.fits",
            "--oiii",
            "oiii.fits",
            "--palette",
            "foraxx-hoo",
            "--preview",
            "hoo.png",
        ])
        .unwrap();
        assert!(matches!(narrowband.command, Command::Color(_)));
    }

    #[test]
    fn master_commands_require_multiple_calibration_frames() {
        assert!(
            Cli::try_parse_from([
                "seiza",
                "master",
                "bias",
                "one.fits",
                "--output",
                "master-bias.fits",
            ])
            .is_err()
        );
        let cli = Cli::try_parse_from([
            "seiza",
            "master",
            "flat",
            "flat-1.fits",
            "flat-2.fits",
            "--output",
            "master-flat.fits",
            "--bias",
            "master-bias.fits",
            "--dark-flat",
            "master-dark-flat.fits",
            "--report",
            "master-flat.json",
        ])
        .unwrap();
        assert!(matches!(cli.command, Command::Master(_)));
    }

    #[test]
    fn download_data_help_leads_with_the_ready_to_use_route() {
        let error = match Cli::try_parse_from(["seiza", "download-data"]) {
            Ok(_) => panic!("download-data without a selection should show guidance"),
            Err(error) => error,
        };
        let help = error.to_string();
        let prebuilt = help.find("\n  prebuilt").expect("prebuilt command in help");
        let tycho2 = help.find("\n  tycho2").expect("advanced command in help");

        assert!(
            help.starts_with(
                "Download ready-to-use catalogs (recommended) or advanced source data"
            )
        );
        assert!(
            prebuilt < tycho2,
            "prebuilt should be listed first:\n{help}"
        );
        assert!(help.contains("RECOMMENDED:"));
        assert!(help.contains("seiza download-data prebuilt --output <directory>"));
        assert!(help.contains("seiza setup"));
        assert!(help.contains("are not needed to use Seiza"));
    }

    #[test]
    fn detection_backend_is_global_and_selectable() {
        let automatic = Cli::try_parse_from(["seiza", "detect", "image.jpg"]).unwrap();
        let before =
            Cli::try_parse_from(["seiza", "--detection-backend", "f32", "detect", "image.jpg"])
                .unwrap();
        let after =
            Cli::try_parse_from(["seiza", "detect", "image.jpg", "--detection-backend", "u8"])
                .unwrap();
        assert!(matches!(
            automatic.detection_backend,
            DetectionBackendArg::Auto
        ));
        assert!(matches!(before.detection_backend, DetectionBackendArg::F32));
        assert!(matches!(after.detection_backend, DetectionBackendArg::U8));
        assert_eq!(automatic.detection_fallback, DetectionFallback::F32);
        assert_eq!(automatic.detection_fallback_hypotheses, 64);
        let no_fallback = Cli::try_parse_from([
            "seiza",
            "solve-blind",
            "image.fits",
            "--data",
            "stars.bin",
            "--detection-fallback",
            "none",
            "--detection-fallback-hypotheses",
            "0",
        ])
        .unwrap();
        assert_eq!(no_fallback.detection_fallback, DetectionFallback::None);
        assert_eq!(no_fallback.detection_fallback_hypotheses, 0);
        assert!(
            Cli::try_parse_from([
                "seiza",
                "detect",
                "image.jpg",
                "--detection-backend",
                "other",
            ])
            .is_err()
        );
    }

    #[test]
    fn auto_retries_astronomy_and_converted_color_inputs() {
        let rgb = LoadedImage::Dynamic(image::DynamicImage::ImageRgb8(image::RgbImage::new(1, 1)));
        let luma =
            LoadedImage::Dynamic(image::DynamicImage::ImageLuma8(image::GrayImage::new(1, 1)));
        let jpeg = std::path::Path::new("image.jpg");
        let fits = std::path::Path::new("image.fits");
        let xisf = std::path::Path::new("image.XISF");
        assert!(auto_can_retry_f32(jpeg, &rgb, DetectBackend::Auto));
        assert!(!auto_can_retry_f32(jpeg, &rgb, DetectBackend::U8));
        assert!(!auto_can_retry_f32(jpeg, &luma, DetectBackend::Auto));
        assert!(auto_can_retry_f32(fits, &luma, DetectBackend::Auto));
        assert!(!auto_can_retry_f32(fits, &luma, DetectBackend::U8));
        assert!(auto_can_retry_f32(xisf, &luma, DetectBackend::Auto));
        assert_eq!(
            astronomy_image_format(xisf),
            Some(AstronomyImageFormat::Xisf)
        );
        assert!(!is_fits_path(xisf));
    }

    #[test]
    fn fits_loader_keeps_u8_mtf_and_linear_f32_paths_distinct() {
        const CARD: usize = 80;
        let card = |keyword: &str, value: &str| {
            let mut card = [b' '; CARD];
            card[..keyword.len()].copy_from_slice(keyword.as_bytes());
            card[8] = b'=';
            card[9] = b' ';
            card[10..10 + value.len()].copy_from_slice(value.as_bytes());
            card
        };
        let mut bytes = Vec::new();
        for (keyword, value) in [
            ("SIMPLE", "T"),
            ("BITPIX", "16"),
            ("NAXIS", "2"),
            ("NAXIS1", "2"),
            ("NAXIS2", "2"),
            ("BZERO", "32768"),
            ("BSCALE", "1"),
        ] {
            bytes.extend_from_slice(&card(keyword, value));
        }
        let mut end = [b' '; CARD];
        end[..3].copy_from_slice(b"END");
        bytes.extend_from_slice(&end);
        bytes.resize(bytes.len().next_multiple_of(2880), b' ');
        for value in [1000u16, 1001, 32768, 65535] {
            bytes.extend_from_slice(&(value ^ 0x8000).to_be_bytes());
        }

        let path = std::env::temp_dir().join(format!(
            "seiza-cli-detection-backends-{}-{}.fits",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::write(&path, bytes).unwrap();
        let compact = load_image(&path, DetectBackend::Auto).unwrap();
        let precise = load_image(&path, DetectBackend::F32).unwrap();

        assert!(matches!(
            compact,
            LoadedImage::Dynamic(image::DynamicImage::ImageLuma8(_))
        ));
        let LoadedImage::LinearF32 { luma, .. } = precise else {
            panic!("forced f32 astronomy image must retain a linear f32 buffer");
        };
        assert_eq!(luma[0], 1000.0 / u16::MAX as f32);
        assert_eq!(luma[1], 1001.0 / u16::MAX as f32);
        assert_ne!(luma[0], luma[1]);

        let mut invocation = SolveInvocation::new(
            &path,
            &compact,
            DetectConfig::default(),
            DetectionFallback::F32,
        );
        let mut passes = Vec::new();
        invocation
            .solve_with_pass(|_, pass| {
                passes.push(pass);
                if pass == DetectionPass::Primary {
                    Err(seiza::Error::Solve("exercise f32 reload".into()))
                } else {
                    Ok(())
                }
            })
            .unwrap();
        assert_eq!(passes, [DetectionPass::Primary, DetectionPass::F32Fallback]);
        std::fs::remove_file(path).unwrap();
    }

    #[test]
    fn solve_invocation_honors_disabled_fallback() {
        let image =
            LoadedImage::Dynamic(image::DynamicImage::ImageRgb8(image::RgbImage::new(2, 2)));
        let mut invocation = SolveInvocation::new(
            std::path::Path::new("image.jpg"),
            &image,
            DetectConfig::default(),
            DetectionFallback::None,
        );
        let mut passes = Vec::new();
        let result: Result<()> = invocation.solve_with_pass(|_, pass| {
            passes.push(pass);
            Err(seiza::Error::Solve("expected miss".into()))
        });
        assert!(result.is_err());
        assert_eq!(passes, [DetectionPass::Primary]);
    }

    #[test]
    fn blind_fallback_budget_only_caps_the_primary_pass() {
        let params = seiza::blind::BlindParams {
            max_hypotheses: 400,
            ..Default::default()
        };
        let primary = blind_params_for_detection_pass(&params, true, 64, DetectionPass::Primary);
        let fallback =
            blind_params_for_detection_pass(&params, true, 64, DetectionPass::F32Fallback);
        let unavailable =
            blind_params_for_detection_pass(&params, false, 64, DetectionPass::Primary);
        let unlimited = blind_params_for_detection_pass(&params, true, 0, DetectionPass::Primary);

        assert_eq!(primary.max_hypotheses, 64);
        assert_eq!(fallback.max_hypotheses, 400);
        assert_eq!(unavailable.max_hypotheses, 400);
        assert_eq!(unlimited.max_hypotheses, 400);
    }

    #[test]
    fn gaia_accepts_a_separated_negative_magnitude() {
        let cli = Cli::try_parse_from([
            "seiza",
            "download-data",
            "gaia",
            "--output",
            "gaia",
            "--max-mag",
            "-1.5",
            "--chunks",
            "1",
        ])
        .unwrap();

        let Command::DownloadData {
            source: DownloadSource::Gaia {
                max_mag, chunks, ..
            },
        } = cli.command
        else {
            panic!("expected Gaia download command");
        };
        assert_eq!(max_mag, -1.5);
        assert_eq!(chunks, 1);
    }

    #[test]
    fn satellite_history_accepts_multiple_explicit_epochs_and_cache() {
        let cli = Cli::try_parse_from([
            "seiza",
            "download-data",
            "satellite-history",
            "--epoch",
            "2025-10-17T12:50:21Z",
            "2025-10-18T12:51:42Z",
            "--cache",
            "satellite-cache",
            "--origin",
        ])
        .unwrap();

        let Command::DownloadData {
            source:
                DownloadSource::SatelliteHistory {
                    epoch,
                    cache,
                    origin,
                },
        } = cli.command
        else {
            panic!("expected satellite history download command");
        };
        assert_eq!(epoch.len(), 2);
        assert_eq!(cache, Some(PathBuf::from("satellite-cache")));
        assert!(origin);
    }

    #[test]
    fn gaia_rejects_zero_chunks_at_parse_time() {
        assert!(
            Cli::try_parse_from([
                "seiza",
                "download-data",
                "gaia",
                "--output",
                "gaia",
                "--chunks",
                "0",
            ])
            .is_err()
        );
    }

    #[test]
    fn satellite_cli_sources_are_explicit_and_mutually_exclusive() {
        assert!(
            Cli::try_parse_from([
                "seiza",
                "solve",
                "image.fits",
                "--scale",
                "1.5",
                "--satellites-celestrak",
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "seiza",
                "solve",
                "image.fits",
                "--scale",
                "1.5",
                "--satellites",
                "elements.json",
                "--satellites-celestrak",
            ])
            .is_err()
        );
    }

    #[test]
    fn satellite_exposure_prefers_unambiguous_fits_bounds() {
        use seiza_fits::HeaderValue::{Float, String as Text};

        let headers = vec![
            ("DATE-OBS".into(), Text("2024-05-02T11:59:00".into())),
            ("DATE-BEG".into(), Text("2024-05-02T12:00:00".into())),
            ("DATE-END".into(), Text("2024-05-02T12:00:30".into())),
            ("EXPTIME".into(), Float(999.0)),
            ("OBSGEO-B".into(), Float(37.3)),
            ("OBSGEO-L".into(), Float(-122.0)),
            ("OBSGEO-H".into(), Float(50.0)),
        ];
        let exposure =
            resolve_single_exposure_from_headers(&headers, None, None, None, None, None).unwrap();
        assert_eq!(exposure.provenance, ExposureProvenance::FitsBounds);
        assert!((exposure.duration_seconds() - 30.0).abs() < 1.0e-6);
        assert_eq!(
            exposure.start_utc,
            UtcTimestamp::parse("2024-05-02T12:00:00Z").unwrap()
        );
    }

    #[test]
    fn satellite_exposure_falls_back_to_date_obs_and_exptime() {
        use seiza_fits::HeaderValue::{Float, String as Text};

        let headers = vec![
            ("DATE-OBS".into(), Text("2024-05-02T12:00:00".into())),
            ("EXPTIME".into(), Float(45.5)),
            ("OBSGEO-X".into(), Float(-2_700_000.0)),
            ("OBSGEO-Y".into(), Float(-4_300_000.0)),
            ("OBSGEO-Z".into(), Float(3_850_000.0)),
        ];
        let exposure =
            resolve_single_exposure_from_headers(&headers, None, None, None, None, None).unwrap();
        assert_eq!(
            exposure.provenance,
            ExposureProvenance::FitsDateObsAndExposure
        );
        assert!((exposure.duration_seconds() - 45.5).abs() < 1.0e-6);
    }

    #[test]
    fn satellite_exposure_supports_date_avg_as_the_midpoint() {
        use seiza_fits::HeaderValue::{Float, String as Text};

        let headers = vec![
            ("DATE-AVG".into(), Text("2024-05-02T12:00:30".into())),
            ("EXPTIME".into(), Float(60.0)),
            ("OBSGEO-B".into(), Float(37.3)),
            ("OBSGEO-L".into(), Float(-122.0)),
            ("OBSGEO-H".into(), Float(50.0)),
        ];
        let exposure =
            resolve_single_exposure_from_headers(&headers, None, None, None, None, None).unwrap();
        assert_eq!(
            exposure.provenance,
            ExposureProvenance::FitsDateAvgAndExposure
        );
        assert_eq!(
            exposure.start_utc,
            UtcTimestamp::parse("2024-05-02T12:00:00Z").unwrap()
        );
        assert_eq!(
            exposure.end_utc,
            UtcTimestamp::parse("2024-05-02T12:01:00Z").unwrap()
        );
    }

    #[test]
    fn satellite_exposure_supports_date_end_as_shutter_close() {
        use seiza_fits::HeaderValue::{Float, String as Text};

        let headers = vec![
            ("DATE-END".into(), Text("2024-05-02T12:01:00".into())),
            ("EXPTIME".into(), Float(60.0)),
            ("OBSGEO-B".into(), Float(37.3)),
            ("OBSGEO-L".into(), Float(-122.0)),
            ("OBSGEO-H".into(), Float(50.0)),
        ];
        let exposure =
            resolve_single_exposure_from_headers(&headers, None, None, None, None, None).unwrap();
        assert_eq!(exposure.provenance, ExposureProvenance::FitsEndAndExposure);
        assert_eq!(
            exposure.start_utc,
            UtcTimestamp::parse("2024-05-02T12:00:00Z").unwrap()
        );
        assert_eq!(
            exposure.end_utc,
            UtcTimestamp::parse("2024-05-02T12:01:00Z").unwrap()
        );
    }

    #[test]
    fn explicit_satellite_metadata_overrides_fits_time_frame() {
        use seiza_fits::HeaderValue::String as Text;

        let headers = vec![
            ("TIMESYS".into(), Text("TDB".into())),
            ("TREFPOS".into(), Text("BARYCENTER".into())),
        ];
        let exposure = resolve_single_exposure_from_headers(
            &headers,
            Some("2024-05-02T12:00:00Z"),
            Some(10.0),
            Some(37.3),
            Some(-122.0),
            Some(50.0),
        )
        .unwrap();
        assert_eq!(exposure.provenance, ExposureProvenance::Explicit);
        assert!((exposure.duration_seconds() - 10.0).abs() < 1.0e-6);
    }

    #[test]
    fn satellite_exposure_requires_observer_and_one_interval() {
        use seiza_fits::HeaderValue::{Float, String as Text};

        let no_observer = vec![
            ("DATE-OBS".into(), Text("2024-05-02T12:00:00".into())),
            ("EXPTIME".into(), Float(30.0)),
        ];
        assert!(
            resolve_single_exposure_from_headers(&no_observer, None, None, None, None, None)
                .is_err()
        );

        let no_duration = vec![
            ("DATE-OBS".into(), Text("2024-05-02T12:00:00".into())),
            ("OBSGEO-B".into(), Float(37.3)),
            ("OBSGEO-L".into(), Float(-122.0)),
            ("OBSGEO-H".into(), Float(50.0)),
        ];
        assert!(
            resolve_single_exposure_from_headers(&no_duration, None, None, None, None, None)
                .is_err()
        );
    }
}