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
use alloc::collections::BTreeMap;
use alloc::str;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use core::marker::Copy;

use chrono::NaiveDateTime;

use crate::buffer::ReadBuffer;
use crate::parsers::{Endian, FromBuffer, FromSlice};
use crate::record::{RecordHeader, StateMetadata, Value};
use crate::EtError;
use crate::{impl_reader, impl_record};

fn read_agilent_header<'r>(rb: &'r mut ReadBuffer, ms_format: bool) -> Result<&'r [u8], EtError> {
    rb.reserve(268)?;

    // figure out how big the header should be and then get it
    let raw_header_size = u32::out_of(&rb[264..268], Endian::Big)? as usize;
    if raw_header_size == 0 {
        return Err(EtError::new("Invalid header length of 0"));
    }
    let mut header_size = 2 * (raw_header_size - 1);
    if !ms_format {
        header_size *= 256;
    }
    if header_size < 512 {
        return Err(EtError::new("Header length too short"));
    }
    rb.extract::<&[u8]>(header_size)
}

#[derive(Debug)]
/// Metadata consistly found in Chemstation file formats
pub struct ChemstationMetadata {
    /// Time the run started (minutes)
    pub start_time: f64,
    /// Time the ended started (minutes)
    pub end_time: f64,
    /// Name of the signal record (specifically used for e.g. MWD traces)
    pub signal_name: String,
    /// Absolute correction to be applied to all data points
    pub offset_correction: f64,
    /// Scaling correction to be applied to all data points
    pub mult_correction: f64,
    /// In what order this run was performed
    pub sequence: u16,
    /// The vial number this run was performed from
    pub vial: u16,
    /// The replicate number of this run
    pub replicate: u16,
    /// The name of the sample
    pub sample: String,
    /// The description of the sample
    pub description: String,
    /// The name of the operator
    pub operator: String,
    /// The date the sample was run
    pub run_date: Option<NaiveDateTime>,
    /// The instrument the sample was run on
    pub instrument: String,
    /// The method the instrument ran
    pub method: String,
}

impl<'r> From<&ChemstationMetadata> for BTreeMap<String, Value<'r>> {
    fn from(metadata: &ChemstationMetadata) -> Self {
        let mut map = BTreeMap::new();
        let _ = map.insert("start_time".to_string(), metadata.start_time.into());
        let _ = map.insert("end_time".to_string(), metadata.end_time.into());
        let _ = map.insert(
            "signal_name".to_string(),
            metadata.signal_name.clone().into(),
        );
        let _ = map.insert(
            "offset_correction".to_string(),
            metadata.offset_correction.into(),
        );
        let _ = map.insert(
            "mult_correction".to_string(),
            metadata.mult_correction.into(),
        );
        let _ = map.insert("sequence".to_string(), metadata.sequence.into());
        let _ = map.insert("vial".to_string(), metadata.vial.into());
        let _ = map.insert("replicate".to_string(), metadata.replicate.into());
        let _ = map.insert("sample".to_string(), metadata.sample.clone().into());
        let _ = map.insert(
            "description".to_string(),
            metadata.description.clone().into(),
        );
        let _ = map.insert("operator".to_string(), metadata.operator.clone().into());
        let _ = map.insert("run_date".to_string(), metadata.run_date.clone().into());
        let _ = map.insert("instrument".to_string(), metadata.instrument.clone().into());
        let _ = map.insert("method".to_string(), metadata.method.clone().into());
        map
    }
}

fn get_metadata(header: &[u8]) -> Result<ChemstationMetadata, EtError> {
    let start_time = f64::from(i32::out_of(&header[282..], Endian::Big)?) / 60000.;
    let end_time = f64::from(i32::out_of(&header[286..], Endian::Big)?) / 60000.;

    let offset_correction = f64::out_of(&header[636..], Endian::Big)?;
    let mult_correction = f64::out_of(&header[644..], Endian::Big)?;

    let signal_name_len = usize::from(header[596]);
    let signal_name = str::from_utf8(&header[597..597 + signal_name_len])?
        .trim()
        .to_string();

    let sample_len = usize::from(header[24]);
    let sample = str::from_utf8(&header[25..25 + sample_len])?
        .trim()
        .to_string();
    let description_len = usize::from(header[86]);
    let description = str::from_utf8(&header[87..87 + description_len])?
        .trim()
        .to_string();
    let operator_len = usize::from(header[148]);
    let operator = str::from_utf8(&header[149..149 + operator_len])?
        .trim()
        .to_string();
    let run_date_len = usize::from(header[178]);
    // We need to detect the date format before we can convert into a
    // NaiveDateTime; not sure the format even maps to the file type
    // (it may be computer-dependent?)
    let raw_run_date = str::from_utf8(&header[179..179 + run_date_len])?.trim();
    let run_date = if let Ok(d) = NaiveDateTime::parse_from_str(raw_run_date, "%d-%b-%y, %H:%M:%S")
    {
        // format in MWD
        Some(d)
    } else if let Ok(d) = NaiveDateTime::parse_from_str(raw_run_date, "%d %b %y %l:%M %P") {
        // format in MS
        Some(d)
    } else if let Ok(d) = NaiveDateTime::parse_from_str(raw_run_date, "%d %b %y %l:%M %P %z") {
        // format in MS with timezone
        Some(d)
    } else if let Ok(d) = NaiveDateTime::parse_from_str(raw_run_date, "%m/%d/%y %I:%M:%S %p") {
        // format in FID
        Some(d)
    } else {
        None
    };

    let instrument_len = usize::from(header[208]);
    let instrument = str::from_utf8(&header[209..209 + instrument_len])?
        .trim()
        .to_string();
    let method_len = usize::from(header[228]);
    let method = str::from_utf8(&header[229..229 + method_len])?
        .trim()
        .to_string();

    // not sure how robust the following are
    let sequence = u16::out_of(&header[252..], Endian::Big)?;
    let vial = u16::out_of(&header[254..], Endian::Big)?;
    let replicate = u16::out_of(&header[256..], Endian::Big)?;

    Ok(ChemstationMetadata {
        start_time,
        end_time,
        signal_name,
        offset_correction,
        mult_correction,
        sequence,
        vial,
        replicate,
        sample,
        description,
        operator,
        run_date,
        instrument,
        method,
    })
}

#[derive(Debug)]
/// Internal state for the ChemstationFid parser
pub struct ChemstationFidState {
    cur_time: f64,
    cur_delta: f64,
    cur_intensity: f64,
    time_step: f64,
    metadata: ChemstationMetadata,
}

impl<'r> StateMetadata<'r> for ChemstationFidState {
    fn metadata(&self) -> BTreeMap<String, Value> {
        (&self.metadata).into()
    }
}

impl<'r> FromBuffer<'r> for ChemstationFidState {
    type State = ();

    fn get(mut rb: &'r mut ReadBuffer, _state: Self::State) -> Result<Self, EtError> {
        let header = read_agilent_header(&mut rb, false)?;
        let metadata = get_metadata(&header)?;

        Ok(ChemstationFidState {
            cur_time: metadata.start_time,
            cur_intensity: 0.,
            cur_delta: 0.,
            time_step: 0.2,
            metadata,
        })
    }
}

#[derive(Clone, Copy, Debug)]
/// A point in a FID trace
pub struct ChemstationFidRecord {
    /// The time recorded at
    pub time: f64,
    /// The intensity recorded
    pub intensity: f64,
}

impl_record!(ChemstationFidRecord: time, intensity);

impl<'r> FromBuffer<'r> for Option<ChemstationFidRecord> {
    type State = &'r mut ChemstationFidState;

    fn get(rb: &'r mut ReadBuffer, state: Self::State) -> Result<Self, EtError> {
        if rb.len() < 2 {
            rb.refill()?;
        }
        if rb.is_empty() && rb.eof() {
            return Ok(None);
        } else if rb.len() == 1 && rb.eof() {
            return Err(EtError::new("FID record was incomplete"));
        }
        let time = state.cur_time;
        state.cur_time += state.time_step;

        let intensity: i16 = rb.extract(Endian::Big)?;
        if intensity == 32767 {
            state.cur_delta = 0.;
            let high_value: i32 = rb.extract(Endian::Big)?;
            let low_value: u16 = rb.extract(Endian::Big)?;
            state.cur_intensity = f64::from(high_value) * 65534. + f64::from(low_value);
        } else {
            state.cur_delta += f64::from(intensity);
            state.cur_intensity += state.cur_delta;
        }

        Ok(Some(ChemstationFidRecord {
            time,
            intensity: state.cur_intensity * state.metadata.mult_correction
                + state.metadata.offset_correction,
        }))
    }
}

#[derive(Debug)]
/// Internal state for the ChemstationMs parser
pub struct ChemstationMsState {
    n_scans_left: usize,
    n_mzs_left: usize,
    cur_time: f64,
    metadata: ChemstationMetadata,
}

impl<'r> StateMetadata<'r> for ChemstationMsState {
    fn metadata(&self) -> BTreeMap<String, Value> {
        (&self.metadata).into()
    }
}

impl<'r> FromBuffer<'r> for ChemstationMsState {
    type State = ();

    fn get(mut rb: &'r mut ReadBuffer, _state: Self::State) -> Result<Self, EtError> {
        let header = read_agilent_header(&mut rb, true)?;
        let metadata = get_metadata(&header)?;
        let n_scans = u32::out_of(&header[278..], Endian::Big)? as usize;

        Ok(ChemstationMsState {
            n_scans_left: n_scans,
            n_mzs_left: 0,
            cur_time: 0.,
            metadata,
        })
    }
}

#[derive(Clone, Copy, Debug)]
/// A single time/mz record from a ChemstationMs file
pub struct ChemstationMsRecord {
    /// The time recorded at
    pub time: f64,
    /// The m/z recorded at
    pub mz: f64,
    /// The intensity recorded
    pub intensity: f64,
}

impl_record!(ChemstationMsRecord: time, mz, intensity);

impl<'r> FromBuffer<'r> for Option<ChemstationMsRecord> {
    type State = &'r mut ChemstationMsState;

    fn get(rb: &'r mut ReadBuffer, state: Self::State) -> Result<Self, EtError> {
        if state.n_scans_left == 0 {
            return Ok(None);
        }

        // refill case
        if state.n_mzs_left == 0 {
            // handle the record header
            let raw_n_mzs_left: u16 = rb.extract(Endian::Big)?;
            if raw_n_mzs_left <= 14 {
                return Err("Invalid record header".into());
            }
            state.n_mzs_left = usize::from((raw_n_mzs_left - 13) / 2);
            state.cur_time = f64::from(rb.extract::<u32>(Endian::Big)?) / 60000.;
            let _ = rb.extract::<&[u8]>(12_usize)?;
        };

        // just read the mz/intensity
        let mz = f64::from(rb.extract::<u16>(Endian::Big)?) / 20.;
        let raw_intensity: u16 = rb.extract(Endian::Big)?;
        let intensity =
            f64::from(raw_intensity & 16383) * 8f64.powi(i32::from(raw_intensity) >> 14);
        if state.n_mzs_left == 1 {
            state.n_scans_left -= 1;
            // eat the footer
            let _ = rb.extract::<&[u8]>(10_usize)?;
        }
        state.n_mzs_left -= 1;

        Ok(Some(ChemstationMsRecord {
            time: state.cur_time,
            mz,
            intensity,
        }))
    }
}

#[derive(Debug)]
/// Internal state for the ChemstationMwd parser
pub struct ChemstationMwdState {
    n_wvs_left: usize,
    cur_time: f64,
    cur_intensity: f64,
    time_step: f64,
    metadata: ChemstationMetadata,
}

impl<'r> StateMetadata<'r> for ChemstationMwdState {
    fn metadata(&self) -> BTreeMap<String, Value> {
        (&self.metadata).into()
    }
}

impl<'r> FromBuffer<'r> for ChemstationMwdState {
    type State = ();

    fn get(mut rb: &'r mut ReadBuffer, _state: Self::State) -> Result<Self, EtError> {
        let header = read_agilent_header(&mut rb, false)?;
        let metadata = get_metadata(&header)?;

        Ok(ChemstationMwdState {
            n_wvs_left: 0,
            cur_time: metadata.start_time,
            cur_intensity: 0.,
            time_step: 0.2,
            metadata,
        })
    }
}

#[derive(Clone, Debug)]
/// A single point from an e.g. moving wavelength detector trace
pub struct ChemstationMwdRecord<'r> {
    /// The name of the signal that's being tracked
    pub signal_name: &'r str,
    /// The time recorded at
    pub time: f64,
    /// The intensity recorded
    pub intensity: f64,
}

impl<'r> RecordHeader for ChemstationMwdRecord<'r> {
    fn header() -> Vec<String> {
        vec![
            "time".to_string(),
            "mz".to_string(),
            "intensity".to_string(),
        ]
    }
}

impl<'r> From<ChemstationMwdRecord<'r>> for Vec<Value<'r>> {
    fn from(record: ChemstationMwdRecord<'r>) -> Self {
        // signal name is something like "MWD A, Sig=210,5 Ref=360,100"
        let mz = record
            .signal_name
            .splitn(2, "Sig=")
            .nth(1)
            .and_then(|last_part| {
                last_part
                    .splitn(2, ',')
                    .next()
                    .and_then(|sig_name| sig_name.parse::<f64>().ok())
            })
            .unwrap_or_else(|| 0.);
        vec![record.time.into(), mz.into(), record.intensity.into()]
    }
}

impl<'r> FromBuffer<'r> for Option<ChemstationMwdRecord<'r>> {
    type State = &'r mut ChemstationMwdState;

    fn get(rb: &'r mut ReadBuffer, state: Self::State) -> Result<Self, EtError> {
        if rb.is_empty() && rb.eof() {
            return Ok(None);
        }
        if state.n_wvs_left == 0 {
            // mask out the top nibble because it's always 0b0001 (i hope?)
            state.n_wvs_left = usize::from(rb.extract::<u16>(Endian::Big)?) & 0b111111111111;
            if state.n_wvs_left == 0 {
                // TODO: consume the rest of the file so this can't accidentally repeat?
                return Ok(None);
            }
        }

        let time = state.cur_time;
        state.cur_time += state.time_step;

        let intensity: i16 = rb.extract(Endian::Big)?;
        if intensity == -32768 {
            state.cur_intensity = f64::from(rb.extract::<i32>(Endian::Big)?);
        } else {
            state.cur_intensity += f64::from(intensity);
        }
        state.n_wvs_left -= 1;

        Ok(Some(ChemstationMwdRecord {
            signal_name: &state.metadata.signal_name,
            time,
            intensity: state.cur_intensity * state.metadata.mult_correction
                + state.metadata.offset_correction,
        }))
    }
}

#[derive(Clone, Copy, Debug)]
/// Internal state for the ChemstationUv parser
pub struct ChemstationUvState {
    n_scans_left: usize,
    n_wvs_left: usize,
    cur_time: f64,
    cur_intensity: f64,
    cur_wv: f64,
    wv_step: f64,
}

impl<'r> StateMetadata<'r> for ChemstationUvState {}

impl<'r> FromBuffer<'r> for ChemstationUvState {
    type State = ();

    fn get(mut rb: &'r mut ReadBuffer, _state: Self::State) -> Result<Self, EtError> {
        let header = read_agilent_header(&mut rb, false)?;
        let n_scans = u32::out_of(&header[278..], Endian::Big)? as usize;

        // TODO: get other metadata
        Ok(ChemstationUvState {
            n_scans_left: n_scans,
            n_wvs_left: 0,
            cur_time: 0.,
            cur_wv: 0.,
            cur_intensity: 0.,
            wv_step: 0.,
        })
    }
}

#[derive(Clone, Copy, Debug)]
/// A record from a ChemstationUv file
pub struct ChemstationUvRecord {
    /// The time recorded at
    pub time: f64,
    /// The wavelength recorded at
    pub wavelength: f64,
    /// The intensity record
    pub intensity: f64,
}

impl_record!(ChemstationUvRecord: time, wavelength, intensity);

impl<'r> FromBuffer<'r> for Option<ChemstationUvRecord> {
    type State = &'r mut ChemstationUvState;

    fn get(rb: &'r mut ReadBuffer, state: Self::State) -> Result<Self, EtError> {
        if state.n_scans_left == 0 {
            return Ok(None);
        }

        // refill case
        if state.n_wvs_left == 0 {
            let _ = rb.extract::<&[u8]>(4_usize)?;
            // let next_pos = usize::from(rb.extract::<u16>(Endian::Little)?);
            state.cur_time = (rb.extract::<u32>(Endian::Little)? as f64) / 60000.;
            let wv_start: u16 = rb.extract(Endian::Little)?;
            let wv_end: u16 = rb.extract(Endian::Little)?;
            let wv_step: u16 = rb.extract(Endian::Little)?;

            state.n_wvs_left = usize::from((wv_end - wv_start) / wv_step) + 1;
            state.cur_wv = f64::from(wv_start) / 20.;
            state.wv_step = f64::from(wv_step) / 20.;
            let _ = rb.extract::<&[u8]>(8_usize)?;
        };

        let delta = rb.extract::<i16>(Endian::Little)?;
        if delta == -32768 {
            state.cur_intensity = f64::from(rb.extract::<u32>(Endian::Little)?);
        } else {
            state.cur_intensity += f64::from(delta);
        }

        if state.n_wvs_left == 1 {
            state.n_scans_left -= 1;
        }
        state.n_wvs_left -= 1;

        Ok(Some(ChemstationUvRecord {
            time: state.cur_time,
            wavelength: state.cur_wv,
            intensity: state.cur_intensity / 2000.,
        }))
    }
}

impl_reader!(
    /// A reader for a Chemstation FID file
    ChemstationFidReader,
    ChemstationFidRecord,
    ChemstationFidState,
    ()
);

impl_reader!(
    /// A reader for a Chemstation MS file
    ChemstationMsReader,
    ChemstationMsRecord,
    ChemstationMsState,
    ()
);

impl_reader!(
    /// A reader for a Chemstation MWD file
    ChemstationMwdReader,
    ChemstationMwdRecord,
    ChemstationMwdState,
    ()
);

impl_reader!(
    /// A reader for a Chemstation UV file
    ChemstationUvReader,
    ChemstationUvRecord,
    ChemstationUvState,
    ()
);

// scratch with offsets for info in different files

// FID - 02 38 31 00 ("81") (missing 01 38 00 00)
// MWD - 02 33 30 00 ("30")
// MS - 01 32 00 00 ("2") (missing 02 32 30?)
// (possibly also 03 31 37 39 and 03 31 38 31 ?)
//  - 5 - "GC / MS Data File" or other?
//  - 24 - Sample Name
//  - 86 - Sample Description?
//  - 148 - Operator Name
//  - 178 - Run Date
//  - 208 - Instrument Name
//  - 218 - LC or GC
//  - 228 - Method Name
//  - 252 - Sequence? (u16)
//  - 254 - Vial? (u16)
//  - 256 - Replicate? (u16)
//  - 260 - TIC Offset? (i32)
//  * 264 - FID/MWD - 512 byte header chunks // 2 + 1
//  - 264 - MS - total header bytes // 2 + 1
//  - 272 - Normalization offset? (i32)
//  * 282 - Start Time (i32)
//  * 286 - End Time (i32)
//  M 322 - Collection software?
//  M 355 - Software Version?
//  - 368 - "GC / MS Data File" as utf16
//  M 405 - Another Version?
//  - 448 - MS - Instrument name as utf16
//  - 530 - lower end of mz/wv range?
//  - 532 - upper end of mz/wv range?
//  - 576 - MS - "GC"
//  - 580 - Units
//  M 596 - Channel Info (str)
//  - 616 - MS - Method directory
//  - 644 - (f32/64?)
//  - 5768 - MS - data start (GC)

// LC - 03 31 33 31 ("131")
//  * 264 - 512 byte header chunks // 2 + 1
//  ? 278 - Number of Records
//  - 858 - Sample Name
//  - 1880 - Operator Name
//  - 2391 - Run Date
//  - 2492 - Instrument Name
//  - 2533 - "LC"
//  - 2574 - Method Name
//  - 3093 - Units
//   4096 - data start?

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

    #[test]
    fn test_chemstation_reader_fid() -> Result<(), EtError> {
        let rb = ReadBuffer::from_slice(include_bytes!("../../tests/data/test_fid.ch"));
        let mut reader = ChemstationFidReader::new(rb, ())?;
        let ChemstationFidRecord { time, intensity } = reader.next()?.unwrap();
        // TODO: try to confirm this time is correct
        assert!((time - 20184.8775).abs() < 0.0001);
        assert!((intensity - 17.500).abs() < 0.001);

        let mut n_mzs = 1;
        while let Some(_) = reader.next()? {
            n_mzs += 1;
        }
        assert_eq!(n_mzs, 2699);
        Ok(())
    }

    #[test]
    fn test_chemstation_reader_ms() -> Result<(), EtError> {
        let rb = ReadBuffer::from_slice(include_bytes!(
            "../../tests/data/carotenoid_extract.d/MSD1.MS"
        ));
        let mut reader = ChemstationMsReader::new(rb, ())?;
        let ChemstationMsRecord {
            time,
            mz,
            intensity,
        } = reader.next()?.unwrap();
        assert!((time - 0.079166).abs() < 0.000001);
        assert!((mz - 915.7).abs() < 0.000001);
        assert_eq!(intensity, 112.);

        let ChemstationMsRecord {
            time,
            mz,
            intensity,
        } = reader.next()?.unwrap();
        assert!((time - 0.079166).abs() < 0.000001);
        assert!((mz - 865.4).abs() < 0.000001);
        assert_eq!(intensity, 184.);

        let mut n_mzs = 2;
        while let Some(_) = reader.next()? {
            n_mzs += 1;
        }
        assert_eq!(n_mzs, 95471);
        Ok(())
    }

    #[test]
    fn test_chemstation_reader_mwd() -> Result<(), EtError> {
        let rb = ReadBuffer::from_slice(include_bytes!(
            "../../tests/data/chemstation_mwd.d/mwd1A.ch"
        ));
        let mut reader = ChemstationMwdReader::new(rb, ())?;
        let ChemstationMwdRecord {
            time,
            signal_name,
            intensity,
        } = reader.next()?.unwrap();
        assert!((time - -0.039667).abs() < 0.000001);
        assert_eq!(signal_name, "MWD A, Sig=210,5 Ref=360,100");
        assert!((intensity - -36.34977).abs() < 0.00001);

        let mut n_mzs = 1;
        while let Some(_) = reader.next()? {
            n_mzs += 1;
        }
        assert_eq!(n_mzs, 1801);
        Ok(())
    }

    #[test]
    fn test_chemstation_reader_uv() -> Result<(), EtError> {
        let rb = ReadBuffer::from_slice(include_bytes!(
            "../../tests/data/carotenoid_extract.d/dad1.uv"
        ));
        let mut reader = ChemstationUvReader::new(rb, ())?;
        let ChemstationUvRecord {
            time,
            wavelength,
            intensity,
        } = reader.next()?.unwrap();
        assert!((time - 0.001333).abs() < 0.000001);
        assert!((wavelength - 200.).abs() < 0.000001);
        assert_eq!(intensity, -15.6675);

        let mut n_mzs = 1;
        while let Some(_) = reader.next()? {
            n_mzs += 1;
        }
        assert_eq!(n_mzs, 6744 * 301);
        Ok(())
    }

    #[test]
    fn test_chemstation_reader_bad_fuzzes() -> Result<(), EtError> {
        let test_data = b"\x012>\n\n\n\n\n\n>*\n\x86\n>\n\n\n\n\n\n\n\n\x14\n\n\n\n\n\n\n\n\xaf%\xa8\x00\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\n\n\n\n\n\n\n\n\n\n\n\n>>>\n*\n\n>>\n\xe3\x86\x86\n>>\n\n\n\n>\n\n\n\xaf%\x00\x00\x00\x00\x00\x00\x01\x04\n\n\n\n\n\n\n\n\n\n\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\n\n\n\n\n\n\n\n\n\n\n\n\n>>>\n*\n\n>>>\n\n\n\n>\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\n\n\n\n\n\n\n>\n\n\n\n>";
        let rb = ReadBuffer::from_slice(test_data);
        assert!(ChemstationMsReader::new(rb, ()).is_err());

        let test_data = b"\x012>\n\n\n\n\n\n>*\n\x86\n>\n\n>\n\xE3\x86\n>\n>\n\n>\n\xE3\x86&\n>@\x10\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\n\n\n\n\n\n\n\n\n\n\n\n\n\n\x02Y\n\n\n\n\xE3\x86\x86\n>\n\n>\n\n>\n*\n\n\n>\n\n>\n\n>\n\xE3\n\n\n\n\n\n\x14\n\n\n\n>\n\xC8>\n\x86\n>\n\n\n\n\n\n\n\n\n\n\n\n>\n\xE3\xCD\xCD\xCD\x00\x00\n\n\n\n\n\n>\n\n>\n\x00\n\x00\n\n\n\n\n\n\n>\n\n\n>\n\n\n\n\n\n\n\n>\n\n\n\n\n\n>\n\n\n\n\x00\x00\n\n\n\x00\n\n\n\n\n\n\n\n\xE3\x00\x00\n\n\n\n\n>\n\n\n>\n\n\n\n\n\n\n\n>\n\n\n\n\n\n>\n\n\n\n\x00\x00\n\n\n\x00\n\n\n\n\n\n\n\n\xE3\x00\x00\x00>\x0b\n\x01\x00>\n\n\n\x00>\n\n\x01\x00>\n\n\n\n\x00\x00\n\n\n\x00\n\n\n\n\n>\n\n>\n\n\n\n\n\n\n\n\n\n\n\n\x02Y\n\n\n\n\xE3\x86\n>>*\n\x86\xE3\x86\n>>*\n\x86\x00R>N\x02\xE3\n>\n>\xC6\n\n>\n\xE3\x00\x00\x00\x00\x00\x00\n\n\n\n\n>\n\xE3\xCD\n>\n\n>\n\xE3\n>@W\n\n+\n\n\n>\n\n>\n\xE3>*\n\x86*\n\x86\xE3\x86\n>>*\n\x86\xE3\x86\n>>*\n\x86\x00R>N\x02\xE3\n>\n>\xC6\n\n>\n\xE3\x00\x00\x00\n\n\n\n\n\n\n\n\n\n\n\n\n\x02Y\n\n\n\n\xE3\x86\x86\n>\n\n>\n\n>\n*\n\n\n>\n\n>\n\n\n\n\n\n\n\n\n\n\n\n\x02Y\n\n\n\n\xE3\x86\x86\n>\n\n>\x01\x00\x00\x00\x00\x00\x00\x01>\n\n>\n\n>\n\xE3\n\n\n\n\n\x01\x00\x00\x00\x00\x00\x00\x00\n\xE3\n>@W>N\x02\xE3\n>\n>\xC6\n\n>\n\xE3\x00\x00\x00";
        let rb = ReadBuffer::from_slice(test_data);
        assert!(ChemstationMsReader::new(rb, ()).is_err());

        let test_data = b"\x012>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\x14\n\n\n\n\n\n\n\n\xAF%\xA8\x00\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nVVVVV\n\n\xAF%\xA8\x00\xFE\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\n\n\n\n\n\n\n\n\n>";
        let rb = ReadBuffer::from_slice(test_data);
        assert!(ChemstationMsReader::new(rb, ()).is_err());

        Ok(())
    }
}