nmr 0.1.2

NMR data tools for Rust
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
use super::parameters::RawAxisUnit;
use super::parameters::decode_fixed_string;
use super::parameters::decode_raw_unit;
use super::semantics::jeol_complex_dims;
use crate::AxisUnit;
use crate::Domain;
use crate::ReadError;
use crate::raw::InputSource;

pub(crate) const HEADER_LEN: usize = 1360;

pub(crate) mod header_offset {
    pub const ENDIAN: usize = 8;
    pub const DIMENSION: usize = 12;
    pub const AXIS_TYPE: usize = 24;
    pub const AXIS_UNIT: usize = 32;
    pub const DATA_POINTS: usize = 176;
    pub const OFFSET_START: usize = 208;
    pub const OFFSET_STOP: usize = 240;
    pub const AXIS_START: usize = 272;
    pub const AXIS_STOP: usize = 336;
    pub const TITLE: usize = 48;
    pub const AXIS_TITLES: usize = 808;
    pub const BASE_FREQUENCY: usize = 1064;
    pub const PARAM_START: usize = 1212;
    pub const PARAM_LENGTH: usize = 1216;
    pub const LIST_START: usize = 1220;
    pub const LIST_LENGTH: usize = 1252;
    pub const DATA_START: usize = 1284;
    pub const DATA_LENGTH: usize = 1288;
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum DataFormat {
    OneD,
    TwoD,
    ThreeD,
    FourD,
    SmallTwoD,
    SmallThreeD,
    SmallFourD,
}

impl DataFormat {
    pub(super) fn decode(code: u8, source: &InputSource) -> Result<Self, ReadError> {
        match code {
            1 => Ok(Self::OneD),
            2 => Ok(Self::TwoD),
            3 => Ok(Self::ThreeD),
            4 => Ok(Self::FourD),
            12 => Ok(Self::SmallTwoD),
            13 => Ok(Self::SmallThreeD),
            14 => Ok(Self::SmallFourD),
            _ => Err(ReadError::unsupported_code(
                source.clone(),
                crate::raw::UnsupportedFeatureCode::NUMERIC_ENCODING,
                format!("unknown JEOL data format {code}"),
            )),
        }
    }

    pub(super) fn dimensions(self) -> usize {
        match self {
            Self::OneD => 1,
            Self::TwoD | Self::SmallTwoD => 2,
            Self::ThreeD | Self::SmallThreeD => 3,
            Self::FourD | Self::SmallFourD => 4,
        }
    }

    pub(super) fn submatrix_edge(self) -> usize {
        match self {
            Self::OneD => 8,
            Self::TwoD => 32,
            Self::ThreeD | Self::FourD => 8,
            Self::SmallTwoD | Self::SmallThreeD | Self::SmallFourD => 4,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum Precision {
    F32,
    F64,
}

impl Precision {
    pub(super) fn decode(code: u8, source: &InputSource) -> Result<Self, ReadError> {
        match code {
            0 => Ok(Self::F64),
            1 => Ok(Self::F32),
            _ => Err(ReadError::unsupported_code(
                source.clone(),
                crate::raw::UnsupportedFeatureCode::NUMERIC_ENCODING,
                format!("unknown JEOL precision code {code}"),
            )),
        }
    }

    pub(super) fn size(self) -> usize {
        match self {
            Self::F32 => 4,
            Self::F64 => 8,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum BodyEndian {
    Big,
    Little,
}

impl AxisUnit {
    pub(super) fn decode(base: u8) -> Option<Self> {
        match base {
            13 => Some(Self::Hertz),
            26 => Some(Self::Ppm),
            28 => Some(Self::Second),
            31 => Some(Self::Tesla),
            _ => None,
        }
    }

    pub(super) fn domain(self) -> Domain {
        match self {
            Self::Second => Domain::Time,
            Self::Hertz | Self::Ppm => Domain::Frequency,
            Self::Tesla | Self::TeslaPerMeter => Domain::Parameter,
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) struct JdfHeader {
    pub(super) input_source: InputSource,
    pub(super) body_endian: BodyEndian,
    pub(super) precision: Precision,
    pub(super) data_format: DataFormat,
    pub(super) axis_types: Vec<u8>,
    pub(super) axis_units: Vec<Option<AxisUnit>>,
    pub(super) raw_axis_units: Vec<RawAxisUnit>,
    pub(super) points_disk: Vec<usize>,
    pub(super) offset_start: Vec<usize>,
    pub(super) offset_stop: Vec<usize>,
    pub(super) axis_start: Vec<f64>,
    pub(super) axis_stop: Vec<f64>,
    pub(super) data_start: usize,
    pub(super) data_length: usize,
    pub(super) title: Option<String>,
    pub(super) axis_titles: Vec<Option<String>>,
    pub(super) base_frequency: Vec<f64>,
    pub(super) param_start: usize,
    pub(super) param_length: usize,
    pub(super) list_start: Vec<usize>,
    pub(super) list_length: Vec<usize>,
}

impl JdfHeader {
    pub(super) fn parse(bytes: &[u8]) -> Result<Self, ReadError> {
        Self::parse_for_source(bytes, bytes.len(), InputSource::memory("jdf"))
    }

    pub(super) fn parse_for_source(
        bytes: &[u8],
        source_len: usize,
        input_source: InputSource,
    ) -> Result<Self, ReadError> {
        if bytes.len() < HEADER_LEN {
            return Err(ReadError::truncated(
                input_source.clone(),
                HEADER_LEN,
                bytes.len(),
            ));
        }
        if &bytes[..8] != b"JEOL.NMR" {
            return Err(ReadError::unrecognized(
                input_source,
                "missing JEOL.NMR magic",
            ));
        }
        let body_endian = match bytes[header_offset::ENDIAN] {
            0 => BodyEndian::Big,
            1 => BodyEndian::Little,
            marker => {
                return Err(ReadError::corrupt(
                    input_source,
                    format!("invalid JEOL body endian marker {marker}"),
                ));
            }
        };
        let ndim = bytes[header_offset::DIMENSION] as usize;
        if !(1..=4).contains(&ndim) {
            return Err(ReadError::unsupported_code(
                input_source,
                crate::raw::UnsupportedFeatureCode::UNSUPPORTED_RANK,
                format!("JEOL dimension {ndim}"),
            ));
        }
        let format_info = bytes[14];
        let dimension_mask = bytes[13];
        if dimension_mask != 0 && dimension_mask.count_ones() as usize != ndim {
            return Err(ReadError::corrupt(
                input_source,
                format!("JEOL dimension-existence mask does not contain {ndim} active axes"),
            ));
        }
        let precision = Precision::decode(format_info >> 6, &input_source)?;
        let data_format = DataFormat::decode(format_info & 0x3f, &input_source)?;
        if data_format.dimensions() != ndim {
            return Err(ReadError::corrupt(
                input_source,
                format!(
                    "JEOL data format has {} dimensions, header declares {ndim}",
                    data_format.dimensions()
                ),
            ));
        }
        let axis_types = bytes[header_offset::AXIS_TYPE..header_offset::AXIS_TYPE + ndim].to_vec();
        for (axis, &axis_type) in axis_types.iter().enumerate() {
            if !matches!(axis_type, 1..=5) {
                return Err(ReadError::unsupported_code(
                    input_source,
                    crate::raw::UnsupportedFeatureCode::COMPONENT_LAYOUT,
                    format!("unknown JEOL axis type {axis_type} on axis {}", axis + 1),
                ));
            }
        }
        let mut axis_units = Vec::with_capacity(ndim);
        let mut raw_axis_units = Vec::with_capacity(ndim);
        for axis in 0..ndim {
            let (unit, raw) = decode_axis_unit(bytes, axis, &input_source)?;
            axis_units.push(unit);
            raw_axis_units.push(raw);
        }
        let read_u32 = |offset: usize| -> Result<usize, ReadError> {
            let end = offset.checked_add(4).ok_or(ReadError::SizeOverflow)?;
            let value = bytes
                .get(offset..end)
                .ok_or_else(|| ReadError::truncated(input_source.clone(), end, bytes.len()))?;
            Ok(u32::from_be_bytes(value.try_into().unwrap()) as usize)
        };
        let read_u64 = |offset: usize| -> Result<usize, ReadError> {
            let end = offset.checked_add(8).ok_or(ReadError::SizeOverflow)?;
            let value = bytes
                .get(offset..end)
                .ok_or_else(|| ReadError::truncated(input_source.clone(), end, bytes.len()))?;
            usize::try_from(u64::from_be_bytes(value.try_into().unwrap()))
                .map_err(|_| ReadError::SizeOverflow)
        };
        let read_f64 = |offset: usize| -> Result<f64, ReadError> {
            let end = offset.checked_add(8).ok_or(ReadError::SizeOverflow)?;
            let value = bytes
                .get(offset..end)
                .ok_or_else(|| ReadError::truncated(input_source.clone(), end, bytes.len()))?;
            Ok(f64::from_be_bytes(value.try_into().unwrap()))
        };
        let mut points_disk = Vec::with_capacity(ndim);
        let mut offset_start = Vec::with_capacity(ndim);
        let mut offset_stop = Vec::with_capacity(ndim);
        let mut axis_start = Vec::with_capacity(ndim);
        let mut axis_stop = Vec::with_capacity(ndim);
        for axis in 0..ndim {
            let points = read_u32(header_offset::DATA_POINTS + axis * 4)?;
            if points == 0 {
                return Err(ReadError::corrupt(
                    input_source.clone(),
                    format!("JEOL axis {} has zero points", axis + 1),
                ));
            }
            points_disk.push(points);
            let start = read_u32(header_offset::OFFSET_START + axis * 4)?;
            let stop = read_u32(header_offset::OFFSET_STOP + axis * 4)?;
            if start > stop || stop >= points {
                return Err(ReadError::corrupt(
                    input_source.clone(),
                    format!(
                        "JEOL axis {} has invalid valid window {start}..={stop} for {points} points",
                        axis + 1
                    ),
                ));
            }
            offset_start.push(start);
            offset_stop.push(stop);
            axis_start.push(read_f64(header_offset::AXIS_START + axis * 8)?);
            axis_stop.push(read_f64(header_offset::AXIS_STOP + axis * 8)?);
        }
        let data_start = read_u32(header_offset::DATA_START)?;
        if data_start < HEADER_LEN {
            return Err(ReadError::corrupt(
                input_source.clone(),
                "JEOL data start precedes the fixed header",
            ));
        }
        let data_length = read_u64(header_offset::DATA_LENGTH)?;
        if data_length > 0
            && data_start
                .checked_add(data_length)
                .is_none_or(|end| end > source_len)
        {
            return Err(ReadError::truncated(
                input_source.clone(),
                data_start
                    .checked_add(data_length)
                    .ok_or(ReadError::SizeOverflow)?,
                source_len,
            ));
        }
        let title = decode_fixed_string(&bytes[header_offset::TITLE..header_offset::TITLE + 124]);
        let axis_titles = (0..ndim)
            .map(|axis| {
                let start = header_offset::AXIS_TITLES + axis * 32;
                decode_fixed_string(&bytes[start..start + 32])
            })
            .collect();
        let base_frequency = (0..ndim)
            .map(|axis| read_f64(header_offset::BASE_FREQUENCY + axis * 8))
            .collect::<Result<Vec<_>, _>>()?;
        let param_start = read_u32(header_offset::PARAM_START)?;
        let param_length = read_u32(header_offset::PARAM_LENGTH)?;
        if param_length > 0
            && (param_start < HEADER_LEN
                || param_start
                    .checked_add(param_length)
                    .is_none_or(|end| end > source_len))
        {
            return Err(ReadError::truncated(
                input_source.clone(),
                param_start
                    .checked_add(param_length)
                    .ok_or(ReadError::SizeOverflow)?,
                source_len,
            ));
        }
        let list_start = (0..ndim)
            .map(|axis| read_u32(header_offset::LIST_START + axis * 4))
            .collect::<Result<Vec<_>, _>>()?;
        let list_length = (0..ndim)
            .map(|axis| read_u32(header_offset::LIST_LENGTH + axis * 4))
            .collect::<Result<Vec<_>, _>>()?;
        for axis in 0..ndim {
            let start = list_start[axis];
            let length = list_length[axis];
            if length != 0
                && (length % 8 != 0
                    || start < HEADER_LEN
                    || start.checked_add(length).is_none_or(|end| end > source_len))
            {
                return Err(ReadError::corrupt(
                    input_source.clone(),
                    format!("JEOL axis {} has an invalid coordinate list", axis + 1),
                ));
            }
        }
        Ok(Self {
            input_source,
            body_endian,
            precision,
            data_format,
            axis_types,
            axis_units,
            raw_axis_units,
            points_disk,
            offset_start,
            offset_stop,
            axis_start,
            axis_stop,
            data_start,
            data_length,
            title,
            axis_titles,
            base_frequency,
            param_start,
            param_length,
            list_start,
            list_length,
        })
    }

    pub(super) fn sections(&self) -> Result<usize, ReadError> {
        jeol_complex_dims(&self.axis_types, &self.input_source).and_then(|complex_dims| {
            1usize
                .checked_shl(complex_dims as u32)
                .ok_or(ReadError::SizeOverflow)
        })
    }
}

pub(crate) fn decode_axis_unit(
    bytes: &[u8],
    axis: usize,
    source: &InputSource,
) -> Result<(Option<AxisUnit>, RawAxisUnit), ReadError> {
    let offset = header_offset::AXIS_UNIT + axis * 2;
    let scaler = *bytes
        .get(offset)
        .ok_or_else(|| ReadError::truncated(source.clone(), offset + 1, bytes.len()))?;
    let base = *bytes
        .get(offset + 1)
        .ok_or_else(|| ReadError::truncated(source.clone(), offset + 2, bytes.len()))?;
    let raw = decode_raw_unit(scaler, base);
    let unit = (raw.power == 1).then(|| AxisUnit::decode(base)).flatten();
    Ok((unit, raw))
}