I have this function to query a timestep series. Currently, it returns the first next value after the queried time (non-inclusively). I need to be able to get the last value inclusively and be able to get the next value non-inclusively.
Create a better interface to this function, maybe using std::ops::Bound, and maybe create query_before and query_after functions or something. But feel free to do something else if you think it's preferred.
/// Represents the bound type for time series queries
#[derive(Debug, Clone, Copy)]
pub enum TimeQueryBound {
/// Include the exact timestamp if it exists
Inclusive,
/// Exclude the exact timestamp
Exclusive,
}
impl TimeSeriesData {
/// Queries the time series for a value at or around the specified timestamp.
///
/// # Arguments
/// * `time` - The timestamp to query
/// * `direction` - Whether to search forward or backward in time
/// * `bound` - Whether to include or exclude the exact timestamp match
///
/// # Returns
/// * `Ok(Array1<i32>)` - The row of data at the found timestamp
/// * `Err(hdf5::Error)` - If there's an error accessing the data
///
/// # Errors
/// Returns an error if the date is not found in the dataset or if there's an HDF5 error
pub fn query(
&self,
time: &NaiveDateTime,
direction: SearchDirection,
bound: TimeQueryBound,
) -> Result<ndarray::Array1<i32>, QueryError> {
let date = time.date();
let date_index = self.days_map.get(&date)
.ok_or(QueryError::DateNotFound(date))?;
// Get the next day's index for boundary checking
let next_day_index = match self.days_map.range((Bound::Excluded(&date), Bound::Unbounded)).next() {
Some((_, &idx)) => idx,
None => self.data.nrows(), // Use total rows if it's the last day
};
let search_range = self.data.slice(s![*date_index..next_day_index, ..]);
let timestamp = Self::time_to_timestamp(time, self.timestamp_factor);
let row_index = match (direction, bound) {
(SearchDirection::Forward, TimeQueryBound::Inclusive) => {
search_range.column(0)
.to_vec()
.partition_point(|ts| ts < ×tamp)
},
(SearchDirection::Forward, TimeQueryBound::Exclusive) => {
search_range.column(0)
.to_vec()
.partition_point(|ts| ts <= ×tamp)
},
(SearchDirection::Backward, TimeQueryBound::Inclusive) => {
search_range.column(0)
.to_vec()
.partition_point(|ts| ts <= ×tamp)
.saturating_sub(1)
},
(SearchDirection::Backward, TimeQueryBound::Exclusive) => {
search_range.column(0)
.to_vec()
.partition_point(|ts| ts < ×tamp)
.saturating_sub(1)
},
};
if row_index >= search_range.nrows() {
return Err(QueryError::NoDataFound);
}
Ok(search_range.row(row_index).to_owned())
}
/// Convenience method to get the next value after the specified time
pub fn query_after(&self, time: &NaiveDateTime) -> Result<ndarray::Array1<i32>, QueryError> {
self.query(time, SearchDirection::Forward, TimeQueryBound::Exclusive)
}
/// Convenience method to get the last value before or at the specified time
pub fn query_before(&self, time: &NaiveDateTime) -> Result<ndarray::Array1<i32>, QueryError> {
self.query(time, SearchDirection::Backward, TimeQueryBound::Inclusive)
}
}
/// Direction for searching in the time series
#[derive(Debug, Clone, Copy)]
pub enum SearchDirection {
/// Search forward in time
Forward,
/// Search backward in time
Backward,
}
/// Errors that can occur during querying
#[derive(Debug, thiserror::Error)]
pub enum QueryError {
#[error("Date {0} not found in dataset")]
DateNotFound(NaiveDate),
#[error("No data found for the specified criteria")]
NoDataFound,
#[error("HDF5 error: {0}")]
Hdf5Error(#[from] hdf5::Error),
}