|
| 1 | +//! Kriging: the global kriging matrix operations and local |
| 2 | +//! (moving-neighborhood) kriging. |
| 3 | +//! |
| 4 | +//! ```text |
| 5 | +//! global.rs global kriging: contract a pre-inverted kriging matrix |
| 6 | +//! local.rs local kriging of one or many fields (weights cached |
| 7 | +//! per target and neighbor mask) |
| 8 | +//! |
| 9 | +//! used by local kriging: |
| 10 | +//! neighbors.rs k-d tree radius search |
| 11 | +//! solve.rs linear algebra: Cholesky + Schur complement, |
| 12 | +//! dense LU / SVD fallback |
| 13 | +//! |
| 14 | +//! test_util.rs test helpers, incl. the reference system builders |
| 15 | +//! build_lhs / build_rhs |
| 16 | +//! ``` |
| 17 | +//! |
| 18 | +//! Dependencies only point downwards: local kriging uses `neighbors` and |
| 19 | +//! `solve`; `solve` knows nothing about kriging. |
| 20 | +//! |
| 21 | +//! The public API is re-exported here, so callers use |
| 22 | +//! `crate::krige::calc_field_krige_local` etc. |
| 23 | +
|
| 24 | +use std::fmt; |
| 25 | + |
| 26 | +pub mod global; |
| 27 | +pub mod local; |
| 28 | +mod neighbors; |
| 29 | +pub mod solve; |
| 30 | +#[cfg(test)] |
| 31 | +mod test_util; |
| 32 | + |
| 33 | +pub use global::{calculator_field_krige, calculator_field_krige_and_variance}; |
| 34 | +pub use local::{calc_field_krige_local, drift_terms_used, LocalKrigeResult, LocalKrigeStats}; |
| 35 | +pub use solve::{solve_dense, solve_kriging, SingularSystem}; |
| 36 | + |
| 37 | +/// Errors of local kriging. |
| 38 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 39 | +pub enum KrigeError { |
| 40 | + /// Inconsistent array shapes or non-finite positions / radius. |
| 41 | + InvalidInput(String), |
| 42 | + /// A local system could not be solved, not even by the least-squares |
| 43 | + /// fallback of [`solve_dense`] (in practice essentially unreachable). |
| 44 | + SingularSystem { |
| 45 | + /// index of the target point |
| 46 | + target_index: usize, |
| 47 | + /// index of the field (e.g. day) |
| 48 | + field_index: usize, |
| 49 | + }, |
| 50 | +} |
| 51 | + |
| 52 | +impl fmt::Display for KrigeError { |
| 53 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 54 | + match self { |
| 55 | + Self::InvalidInput(msg) => write!(f, "invalid input: {msg}"), |
| 56 | + Self::SingularSystem { |
| 57 | + target_index, |
| 58 | + field_index, |
| 59 | + } => write!( |
| 60 | + f, |
| 61 | + "local kriging matrix for target point {target_index} and field \ |
| 62 | + {field_index} is singular even for the least-squares fallback" |
| 63 | + ), |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl std::error::Error for KrigeError {} |
0 commit comments