spectral-io 0.4.0

Read, write and validate optical spectral data files (UV-Vis, visible-range spectra)
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
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
//! Parser for the SpectraShop tab-separated text export format.
//!
//! ## Format licensing
//!
//! The SpectraShop format is proprietary to Robin Myers Imaging. The format
//! specification is published at:
//! <https://www.chromaxion.com/spectral_library/SpectraShop_Import-Export_Format.pdf>
//! specifically to allow third-party readers and writers.
//!
//! ## Data-file licensing (Chromaxion Spectral Library)
//!
//! Spectral data files distributed by Robin Myers Imaging / Chromaxion are free
//! for personal, scientific, and teaching use. Redistribution requires attribution
//! to Chromaxion.com or Robin Myers. Commercial sale requires express written
//! permission. All data © Robin D. Myers, all rights reserved worldwide.
//! See <https://www.chromaxion.com/spectral-library.php> for the full terms.

use super::ALLOWED_ILLUMINANTS;
use super::*;

// Accumulated file-level metadata parsed from a SpectraShop header block.
#[derive(Default, Clone)]
struct SsMeta {
    file_descriptor: Option<String>,
    spectrum_type: Option<String>,
    created: Option<String>,
    originator: Option<String>,
    acquire_note: Option<String>,
    note: Option<String>,
    illuminant: Option<String>,
    observer: Option<String>,
    instrumentation: Option<String>,
    instrument_serial: Option<String>,
    measurement_geometry: Option<String>,
    measurement_source: Option<String>,
    measurement_aperture: Option<String>,
    measurement_filter: Option<String>,
    sample_backing: Option<String>,
    surface: Option<String>,
    manufacturer: Option<String>,
    material: Option<String>,
    model_num: Option<String>,
    prod_date: Option<String>,
    serial: Option<String>,
    nmeasure: Option<u32>,
    sw_version: Option<String>,
}

pub(super) fn ss_parse(input: &str, source_file: Option<&str>) -> Result<SpectrumFile> {
    let mut meta = SsMeta::default();
    let mut in_data_format = false;
    let mut in_data = false;

    // A SpectraShop file can contain multiple BEGIN_DATA_FORMAT/BEGIN_DATA block
    // pairs (e.g., different wavelength ranges in the same file).  Each pair is
    // processed independently when END_DATA is encountered.
    let mut current_format: Vec<String> = Vec::new();
    let mut current_data: Vec<String> = Vec::new();
    let mut all_records: Vec<SpectrumRecord> = Vec::new();
    let mut global_idx: usize = 0;
    let mut saw_data_block = false;
    let mut any_records_parsed = false;

    for line in input.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }

        // Section markers (case-insensitive exact match on the trimmed line)
        let upper = trimmed.to_uppercase();
        if upper == "BEGIN_DATA_FORMAT" {
            current_format.clear();
            in_data_format = true;
            continue;
        }
        if upper == "END_DATA_FORMAT" {
            in_data_format = false;
            continue;
        }
        if upper == "BEGIN_DATA" {
            current_data.clear();
            in_data = true;
            saw_data_block = true;
            continue;
        }
        if upper == "END_DATA" {
            in_data = false;
            // Process the completed format+data section.
            if !current_format.is_empty() && !current_data.is_empty() {
                any_records_parsed = true;
                let n_fields = current_format.len();
                if current_data.len() % n_fields != 0 {
                    return Err(SpectrumFileError::SchemaValidation(format!(
                        "SpectraShop: data token count ({}) is not a multiple of \
                         field count ({}) in section ending near record {}",
                        current_data.len(),
                        n_fields,
                        global_idx
                    )));
                }
                let n_section = current_data.len() / n_fields;
                for r in 0..n_section {
                    let row = &current_data[r * n_fields..(r + 1) * n_fields];
                    all_records.push(ss_build_record(
                        &current_format,
                        row,
                        &meta,
                        global_idx,
                        source_file,
                    )?);
                    global_idx += 1;
                }
            }
            continue;
        }

        if in_data_format {
            for field in trimmed.split_whitespace() {
                current_format.push(field.to_uppercase());
            }
            continue;
        }
        if in_data {
            ss_tokenize_into(trimmed, &mut current_data);
            continue;
        }

        // Header / inter-section metadata line: KEYWORD<tab>value.
        // These can appear before the first section or between sections.
        if let Some((key, value)) = ss_split_kv(trimmed) {
            ss_apply_kv(&mut meta, &key, &value);
        }
    }

    if !saw_data_block {
        return Err(SpectrumFileError::SchemaValidation(
            "SpectraShop: no BEGIN_DATA/END_DATA block found".into(),
        ));
    }
    if !any_records_parsed {
        return Err(SpectrumFileError::SchemaValidation(
            "SpectraShop: BEGIN_DATA/END_DATA block present but contained no parseable records \
             (check that BEGIN_DATA_FORMAT is also present and non-empty)"
                .into(),
        ));
    }
    if all_records.is_empty() {
        return Err(SpectrumFileError::SchemaValidation(
            "SpectraShop: file contains no spectral records".into(),
        ));
    }

    let schema_version = "1.0.0".to_string();
    if all_records.len() == 1 {
        Ok(SpectrumFile::Single {
            schema_version,
            spectrum: Box::new(all_records.into_iter().next().unwrap()),
        })
    } else {
        Ok(SpectrumFile::Batch {
            schema_version,
            batch_metadata: None,
            spectra: all_records,
        })
    }
}

// Apply a parsed KEYWORD+VALUE pair to the SsMeta accumulator.
fn ss_apply_kv(meta: &mut SsMeta, key: &str, value: &str) {
    match key {
        // "SpectraShop\t5.0" first line — treat version as sw_version
        "SPECTRASHOP" => {
            if meta.sw_version.is_none() {
                meta.sw_version = ss_str(value);
            }
        }
        "FILE_DESCRIPTOR" => meta.file_descriptor = ss_str(value),
        "SPECTRUM_TYPE" => meta.spectrum_type = ss_str(value),
        "CREATED" => meta.created = ss_str(value),
        "ORIGINATOR" => meta.originator = ss_str(value),
        "ACQUIRE_NOTE" => meta.acquire_note = ss_str(value),
        "NOTE" => meta.note = ss_str(value),
        "ILLUMINANT" => meta.illuminant = ss_str(value),
        "OBSERVER" => meta.observer = ss_str(value),
        "INSTRUMENTATION" => meta.instrumentation = ss_str(value),
        "INSTRUMENT_SERIAL" | "INSTRUMENT_SERIAL_NUMBER" => meta.instrument_serial = ss_str(value),
        "MEASUREMENT_GEOMETRY" => meta.measurement_geometry = ss_str(value),
        "MEASUREMENT_SOURCE" => meta.measurement_source = ss_str(value),
        "MEASUREMENT_APERTURE" => meta.measurement_aperture = ss_str(value),
        "MEASUREMENT_FILTER" => meta.measurement_filter = ss_str(value),
        "SAMPLE_BACKING" => meta.sample_backing = ss_str(value),
        "SURFACE" => meta.surface = ss_str(value),
        "MANUFACTURER" => meta.manufacturer = ss_str(value),
        "MATERIAL" => meta.material = ss_str(value),
        "MODEL" | "MODEL_NUM" => meta.model_num = ss_str(value),
        "PROD_DATE" => meta.prod_date = ss_str(value),
        "SERIAL" => meta.serial = ss_str(value),
        "NMEASUREMENTS" | "NMEASURE" => {
            meta.nmeasure = ss_str(value)
                .as_deref()
                .unwrap_or(value)
                .trim()
                .parse::<u32>()
                .ok()
        }
        "SW_VERSION" => meta.sw_version = ss_str(value),
        // Informational / ignored
        "NUMBER_OF_SETS" | "NUMBER_OF_FIELDS" | "RGB_SPACE" => {}
        _ => {}
    }
}

// Split "KEYWORD<whitespace>rest" → (UPPERCASE_KEY, rest).
fn ss_split_kv(line: &str) -> Option<(String, String)> {
    let sep = line
        .find('\t')
        .or_else(|| line.find(|c: char| c.is_whitespace()))?;
    let key = line[..sep].trim().to_uppercase();
    let value = line[sep..].trim().to_string();
    if key.is_empty() {
        return None;
    }
    Some((key, value))
}

// Strip surrounding double-quotes and return None for empty strings.
fn ss_str(s: &str) -> Option<String> {
    let s = s.trim();
    let s = s
        .strip_prefix('"')
        .and_then(|s| s.strip_suffix('"'))
        .unwrap_or(s);
    if s.is_empty() {
        None
    } else {
        Some(s.to_string())
    }
}

// Parse a decimal number; accepts comma as decimal separator.
fn ss_float(s: &str) -> Option<f64> {
    s.trim().replace(',', ".").parse::<f64>().ok()
}

// Parse "4 mm" → Some(4.0); "LAV" → None.
fn ss_aperture_mm(s: &str) -> Option<f64> {
    ss_float(s.split_whitespace().next().unwrap_or(s))
}

// Map SpectraShop SPECTRUM_TYPE to MeasurementType.
fn ss_measurement_type(s: &str) -> MeasurementType {
    let lower = s.trim().to_lowercase();
    if lower.contains("transmis") {
        MeasurementType::Transmittance
    } else if lower.contains("emissive") || lower.contains("irradiance") {
        MeasurementType::Irradiance
    } else {
        MeasurementType::Reflectance
    }
}

// Map SpectraShop OBSERVER string to a CIE observer enum value.
fn ss_observer(s: &str) -> Option<String> {
    let lower = s.trim().to_lowercase();
    if lower.contains("10") {
        Some("CIE 1964 10 degree".into())
    } else if lower.contains("2") {
        Some("CIE 1931 2 degree".into())
    } else {
        None
    }
}

// Map a SpectraShop ILLUMINANT string to an allowed illuminant code, if recognised.
fn ss_illuminant(s: &str) -> Option<String> {
    if ALLOWED_ILLUMINANTS.contains(&s) && s != "custom" {
        Some(s.to_string())
    } else {
        None
    }
}

// Build a WavelengthAxis from a list of wavelengths, using range_nm for regular grids.
fn ss_wavelength_axis(wls: &[f64]) -> WavelengthAxis {
    if wls.len() >= 2 {
        let interval = wls[1] - wls[0];
        let regular = interval > 0.0
            && wls
                .windows(2)
                .all(|w| (w[1] - w[0] - interval).abs() < 0.001);
        if regular {
            return WavelengthAxis {
                values_nm: None,
                range_nm: Some(WavelengthRange {
                    start: wls[0],
                    end: *wls.last().unwrap(),
                    interval,
                }),
            };
        }
    }
    WavelengthAxis {
        values_nm: Some(wls.to_vec()),
        range_nm: None,
    }
}

// Parse a date from ISO 8601 (YYYY-MM-DD...) or MM/DD/YYYY formats.
fn ss_parse_date(s: &str) -> Option<String> {
    let s = s.trim();
    if s.len() >= 10 && s.as_bytes().get(4) == Some(&b'-') && s.as_bytes().get(7) == Some(&b'-') {
        return Some(s[..10].to_string());
    }
    let parts: Vec<&str> = s.splitn(3, '/').collect();
    if parts.len() == 3 {
        let month = parts[0].trim();
        let day = parts[1].trim();
        let year = parts[2].split_whitespace().next().unwrap_or("");
        if month.parse::<u32>().is_ok()
            && day.parse::<u32>().is_ok()
            && year.len() == 4
            && year.parse::<u32>().is_ok()
        {
            return Some(format!("{year}-{month:0>2}-{day:0>2}"));
        }
    }
    None
}

// Tokenize a SpectraShop data line into tab-delimited fields, stripping the
// outermost pair of quotes from each field. This correctly handles sample names
// that contain embedded quotes (e.g. `"United Drug Co. "Graph Blue""`), because
// only the first and last `"` are stripped rather than the first matching pair.
fn ss_tokenize_into(line: &str, tokens: &mut Vec<String>) {
    for field in line.split('\t') {
        if field.is_empty() {
            continue; // trailing or adjacent tabs — skip
        }
        let s = field.trim();
        if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
            tokens.push(s[1..s.len() - 1].to_string());
        } else if !s.is_empty() {
            tokens.push(s.to_string());
        }
    }
}

fn ss_build_record(
    fields: &[String],
    values: &[String],
    meta: &SsMeta,
    idx: usize,
    source_file: Option<&str>,
) -> Result<SpectrumRecord> {
    // SAMPLE_ID1 = machine/file id (becomes SpectrumRecord.id)
    // SAMPLE_ID2 / SAMPLE_NAME = human label (becomes metadata.title; falls back to SAMPLE_ID1)
    // SAMPLE_ID3 = rarely used; goes to custom if non-empty
    let mut ss_id1: Option<String> = None;
    let mut ss_id2: Option<String> = None;
    let mut wls: Vec<f64> = Vec::new();
    let mut spectral_vals: Vec<f64> = Vec::new();
    let mut custom_map: serde_json::Map<String, serde_json::Value> = serde_json::Map::new();

    let mut i = 0;
    while i < fields.len() {
        let field = fields[i].as_str();
        let val = &values[i];

        match field {
            "SAMPLE_ID" | "SAMPLE_ID1" => {
                ss_id1 = ss_str(val);
                i += 1;
            }
            "SAMPLE_NAME" | "SAMPLE_ID2" => {
                ss_id2 = ss_str(val);
                i += 1;
            }
            "SAMPLE_ID3" => {
                if let Some(s) = ss_str(val) {
                    custom_map.insert("sample_id3".into(), serde_json::Value::String(s));
                }
                i += 1;
            }
            "SPECTRAL_NM" => {
                // Consume the paired SPECTRAL_NM + SPECTRAL_VAL together.
                if i + 1 < fields.len() && fields[i + 1] == "SPECTRAL_VAL" {
                    if let (Some(wl), Some(sv)) = (ss_float(val), ss_float(&values[i + 1])) {
                        wls.push(wl);
                        spectral_vals.push(sv);
                    }
                    i += 2;
                } else {
                    i += 1;
                }
            }
            "SPECTRAL_VAL" => i += 1,
            _ => {
                if let Some(s) = ss_str(val) {
                    custom_map.insert(field.to_lowercase(), serde_json::Value::String(s));
                }
                i += 1;
            }
        }
    }

    if wls.is_empty() {
        return Err(SpectrumFileError::SchemaValidation(format!(
            "SpectraShop: record {idx} has no SPECTRAL_NM/SPECTRAL_VAL pairs"
        )));
    }

    // id comes from SAMPLE_ID / SAMPLE_ID1.
    // title prefers SAMPLE_NAME / SAMPLE_ID2 (the human label) and falls back to
    // SAMPLE_ID / SAMPLE_ID1 when no separate name field is present (e.g. Smarties).
    let id = ss_id1.clone().unwrap_or_else(|| format!("{}", idx + 1));
    let title = ss_id2.clone().or_else(|| ss_id1.clone());
    let date = meta
        .created
        .as_deref()
        .and_then(ss_parse_date)
        .unwrap_or_else(|| "1970-01-01".to_string());
    let measurement_type = meta
        .spectrum_type
        .as_deref()
        .map(ss_measurement_type)
        .unwrap_or(MeasurementType::Reflectance);

    let instrument =
        (meta.instrumentation.is_some() || meta.instrument_serial.is_some()).then(|| Instrument {
            manufacturer: None,
            model: meta.instrumentation.clone(),
            serial_number: meta.instrument_serial.clone(),
            detector_type: None,
            light_source: None,
        });

    let aperture_mm = meta
        .measurement_aperture
        .as_deref()
        .and_then(ss_aperture_mm);
    let filter = meta.measurement_filter.as_deref().and_then(|s| {
        if s.eq_ignore_ascii_case("none") {
            None
        } else {
            Some(s.to_string())
        }
    });
    let mc_any = meta.measurement_geometry.is_some()
        || aperture_mm.is_some()
        || filter.is_some()
        || meta.nmeasure.is_some();
    let measurement_conditions = mc_any.then(|| MeasurementConditions {
        integration_time_ms: None,
        averaging: meta.nmeasure,
        temperature_celsius: None,
        geometry: meta.measurement_geometry.clone(),
        specular_component: None,
        spectral_resolution_nm: None,
        measurement_aperture_mm: aperture_mm,
        measurement_filter: filter,
    });

    let illuminant = meta.illuminant.as_deref().and_then(ss_illuminant);
    let observer = meta.observer.as_deref().and_then(ss_observer);
    let color_science = (illuminant.is_some() || observer.is_some()).then_some(ColorScience {
        illuminant,
        illuminant_custom_sd: None,
        cie_observer: observer,
        white_reference: None,
        results: None,
    });

    // File-level fields that don't map to explicit SpectrumRecord fields go in custom.
    for (k, v) in [
        ("manufacturer", &meta.manufacturer),
        ("material", &meta.material),
        ("model_num", &meta.model_num),
        ("serial", &meta.serial),
        ("prod_date", &meta.prod_date),
        ("measurement_source", &meta.measurement_source),
    ] {
        if let Some(s) = v {
            custom_map
                .entry(k)
                .or_insert_with(|| serde_json::Value::String(s.clone()));
        }
    }
    let custom = (!custom_map.is_empty()).then_some(serde_json::Value::Object(custom_map));

    let notes: Vec<&str> = [&meta.acquire_note, &meta.note]
        .iter()
        .filter_map(|o| o.as_deref())
        .collect();
    let notes_str = (!notes.is_empty()).then(|| notes.join("; "));

    Ok(SpectrumRecord {
        id,
        metadata: SpectrumMetadata {
            measurement_type,
            date,
            title,
            description: meta.file_descriptor.clone(),
            sample_id: None,
            time: None,
            operator: meta.originator.clone(),
            instrument,
            measurement_conditions,
            surface: meta.surface.clone(),
            sample_backing: meta.sample_backing.clone(),
            tags: None,
            copyright: None,
            custom,
        },
        wavelength_axis: ss_wavelength_axis(&wls),
        spectral_data: SpectralData {
            values: spectral_vals,
            uncertainty: None,
            scale: None,
        },
        color_science,
        provenance: Some(Provenance {
            software: Some("SpectraShop".into()),
            software_version: meta.sw_version.clone(),
            source_file: source_file.map(str::to_string),
            source_format: Some("SpectraShop Text".into()),
            processing_steps: None,
            notes: notes_str,
        }),
    })
}

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

    fn ss_input_single() -> String {
        // Tab-separated SpectraShop header + data block with 3 wavelength points.
        [
            "FILE_DESCRIPTOR\tTest file",
            "SPECTRUM_TYPE\tReflective",
            "CREATED\t2014-05-15",
            "ORIGINATOR\tTest User",
            "ILLUMINANT\tD65",
            "OBSERVER\t2 degree",
            "INSTRUMENTATION\tCM-700d",
            "INSTRUMENT_SERIAL\tABC123",
            "MEASUREMENT_GEOMETRY\td:8",
            "MEASUREMENT_FILTER\tNone",
            "SAMPLE_BACKING\tBlack",
            "NMEASURE\t3",
            "SW_VERSION\t5.0.0",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSAMPLE_NAME\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\tRedTile\t380\t0.048\t390\t0.051\t400\t0.054",
            "END_DATA",
        ]
        .join("\n")
    }

    #[test]
    fn spectrashop_single_record() {
        let file = SpectrumFile::from_spectrashop_str(&ss_input_single()).unwrap();
        assert!(matches!(file, SpectrumFile::Single { .. }));
        let spectra = file.spectra();
        assert_eq!(spectra.len(), 1);
        let sp = spectra[0];
        assert_eq!(sp.id, "s1");
        assert_eq!(sp.metadata.title.as_deref(), Some("RedTile"));
        assert_eq!(sp.metadata.description.as_deref(), Some("Test file"));
        assert_eq!(sp.metadata.operator.as_deref(), Some("Test User"));
        assert_eq!(sp.metadata.date, "2014-05-15");
        assert_eq!(sp.metadata.sample_backing.as_deref(), Some("Black"));
        assert!(matches!(
            sp.metadata.measurement_type,
            MeasurementType::Reflectance
        ));
        assert_eq!(sp.n_points(), 3);
        let cs = sp.color_science.as_ref().unwrap();
        assert_eq!(cs.illuminant.as_deref(), Some("D65"));
        assert_eq!(cs.cie_observer.as_deref(), Some("CIE 1931 2 degree"));
        let mc = sp.metadata.measurement_conditions.as_ref().unwrap();
        assert_eq!(mc.geometry.as_deref(), Some("d:8"));
        assert!(mc.measurement_filter.is_none()); // "None" filtered out
        assert_eq!(mc.averaging, Some(3));
        let prov = sp.provenance.as_ref().unwrap();
        assert_eq!(prov.software.as_deref(), Some("SpectraShop"));
        assert_eq!(prov.software_version.as_deref(), Some("5.0.0"));
        assert_eq!(prov.source_format.as_deref(), Some("SpectraShop Text"));
        let instr = sp.metadata.instrument.as_ref().unwrap();
        assert_eq!(instr.model.as_deref(), Some("CM-700d"));
        assert_eq!(instr.serial_number.as_deref(), Some("ABC123"));
    }

    #[test]
    fn spectrashop_batch_two_records() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "1\t380\t0.1\t390\t0.2",
            "2\t380\t0.3\t390\t0.4",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        assert!(matches!(file, SpectrumFile::Batch { .. }));
        assert_eq!(file.spectra().len(), 2);
        assert_eq!(file.spectra()[0].id, "1");
        assert_eq!(file.spectra()[1].id, "2");
    }

    #[test]
    fn spectrashop_regular_grid_uses_range_nm() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2\t400\t0.3",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let sp = &file.spectra()[0];
        assert!(sp.wavelength_axis.range_nm.is_some());
        assert!(sp.wavelength_axis.values_nm.is_none());
        let r = sp.wavelength_axis.range_nm.as_ref().unwrap();
        assert!((r.start - 380.0).abs() < 1e-9);
        assert!((r.end - 400.0).abs() < 1e-9);
        assert!((r.interval - 10.0).abs() < 1e-9);
    }

    #[test]
    fn spectrashop_irregular_grid_uses_values_nm() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t400\t0.2\t450\t0.3",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let sp = &file.spectra()[0];
        assert!(sp.wavelength_axis.values_nm.is_some());
        assert!(sp.wavelength_axis.range_nm.is_none());
    }

    #[test]
    fn spectrashop_date_mm_dd_yyyy() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "CREATED\t05/15/2014 13:40:29",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        assert_eq!(file.spectra()[0].metadata.date, "2014-05-15");
    }

    #[test]
    fn spectrashop_transmissive_type() {
        let input = [
            "SPECTRUM_TYPE\tTransmissive",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        assert!(matches!(
            file.spectra()[0].metadata.measurement_type,
            MeasurementType::Transmittance
        ));
    }

    #[test]
    fn spectrashop_10_degree_observer() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "OBSERVER\t10 degree",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let cs = file.spectra()[0].color_science.as_ref().unwrap();
        assert_eq!(cs.cie_observer.as_deref(), Some("CIE 1964 10 degree"));
    }

    #[test]
    fn spectrashop_aperture_mm_parsed() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "MEASUREMENT_GEOMETRY\td:8",
            "MEASUREMENT_APERTURE\t4 mm",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let mc = file.spectra()[0]
            .metadata
            .measurement_conditions
            .as_ref()
            .unwrap();
        assert!((mc.measurement_aperture_mm.unwrap() - 4.0).abs() < 1e-9);
    }

    #[test]
    fn spectrashop_lav_aperture_is_none() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "MEASUREMENT_GEOMETRY\td:8",
            "MEASUREMENT_APERTURE\tLAV",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let mc = file.spectra()[0]
            .metadata
            .measurement_conditions
            .as_ref()
            .unwrap();
        assert!(mc.measurement_aperture_mm.is_none());
    }

    #[test]
    fn spectrashop_quoted_sample_name_with_spaces() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSAMPLE_NAME\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t\"Red Munsell\"\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        assert_eq!(
            file.spectra()[0].metadata.title.as_deref(),
            Some("Red Munsell")
        );
    }

    #[test]
    fn spectrashop_missing_data_format_is_error() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        assert!(matches!(
            SpectrumFile::from_spectrashop_str(&input),
            Err(SpectrumFileError::SchemaValidation(_))
        ));
    }

    #[test]
    fn spectrashop_empty_data_block_is_error() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "END_DATA",
        ]
        .join("\n");

        assert!(matches!(
            SpectrumFile::from_spectrashop_str(&input),
            Err(SpectrumFileError::SchemaValidation(_))
        ));
    }

    #[test]
    fn spectrashop_id_fallback_when_no_sample_id_field() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "BEGIN_DATA_FORMAT",
            "SPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        // No SAMPLE_ID field → fallback to "1"
        assert_eq!(file.spectra()[0].id, "1");
    }

    #[test]
    fn spectrashop_multiple_data_blocks() {
        // Files may contain more than one BEGIN_DATA block reusing a single
        // BEGIN_DATA_FORMAT; records from all blocks are merged into a single batch.
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
            "BEGIN_DATA",
            "s2\t380\t0.3\t390\t0.4",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        assert!(matches!(file, SpectrumFile::Batch { .. }));
        assert_eq!(file.spectra().len(), 2);
        assert_eq!(file.spectra()[0].id, "s1");
        assert_eq!(file.spectra()[1].id, "s2");
    }

    #[test]
    fn spectrashop_note_in_provenance() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "NOTE\tCalibrated 2024-01",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let prov = file.spectra()[0].provenance.as_ref().unwrap();
        assert_eq!(prov.notes.as_deref(), Some("Calibrated 2024-01"));
    }

    #[test]
    fn spectrashop_both_notes_joined() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "ACQUIRE_NOTE\tFirst note",
            "NOTE\tSecond note",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let prov = file.spectra()[0].provenance.as_ref().unwrap();
        assert_eq!(prov.notes.as_deref(), Some("First note; Second note"));
    }

    #[test]
    fn spectrashop_measurement_filter_preserved() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "MEASUREMENT_GEOMETRY\t45/0",
            "MEASUREMENT_FILTER\tD65",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let mc = file.spectra()[0]
            .metadata
            .measurement_conditions
            .as_ref()
            .unwrap();
        assert_eq!(mc.measurement_filter.as_deref(), Some("D65"));
    }

    #[test]
    fn spectrashop_sample_id3_in_custom() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID1\tSAMPLE_ID2\tSAMPLE_ID3\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "42\tDeep Red\tWarm Red\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let sp = file.spectra()[0];
        assert_eq!(sp.id, "42");
        assert_eq!(sp.metadata.title.as_deref(), Some("Deep Red"));
        let custom = sp.metadata.custom.as_ref().expect("custom must be set");
        assert_eq!(
            custom.get("sample_id3").and_then(|v| v.as_str()),
            Some("Warm Red")
        );
    }

    #[test]
    fn spectrashop_unknown_data_field_in_custom() {
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tMY_CUSTOM_FIELD\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\tmy_value\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let custom = file.spectra()[0]
            .metadata
            .custom
            .as_ref()
            .expect("custom must be set");
        assert_eq!(
            custom.get("my_custom_field").and_then(|v| v.as_str()),
            Some("my_value")
        );
    }

    #[test]
    fn spectrashop_aperture_comma_decimal() {
        // European locale files use comma as decimal separator.
        let input = [
            "SPECTRUM_TYPE\tReflective",
            "MEASUREMENT_GEOMETRY\t45/0",
            "MEASUREMENT_APERTURE\t4,5 mm",
            "BEGIN_DATA_FORMAT",
            "SAMPLE_ID\tSPECTRAL_NM\tSPECTRAL_VAL\tSPECTRAL_NM\tSPECTRAL_VAL",
            "END_DATA_FORMAT",
            "BEGIN_DATA",
            "s1\t380\t0.1\t390\t0.2",
            "END_DATA",
        ]
        .join("\n");

        let file = SpectrumFile::from_spectrashop_str(&input).unwrap();
        let mc = file.spectra()[0]
            .metadata
            .measurement_conditions
            .as_ref()
            .unwrap();
        assert!((mc.measurement_aperture_mm.unwrap() - 4.5).abs() < 1e-9);
    }
}