|
| 1 | +use crate::templates::template::{Template, TemplateType}; |
| 2 | +use crate::utils::{read_u16_from_bytes, read_u32_from_bytes}; |
| 3 | +use chrono::{prelude::*, Duration}; |
| 4 | + |
| 5 | +use super::product_template::ProductTemplate; |
| 6 | +use super::tables::{ |
| 7 | + DerivedForecastType, FixedSurfaceType, GeneratingProcess, TimeUnit, |
| 8 | + TypeOfStatisticalProcessing, TypeOfTimeInterval, |
| 9 | +}; |
| 10 | +use super::HorizontalAnalysisForecastTemplate; |
| 11 | + |
| 12 | +pub struct DerivedEnsembleForecastTimeIntervalReferenceTemplate { |
| 13 | + data: Vec<u8>, |
| 14 | + discipline: u8, |
| 15 | +} |
| 16 | + |
| 17 | +impl Template for DerivedEnsembleForecastTimeIntervalReferenceTemplate { |
| 18 | + fn data(&self) -> &[u8] { |
| 19 | + &self.data |
| 20 | + } |
| 21 | + |
| 22 | + fn template_number(&self) -> u16 { |
| 23 | + 107 |
| 24 | + } |
| 25 | + |
| 26 | + fn template_type(&self) -> TemplateType { |
| 27 | + TemplateType::Product |
| 28 | + } |
| 29 | + |
| 30 | + fn template_name(&self) -> &str { |
| 31 | + "Derived forecasts based on all ensemble members at a horizontal level |
| 32 | + or in a horizontal layer in a continuous or non-continuous time interval |
| 33 | + with reference to a normal" |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl DerivedEnsembleForecastTimeIntervalReferenceTemplate { |
| 38 | + pub fn new(data: Vec<u8>, discipline: u8) -> Self { |
| 39 | + Self { data, discipline } |
| 40 | + } |
| 41 | + |
| 42 | + pub fn first_fixed_surface_scale_factor(&self) -> i8 { |
| 43 | + as_signed!(self.data[23], 8, i8) |
| 44 | + } |
| 45 | + |
| 46 | + pub fn first_fixed_surface_scaled_value(&self) -> i32 { |
| 47 | + as_signed!(read_u32_from_bytes(&self.data, 24).unwrap_or(0), 32, i32) |
| 48 | + } |
| 49 | + |
| 50 | + pub fn second_fixed_surface_scale_factor(&self) -> i8 { |
| 51 | + as_signed!(self.data[29], 8, i8) |
| 52 | + } |
| 53 | + |
| 54 | + pub fn second_fixed_surface_scaled_value(&self) -> i32 { |
| 55 | + as_signed!(read_u32_from_bytes(&self.data, 30).unwrap_or(0), 32, i32) |
| 56 | + } |
| 57 | + |
| 58 | + pub fn valid_end_date(&self) -> DateTime<Utc> { |
| 59 | + let data = self.data(); |
| 60 | + let year = read_u16_from_bytes(data, 34).unwrap_or(0) as i32; |
| 61 | + let month = data[36] as u32; |
| 62 | + let day = data[37] as u32; |
| 63 | + let hour = data[38] as u32; |
| 64 | + let minute = data[39] as u32; |
| 65 | + let second = data[40] as u32; |
| 66 | + |
| 67 | + Utc.with_ymd_and_hms(year as i32, month, day, hour, minute, second) |
| 68 | + .unwrap() |
| 69 | + } |
| 70 | + |
| 71 | + pub fn number_of_time_ranges(&self) -> u8 { |
| 72 | + self.data()[41] |
| 73 | + } |
| 74 | + |
| 75 | + pub fn number_of_values_missing_from_stats(&self) -> u32 { |
| 76 | + read_u32_from_bytes(self.data(), 42).unwrap_or(0) |
| 77 | + } |
| 78 | + |
| 79 | + pub fn type_of_time_interval(&self) -> TypeOfTimeInterval { |
| 80 | + self.data()[47].into() |
| 81 | + } |
| 82 | + |
| 83 | + pub fn statistical_process_time_unit(&self) -> TimeUnit { |
| 84 | + self.data()[48].into() |
| 85 | + } |
| 86 | + |
| 87 | + pub fn statistical_process_time_interval(&self) -> u32 { |
| 88 | + read_u32_from_bytes(self.data(), 49).unwrap_or(0) |
| 89 | + } |
| 90 | + |
| 91 | + pub fn number_of_forecasts_in_ensemble(&self) -> u8 { |
| 92 | + self.data[59] |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl ProductTemplate for DerivedEnsembleForecastTimeIntervalReferenceTemplate { |
| 97 | + fn discipline(&self) -> u8 { |
| 98 | + self.discipline |
| 99 | + } |
| 100 | + |
| 101 | + fn category_value(&self) -> u8 { |
| 102 | + self.data[9] |
| 103 | + } |
| 104 | + |
| 105 | + fn parameter_value(&self) -> u8 { |
| 106 | + self.data[10] |
| 107 | + } |
| 108 | + |
| 109 | + fn generating_process(&self) -> GeneratingProcess { |
| 110 | + self.data[11].into() |
| 111 | + } |
| 112 | + |
| 113 | + fn time_unit(&self) -> TimeUnit { |
| 114 | + self.data[17].into() |
| 115 | + } |
| 116 | + |
| 117 | + fn time_increment_unit(&self) -> Option<TimeUnit> { |
| 118 | + Some(self.data()[53].into()) |
| 119 | + } |
| 120 | + |
| 121 | + fn time_interval(&self) -> u32 { |
| 122 | + read_u32_from_bytes(&self.data, 18).unwrap_or(0) |
| 123 | + } |
| 124 | + |
| 125 | + fn time_increment_interval(&self) -> Option<u32> { |
| 126 | + Some(read_u32_from_bytes(self.data(), 54).unwrap_or(0)) |
| 127 | + } |
| 128 | + |
| 129 | + fn forecast_datetime(&self, reference_date: DateTime<Utc>) -> DateTime<Utc> { |
| 130 | + let offset_duration: Duration = self.time_interval_duration(); |
| 131 | + reference_date + offset_duration |
| 132 | + } |
| 133 | + |
| 134 | + fn forecast_end_datetime(&self, _reference_date: DateTime<Utc>) -> Option<DateTime<Utc>> { |
| 135 | + Some(self.valid_end_date()) |
| 136 | + } |
| 137 | + |
| 138 | + fn first_fixed_surface_type(&self) -> FixedSurfaceType { |
| 139 | + self.data[22].into() |
| 140 | + } |
| 141 | + |
| 142 | + fn first_fixed_surface_value(&self) -> Option<f64> { |
| 143 | + HorizontalAnalysisForecastTemplate::scale_value( |
| 144 | + self.first_fixed_surface_scale_factor(), |
| 145 | + self.first_fixed_surface_scaled_value(), |
| 146 | + ) |
| 147 | + } |
| 148 | + |
| 149 | + fn second_fixed_surface_type(&self) -> FixedSurfaceType { |
| 150 | + self.data[28].into() |
| 151 | + } |
| 152 | + |
| 153 | + fn second_fixed_surface_value(&self) -> Option<f64> { |
| 154 | + HorizontalAnalysisForecastTemplate::scale_value( |
| 155 | + self.second_fixed_surface_scale_factor(), |
| 156 | + self.second_fixed_surface_scaled_value(), |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + fn derived_forecast_type(&self) -> Option<DerivedForecastType> { |
| 161 | + Some(self.data[58].into()) |
| 162 | + } |
| 163 | + |
| 164 | + fn statistical_process_type(&self) -> Option<TypeOfStatisticalProcessing> { |
| 165 | + Some(self.data()[46].into()) |
| 166 | + } |
| 167 | + |
| 168 | + fn is_anomaly(&self) -> bool { |
| 169 | + true |
| 170 | + } |
| 171 | +} |
0 commit comments