Skip to main content

nexrad_data/aws/realtime/
chunk.rs

1use crate::result::aws::AWSError::UnrecognizedChunkFormat;
2use crate::result::Error::AWS;
3use crate::volume;
4
5/// A chunk of real-time data within a volume. Chunks are ordered and when concatenated together
6/// form a complete volume of radar data. All chunks contain an LDM record with radar data messages.
7#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8pub enum Chunk<'a> {
9    /// The start of a new volume. This chunk will begin with an Archive II volume header followed
10    /// by a compressed LDM record.
11    Start(volume::File),
12    /// An intermediate or end chunk. This chunk will contain a compressed LDM record with radar
13    /// data messages.
14    IntermediateOrEnd(volume::Record<'a>),
15}
16
17impl Chunk<'_> {
18    /// Creates a new chunk from the provided data. The data is expected to be in one of two formats:
19    ///
20    /// 1. An Archive II volume header followed by a compressed LDM record, or a "start" chunk.
21    /// 2. A compressed LDM record, or an "intermediate" or "end" chunk.
22    ///
23    /// The chunk type is determined by the data's format.
24    pub fn new(data: Vec<u8>) -> crate::result::Result<Self> {
25        // Check if the data begins with an Archive II volume header, indicating a "start" chunk
26        if data[0..3].as_ref() == b"AR2" {
27            let file = volume::File::new(data);
28            return Ok(Self::Start(file));
29        }
30
31        // Check if the data begins with a BZ compressed record, indicating an "intermediate" or "end" chunk
32        if data[4..6].as_ref() == b"BZ" {
33            let record = volume::Record::new(data);
34            return Ok(Self::IntermediateOrEnd(record));
35        }
36
37        Err(AWS(UnrecognizedChunkFormat))
38    }
39
40    /// The data contained within this chunk.
41    pub fn data(&self) -> &[u8] {
42        match self {
43            Self::Start(file) => file.data(),
44            Self::IntermediateOrEnd(record) => record.data(),
45        }
46    }
47}