diff --git a/src/gpubox_files/mod.rs b/src/gpubox_files/mod.rs index e513ca5..3f95123 100644 --- a/src/gpubox_files/mod.rs +++ b/src/gpubox_files/mod.rs @@ -10,6 +10,7 @@ use std::collections::BTreeMap; use std::collections::HashSet; use std::fmt; use std::path::Path; +use std::sync::OnceLock; use fitsio::hdu::FitsHdu; use rayon::prelude::*; @@ -118,22 +119,37 @@ impl<'a> std::cmp::PartialEq for TempGpuBoxFile<'a> { } } -lazy_static::lazy_static! { - // MWAX: 1234567890_1234567890123_ch123_123.fits - // obsid datetime chan batch - static ref RE_MWAX: Regex = - Regex::new(r"\d{10}_\d{8}(.)?\d{6}_ch(?P\d{3})_(?P\d{3}).fits").unwrap(); - // Legacy MWA: 1234567890_1234567890123_gpubox12_12.fits - // obsid datetime chan batch - static ref RE_LEGACY_BATCH: Regex = - Regex::new(r"\d{10}_\d{14}_gpubox(?P\d{2})_(?P\d{2}).fits").unwrap(); - // Old Legacy MWA: 1234567890_1234567890123_gpubox12.fits - // obsid datetime chan - static ref RE_OLD_LEGACY_FORMAT: Regex = - Regex::new(r"\d{10}_\d{14}_gpubox(?P\d{2}).fits").unwrap(); - static ref RE_BAND: Regex = Regex::new(r"\d{10}_\d{14}_(ch|gpubox)(?P\d+)").unwrap(); +// MWAX: 1234567890_1234567890123_ch123_123.fits +// obsid datetime chan batch +fn re_mwax() -> &'static Regex { + static RE_MWAX: OnceLock = OnceLock::new(); + RE_MWAX.get_or_init(|| { + Regex::new(r"\d{10}_\d{8}(.)?\d{6}_ch(?P\d{3})_(?P\d{3}).fits").unwrap() + }) +} + +// Legacy MWA: 1234567890_1234567890123_gpubox12_12.fits +// obsid datetime chan batch +fn re_legacy_batch() -> &'static Regex { + static RE_LEGACY_BATCH: OnceLock = OnceLock::new(); + RE_LEGACY_BATCH.get_or_init(|| { + Regex::new(r"\d{10}_\d{14}_gpubox(?P\d{2})_(?P\d{2}).fits").unwrap() + }) } +// Old Legacy MWA: 1234567890_1234567890123_gpubox12.fits +// obsid datetime chan +fn re_old_legacy_format() -> &'static Regex { + static RE_OLD_LEGACY_FORMAT: OnceLock = OnceLock::new(); + RE_OLD_LEGACY_FORMAT + .get_or_init(|| Regex::new(r"\d{10}_\d{14}_gpubox(?P\d{2}).fits").unwrap()) +} + +// fn re_band() -> &'static Regex { +// static RE_BAND: OnceLock = OnceLock::new(); +// RE_BAND.get_or_init(|| Regex::new(r"\d{10}_\d{14}_(ch|gpubox)(?P\d+)").unwrap()) +// } + /// A type alias for a horrible type: /// `BTreeMap>` /// @@ -347,7 +363,7 @@ fn determine_gpubox_batches>( .as_ref() .to_str() .expect("gpubox filename is not UTF-8 compliant"); - match RE_MWAX.captures(g) { + match re_mwax().captures(g) { Some(caps) => { // Check if we've already matched any files as being the old // format. If so, then we've got a mix, and we should exit @@ -368,7 +384,7 @@ fn determine_gpubox_batches>( } // Try to match the legacy format. - None => match RE_LEGACY_BATCH.captures(g) { + None => match re_legacy_batch().captures(g) { Some(caps) => { match format { None => format = Some(MWAVersion::CorrLegacy), @@ -384,7 +400,7 @@ fn determine_gpubox_batches>( } // Try to match the old legacy format. - None => match RE_OLD_LEGACY_FORMAT.captures(g) { + None => match re_old_legacy_format().captures(g) { Some(caps) => { match format { None => format = Some(MWAVersion::CorrOldLegacy), diff --git a/src/voltage_files/mod.rs b/src/voltage_files/mod.rs index 141e1ac..201012b 100644 --- a/src/voltage_files/mod.rs +++ b/src/voltage_files/mod.rs @@ -13,6 +13,7 @@ use std::collections::HashMap; use std::collections::HashSet; use std::fmt; use std::path::Path; +use std::sync::OnceLock; #[cfg(feature = "python")] use pyo3_stub_gen_derive::gen_stub_pyclass; @@ -117,15 +118,22 @@ impl<'a> std::cmp::PartialEq for TempVoltageFile<'a> { } } -lazy_static::lazy_static! { - // 1234567890_1234567890_123.sub - // obsid subobsid chan - static ref RE_MWAX_VCS: Regex = - Regex::new(r"(?P\d{10})_(?P\d{10})_(?P\d{1,3})\.sub").unwrap(); - // 1234567890_1234567890_123.dat - // obsid subobsid chan - static ref RE_LEGACY_VCS_RECOMBINED: Regex = - Regex::new(r"(?P\d{10})_(?P\d{10})_ch(?P\d{1,3})\.dat").unwrap(); +// 1234567890_1234567890_123.sub +// obsid subobsid chan +fn re_mwax_vcs() -> &'static Regex { + static RE_MWAX_VCS: OnceLock = OnceLock::new(); + RE_MWAX_VCS.get_or_init(|| { + Regex::new(r"(?P\d{10})_(?P\d{10})_(?P\d{1,3})\.sub").unwrap() + }) +} + +// 1234567890_1234567890_123.dat +// obsid subobsid chan +fn re_legacy_vcs_recombined() -> &'static Regex { + static RE_LEGACY_VCS_RECOMBINED: OnceLock = OnceLock::new(); + RE_LEGACY_VCS_RECOMBINED.get_or_init(|| { + Regex::new(r"(?P\d{10})_(?P\d{10})_ch(?P\d{1,3})\.dat").unwrap() + }) } /// A type alias for a horrible type: @@ -260,7 +268,7 @@ fn determine_voltage_file_gpstime_batches>( .expect("Voltage filename is not UTF-8 compliant"); let new_temp_voltage_file: TempVoltageFile = { - match RE_MWAX_VCS.captures(v) { + match re_mwax_vcs().captures(v) { Some(caps) => { // Check if we've already matched any files as being the old // format. If so, then we've got a mix, and we should exit @@ -282,7 +290,7 @@ fn determine_voltage_file_gpstime_batches>( } // Try to match the legacy format. - None => match RE_LEGACY_VCS_RECOMBINED.captures(v) { + None => match re_legacy_vcs_recombined().captures(v) { Some(caps) => { match format { None => format = Some(MWAVersion::VCSLegacyRecombined),