fits-io 0.2.0

A pure-Rust FITS file reading and writing library inspired by CFITSIO, focused on safety, clarity, and performance.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! The polynomial corrections a plate solver fits on top of a projection.
//!
//! A real telescope is not the ideal one a projection describes: the optics bend
//! the field, and the detector is not perfectly flat or square. Two conventions
//! for writing that down are common enough to meet in the wild, and both are
//! polynomials fitted alongside the WCS:
//!
//! * **SIP**, written by `astrometry.net` and most amateur plate solvers, which
//!   corrects the *pixel* offsets before the header's matrix sees them. A header
//!   using it says so in CTYPEn, as `RA---TAN-SIP`.
//! * **TPV**, written by `SCAMP` and the IRAF-descended pipelines, which
//!   corrects the *intermediate world coordinates* after the matrix. Its CTYPEn
//!   reads `RA---TPV`.
//!
//! Ignoring either leaves coordinates that are right at the centre of the frame
//! and wrong by seconds of arc at its corners.

use crate::header::{Header, Value};

/// A distortion sitting between the pixel grid and the projection.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Distortion {
    /// The header fits the ideal projection with no correction.
    None,
    /// Simple Imaging Polynomial, applied to pixel offsets from the reference
    /// pixel.
    Sip(Sip),
    /// The TPV polynomial, applied to intermediate world coordinates.
    Tpv(Box<Tpv>),
}

impl Distortion {
    /// Reads whichever distortion `header` carries, given the CTYPE1 it was read
    /// with.
    pub(crate) fn from_header(header: &Header, ctype: Option<&str>) -> Self {
        let ctype = ctype.unwrap_or_default().trim();

        if ctype.ends_with("-SIP")
            && let Some(sip) = Sip::from_header(header)
        {
            return Distortion::Sip(sip);
        }

        if ctype.ends_with("TPV")
            && let Some(tpv) = Tpv::from_header(header)
        {
            return Distortion::Tpv(Box::new(tpv));
        }

        Distortion::None
    }

    /// Corrects pixel offsets from the reference pixel, before the header's
    /// matrix is applied.
    pub(crate) fn correct_pixel(&self, offset: (f64, f64)) -> (f64, f64) {
        match self {
            Distortion::Sip(sip) => sip.correct(offset),
            _ => offset,
        }
    }

    /// Undoes [`Distortion::correct_pixel`].
    pub(crate) fn uncorrect_pixel(&self, corrected: (f64, f64)) -> (f64, f64) {
        match self {
            Distortion::Sip(sip) => sip.uncorrect(corrected),
            _ => corrected,
        }
    }

    /// Corrects intermediate world coordinates, after the header's matrix.
    pub(crate) fn correct_intermediate(&self, intermediate: (f64, f64)) -> (f64, f64) {
        match self {
            Distortion::Tpv(tpv) => tpv.correct(intermediate),
            _ => intermediate,
        }
    }

    /// Undoes [`Distortion::correct_intermediate`].
    pub(crate) fn uncorrect_intermediate(&self, corrected: (f64, f64)) -> (f64, f64) {
        match self {
            Distortion::Tpv(tpv) => invert_numerically(|point| tpv.correct(point), corrected),
            _ => corrected,
        }
    }
}

/// The SIP polynomials: `A` and `B` forwards, `AP` and `BP` back.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Sip {
    forward: (Polynomial, Polynomial),
    /// The fitted inverse, where the header carries one. A header without it is
    /// inverted by iterating the forward polynomial instead.
    inverse: Option<(Polynomial, Polynomial)>,
}

impl Sip {
    /// Reads the SIP coefficients, or `None` where the header has none.
    fn from_header(header: &Header) -> Option<Self> {
        let forward = (
            Polynomial::from_header(header, "A")?,
            Polynomial::from_header(header, "B")?,
        );

        let inverse = Polynomial::from_header(header, "AP")
            .zip(Polynomial::from_header(header, "BP"))
            .filter(|(a, b)| !a.is_empty() || !b.is_empty());

        Some(Self { forward, inverse })
    }

    /// The corrected pixel offset for a raw one.
    fn correct(&self, (u, v): (f64, f64)) -> (f64, f64) {
        (
            u + self.forward.0.evaluate(u, v),
            v + self.forward.1.evaluate(u, v),
        )
    }

    /// The raw pixel offset for a corrected one.
    fn uncorrect(&self, (u, v): (f64, f64)) -> (f64, f64) {
        match &self.inverse {
            Some((a, b)) => (u + a.evaluate(u, v), v + b.evaluate(u, v)),
            // A header that fitted no inverse still has one; it just has to be
            // found rather than read.
            None => invert_numerically(|point| self.correct(point), (u, v)),
        }
    }
}

/// A polynomial in two variables, as SIP writes it: `A_i_j` is the coefficient
/// of `u^i v^j`.
#[derive(Debug, Clone, PartialEq, Default)]
struct Polynomial {
    /// `(i, j, coefficient)`, in no particular order.
    terms: Vec<(u32, u32, f64)>,
}

impl Polynomial {
    /// Reads the `<name>_ORDER` card and every `<name>_i_j` under it.
    ///
    /// A header that gives coefficients without an order is read anyway, up to
    /// the highest order SIP allows; leaving them out would mean quietly
    /// dropping a correction the header asked for.
    fn from_header(header: &Header, name: &str) -> Option<Self> {
        const MAX_ORDER: u32 = 9;

        let order = number(header, &format!("{}_ORDER", name))
            .map(|order| order.max(0.0).min(MAX_ORDER as f64) as u32);

        let mut terms = Vec::new();
        let limit = order.unwrap_or(MAX_ORDER);

        for i in 0..=limit {
            for j in 0..=(limit - i) {
                if let Some(coefficient) = number(header, &format!("{}_{}_{}", name, i, j))
                    && coefficient != 0.0
                {
                    terms.push((i, j, coefficient));
                }
            }
        }

        if order.is_none() && terms.is_empty() {
            return None;
        }

        Some(Self { terms })
    }

    fn is_empty(&self) -> bool {
        self.terms.is_empty()
    }

    fn evaluate(&self, u: f64, v: f64) -> f64 {
        self.terms
            .iter()
            .map(|(i, j, coefficient)| coefficient * u.powi(*i as i32) * v.powi(*j as i32))
            .sum()
    }
}

/// The TPV polynomial: `PV1_k` for the first axis and `PV2_k` for the second.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Tpv {
    first: [f64; TPV_TERMS],
    second: [f64; TPV_TERMS],
}

/// How many terms the TPV convention defines, up to seventh order.
const TPV_TERMS: usize = 40;

impl Tpv {
    /// Reads the PVi_k coefficients, or `None` where the header carries none.
    fn from_header(header: &Header) -> Option<Self> {
        let read = |axis: usize| {
            let mut coefficients = [0.0; TPV_TERMS];
            let mut any = false;

            for (term, slot) in coefficients.iter_mut().enumerate() {
                if let Some(value) = number(header, &format!("PV{}_{}", axis, term)) {
                    *slot = value;
                    any = true;
                }
            }

            (coefficients, any)
        };

        let (first, first_present) = read(1);
        let (second, second_present) = read(2);

        if !first_present && !second_present {
            return None;
        }

        // A TPV header that leaves an axis out means the identity there, which
        // is the linear term alone.
        let mut tpv = Self { first, second };
        if !first_present {
            tpv.first[1] = 1.0;
        }
        if !second_present {
            tpv.second[1] = 1.0;
        }

        Some(tpv)
    }

    /// The corrected intermediate coordinates for the raw ones.
    fn correct(&self, (x, y): (f64, f64)) -> (f64, f64) {
        // The second axis uses the same terms with its own coordinate first,
        // which is what makes one table of coefficients serve both.
        (
            evaluate_tpv(&self.first, x, y),
            evaluate_tpv(&self.second, y, x),
        )
    }
}

/// Evaluates the TPV series, whose terms run in the order the convention fixes:
/// the constant, the linear terms, the radial term, then each higher order in
/// turn with its own radial term at odd orders.
fn evaluate_tpv(coefficients: &[f64; TPV_TERMS], x: f64, y: f64) -> f64 {
    let r = x.hypot(y);

    let terms: [f64; TPV_TERMS] = [
        1.0,
        x,
        y,
        r,
        x * x,
        x * y,
        y * y,
        x * x * x,
        x * x * y,
        x * y * y,
        y * y * y,
        r * r * r,
        x.powi(4),
        x.powi(3) * y,
        x * x * y * y,
        x * y.powi(3),
        y.powi(4),
        x.powi(5),
        x.powi(4) * y,
        x.powi(3) * y * y,
        x * x * y.powi(3),
        x * y.powi(4),
        y.powi(5),
        r.powi(5),
        x.powi(6),
        x.powi(5) * y,
        x.powi(4) * y * y,
        x.powi(3) * y.powi(3),
        x * x * y.powi(4),
        x * y.powi(5),
        y.powi(6),
        x.powi(7),
        x.powi(6) * y,
        x.powi(5) * y * y,
        x.powi(4) * y.powi(3),
        x.powi(3) * y.powi(4),
        x * x * y.powi(5),
        x * y.powi(6),
        y.powi(7),
        r.powi(7),
    ];

    coefficients
        .iter()
        .zip(terms)
        .map(|(coefficient, term)| coefficient * term)
        .sum()
}

/// Finds the point `forward` maps onto `target`.
///
/// A distortion is a small correction to the identity, so Newton's method on a
/// numerically differentiated Jacobian settles in a few steps. Where it does not
/// settle — a polynomial fitted far outside the frame it was fitted in can fold
/// the plane over — the best point it reached is returned, which is still the
/// nearest thing to an answer there is.
fn invert_numerically(
    forward: impl Fn((f64, f64)) -> (f64, f64),
    target: (f64, f64),
) -> (f64, f64) {
    /// Close enough that a further step would move the answer by less than a
    /// millionth of a pixel.
    const TOLERANCE: f64 = 1e-12;
    /// The step the Jacobian is measured over, small beside a pixel and large
    /// beside the rounding of a double.
    const STEP: f64 = 1e-6;

    let mut point = target;

    for _ in 0..24 {
        let (fx, fy) = forward(point);
        let residual = (fx - target.0, fy - target.1);

        if residual.0.abs() < TOLERANCE && residual.1.abs() < TOLERANCE {
            break;
        }

        let (dx_x, dx_y) = forward((point.0 + STEP, point.1));
        let (dy_x, dy_y) = forward((point.0, point.1 + STEP));

        let jacobian = [
            [(dx_x - fx) / STEP, (dy_x - fx) / STEP],
            [(dx_y - fy) / STEP, (dy_y - fy) / STEP],
        ];

        let determinant = jacobian[0][0] * jacobian[1][1] - jacobian[0][1] * jacobian[1][0];
        if determinant == 0.0 || !determinant.is_finite() {
            break;
        }

        let step = (
            (jacobian[1][1] * residual.0 - jacobian[0][1] * residual.1) / determinant,
            (jacobian[0][0] * residual.1 - jacobian[1][0] * residual.0) / determinant,
        );

        point = (point.0 - step.0, point.1 - step.1);

        if !point.0.is_finite() || !point.1.is_finite() {
            return target;
        }
    }

    point
}

/// The number a card holds, whether it was written as an integer or a float.
pub(crate) fn number(header: &Header, key: &str) -> Option<f64> {
    match header.card(key)? {
        Value::Float { value, .. } => Some(value),
        Value::Integer { value, .. } => Some(value as f64),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::{Distortion, Polynomial, Sip, Tpv, invert_numerically};
    use crate::header::Header;

    fn sip_header() -> Header {
        let mut header = Header::default();

        header.set_card("A_ORDER", 2_i64).unwrap();
        header.set_card("A_2_0", 1e-5).unwrap();
        header.set_card("B_ORDER", 2_i64).unwrap();
        header.set_card("B_0_2", -2e-5).unwrap();

        header
    }

    #[test]
    fn a_sip_header_is_read_only_when_the_ctype_asks_for_it() {
        let header = sip_header();

        assert!(matches!(
            Distortion::from_header(&header, Some("RA---TAN-SIP")),
            Distortion::Sip(_)
        ));
        // The same coefficients without the CTYPE saying so are not applied: a
        // header that does not claim SIP is not corrected by it.
        assert_eq!(
            Distortion::from_header(&header, Some("RA---TAN")),
            Distortion::None
        );
    }

    #[test]
    fn sip_moves_a_pixel_by_its_polynomial() {
        let sip = Sip::from_header(&sip_header()).expect("the header carries SIP coefficients");

        // u + A_2_0 u^2, v + B_0_2 v^2
        assert_eq!(sip.correct((100.0, 50.0)), (100.1, 49.95));
    }

    #[test]
    fn sip_undoes_itself_even_without_a_fitted_inverse() {
        let sip = Sip::from_header(&sip_header()).expect("the header carries SIP coefficients");

        for point in [(0.0, 0.0), (100.0, 50.0), (-800.0, 600.0)] {
            let corrected = sip.correct(point);
            let back = sip.uncorrect(corrected);

            assert!(
                (back.0 - point.0).abs() < 1e-6 && (back.1 - point.1).abs() < 1e-6,
                "{point:?} came back as {back:?}"
            );
        }
    }

    #[test]
    fn a_fitted_inverse_is_used_when_the_header_has_one() {
        let mut header = sip_header();
        header.set_card("AP_ORDER", 2_i64).unwrap();
        header.set_card("AP_1_0", 0.5).unwrap();
        header.set_card("BP_ORDER", 2_i64).unwrap();

        let sip = Sip::from_header(&header).expect("the header carries SIP coefficients");

        // The fitted inverse is used as given, right or wrong: u + 0.5u.
        assert_eq!(sip.uncorrect((10.0, 0.0)), (15.0, 0.0));
    }

    #[test]
    fn a_polynomial_with_no_coefficients_at_all_is_not_a_polynomial() {
        assert!(Polynomial::from_header(&Header::default(), "A").is_none());
    }

    #[test]
    fn tpv_reads_its_coefficients_and_undoes_itself() {
        let mut header = Header::default();
        header.set_card("PV1_0", 0.0).unwrap();
        header.set_card("PV1_1", 1.0).unwrap();
        header.set_card("PV1_4", 1e-4).unwrap();
        header.set_card("PV2_1", 1.0).unwrap();

        let tpv = Tpv::from_header(&header).expect("the header carries PV coefficients");

        // x + 1e-4 x^2 on the first axis, y untouched on the second.
        let corrected = tpv.correct((2.0, 3.0));
        assert!((corrected.0 - (2.0 + 4e-4)).abs() < 1e-12, "{corrected:?}");
        assert!((corrected.1 - 3.0).abs() < 1e-12, "{corrected:?}");

        let distortion = Distortion::Tpv(Box::new(tpv));
        let back = distortion.uncorrect_intermediate(corrected);
        assert!(
            (back.0 - 2.0).abs() < 1e-9 && (back.1 - 3.0).abs() < 1e-9,
            "{back:?}"
        );
    }

    #[test]
    fn a_map_with_no_inverse_gives_back_what_it_was_asked_about() {
        // A constant map sends everything to one point, so nothing maps back;
        // the answer is the point asked about rather than an infinity.
        let point = invert_numerically(|_| (1.0, 1.0), (5.0, 5.0));

        assert!(point.0.is_finite() && point.1.is_finite(), "{point:?}");
    }
}