Skip to main content

wxy_domain/
field.rs

1//! Fields: values on a domain.
2
3/// Element types. One shared vocabulary for every adapter (survey M3).
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum DataType {
6    Bool,
7    U8,
8    U16,
9    U32,
10    U64,
11    I8,
12    I16,
13    I32,
14    I64,
15    F16,
16    F32,
17    F64,
18    Complex64,
19    Complex128,
20    String,
21    Bytes,
22}
23
24impl DataType {
25    /// Size in bytes, or `None` for variable-length types.
26    pub fn size(&self) -> Option<usize> {
27        use DataType::*;
28        Some(match self {
29            Bool | U8 | I8 => 1,
30            U16 | I16 | F16 => 2,
31            U32 | I32 | F32 => 4,
32            U64 | I64 | F64 | Complex64 => 8,
33            Complex128 => 16,
34            String | Bytes => return None,
35        })
36    }
37}
38
39/// How stored numbers become values.
40#[derive(Debug, Clone, Default, PartialEq)]
41pub struct ValueSemantics {
42    pub nodata: Option<f64>,
43    pub scale: Option<f64>,
44    pub offset: Option<f64>,
45    pub units: Option<String>,
46}
47
48/// Which element of a domain a value lives on (design doc 02, DM5).
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
50pub enum Location {
51    /// The element itself: a grid cell, a DGGS cell, a feature.
52    Cell,
53    /// Mesh nodes (vertices), or grid corners.
54    Node,
55    /// Mesh edges, or grid faces in a staggered layout.
56    Edge,
57    /// Mesh faces.
58    Face,
59    /// Layered mesh volumes.
60    Volume,
61}
62
63/// Values on a domain.
64///
65/// Where the numbers come from is not part of this type: sources live in
66/// `wxy-source`, so this crate stays free of I/O dependencies.
67#[derive(Debug, Clone)]
68pub struct Field {
69    pub name: String,
70    pub dtype: DataType,
71    pub values: ValueSemantics,
72    /// Index into the owning collection's domains.
73    pub domain: usize,
74    pub location: Location,
75    /// Index of a dim that holds vector components (`u`, `v`), if any.
76    /// A warp must rotate such fields, not resample them as scalars.
77    pub components: Option<usize>,
78}
79
80impl Field {
81    pub fn new(name: impl Into<String>, dtype: DataType, domain: usize) -> Self {
82        Self {
83            name: name.into(),
84            dtype,
85            values: ValueSemantics::default(),
86            domain,
87            location: Location::Cell,
88            components: None,
89        }
90    }
91}