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
use crate::aws::realtime::{ChunkType, VolumeIndex};
use chrono::{DateTime, Utc};

/// Identifies a volume chunk within the real-time NEXRAD data bucket. These chunks are uploaded
/// every few seconds and contain a portion of the radar data for a specific volume.
#[derive(Debug, Clone)]
pub struct ChunkIdentifier {
    site: String,
    volume: VolumeIndex,
    name: String,
    date_time: Option<DateTime<Utc>>,
}

impl ChunkIdentifier {
    /// Creates a new chunk identifier.
    pub fn new(
        site: String,
        volume: VolumeIndex,
        name: String,
        date_time: Option<DateTime<Utc>>,
    ) -> Self {
        Self {
            site,
            volume,
            name,
            date_time,
        }
    }

    /// Creates a new chunk identifier with the given sequence number. The chunk type will be
    /// inferred from the sequence and date/time will be omitted since it is unknown.
    pub fn with_sequence(&self, sequence: usize) -> Self {
        let name = format!(
            "{}-{:03}-{}",
            self.name_prefix(),
            sequence,
            match sequence {
                1 => "S",
                55 => "E",
                _ => "I",
            }
        );

        Self {
            site: self.site.clone(),
            volume: self.volume,
            name,
            date_time: None,
        }
    }

    /// The chunk's radar site identifier.
    pub fn site(&self) -> &str {
        &self.site
    }

    /// The chunk's rotating volume index.
    pub fn volume(&self) -> &VolumeIndex {
        &self.volume
    }

    /// The chunk's name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The chunk's name prefix.
    pub fn name_prefix(&self) -> &str {
        &self.name[..15]
    }

    /// The sequence number of this chunk within the volume.
    pub fn sequence(&self) -> Option<usize> {
        self.name.split('-').nth(2).and_then(|s| s.parse().ok())
    }

    /// The position of this chunk within the volume.
    pub fn chunk_type(&self) -> Option<ChunkType> {
        match self.name.chars().last() {
            Some('S') => Some(ChunkType::Start),
            Some('I') => Some(ChunkType::Intermediate),
            Some('E') => Some(ChunkType::End),
            _ => None,
        }
    }

    /// The date and time this chunk was uploaded.
    pub fn date_time(&self) -> Option<DateTime<Utc>> {
        self.date_time
    }

    /// Identifies the next chunk's expected location.
    pub fn next_chunk(&self) -> Option<NextChunk> {
        let sequence = self.sequence()?;
        let mut chunk_type;

        if sequence < 55 {
            let next_sequence = sequence + 1;

            chunk_type = ChunkType::Intermediate;
            if next_sequence == 55 {
                chunk_type = ChunkType::End;
            }

            let name = format!(
                "{}-{:03}-{}",
                self.name_prefix(),
                next_sequence,
                match chunk_type {
                    ChunkType::Start => "S",
                    ChunkType::Intermediate => "I",
                    ChunkType::End => "E",
                }
            );

            let next_chunk = ChunkIdentifier::new(self.site().to_string(), self.volume, name, None);
            return Some(NextChunk::Sequence(next_chunk));
        }

        let mut volume = self.volume.as_number() + 1;
        if volume > 999 {
            volume = 1;
        }

        Some(NextChunk::Volume(VolumeIndex::new(volume)))
    }
}

/// Identifies where to find the next expected chunk.
pub enum NextChunk {
    /// The next chunk is expected to be located in the same volume at this sequence. The
    /// [ChunkIdentifier::with_sequence] method can be used to create the next chunk's identifier
    /// and it can be downloaded using the [crate::aws::realtime::download_chunk()] function. You
    /// may need to poll by checking if that function returns
    /// [crate::result::aws::AWSError::S3ObjectNotFoundError].
    Sequence(ChunkIdentifier),

    /// The chunk is expected to be located in the next volume. The next volume's chunks can be
    /// listed using the [crate::aws::realtime::list_chunks_in_volume()] function.
    Volume(VolumeIndex),
}

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

    use chrono::TimeZone;

    #[test]
    fn test_chunk_identifier() {
        let site = "KTLX";
        let volume = 50;
        let name = "20240813-123330-014-I";
        let date_time = Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap();

        let chunk = ChunkIdentifier::new(
            site.to_string(),
            VolumeIndex::new(volume),
            name.to_string(),
            Some(date_time),
        );

        assert_eq!(chunk.site(), site);
        assert_eq!(chunk.volume().as_number(), 50);
        assert_eq!(chunk.name(), name);
        assert_eq!(chunk.name_prefix(), "20240813-123330");
        assert_eq!(chunk.chunk_type(), Some(ChunkType::Intermediate));
        assert_eq!(chunk.sequence(), Some(14));
        assert_eq!(chunk.date_time(), Some(date_time));
    }

    #[test]
    fn test_next_chunk_start() {
        let site = "KTLX";
        let volume = 50;
        let name = "20240813-123330-001-S";
        let date_time = Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap();

        let chunk = ChunkIdentifier::new(
            site.to_string(),
            VolumeIndex::new(volume),
            name.to_string(),
            Some(date_time),
        );

        let next_chunk = chunk.next_chunk().expect("Expected next chunk");
        match next_chunk {
            NextChunk::Sequence(next_chunk) => {
                assert_eq!(next_chunk.site(), site);
                assert_eq!(next_chunk.volume().as_number(), 50);
                assert_eq!(next_chunk.name(), "20240813-123330-002-I");
                assert_eq!(next_chunk.name_prefix(), "20240813-123330");
                assert_eq!(next_chunk.sequence(), Some(2));
                assert_eq!(next_chunk.chunk_type(), Some(ChunkType::Intermediate));
                assert_eq!(next_chunk.date_time(), None);
            }
            _ => panic!("Expected sequence"),
        }
    }

    #[test]
    fn test_next_chunk_intermediate() {
        let site = "KTLX";
        let volume = 999;
        let name = "20240813-123330-014-I";
        let date_time = Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap();

        let chunk = ChunkIdentifier::new(
            site.to_string(),
            VolumeIndex::new(volume),
            name.to_string(),
            Some(date_time),
        );

        let next_chunk = chunk.next_chunk().expect("Expected next chunk");
        match next_chunk {
            NextChunk::Sequence(next_chunk) => {
                assert_eq!(next_chunk.site(), site);
                assert_eq!(next_chunk.volume().as_number(), 999);
                assert_eq!(next_chunk.name(), "20240813-123330-015-I");
                assert_eq!(next_chunk.name_prefix(), "20240813-123330");
                assert_eq!(next_chunk.sequence(), Some(15));
                assert_eq!(next_chunk.chunk_type(), Some(ChunkType::Intermediate));
                assert_eq!(next_chunk.date_time(), None);
            }
            _ => panic!("Expected sequence"),
        }
    }

    #[test]
    fn test_next_chunk_end() {
        let site = "KTLX";
        let volume = 50;
        let name = "20240813-123330-055-E";
        let date_time = Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap();

        let chunk = ChunkIdentifier::new(
            site.to_string(),
            VolumeIndex::new(volume),
            name.to_string(),
            Some(date_time),
        );

        let next_chunk = chunk.next_chunk().expect("Expected next chunk");
        match next_chunk {
            NextChunk::Volume(next_volume) => {
                assert_eq!(next_volume.as_number(), 51);
            }
            _ => panic!("Expected volume"),
        }
    }

    #[test]
    fn test_next_chunk_last_volume() {
        let site = "KTLX";
        let volume = 999;
        let name = "20240813-123330-055-E";
        let date_time = Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap();

        let chunk = ChunkIdentifier::new(
            site.to_string(),
            VolumeIndex::new(volume),
            name.to_string(),
            Some(date_time),
        );

        let next_chunk = chunk.next_chunk().expect("Expected next chunk");
        match next_chunk {
            NextChunk::Volume(next_volume) => {
                assert_eq!(next_volume.as_number(), 1);
            }
            _ => panic!("Expected volume"),
        }
    }

    #[test]
    fn test_chunk_from_sequence() {
        let site = "KTLX";
        let volume = 50;
        let name = "20240813-123330-014-I";
        let date_time = Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap();

        let chunk = ChunkIdentifier::new(
            site.to_string(),
            VolumeIndex::new(volume),
            name.to_string(),
            Some(date_time),
        );

        let next_chunk = chunk.with_sequence(15);
        assert_eq!(next_chunk.site(), site);
        assert_eq!(next_chunk.volume().as_number(), 50);
        assert_eq!(next_chunk.name(), "20240813-123330-015-I");
        assert_eq!(next_chunk.name_prefix(), "20240813-123330");
        assert_eq!(next_chunk.sequence(), Some(15));
        assert_eq!(next_chunk.chunk_type(), Some(ChunkType::Intermediate));
        assert_eq!(next_chunk.date_time(), None);

        assert_eq!(chunk.with_sequence(1).chunk_type(), Some(ChunkType::Start));
        assert_eq!(chunk.with_sequence(55).chunk_type(), Some(ChunkType::End));
    }
}