boreholeio 0.1.0

A library for interacting with borehole.io, a subsurface data management, delivery and visualisation platform
Documentation
use super::{Error, MeasurementArray};
use crate::{
    schema::{array_uri_reference::ArrayUriReference, axes::ChannelAxis, Label},
    strided_array_file::{extract_array_index, ArrayProxy, StridedArrayFile},
};
use std::{path::PathBuf, sync::Arc};

/// An object that owns the data from a strided array file in memory and
/// provides fast access to views of the data.
#[derive(Debug)]
pub(crate) struct StridedArrayData<'a> {
    /// The data itself.
    #[expect(dead_code)]
    strided_array_file: StridedArrayFile<'a>,
    /// Data view whose elements type is incapsulated into `ArrayProxy`.
    data: ArrayProxy<'a>,
    /// Coordinate system of the data.
    axes: Vec<ChannelAxis>,
    /// Optional label object defining the implicit measurement axis, if one
    /// exists. Is not `None` for e.g. grayscale images.
    label: Option<Label>,
}

impl<'a> StridedArrayData<'a> {
    /// Tries to read the input file, verifies its consistency with the
    /// provided metadata and constructs `Arc<Self>` out of it.
    pub(crate) fn from_uri(
        uri: &ArrayUriReference,
        axes: Vec<ChannelAxis>,
        label: Option<Label>,
    ) -> Result<Arc<dyn MeasurementArray<'a> + 'a>, Error> {
        // Extract file path from the provided URI.
        let path: PathBuf = (&uri.with_fragment(None)).try_into()?;
        // Extract array index from the URI. Assume `0` when it is not provided
        // explicitly.
        let array_index = extract_array_index(uri)?.unwrap_or(0);
        // Read the strided array file.
        let strided_array_file = StridedArrayFile::new(path);
        // Get array in question.
        let data = strided_array_file.try_get(array_index)?.clone();
        // Verify metadata consistency.
        if data.dimensions() != axes.len() {
            return Err("Inconsistent data and axes dimensionality".into());
        }

        Ok(Arc::new(Self {
            strided_array_file,
            data,
            axes,
            label,
        }))
    }
}

impl<'a> MeasurementArray<'a> for StridedArrayData<'a> {
    fn data(&self) -> ArrayProxy<'a> {
        self.data.clone()
    }

    fn axes(&self) -> &Vec<ChannelAxis> {
        &self.axes
    }

    fn label(&self) -> &Option<Label> {
        &self.label
    }
}