arcsec 0.3.0

Astrometric plate solver: find where a telescope was pointing from an image
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
//! Command-line definitions: the ASTAP-compatible solver flags and the
//! `arcsec catalog` subcommands.
//!
//! The flag names, the stdout format, the output files and the exit codes are a
//! compatibility contract with ASTAP; change them only deliberately.

use std::ffi::OsString;
use std::path::PathBuf;

use clap::{Arg, ArgAction, Command, value_parser};

pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// ASTAP's multi-letter options, which it spells with a single dash (`-fov 1.2`).
///
/// clap would read `-fov` as `-f ov`, so [`normalize_astap_args`] rewrites these to
/// the double-dash form before parsing. Every one of them was a parse error before
/// the rewrite, so accepting them changes nothing for the double-dash spelling.
const ASTAP_SINGLE_DASH: &[&str] = &[
    "fov", "ra", "spd", "check", "sip", "speed", "wcs", "log", "update", "progress", "analyse",
    "extract", "extract2",
];

/// Rewrite ASTAP's single-dash long options (`-fov`, `-ra`, `-spd`, `-progress`, ...)
/// to the `--fov` form clap expects, so arcsec accepts an `astap_cli` command line
/// verbatim.
///
/// Only a token that is exactly one of those names is rewritten. The first element
/// (the program name) is left alone, and so is everything after a `--`.
pub fn normalize_astap_args<I>(args: I) -> Vec<OsString>
where
    I: IntoIterator<Item = OsString>,
{
    let mut out = Vec::new();
    let mut rest_verbatim = false;
    for (i, arg) in args.into_iter().enumerate() {
        if i == 0 || rest_verbatim {
            out.push(arg);
            continue;
        }
        if arg == "--" {
            rest_verbatim = true;
            out.push(arg);
            continue;
        }
        let rewritten = arg
            .to_str()
            .and_then(|s| s.strip_prefix('-'))
            .filter(|name| ASTAP_SINGLE_DASH.contains(name))
            .map(|name| OsString::from(format!("--{name}")));
        out.push(rewritten.unwrap_or(arg));
    }
    out
}

/// The solver's command line.
pub fn solver_command() -> Command {
    Command::new("arcsec")
        .version(VERSION)
        .about("Astrometric plate solver with an ASTAP-compatible command line")
        .after_help(
            "ASTAP's single-dash spellings (-fov, -ra, -spd, -progress, ...) are accepted too.\n\
             Catalogues: run `arcsec catalog --help`.\n\
             Exit codes: 0 solved, 1 no solution or usage error, 2 too few stars, 16 file error,\n\
             32 star database or index not found, 33 star database read error.",
        )
        .arg(
            Arg::new("file")
                .short('f')
                .value_name("FILE")
                .value_parser(value_parser!(PathBuf))
                .help("Image to solve: FITS, XISF or ASDF"),
        )
        .arg(
            Arg::new("radius")
                .short('r')
                .value_name("DEG")
                .value_parser(value_parser!(f64))
                .allow_hyphen_values(true)
                .default_value("180")
                .help("Radius of the area to search, in degrees"),
        )
        .arg(
            Arg::new("fov")
                .long("fov")
                .value_name("DEG")
                .value_parser(value_parser!(f64))
                .allow_hyphen_values(true)
                .help("Field height in degrees; 0 or absent to take it from the header"),
        )
        .arg(
            Arg::new("ra")
                .long("ra")
                .value_name("HOURS")
                .value_parser(value_parser!(f64))
                .allow_hyphen_values(true)
                .help("Right ascension of the search centre, in hours"),
        )
        .arg(
            Arg::new("spd")
                .long("spd")
                .value_name("DEG")
                .value_parser(value_parser!(f64))
                .allow_hyphen_values(true)
                .help("South pole distance of the search centre (90 + Dec), in degrees"),
        )
        .arg(
            Arg::new("stars")
                .short('s')
                .value_name("N")
                .value_parser(value_parser!(usize))
                .default_value("500")
                .help("Maximum number of stars to use"),
        )
        .arg(
            Arg::new("tolerance")
                .short('t')
                .value_name("TOL")
                .value_parser(value_parser!(f64))
                .default_value("0.007")
                .help("Quad matching tolerance"),
        )
        .arg(
            Arg::new("hfd-min")
                .short('m')
                .value_name("ARCSEC")
                .value_parser(value_parser!(f64))
                .default_value("1.5")
                .help("Minimum star size (HFD), in arcseconds"),
        )
        .arg(
            Arg::new("downsample")
                .short('z')
                .value_name("FACTOR")
                .value_parser(value_parser!(u32))
                .help("Downsample (bin) factor; 0 or absent for automatic"),
        )
        .arg(
            Arg::new("check")
                .long("check")
                .value_name("y|n")
                .num_args(0..=1)
                .default_missing_value("y")
                .value_parser(["y", "n"])
                .help("Even out a raw one-shot-colour (Bayer) image before solving; for unbinned raw OSC frames only"),
        )
        .arg(
            Arg::new("database")
                .short('d')
                .value_name("DIR")
                .value_parser(value_parser!(PathBuf))
                .help("Star database directory [default: the `arcsec catalog` directory if it holds one, else the current directory]"),
        )
        .arg(
            Arg::new("db-abbrev")
                .short('D')
                .value_name("NAME")
                .help("Star database to use: d80, d50, g05, w08, ... [default: chosen by field size]"),
        )
        .arg(
            Arg::new("output")
                .short('o')
                .value_name("BASE")
                .value_parser(value_parser!(PathBuf))
                .help("Base path for the output files [default: the image path without its extension]"),
        )
        .arg(
            Arg::new("sip")
                .long("sip")
                .value_name("y|n")
                .num_args(0..=1)
                .default_missing_value("y")
                .value_parser(["y", "n"])
                .help("Add third-order SIP (Simple Imaging Polynomial) distortion terms to the .wcs file and --update, when the field shows significant distortion"),
        )
        .arg(
            Arg::new("speed")
                .long("speed")
                .value_name("MODE")
                .value_parser(["auto", "slow"])
                .default_value("auto")
                .help("Search mode: slow reads twice the field at every search position, for more overlap"),
        )
        .arg(
            Arg::new("index")
                .long("index")
                .short('i')
                .value_name("PATH")
                .value_parser(value_parser!(PathBuf))
                .help("Blind index for solving with no position: an arcsec index (.arcsecix, see `arcsec catalog index build`), an Astrometry.net index file, or a directory holding either"),
        )
        .arg(
            Arg::new("threads")
                .long("threads")
                .value_name("N")
                .value_parser(value_parser!(usize))
                .default_value("0")
                .help("Maximum worker threads; 0 for one per core"),
        )
        .arg(
            Arg::new("method")
                .long("method")
                .value_name("METHOD")
                .value_parser(["quads", "tetra"])
                .default_value("quads")
                .help("Catalogue matching method; tetra (three-star triangles) is experimental"),
        )
        .arg(
            Arg::new("wcs")
                .long("wcs")
                .action(ArgAction::SetTrue)
                .help("Write an Astrometry.net-style .wcs file (always written; accepted for compatibility)"),
        )
        .arg(
            Arg::new("log")
                .long("log")
                .action(ArgAction::SetTrue)
                .help("Write the solver log to <BASE>.log"),
        )
        .arg(
            Arg::new("update")
                .long("update")
                .action(ArgAction::SetTrue)
                .help("Write the solution into the input FITS header"),
        )
        .arg(
            Arg::new("progress")
                .long("progress")
                .action(ArgAction::SetTrue)
                .help("Log every progress step to stderr"),
        )
        .arg(
            Arg::new("analyse")
                .long("analyse")
                .value_name("SNR_MIN")
                .value_parser(value_parser!(f64))
                .help("Analyse only, without solving: print the median HFD and number of stars (SNR_MIN 0 means 30)"),
        )
        .arg(
            Arg::new("extract")
                .long("extract")
                .value_name("SNR_MIN")
                .value_parser(value_parser!(f64))
                .help("Analyse only, and write every star found to <IMAGE>.csv (SNR_MIN 0 means 30)"),
        )
        .arg(
            Arg::new("extract2")
                .long("extract2")
                .value_name("SNR_MIN")
                .value_parser(value_parser!(f64))
                .help("Solve (with SIP), then write every star found, with its RA and Dec, to <IMAGE>.csv"),
        )
}

/// `arcsec catalog ...`.
pub fn catalog_command() -> Command {
    Command::new("catalog")
        .bin_name("arcsec catalog")
        .about("Install and manage star catalogues")
        .subcommand_required(true)
        .arg_required_else_help(true)
        .arg(
            Arg::new("dir")
                .long("dir")
                .global(true)
                .value_name("DIR")
                .value_parser(value_parser!(PathBuf))
                .help("Catalogue directory [default: $ARCSEC_CATALOG_DIR, else the per-platform data directory]"),
        )
        .subcommand(Command::new("list").about("Show every catalogue and whether it is installed"))
        .subcommand(Command::new("path").about("Print the catalogue directory"))
        .subcommand(
            Command::new("recommend")
                .about("Suggest catalogues for a field size")
                .arg(
                    Arg::new("fov")
                        .long("fov")
                        .value_name("DEG")
                        .value_parser(value_parser!(f64))
                        .help("Field of view in degrees"),
                )
                .arg(
                    Arg::new("like")
                        .long("like")
                        .value_name("IMAGE")
                        .value_parser(value_parser!(PathBuf))
                        .help("Take the field size from this image's header instead"),
                )
                .arg(
                    Arg::new("photometry")
                        .long("photometry")
                        .action(ArgAction::SetTrue)
                        .help("Also suggest a photometric catalogue (colour calibration)"),
                ),
        )
        .subcommand(
            Command::new("install")
                .about("Download and unpack catalogues")
                .arg(
                    Arg::new("names")
                        .value_name("NAME")
                        .required(true)
                        .num_args(1..)
                        .help("Catalogue names, e.g. d50 v05 g05"),
                )
                .arg(
                    Arg::new("yes")
                        .long("yes")
                        .short('y')
                        .action(ArgAction::SetTrue)
                        .help("Do not ask for confirmation"),
                )
                .arg(
                    Arg::new("keep")
                        .long("keep-archive")
                        .action(ArgAction::SetTrue)
                        .help("Keep the downloaded archive after extracting"),
                ),
        )
        .subcommand(
            Command::new("remove")
                .about("Delete installed catalogue files")
                .arg(
                    Arg::new("names")
                        .value_name("NAME")
                        .required(true)
                        .num_args(1..)
                        .help("Catalogue names, as shown by `arcsec catalog list`"),
                )
                .arg(
                    Arg::new("yes")
                        .long("yes")
                        .short('y')
                        .action(ArgAction::SetTrue)
                        .help("Do not ask for confirmation"),
                ),
        )
        .subcommand(
            Command::new("verify")
                .about("Check installed catalogues for missing or truncated files"),
        )
        .subcommand(
            Command::new("index")
                .about("Build or inspect arcsec's blind index (no download: built from an installed star database)")
                .subcommand_required(true)
                .arg_required_else_help(true)
                .subcommand(
                    Command::new("build")
                        .about("Build a blind index from an installed star database")
                        .arg(
                            Arg::new("db")
                                .long("db")
                                .value_name("DIR")
                                .value_parser(value_parser!(PathBuf))
                                .help("Directory holding the star database [default: the catalogue directory]"),
                        )
                        .arg(
                            Arg::new("name")
                                .long("name")
                                .short('D')
                                .value_name("DB")
                                .help("Database to build from, e.g. d80 [default: the deepest installed]"),
                        )
                        .arg(
                            Arg::new("min-fov")
                                .long("min-fov")
                                .value_name("DEG")
                                .value_parser(value_parser!(f64))
                                .default_value("0.3")
                                .help("Smallest field (short side, degrees) to support; smaller fields need much larger indexes"),
                        )
                        .arg(
                            Arg::new("max-fov")
                                .long("max-fov")
                                .value_name("DEG")
                                .value_parser(value_parser!(f64))
                                .default_value("30")
                                .help("Largest field (short side, degrees) to support"),
                        )
                        .arg(
                            Arg::new("out")
                                .long("out")
                                .short('o')
                                .value_name("FILE")
                                .value_parser(value_parser!(PathBuf))
                                .help("Output file [default: <catalogue dir>/<db>.arcsecix]"),
                        )
                        .arg(
                            Arg::new("threads")
                                .long("threads")
                                .value_name("N")
                                .value_parser(value_parser!(usize))
                                .default_value("0")
                                .help("Worker threads; 0 for one per core"),
                        ),
                )
                .subcommand(
                    Command::new("info")
                        .about("Describe a blind index: tiers, sizes, source")
                        .arg(
                            Arg::new("file")
                                .value_name("FILE")
                                .value_parser(value_parser!(PathBuf))
                                .help("Index file [default: every *.arcsecix in the catalogue directory]"),
                        ),
                ),
        )
}

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

    fn norm(args: &[&str]) -> Vec<String> {
        normalize_astap_args(args.iter().map(OsString::from))
            .into_iter()
            .map(|s| s.into_string().unwrap())
            .collect()
    }

    #[test]
    fn command_definitions_are_valid() {
        solver_command().debug_assert();
        catalog_command().debug_assert();
    }

    #[test]
    fn astap_single_dash_options_are_rewritten() {
        assert_eq!(
            norm(&[
                "arcsec",
                "-f",
                "x.fits",
                "-fov",
                "1.2",
                "-ra",
                "5.5",
                "-spd",
                "97",
                "-progress"
            ]),
            [
                "arcsec",
                "-f",
                "x.fits",
                "--fov",
                "1.2",
                "--ra",
                "5.5",
                "--spd",
                "97",
                "--progress"
            ]
        );
    }

    #[test]
    fn everything_else_is_left_alone() {
        let args = [
            "arcsec",
            "-f",
            "-fov.fits",
            "-r",
            "-30",
            "--fov",
            "0",
            "-z",
            "2",
            "-D",
            "d50",
        ];
        assert_eq!(norm(&args), args);
        // After `--`, nothing is an option.
        assert_eq!(norm(&["arcsec", "--", "-fov"]), ["arcsec", "--", "-fov"]);
        // The program name is never rewritten.
        assert_eq!(norm(&["-log"]), ["-log"]);
    }

    #[test]
    fn astap_command_line_parses() {
        let m = solver_command()
            .try_get_matches_from(normalize_astap_args(
                [
                    "astap_cli",
                    "-f",
                    "a.fits",
                    "-fov",
                    "0",
                    "-r",
                    "30",
                    "-ra",
                    "5.5",
                    "-spd",
                    "97",
                    "-z",
                    "0",
                    "-s",
                    "500",
                    "-wcs",
                    "-log",
                    "-update",
                    "-speed",
                    "auto",
                ]
                .map(OsString::from),
            ))
            .expect("an astap_cli command line must parse");
        assert_eq!(m.get_one::<f64>("fov"), Some(&0.0));
        assert_eq!(m.get_one::<f64>("spd"), Some(&97.0));
        assert!(m.get_flag("wcs") && m.get_flag("log") && m.get_flag("update"));
    }

    fn parse(args: &[&str]) -> Result<clap::ArgMatches, clap::Error> {
        solver_command().try_get_matches_from(normalize_astap_args(
            args.iter().copied().map(OsString::from),
        ))
    }

    #[test]
    fn sip_and_check_take_an_optional_y_or_n() {
        let m = parse(&["astap_cli", "-f", "a.fits", "-sip", "-check", "y", "-wcs"]).unwrap();
        assert_eq!(m.get_one::<String>("sip").map(String::as_str), Some("y"));
        assert_eq!(m.get_one::<String>("check").map(String::as_str), Some("y"));
        assert!(m.get_flag("wcs"), "a following flag is not the value");
        let m = parse(&["astap_cli", "-sip", "n", "-check"]).unwrap();
        assert_eq!(m.get_one::<String>("sip").map(String::as_str), Some("n"));
        assert_eq!(m.get_one::<String>("check").map(String::as_str), Some("y"));
        let m = parse(&["astap_cli", "-f", "a.fits"]).unwrap();
        assert!(m.get_one::<String>("sip").is_none() && m.get_one::<String>("check").is_none());
        assert!(parse(&["astap_cli", "-sip", "maybe"]).is_err());
    }

    #[test]
    fn speed_is_auto_or_slow() {
        let m = parse(&["astap_cli", "-speed", "slow"]).unwrap();
        assert_eq!(
            m.get_one::<String>("speed").map(String::as_str),
            Some("slow")
        );
        let m = parse(&["astap_cli"]).unwrap();
        assert_eq!(
            m.get_one::<String>("speed").map(String::as_str),
            Some("auto")
        );
        assert!(parse(&["astap_cli", "-speed", "fast"]).is_err());
    }

    #[test]
    fn analyse_options_take_a_minimum_snr() {
        let m = parse(&[
            "astap_cli",
            "-f",
            "a.fits",
            "-analyse",
            "30",
            "-extract",
            "0",
        ])
        .unwrap();
        assert_eq!(m.get_one::<f64>("analyse"), Some(&30.0));
        assert_eq!(m.get_one::<f64>("extract"), Some(&0.0));
        let m = parse(&["astap_cli", "-extract2", "20"]).unwrap();
        assert_eq!(m.get_one::<f64>("extract2"), Some(&20.0));
    }

    #[test]
    fn an_unknown_method_is_rejected() {
        let r = solver_command().try_get_matches_from(["arcsec", "--method", "triangles"]);
        assert!(r.is_err());
    }
}