Skip to content

Commit

Permalink
no more lazy static
Browse files Browse the repository at this point in the history
  • Loading branch information
d3v-null committed Oct 29, 2024
1 parent 397f400 commit 4bcdaf8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
50 changes: 33 additions & 17 deletions src/gpubox_files/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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<channel>\d{3})_(?P<batch>\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<band>\d{2})_(?P<batch>\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<band>\d{2}).fits").unwrap();
static ref RE_BAND: Regex = Regex::new(r"\d{10}_\d{14}_(ch|gpubox)(?P<band>\d+)").unwrap();
// MWAX: 1234567890_1234567890123_ch123_123.fits
// obsid datetime chan batch
fn re_mwax() -> &'static Regex {
static RE_MWAX: OnceLock<Regex> = OnceLock::new();
RE_MWAX.get_or_init(|| {
Regex::new(r"\d{10}_\d{8}(.)?\d{6}_ch(?P<channel>\d{3})_(?P<batch>\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<Regex> = OnceLock::new();
RE_LEGACY_BATCH.get_or_init(|| {
Regex::new(r"\d{10}_\d{14}_gpubox(?P<band>\d{2})_(?P<batch>\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<Regex> = OnceLock::new();
RE_OLD_LEGACY_FORMAT
.get_or_init(|| Regex::new(r"\d{10}_\d{14}_gpubox(?P<band>\d{2}).fits").unwrap())
}

// fn re_band() -> &'static Regex {
// static RE_BAND: OnceLock<Regex> = OnceLock::new();
// RE_BAND.get_or_init(|| Regex::new(r"\d{10}_\d{14}_(ch|gpubox)(?P<band>\d+)").unwrap())
// }

/// A type alias for a horrible type:
/// `BTreeMap<u64, BTreeMap<usize, (usize, usize)>>`
///
Expand Down Expand Up @@ -347,7 +363,7 @@ fn determine_gpubox_batches<T: AsRef<Path>>(
.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
Expand All @@ -368,7 +384,7 @@ fn determine_gpubox_batches<T: AsRef<Path>>(
}

// 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),
Expand All @@ -384,7 +400,7 @@ fn determine_gpubox_batches<T: AsRef<Path>>(
}

// 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),
Expand Down
30 changes: 19 additions & 11 deletions src/voltage_files/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<obs_id>\d{10})_(?P<gpstime>\d{10})_(?P<channel>\d{1,3})\.sub").unwrap();
// 1234567890_1234567890_123.dat
// obsid subobsid chan
static ref RE_LEGACY_VCS_RECOMBINED: Regex =
Regex::new(r"(?P<obs_id>\d{10})_(?P<gpstime>\d{10})_ch(?P<channel>\d{1,3})\.dat").unwrap();
// 1234567890_1234567890_123.sub
// obsid subobsid chan
fn re_mwax_vcs() -> &'static Regex {
static RE_MWAX_VCS: OnceLock<Regex> = OnceLock::new();
RE_MWAX_VCS.get_or_init(|| {
Regex::new(r"(?P<obs_id>\d{10})_(?P<gpstime>\d{10})_(?P<channel>\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<Regex> = OnceLock::new();
RE_LEGACY_VCS_RECOMBINED.get_or_init(|| {
Regex::new(r"(?P<obs_id>\d{10})_(?P<gpstime>\d{10})_ch(?P<channel>\d{1,3})\.dat").unwrap()
})
}

/// A type alias for a horrible type:
Expand Down Expand Up @@ -260,7 +268,7 @@ fn determine_voltage_file_gpstime_batches<T: AsRef<Path>>(
.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
Expand All @@ -282,7 +290,7 @@ fn determine_voltage_file_gpstime_batches<T: AsRef<Path>>(
}

// 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),
Expand Down

0 comments on commit 4bcdaf8

Please sign in to comment.