radar-g/src/data/mod.rs
2024-04-12 18:37:41 +08:00

152 lines
4.2 KiB
Rust

pub mod meta;
use crate::errors::DataError;
use async_trait::async_trait;
pub use meta::MetaInfo;
use ndarray::iter::Indices;
use ndarray::{
s, Array, Array1, Array2, Array3, ArrayBase, Axis, DataMut, Dimension, Ix1, Ix2, Ix3,
OwnedRepr, RawDataClone, Slice, ViewRepr,
};
use npyz::{npz::NpzArchive, Deserialize};
use num_traits::{AsPrimitive, FromPrimitive, Num};
use std::{self, f64::consts::PI, fmt::Debug, io::BufReader, path::Path};
pub type Radar2d<T> = RadarData2d<T, OwnedRepr<T>, OwnedRepr<f64>, OwnedRepr<f64>>;
pub type Radar3d<T> = RadarData3d<T, OwnedRepr<T>, OwnedRepr<f64>, OwnedRepr<f64>, OwnedRepr<f64>>;
pub type Radar2dRef<'a, T> = RadarData2d<T, ViewRepr<&'a T>, ViewRepr<&'a f64>, ViewRepr<&'a f64>>;
pub type Radar3dRef<'a, T> =
RadarData3d<T, ViewRepr<&'a T>, ViewRepr<&'a f64>, ViewRepr<&'a f64>, ViewRepr<&'a f64>>;
#[derive(Debug, Clone, Copy)]
pub enum DataType {
F64,
I64,
U64,
F32,
I32,
U32,
I16,
U16,
I8,
U8,
}
#[derive(Clone, Copy, Debug)]
pub enum CoordType {
Polar,
LatLon,
}
pub trait MultiDimensionData<T>
where
T: Num + Clone + PartialEq + PartialOrd,
{
fn map_by_fn<F>(&mut self, f: F)
where
F: FnMut(&mut T);
}
#[derive(Clone)]
pub struct RadarData2d<T, Raw, X, Y, I = Ix1>
where
T: Num + Clone + PartialEq + PartialOrd,
Raw: ndarray::Data<Elem = T> + ndarray::RawDataClone,
X: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Y: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
{
pub dim1: ArrayBase<X, I>,
pub dim2: ArrayBase<Y, I>,
pub data: ArrayBase<Raw, Ix2>,
pub fill_value: T,
pub coord_type: CoordType,
}
impl<T> Radar2d<T>
where
T: Num + Clone + PartialEq + PartialOrd,
{
pub fn as_ref<'a>(&'a self) -> Radar2dRef<'a, T> {
RadarData2d {
dim1: self.dim1.view(),
dim2: self.dim2.view(),
data: self.data.view(),
fill_value: self.fill_value.clone(),
coord_type: self.coord_type.clone(),
}
}
}
#[derive(Clone)]
pub struct RadarData3d<T, Raw, X, Y, Z, I = Ix1>
where
T: Num + Clone + PartialEq + PartialOrd,
Raw: ndarray::Data<Elem = T> + ndarray::RawDataClone,
X: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Y: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Z: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
{
pub dim1: ArrayBase<X, I>,
pub dim2: ArrayBase<Y, I>,
pub dim3: ArrayBase<Z, I>,
pub data: ArrayBase<Raw, Ix3>,
pub fill_value: T,
pub coord_type: CoordType,
}
impl<T, Raw, X, Y, Z> RadarData3d<T, Raw, X, Y, Z>
where
T: Num + Clone + PartialEq + PartialOrd,
X: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Y: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Z: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Raw: ndarray::Data<Elem = T> + ndarray::RawDataClone,
{
pub fn index_axis(&self, axis: Axis, slice: usize) -> Radar2dRef<T> {
let shape = self.data.shape();
println!("shape: {:?}", shape);
let dim1 = self.dim1.view();
let dim2 = self.dim2.view();
let view = self.data.index_axis(axis, slice);
RadarData2d {
dim1,
dim2,
data: view,
fill_value: self.fill_value.clone(),
coord_type: self.coord_type.clone(),
}
}
}
impl<T, Raw, X, Y, Z> MultiDimensionData<T> for RadarData3d<T, Raw, X, Y, Z>
where
T: Num + Clone,
T: PartialEq + PartialOrd,
X: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Y: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Z: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Raw: ndarray::Data<Elem = T> + RawDataClone + DataMut,
{
fn map_by_fn<F>(&mut self, f: F)
where
F: FnMut(&mut T),
{
self.data.map_inplace(f);
}
}
impl<T, Raw, X, Y> MultiDimensionData<T> for RadarData2d<T, Raw, X, Y>
where
T: Num + Clone,
T: PartialEq + PartialOrd,
X: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Y: ndarray::Data<Elem = f64> + ndarray::RawDataClone,
Raw: ndarray::Data<Elem = T> + RawDataClone + DataMut,
{
fn map_by_fn<F>(&mut self, f: F)
where
F: FnMut(&mut T),
{
self.data.map_inplace(f);
}
}