oxigdal-temporal 0.1.4

Multi-temporal raster analysis for OxiGDAL - Time series, change detection, phenology, and data cube operations
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
//! Time Series Raster Module
//!
//! This module provides time-indexed raster collections with temporal metadata,
//! efficient storage with lazy loading, temporal querying, and gap detection.

use crate::error::{Result, TemporalError};
use chrono::{DateTime, NaiveDate, NaiveDateTime, Utc};
use scirs2_core::ndarray::{Array2, Array3, s};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use tracing::{debug, info, warn};

pub mod collection;
pub mod datacube;

pub use collection::*;
pub use datacube::*;

/// Temporal resolution for time series
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TemporalResolution {
    /// Daily resolution
    Daily,
    /// Weekly resolution
    Weekly,
    /// Monthly resolution
    Monthly,
    /// Yearly resolution
    Yearly,
    /// Custom interval in seconds
    Custom(i64),
}

impl TemporalResolution {
    /// Get the duration in seconds
    #[must_use]
    pub fn as_seconds(&self) -> i64 {
        match self {
            Self::Daily => 86400,
            Self::Weekly => 604800,
            Self::Monthly => 2592000, // Approximate 30 days
            Self::Yearly => 31536000, // 365 days
            Self::Custom(secs) => *secs,
        }
    }

    /// Get the duration in days
    #[must_use]
    pub fn as_days(&self) -> f64 {
        self.as_seconds() as f64 / 86400.0
    }
}

/// Temporal metadata for a single raster
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalMetadata {
    /// Timestamp (UTC)
    pub timestamp: DateTime<Utc>,
    /// Acquisition date
    pub acquisition_date: NaiveDate,
    /// Sensor/satellite identifier
    pub sensor: Option<String>,
    /// Scene identifier
    pub scene_id: Option<String>,
    /// Cloud cover percentage (0-100)
    pub cloud_cover: Option<f32>,
    /// Quality score (0-1)
    pub quality_score: Option<f32>,
    /// Processing level
    pub processing_level: Option<String>,
    /// Custom metadata
    pub custom: BTreeMap<String, String>,
}

impl TemporalMetadata {
    /// Create new temporal metadata
    #[must_use]
    pub fn new(timestamp: DateTime<Utc>, acquisition_date: NaiveDate) -> Self {
        Self {
            timestamp,
            acquisition_date,
            sensor: None,
            scene_id: None,
            cloud_cover: None,
            quality_score: None,
            processing_level: None,
            custom: BTreeMap::new(),
        }
    }

    /// Create from naive datetime
    pub fn from_naive_datetime(dt: NaiveDateTime) -> Result<Self> {
        let timestamp = DateTime::from_naive_utc_and_offset(dt, Utc);
        let acquisition_date = dt.date();
        Ok(Self::new(timestamp, acquisition_date))
    }

    /// Set sensor information
    #[must_use]
    pub fn with_sensor(mut self, sensor: impl Into<String>) -> Self {
        self.sensor = Some(sensor.into());
        self
    }

    /// Set scene ID
    #[must_use]
    pub fn with_scene_id(mut self, scene_id: impl Into<String>) -> Self {
        self.scene_id = Some(scene_id.into());
        self
    }

    /// Set cloud cover percentage
    #[must_use]
    pub fn with_cloud_cover(mut self, cloud_cover: f32) -> Self {
        self.cloud_cover = Some(cloud_cover.clamp(0.0, 100.0));
        self
    }

    /// Set quality score
    #[must_use]
    pub fn with_quality_score(mut self, quality_score: f32) -> Self {
        self.quality_score = Some(quality_score.clamp(0.0, 1.0));
        self
    }

    /// Set processing level
    #[must_use]
    pub fn with_processing_level(mut self, level: impl Into<String>) -> Self {
        self.processing_level = Some(level.into());
        self
    }

    /// Add custom metadata field
    #[must_use]
    pub fn with_custom(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.custom.insert(key.into(), value.into());
        self
    }
}

/// Time-indexed raster entry
#[derive(Debug, Clone)]
pub struct TemporalRasterEntry {
    /// Temporal metadata
    pub metadata: TemporalMetadata,
    /// Raster data (lazy-loaded)
    pub data: Option<Array3<f64>>,
    /// Data source path (for lazy loading)
    pub source_path: Option<String>,
}

impl TemporalRasterEntry {
    /// Create new entry with in-memory data
    #[must_use]
    pub fn new_loaded(metadata: TemporalMetadata, data: Array3<f64>) -> Self {
        Self {
            metadata,
            data: Some(data),
            source_path: None,
        }
    }

    /// Create new entry with lazy loading
    #[must_use]
    pub fn new_lazy(metadata: TemporalMetadata, source_path: String) -> Self {
        Self {
            metadata,
            data: None,
            source_path: Some(source_path),
        }
    }

    /// Check if data is loaded
    #[must_use]
    pub fn is_loaded(&self) -> bool {
        self.data.is_some()
    }

    /// Get data dimensions (if loaded)
    #[must_use]
    pub fn shape(&self) -> Option<(usize, usize, usize)> {
        self.data
            .as_ref()
            .map(|d| (d.shape()[0], d.shape()[1], d.shape()[2]))
    }
}

/// Time series raster collection
///
/// This struct represents a collection of rasters indexed by time,
/// with support for temporal queries, gap detection, and lazy loading.
#[derive(Debug, Clone)]
pub struct TimeSeriesRaster {
    /// Time-indexed rasters (sorted by timestamp)
    entries: BTreeMap<i64, TemporalRasterEntry>,
    /// Temporal resolution
    resolution: Option<TemporalResolution>,
    /// Expected spatial dimensions (height, width, bands)
    expected_shape: Option<(usize, usize, usize)>,
}

impl TimeSeriesRaster {
    /// Create a new empty time series raster collection
    #[must_use]
    pub fn new() -> Self {
        Self {
            entries: BTreeMap::new(),
            resolution: None,
            expected_shape: None,
        }
    }

    /// Create with expected resolution
    #[must_use]
    pub fn with_resolution(resolution: TemporalResolution) -> Self {
        Self {
            entries: BTreeMap::new(),
            resolution: Some(resolution),
            expected_shape: None,
        }
    }

    /// Create with expected shape
    #[must_use]
    pub fn with_shape(height: usize, width: usize, bands: usize) -> Self {
        Self {
            entries: BTreeMap::new(),
            resolution: None,
            expected_shape: Some((height, width, bands)),
        }
    }

    /// Add a raster with timestamp
    ///
    /// # Errors
    /// Returns error if shape doesn't match expected dimensions
    pub fn add_raster(&mut self, metadata: TemporalMetadata, data: Array3<f64>) -> Result<()> {
        // Validate shape if expected shape is set
        if let Some((exp_h, exp_w, exp_b)) = self.expected_shape {
            let shape = data.shape();
            if shape[0] != exp_h || shape[1] != exp_w || shape[2] != exp_b {
                return Err(TemporalError::dimension_mismatch(
                    format!("{}x{}x{}", exp_h, exp_w, exp_b),
                    format!("{}x{}x{}", shape[0], shape[1], shape[2]),
                ));
            }
        } else {
            // Set expected shape from first raster
            let shape = data.shape();
            self.expected_shape = Some((shape[0], shape[1], shape[2]));
        }

        let timestamp_key = metadata.timestamp.timestamp();
        let entry = TemporalRasterEntry::new_loaded(metadata, data);
        self.entries.insert(timestamp_key, entry);

        debug!("Added raster at timestamp {}", timestamp_key);
        Ok(())
    }

    /// Add a raster with lazy loading
    ///
    /// # Errors
    /// Returns error if metadata is invalid
    pub fn add_raster_lazy(
        &mut self,
        metadata: TemporalMetadata,
        source_path: String,
    ) -> Result<()> {
        let timestamp_key = metadata.timestamp.timestamp();
        let entry = TemporalRasterEntry::new_lazy(metadata, source_path);
        self.entries.insert(timestamp_key, entry);

        debug!("Added lazy raster at timestamp {}", timestamp_key);
        Ok(())
    }

    /// Get the number of rasters in the time series
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if time series is empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get temporal resolution
    #[must_use]
    pub fn resolution(&self) -> Option<TemporalResolution> {
        self.resolution
    }

    /// Set temporal resolution
    pub fn set_resolution(&mut self, resolution: TemporalResolution) {
        self.resolution = Some(resolution);
    }

    /// Get expected shape
    #[must_use]
    pub fn expected_shape(&self) -> Option<(usize, usize, usize)> {
        self.expected_shape
    }

    /// Get time range (start, end)
    #[must_use]
    pub fn time_range(&self) -> Option<(DateTime<Utc>, DateTime<Utc>)> {
        if self.is_empty() {
            return None;
        }

        let start_ts = self.entries.keys().next()?;
        let end_ts = self.entries.keys().next_back()?;

        let start = DateTime::from_timestamp(*start_ts, 0)?;
        let end = DateTime::from_timestamp(*end_ts, 0)?;

        Some((start, end))
    }

    /// Get all timestamps
    #[must_use]
    pub fn timestamps(&self) -> Vec<DateTime<Utc>> {
        self.entries
            .keys()
            .filter_map(|&ts| DateTime::from_timestamp(ts, 0))
            .collect()
    }

    /// Get raster at specific timestamp
    #[must_use]
    pub fn get_at_timestamp(&self, timestamp: &DateTime<Utc>) -> Option<&TemporalRasterEntry> {
        self.entries.get(&timestamp.timestamp())
    }

    /// Get mutable raster at specific timestamp
    pub fn get_at_timestamp_mut(
        &mut self,
        timestamp: &DateTime<Utc>,
    ) -> Option<&mut TemporalRasterEntry> {
        self.entries.get_mut(&timestamp.timestamp())
    }

    /// Get raster by index
    ///
    /// # Errors
    /// Returns error if index is out of bounds
    pub fn get_by_index(&self, index: usize) -> Result<&TemporalRasterEntry> {
        self.entries
            .values()
            .nth(index)
            .ok_or_else(|| TemporalError::time_index_out_of_bounds(index, 0, self.len()))
    }

    /// Query rasters within a time range
    #[must_use]
    pub fn query_range(
        &self,
        start: &DateTime<Utc>,
        end: &DateTime<Utc>,
    ) -> Vec<&TemporalRasterEntry> {
        let start_ts = start.timestamp();
        let end_ts = end.timestamp();

        self.entries
            .range(start_ts..=end_ts)
            .map(|(_, entry)| entry)
            .collect()
    }

    /// Detect gaps in the time series based on expected resolution
    ///
    /// # Errors
    /// Returns error if resolution is not set
    pub fn detect_gaps(&self) -> Result<Vec<(DateTime<Utc>, DateTime<Utc>)>> {
        let resolution = self
            .resolution
            .ok_or_else(|| TemporalError::invalid_input("Temporal resolution not set"))?;

        let mut gaps = Vec::new();
        let timestamps: Vec<i64> = self.entries.keys().copied().collect();

        if timestamps.len() < 2 {
            return Ok(gaps);
        }

        let expected_interval = resolution.as_seconds();

        for i in 0..timestamps.len() - 1 {
            let current = timestamps[i];
            let next = timestamps[i + 1];
            let actual_interval = next - current;

            // Allow 10% tolerance
            let tolerance = (expected_interval as f64 * 0.1) as i64;
            if actual_interval > expected_interval + tolerance {
                if let (Some(gap_start), Some(gap_end)) = (
                    DateTime::from_timestamp(current, 0),
                    DateTime::from_timestamp(next, 0),
                ) {
                    gaps.push((gap_start, gap_end));
                    warn!(
                        "Gap detected between {} and {} ({} seconds)",
                        gap_start, gap_end, actual_interval
                    );
                }
            }
        }

        info!("Detected {} gaps in time series", gaps.len());
        Ok(gaps)
    }

    /// Get statistics about the time series
    #[must_use]
    pub fn stats(&self) -> TimeSeriesStats {
        let count = self.len();
        let loaded_count = self.entries.values().filter(|e| e.is_loaded()).count();
        let lazy_count = count - loaded_count;

        let (time_range_start, time_range_end) = self
            .time_range()
            .map(|(s, e)| (Some(s), Some(e)))
            .unwrap_or((None, None));

        let avg_cloud_cover = if count > 0 {
            let sum: f32 = self
                .entries
                .values()
                .filter_map(|e| e.metadata.cloud_cover)
                .sum();
            let cloud_count = self
                .entries
                .values()
                .filter(|e| e.metadata.cloud_cover.is_some())
                .count();
            if cloud_count > 0 {
                Some(sum / cloud_count as f32)
            } else {
                None
            }
        } else {
            None
        };

        TimeSeriesStats {
            count,
            loaded_count,
            lazy_count,
            time_range_start,
            time_range_end,
            resolution: self.resolution,
            avg_cloud_cover,
        }
    }

    /// Filter rasters by cloud cover threshold
    ///
    /// # Errors
    /// Returns error if cloud cover threshold is invalid
    pub fn filter_by_cloud_cover(&mut self, max_cloud_cover: f32) -> Result<usize> {
        if !(0.0..=100.0).contains(&max_cloud_cover) {
            return Err(TemporalError::invalid_parameter(
                "max_cloud_cover",
                "must be between 0 and 100",
            ));
        }

        let original_count = self.len();
        self.entries.retain(|_, entry| {
            entry
                .metadata
                .cloud_cover
                .is_none_or(|cc| cc <= max_cloud_cover)
        });

        let removed = original_count - self.len();
        info!(
            "Filtered {} rasters with cloud cover > {}%",
            removed, max_cloud_cover
        );
        Ok(removed)
    }

    /// Filter rasters by quality score threshold
    ///
    /// # Errors
    /// Returns error if quality threshold is invalid
    pub fn filter_by_quality(&mut self, min_quality: f32) -> Result<usize> {
        if !(0.0..=1.0).contains(&min_quality) {
            return Err(TemporalError::invalid_parameter(
                "min_quality",
                "must be between 0 and 1",
            ));
        }

        let original_count = self.len();
        self.entries.retain(|_, entry| {
            entry
                .metadata
                .quality_score
                .is_none_or(|qs| qs >= min_quality)
        });

        let removed = original_count - self.len();
        info!(
            "Filtered {} rasters with quality < {}",
            removed, min_quality
        );
        Ok(removed)
    }

    /// Extract pixel time series at specific location
    ///
    /// # Errors
    /// Returns error if coordinates are out of bounds or data not loaded
    pub fn extract_pixel_timeseries(
        &self,
        row: usize,
        col: usize,
        band: usize,
    ) -> Result<Vec<f64>> {
        let mut values = Vec::with_capacity(self.len());

        for entry in self.entries.values() {
            let data = entry.data.as_ref().ok_or_else(|| {
                TemporalError::invalid_input("Data not loaded. Call load_data() first")
            })?;

            // Validate bounds
            let shape = data.shape();
            if row >= shape[0] || col >= shape[1] || band >= shape[2] {
                return Err(TemporalError::invalid_parameter(
                    "coordinates",
                    format!(
                        "({},{},{}) out of bounds for shape ({},{},{})",
                        row, col, band, shape[0], shape[1], shape[2]
                    ),
                ));
            }

            values.push(data[[row, col, band]]);
        }

        Ok(values)
    }

    /// Extract spatial slice at specific time
    ///
    /// # Errors
    /// Returns error if timestamp not found or data not loaded
    pub fn extract_spatial_slice(&self, timestamp: &DateTime<Utc>) -> Result<Array3<f64>> {
        let entry = self.get_at_timestamp(timestamp).ok_or_else(|| {
            TemporalError::invalid_input(format!("Timestamp {} not found", timestamp))
        })?;

        entry
            .data
            .as_ref()
            .cloned()
            .ok_or_else(|| TemporalError::invalid_input("Data not loaded"))
    }

    /// Extract temporal slice for specific band
    ///
    /// # Errors
    /// Returns error if band is out of bounds or data not loaded
    pub fn extract_temporal_slice(&self, band: usize) -> Result<Vec<Array2<f64>>> {
        let mut slices = Vec::with_capacity(self.len());

        for entry in self.entries.values() {
            let data = entry
                .data
                .as_ref()
                .ok_or_else(|| TemporalError::invalid_input("Data not loaded"))?;

            if band >= data.shape()[2] {
                return Err(TemporalError::invalid_parameter(
                    "band",
                    format!("band {} out of bounds (max: {})", band, data.shape()[2] - 1),
                ));
            }

            // Extract 2D slice for this band
            let slice = data.slice(s![.., .., band]).to_owned();
            slices.push(slice);
        }

        Ok(slices)
    }

    /// Calculate temporal statistics at each pixel
    ///
    /// # Errors
    /// Returns error if data not loaded or insufficient data
    pub fn pixel_statistics(&self) -> Result<PixelStatistics> {
        if self.is_empty() {
            return Err(TemporalError::insufficient_data(
                "No rasters in time series",
            ));
        }

        // Get shape from first entry
        let first_entry = self
            .entries
            .values()
            .next()
            .ok_or_else(|| TemporalError::insufficient_data("No entries"))?;
        let data = first_entry
            .data
            .as_ref()
            .ok_or_else(|| TemporalError::invalid_input("Data not loaded"))?;
        let (height, width, bands) = (data.shape()[0], data.shape()[1], data.shape()[2]);

        let mut mean = Array3::zeros((height, width, bands));
        let mut min = Array3::from_elem((height, width, bands), f64::INFINITY);
        let mut max = Array3::from_elem((height, width, bands), f64::NEG_INFINITY);
        let mut std_dev = Array3::zeros((height, width, bands));

        let count = self.len() as f64;

        // First pass: calculate mean, min, max
        for entry in self.entries.values() {
            let data = entry
                .data
                .as_ref()
                .ok_or_else(|| TemporalError::invalid_input("Data not loaded"))?;

            for i in 0..height {
                for j in 0..width {
                    for k in 0..bands {
                        let val = data[[i, j, k]];
                        mean[[i, j, k]] += val / count;
                        if val < min[[i, j, k]] {
                            min[[i, j, k]] = val;
                        }
                        if val > max[[i, j, k]] {
                            max[[i, j, k]] = val;
                        }
                    }
                }
            }
        }

        // Second pass: calculate standard deviation
        for entry in self.entries.values() {
            let data = entry
                .data
                .as_ref()
                .ok_or_else(|| TemporalError::invalid_input("Data not loaded"))?;

            for i in 0..height {
                for j in 0..width {
                    for k in 0..bands {
                        let diff = data[[i, j, k]] - mean[[i, j, k]];
                        std_dev[[i, j, k]] += diff * diff / count;
                    }
                }
            }
        }

        // Take square root for std_dev
        std_dev.mapv_inplace(f64::sqrt);

        Ok(PixelStatistics {
            mean,
            min,
            max,
            std_dev,
        })
    }

    /// Get all entries
    #[must_use]
    pub fn entries(&self) -> &BTreeMap<i64, TemporalRasterEntry> {
        &self.entries
    }

    /// Get mutable entries
    pub fn entries_mut(&mut self) -> &mut BTreeMap<i64, TemporalRasterEntry> {
        &mut self.entries
    }

    /// Iterate over entries in chronological order
    pub fn iter(&self) -> impl Iterator<Item = (&i64, &TemporalRasterEntry)> {
        self.entries.iter()
    }
}

impl Default for TimeSeriesRaster {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics computed across temporal dimension for each pixel
#[derive(Debug, Clone)]
pub struct PixelStatistics {
    /// Mean value over time for each pixel
    pub mean: Array3<f64>,
    /// Minimum value over time for each pixel
    pub min: Array3<f64>,
    /// Maximum value over time for each pixel
    pub max: Array3<f64>,
    /// Standard deviation over time for each pixel
    pub std_dev: Array3<f64>,
}

/// Time series statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeSeriesStats {
    /// Total number of rasters
    pub count: usize,
    /// Number of loaded rasters
    pub loaded_count: usize,
    /// Number of lazy-loaded rasters
    pub lazy_count: usize,
    /// Start of time range
    pub time_range_start: Option<DateTime<Utc>>,
    /// End of time range
    pub time_range_end: Option<DateTime<Utc>>,
    /// Temporal resolution
    pub resolution: Option<TemporalResolution>,
    /// Average cloud cover
    pub avg_cloud_cover: Option<f32>,
}