srfits 0.1.2

Pure-Rust FITS I/O library, validated against CFITSIO 4.7.0
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
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
//! ASCII-table TFORM parsing (`ffasfm`), binary TFORM (`ffbnfm`),
//! column spacing (`ffgabc`), and C `printf`-compatible field formatting.

use crate::card::format_exp_double;
use crate::error::{FitsError, Result};
use crate::status::{BAD_TFORM, BAD_TFORM_DTYPE, NUM_OVERFLOW};
use crate::types::{
    FLEN_VALUE, TBIT, TBYTE, TCOMPLEX, TDBLCOMPLEX, TDOUBLE, TFLOAT, TLOGICAL, TLONG, TLONGLONG,
    TSBYTE, TSHORT, TSTRING, TULONG, TULONGLONG, TUSHORT,
};

/// ASCII-table TFORM letter.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AsciiKind {
    /// `Aw` character field.
    A,
    /// `Iw` integer.
    I,
    /// `Fw.d` fixed-point.
    F,
    /// `Ew.d` exponential.
    E,
    /// `Dw.d` exponential (written with `E`, matching CFITSIO `ffcfmt`).
    D,
}

/// Parsed ASCII `TFORMn`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AsciiTform {
    /// Format letter.
    pub kind: AsciiKind,
    /// Field width in characters.
    pub width: usize,
    /// Decimal places (`F`/`E`/`D`); 0 for `A`/`I`.
    pub decimals: usize,
    /// CFITSIO datatype code (`TSTRING` / `TSHORT` / `TLONG` / `TFLOAT` / `TDOUBLE`).
    pub datacode: i32,
}

impl AsciiTform {
    /// `fits_ascii_tform` / `ffasfm`.
    pub fn parse(tform: &str) -> Result<Self> {
        parse_ascii_tform(tform)
    }

    /// C `printf` conversion matching `ffcfmt` (values printed as `double`).
    #[must_use]
    pub fn c_format(self) -> String {
        match self.kind {
            AsciiKind::A => format!("%{}s", self.width),
            AsciiKind::I => format!("%{}.0f", self.width),
            AsciiKind::F => format!("%{}.{}f", self.width, self.decimals),
            AsciiKind::E | AsciiKind::D => format!("%{}.{}E", self.width, self.decimals),
        }
    }
}

/// Parse an ASCII-table TFORM (`ffasfm`).
pub fn parse_ascii_tform(tform: &str) -> Result<AsciiTform> {
    let trimmed = tform.trim_start();
    if trimmed.len() > FLEN_VALUE - 1 {
        return Err(FitsError::new(BAD_TFORM));
    }
    let form: String = trimmed.chars().map(|c| c.to_ascii_uppercase()).collect();
    if form.is_empty() {
        return Err(FitsError::new(BAD_TFORM));
    }
    let first = form.as_bytes()[0];
    let mut datacode = match first {
        b'A' => TSTRING,
        b'I' => TLONG,
        b'F' | b'E' => TFLOAT,
        b'D' => TDOUBLE,
        _ => {
            return Err(FitsError::with_message(
                BAD_TFORM_DTYPE,
                format!("Illegal ASCII table TFORMn datatype: '{tform}'"),
            ));
        }
    };
    let rest = &form[1..];
    let kind = match first {
        b'A' => AsciiKind::A,
        b'I' => AsciiKind::I,
        b'F' => AsciiKind::F,
        b'E' => AsciiKind::E,
        _ => AsciiKind::D,
    };

    let (width, decimals) = if kind == AsciiKind::A || kind == AsciiKind::I {
        let width = parse_c_long(rest)?;
        if width <= 0 {
            return Err(FitsError::new(BAD_TFORM));
        }
        if width <= 4 && datacode == TLONG {
            datacode = TSHORT;
        }
        (width as usize, 0usize)
    } else {
        let fwidth = parse_c_double(rest)?;
        if fwidth <= 0.0 {
            return Err(FitsError::new(BAD_TFORM));
        }
        let width = fwidth as i64;
        if width > 7 && first == b'F' {
            datacode = TDOUBLE;
        }
        // CFITSIO advances `form` by 1 or 2 characters from the start of the
        // width field, then looks for a '.' — not by digit count of `width`.
        let skip = if width < 10 { 1usize } else { 2usize };
        let mut decimals = 0i64;
        if rest.len() > skip && rest.as_bytes()[skip] == b'.' {
            decimals = parse_c_long(&rest[skip + 1..])?;
            if decimals >= width {
                return Err(FitsError::new(BAD_TFORM));
            }
            if decimals > 6 && first == b'E' {
                datacode = TDOUBLE;
            }
        }
        (width as usize, decimals.max(0) as usize)
    };

    Ok(AsciiTform {
        kind,
        width,
        decimals,
        datacode,
    })
}

/// `fits_get_tbcol` / `ffgabc`: 1-based TBCOL and total row width.
///
/// `space` is the number of blanks between columns (CFITSIO recommends 1).
pub fn ascii_column_starts(tforms: &[&str], space: i64) -> Result<(i64, Vec<i64>)> {
    if tforms.is_empty() {
        return Ok((0, Vec::new()));
    }
    let mut rowlen = 0i64;
    let mut tbcol = Vec::with_capacity(tforms.len());
    for tform in tforms {
        tbcol.push(rowlen + 1);
        let parsed = parse_ascii_tform(tform)?;
        rowlen += parsed.width as i64 + space;
    }
    rowlen -= space;
    Ok((rowlen, tbcol))
}

/// Format `value` into a `width`-character ASCII field (`snprintf` + `ffcfmt`).
pub fn format_ascii_number(value: f64, tform: &AsciiTform) -> Result<Vec<u8>> {
    if !value.is_finite() {
        return Err(FitsError::new(NUM_OVERFLOW));
    }
    let body = match tform.kind {
        AsciiKind::A => {
            return Err(FitsError::new(crate::status::BAD_ATABLE_FORMAT));
        }
        AsciiKind::I => format!("{value:.0}").replace(',', "."),
        AsciiKind::F => format!("{value:.prec$}", prec = tform.decimals).replace(',', "."),
        AsciiKind::E | AsciiKind::D => format_exp_double(value, tform.decimals),
    };
    if body.len() > tform.width {
        return Err(FitsError::new(NUM_OVERFLOW));
    }
    let mut out = vec![b' '; tform.width];
    let start = tform.width - body.len();
    out[start..].copy_from_slice(body.as_bytes());
    Ok(out)
}

/// Left-justify `s` in a `width`-character field, space-padded (or truncated).
#[must_use]
pub fn format_ascii_string(s: &str, width: usize) -> Vec<u8> {
    let mut out = vec![b' '; width];
    let bytes = s.as_bytes();
    let n = bytes.len().min(width);
    out[..n].copy_from_slice(&bytes[..n]);
    out
}

/// Decode an ASCII numeric field (`fffstr*`).
///
/// Implied decimal places from TFORM apply only when the field has no `.`.
pub fn parse_ascii_number(field: &[u8], implied_decimals: usize) -> Result<f64> {
    let mut i = 0usize;
    let n = field.len();
    while i < n && field[i] == b' ' {
        i += 1;
    }
    if i == n {
        return Ok(0.0);
    }
    let mut sign = 1.0f64;
    if field[i] == b'-' || field[i] == b'+' {
        if field[i] == b'-' {
            sign = -1.0;
        }
        i += 1;
        while i < n && field[i] == b' ' {
            i += 1;
        }
    }
    let mut val = 0.0f64;
    let mut power = 1.0f64;
    let mut decpt = false;
    while i < n && field[i].is_ascii_digit() {
        val = val * 10.0 + f64::from(field[i] - b'0');
        i += 1;
        while i < n && field[i] == b' ' {
            i += 1;
        }
    }
    if i < n && (field[i] == b'.' || field[i] == b',') {
        decpt = true;
        i += 1;
        while i < n && field[i] == b' ' {
            i += 1;
        }
        while i < n && field[i].is_ascii_digit() {
            val = val * 10.0 + f64::from(field[i] - b'0');
            power *= 10.0;
            i += 1;
            while i < n && field[i] == b' ' {
                i += 1;
            }
        }
    }
    let mut exponent = 0i32;
    let mut esign = 1i32;
    if i < n && (field[i] == b'E' || field[i] == b'D' || field[i] == b'e' || field[i] == b'd') {
        i += 1;
        while i < n && field[i] == b' ' {
            i += 1;
        }
        if i < n && (field[i] == b'-' || field[i] == b'+') {
            if field[i] == b'-' {
                esign = -1;
            }
            i += 1;
            while i < n && field[i] == b' ' {
                i += 1;
            }
        }
        while i < n && field[i].is_ascii_digit() {
            exponent = exponent * 10 + i32::from(field[i] - b'0');
            i += 1;
            while i < n && field[i] == b' ' {
                i += 1;
            }
        }
    }
    let mut dvalue = sign * val / power;
    if exponent != 0 {
        dvalue *= 10f64.powi(esign * exponent);
    } else if !decpt && implied_decimals > 0 {
        dvalue /= 10f64.powi(implied_decimals as i32);
    }
    Ok(dvalue)
}

/// True if `field` matches the ASCII TNULLn string (`strncmp` of `tnull.len()`).
#[must_use]
pub fn field_is_null(field: &[u8], tnull: &str) -> bool {
    if tnull.is_empty() {
        return false;
    }
    let nb = tnull.as_bytes();
    if nb.len() > field.len() {
        return false;
    }
    field.get(..nb.len()) == Some(nb)
}

/// Trim trailing blanks from an ASCII string field (`ffgcls2`).
#[must_use]
pub fn trim_ascii_field(field: &[u8]) -> String {
    let mut end = field.len();
    while end > 0 && field[end - 1] == b' ' {
        end -= 1;
    }
    String::from_utf8_lossy(&field[..end]).into_owned()
}

fn parse_c_long(s: &str) -> Result<i64> {
    let t = s.trim_start();
    if t.is_empty() {
        return Err(FitsError::new(BAD_TFORM));
    }
    let mut end = 0usize;
    if t.as_bytes().first() == Some(&b'+') || t.as_bytes().first() == Some(&b'-') {
        end = 1;
    }
    while end < t.len() && t.as_bytes()[end].is_ascii_digit() {
        end += 1;
    }
    if end == 0 || (end == 1 && !t.as_bytes()[0].is_ascii_digit()) {
        return Err(FitsError::new(BAD_TFORM));
    }
    t[..end]
        .parse::<i64>()
        .map_err(|_| FitsError::new(BAD_TFORM))
}

fn parse_c_double(s: &str) -> Result<f64> {
    let t = s.trim_start();
    if t.is_empty() {
        return Err(FitsError::new(BAD_TFORM));
    }
    let mut end = 0usize;
    let b = t.as_bytes();
    if b.first() == Some(&b'+') || b.first() == Some(&b'-') {
        end = 1;
    }
    let start_digits = end;
    while end < b.len() && b[end].is_ascii_digit() {
        end += 1;
    }
    if end < b.len() && b[end] == b'.' {
        end += 1;
        while end < b.len() && b[end].is_ascii_digit() {
            end += 1;
        }
    }
    if end == start_digits {
        return Err(FitsError::new(BAD_TFORM));
    }
    t[..end]
        .parse::<f64>()
        .map_err(|_| FitsError::new(BAD_TFORM))
}

/// Binary-table TFORM letter (after optional repeat and `P`/`Q`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryKind {
    /// Logical `L`.
    L,
    /// Bit `X`.
    X,
    /// Unsigned byte `B`.
    B,
    /// Signed byte `S` (stored as `B` + TZERO = -128).
    S,
    /// Int16 `I`.
    I,
    /// UInt16 `U` (stored as `I` + TZERO = 32768).
    U,
    /// Int32 `J`.
    J,
    /// UInt32 `V` (stored as `J` + TZERO = 2^31).
    V,
    /// Int64 `K`.
    K,
    /// UInt64 `W` (stored as `K` + TZERO = 2^63).
    W,
    /// ASCII `A`.
    A,
    /// Float32 `E`.
    E,
    /// Float64 `D`.
    D,
    /// Complex64 `C`.
    C,
    /// Complex128 `M`.
    M,
}

/// Variable-length descriptor kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VariableKind {
    /// Fixed-length column.
    None,
    /// 32-bit descriptor (`P`).
    P,
    /// 64-bit descriptor (`Q`).
    Q,
}

/// Parsed binary `TFORMn` (`ffbnfm`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BinaryTform {
    /// Repeat count (1 if omitted).
    pub repeat: i64,
    /// Type letter.
    pub kind: BinaryKind,
    /// `P` / `Q` / fixed.
    pub variable: VariableKind,
    /// Element width in bytes (string unit width for `A`; 1 for bits).
    pub width: usize,
    /// CFITSIO datatype code (negative if variable).
    pub datacode: i32,
}

impl BinaryTform {
    /// `fits_binary_tform` / `ffbnfm`.
    pub fn parse(tform: &str) -> Result<Self> {
        parse_binary_tform(tform)
    }

    /// Bytes occupied in the fixed part of a table row.
    #[must_use]
    pub fn row_nbytes(self) -> usize {
        match self.variable {
            VariableKind::P => 8,
            VariableKind::Q => 16,
            VariableKind::None => match self.kind {
                BinaryKind::X => (self.repeat as usize).div_ceil(8),
                BinaryKind::A => self.repeat as usize,
                _ => self.repeat as usize * self.elem_nbytes(),
            },
        }
    }

    /// Bytes per stored element (heap element for VLA).
    #[must_use]
    pub fn elem_nbytes(self) -> usize {
        match self.kind {
            BinaryKind::L | BinaryKind::B | BinaryKind::S | BinaryKind::A | BinaryKind::X => 1,
            BinaryKind::I | BinaryKind::U => 2,
            BinaryKind::J | BinaryKind::V | BinaryKind::E => 4,
            BinaryKind::K | BinaryKind::W | BinaryKind::D | BinaryKind::C => 8,
            BinaryKind::M => 16,
        }
    }

    /// True if this is a `P`/`Q` variable-length column.
    #[must_use]
    pub fn is_variable(self) -> bool {
        self.variable != VariableKind::None
    }

    /// TFORM comment suffix after `"data format of field"` (`ffphbn`).
    ///
    /// Signed-byte (`S`) gets no suffix (CFITSIO rewrites `S`→`B` without
    /// appending `": BYTE"`).
    #[must_use]
    pub fn tform_comment_suffix(self) -> &'static str {
        if self.is_variable() {
            return ": variable length array";
        }
        match self.kind {
            BinaryKind::A => ": ASCII Character",
            BinaryKind::X => ": BIT",
            BinaryKind::B => ": BYTE",
            BinaryKind::S => "",
            BinaryKind::L => ": 1-byte LOGICAL",
            BinaryKind::I | BinaryKind::U => ": 2-byte INTEGER",
            BinaryKind::J | BinaryKind::V => ": 4-byte INTEGER",
            BinaryKind::K | BinaryKind::W => ": 8-byte INTEGER",
            BinaryKind::E => ": 4-byte REAL",
            BinaryKind::D => ": 8-byte DOUBLE",
            BinaryKind::C => ": COMPLEX",
            BinaryKind::M => ": DOUBLE COMPLEX",
        }
    }

    /// TFORM string stored in the header (`S`/`U`/`V`/`W` rewritten).
    #[must_use]
    pub fn stored_code(tform: &str) -> String {
        let upper = tform.trim().to_ascii_uppercase();
        let parsed = parse_binary_tform(&upper).ok();
        let (from, to) = match parsed.map(|p| p.kind) {
            Some(BinaryKind::S) => (b'S', b'B'),
            Some(BinaryKind::U) => (b'U', b'I'),
            Some(BinaryKind::V) => (b'V', b'J'),
            Some(BinaryKind::W) => (b'W', b'K'),
            _ => return upper,
        };
        let mut bytes = upper.into_bytes();
        if let Some(i) = bytes.iter().position(|&c| c == from) {
            bytes[i] = to;
        }
        String::from_utf8(bytes).unwrap_or_default()
    }
}

/// Parse a binary-table TFORM (`ffbnfm`).
pub fn parse_binary_tform(tform: &str) -> Result<BinaryTform> {
    let trimmed = tform.trim_start();
    if trimmed.is_empty() {
        return Err(FitsError::new(BAD_TFORM));
    }
    if trimmed.len() > FLEN_VALUE - 1 {
        return Err(FitsError::new(BAD_TFORM));
    }
    let form: String = trimmed.chars().map(|c| c.to_ascii_uppercase()).collect();
    let bytes = form.as_bytes();
    let mut i = 0usize;
    while i < bytes.len() && bytes[i].is_ascii_digit() {
        i += 1;
    }
    let repeat = if i == 0 {
        1i64
    } else {
        form[..i]
            .parse::<i64>()
            .map_err(|_| FitsError::new(BAD_TFORM))?
    };
    let rest = &form[i..];
    if rest.is_empty() {
        return Err(FitsError::new(BAD_TFORM_DTYPE));
    }
    let mut rbytes = rest.as_bytes();
    let mut variable = VariableKind::None;
    if rbytes[0] == b'P' {
        variable = VariableKind::P;
        rbytes = &rbytes[1..];
    } else if rbytes[0] == b'Q' {
        variable = VariableKind::Q;
        rbytes = &rbytes[1..];
    }
    if rbytes.is_empty() {
        return Err(FitsError::new(BAD_TFORM_DTYPE));
    }
    let (kind, mut datacode, mut width) = match rbytes[0] {
        b'U' => (BinaryKind::U, TUSHORT, 2usize),
        b'I' => (BinaryKind::I, TSHORT, 2),
        b'V' => (BinaryKind::V, TULONG, 4),
        b'W' => (BinaryKind::W, TULONGLONG, 8),
        b'J' => (BinaryKind::J, TLONG, 4),
        b'K' => (BinaryKind::K, TLONGLONG, 8),
        b'E' => (BinaryKind::E, TFLOAT, 4),
        b'D' => (BinaryKind::D, TDOUBLE, 8),
        b'A' => (BinaryKind::A, TSTRING, repeat as usize),
        b'L' => (BinaryKind::L, TLOGICAL, 1),
        b'X' => (BinaryKind::X, TBIT, 1),
        b'B' => (BinaryKind::B, TBYTE, 1),
        b'S' => (BinaryKind::S, TSBYTE, 1),
        b'C' => (BinaryKind::C, TCOMPLEX, 8),
        b'M' => (BinaryKind::M, TDBLCOMPLEX, 16),
        _ => {
            return Err(FitsError::with_message(
                BAD_TFORM_DTYPE,
                format!("Illegal binary table TFORMn datatype: '{tform}' "),
            ));
        }
    };
    if kind == BinaryKind::A {
        let after = rbytes.get(1..).unwrap_or(&[]);
        let after = after.strip_prefix(b"(").unwrap_or(after);
        if !after.is_empty() {
            if let Ok(w) = parse_leading_long(after) {
                let variable_a = variable != VariableKind::None;
                if !variable_a && w > repeat {
                    width = repeat as usize;
                } else {
                    width = w as usize;
                }
            }
        }
    }
    if variable != VariableKind::None {
        datacode = -datacode;
    }
    Ok(BinaryTform {
        repeat,
        kind,
        variable,
        width,
        datacode,
    })
}

/// Sum of per-row widths and 0-based byte offsets (`ffgtbc` analogue).
pub fn binary_column_offsets(tforms: &[&str]) -> Result<(i64, Vec<i64>)> {
    let mut off = 0i64;
    let mut tb = Vec::with_capacity(tforms.len());
    for tf in tforms {
        tb.push(off);
        off += parse_binary_tform(tf)?.row_nbytes() as i64;
    }
    Ok((off, tb))
}

fn parse_leading_long(bytes: &[u8]) -> Result<i64> {
    let mut i = 0usize;
    while i < bytes.len() && bytes[i].is_ascii_digit() {
        i += 1;
    }
    if i == 0 {
        return Err(FitsError::new(BAD_TFORM));
    }
    std::str::from_utf8(&bytes[..i])
        .unwrap_or("0")
        .parse::<i64>()
        .map_err(|_| FitsError::new(BAD_TFORM))
}

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

    #[test]
    fn parse_standard_tforms() {
        let a = AsciiTform::parse("A15").unwrap();
        assert_eq!(a.kind, AsciiKind::A);
        assert_eq!(a.width, 15);
        assert_eq!(a.datacode, TSTRING);

        let i = AsciiTform::parse("I11").unwrap();
        assert_eq!(i.kind, AsciiKind::I);
        assert_eq!(i.width, 11);
        assert_eq!(i.datacode, TLONG);

        let i4 = AsciiTform::parse("I4").unwrap();
        assert_eq!(i4.datacode, TSHORT);

        let f = AsciiTform::parse("F15.6").unwrap();
        assert_eq!(f.kind, AsciiKind::F);
        assert_eq!(f.width, 15);
        assert_eq!(f.decimals, 6);
        assert_eq!(f.datacode, TDOUBLE); // width > 7

        let e = AsciiTform::parse("E13.5").unwrap();
        assert_eq!(e.kind, AsciiKind::E);
        assert_eq!(e.width, 13);
        assert_eq!(e.decimals, 5);

        let d = AsciiTform::parse("D22.14").unwrap();
        assert_eq!(d.kind, AsciiKind::D);
        assert_eq!(d.width, 22);
        assert_eq!(d.decimals, 14);
        assert_eq!(d.datacode, TDOUBLE);
    }

    #[test]
    fn ffgabc_spacing() {
        let tforms = ["A15", "I11", "F15.6", "E13.5", "D22.14"];
        let (rowlen, tbcol) = ascii_column_starts(&tforms, 1).unwrap();
        assert_eq!(rowlen, 80);
        assert_eq!(tbcol, vec![1, 17, 29, 45, 59]);
    }

    #[test]
    fn bad_tform_codes() {
        assert_eq!(
            AsciiTform::parse("X10").unwrap_err().status,
            BAD_TFORM_DTYPE
        );
        assert_eq!(AsciiTform::parse("").unwrap_err().status, BAD_TFORM);
        assert_eq!(AsciiTform::parse("I0").unwrap_err().status, BAD_TFORM);
        assert_eq!(AsciiTform::parse("   ").unwrap_err().status, BAD_TFORM);
    }

    #[test]
    fn number_format_matches_c_width() {
        let i = AsciiTform::parse("I11").unwrap();
        assert_eq!(format_ascii_number(1.0, &i).unwrap(), b"          1");
        assert_eq!(format_ascii_number(333.0, &i).unwrap(), b"        333");

        let f = AsciiTform::parse("F15.6").unwrap();
        assert_eq!(format_ascii_number(1.25, &f).unwrap(), b"       1.250000");

        let e = AsciiTform::parse("E13.5").unwrap();
        assert_eq!(format_ascii_number(1.25, &e).unwrap(), b"  1.25000E+00");
        assert_eq!(format_ascii_number(2.5e10, &e).unwrap(), b"  2.50000E+10");
        assert_eq!(format_ascii_number(-3.75e-4, &e).unwrap(), b" -3.75000E-04");
    }

    #[test]
    fn parse_binary_tforms() {
        let a = BinaryTform::parse("1A").unwrap();
        assert_eq!(a.kind, BinaryKind::A);
        assert_eq!(a.repeat, 1);
        assert_eq!(a.datacode, TSTRING);
        assert_eq!(a.row_nbytes(), 1);

        let x = BinaryTform::parse("1X").unwrap();
        assert_eq!(x.kind, BinaryKind::X);
        assert_eq!(x.row_nbytes(), 1);

        let j = BinaryTform::parse("J").unwrap();
        assert_eq!(j.repeat, 1);
        assert_eq!(j.datacode, TLONG);
        assert_eq!(j.row_nbytes(), 4);

        let pj = BinaryTform::parse("1PJ").unwrap();
        assert_eq!(pj.variable, VariableKind::P);
        assert_eq!(pj.datacode, -TLONG);
        assert_eq!(pj.row_nbytes(), 8);

        let qk = BinaryTform::parse("1QK").unwrap();
        assert_eq!(qk.variable, VariableKind::Q);
        assert_eq!(qk.row_nbytes(), 16);

        let u = BinaryTform::parse("1U").unwrap();
        assert_eq!(u.kind, BinaryKind::U);
        assert_eq!(BinaryTform::stored_code("1U"), "1I");
        assert_eq!(BinaryTform::stored_code("1S"), "1B");

        let (naxis1, off) =
            binary_column_offsets(&["1A", "1L", "1X", "1B", "1I", "1J", "1E", "1D"]).unwrap();
        assert_eq!(naxis1, 22);
        assert_eq!(off, vec![0, 1, 2, 3, 4, 6, 10, 14]);
    }
}