Add CLI parsing. The simplest parser MVN data ready to use
This commit is contained in:
parent
31f3413ab0
commit
5f6fd039f7
140
Cargo.lock
generated
140
Cargo.lock
generated
@ -17,12 +17,64 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "asotr_csv"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"chrono",
|
||||
"clap",
|
||||
"strum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -72,12 +124,51 @@ dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "769b0145982b4b48713e01ec42d61614425f27b7058bda7180a3a41f30104796"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b26884eb4b57140e4d2d93652abfa49498b938b3c9179f9fc487b0acc3edad7"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation-sys"
|
||||
version = "0.8.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.61"
|
||||
@ -101,6 +192,12 @@ dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.77"
|
||||
@ -168,6 +265,34 @@ version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "strum"
|
||||
version = "0.26.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
|
||||
dependencies = [
|
||||
"strum_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strum_macros"
|
||||
version = "0.26.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustversion",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.96"
|
||||
@ -185,6 +310,12 @@ version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.100"
|
||||
@ -252,6 +383,15 @@ dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.59.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.6"
|
||||
|
@ -6,3 +6,5 @@ edition = "2021"
|
||||
[dependencies]
|
||||
byteorder = "1.4.3"
|
||||
chrono = "0.4"
|
||||
strum = { version = "0.26", features = ["derive"] }
|
||||
clap = "4.*"
|
||||
|
6
asotr_parse.sh
Normal file
6
asotr_parse.sh
Normal file
@ -0,0 +1,6 @@
|
||||
ls | grep -P '.*data01.asotr01' | xargs -I {} ./asotr_csv -f {} >> ../asotr1_T.csv
|
||||
ls | grep -P '.*data02.asotr01' | xargs -I {} ./asotr_csv -f {} >> ../asotr1_P.csv
|
||||
ls | grep -P '.*data06.asotr01' | xargs -I {} ./asotr_csv -f {} >> ../asotr1_TSET.csv
|
||||
ls | grep -P '.*data01.asotr02' | xargs -I {} ./asotr_csv -f {} >> ../asotr2_T.csv
|
||||
ls | grep -P '.*data02.asotr02' | xargs -I {} ./asotr_csv -f {} >> ../asotr2_P.csv
|
||||
ls | grep -P '.*data06.asotr02' | xargs -I {} ./asotr_csv -f {} >> ../asotr2_TSET.csv
|
198
src/main.rs
198
src/main.rs
@ -1,54 +1,36 @@
|
||||
use clap::{Arg, Command};
|
||||
|
||||
pub mod asotr_data {
|
||||
use std::{fs::File, io::Read};
|
||||
use byteorder::{LittleEndian, ReadBytesExt};
|
||||
use chrono::{DateTime, Utc};
|
||||
use std::time::{SystemTime, UNIX_EPOCH, Duration};
|
||||
use strum::FromRepr;
|
||||
|
||||
// #[derive(Debug)]
|
||||
// pub struct Temp {
|
||||
// time_s: u64,
|
||||
// time_mks: u16,
|
||||
// datetime: String,
|
||||
// date: String,
|
||||
// time: String,
|
||||
// ch: [f32; 6]
|
||||
// }
|
||||
#[derive(Debug, FromRepr, PartialEq)]
|
||||
enum AsotrDataType {
|
||||
Temp = 1,
|
||||
Pow = 2,
|
||||
TempSet = 6,
|
||||
}
|
||||
|
||||
// impl Temp {
|
||||
// pub fn new(tstamp_s: u64, tstamp_mks: u16, chan: [f32; 6]) -> Temp {
|
||||
// Temp {
|
||||
// time_s: tstamp_s,
|
||||
// time_mks: tstamp_mks,
|
||||
// datetime: "".to_string(),
|
||||
// date: "".to_string(),
|
||||
// time: "".to_string(),
|
||||
// ch: chan,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// #[derive(Debug)]
|
||||
// pub struct Pow {
|
||||
// time_s: u64,
|
||||
// time_mks: u16,
|
||||
// datetime: String,
|
||||
// date: String,
|
||||
// time: String,
|
||||
// ch: [u16; 6]
|
||||
// }
|
||||
|
||||
// impl Pow {
|
||||
// pub fn new(tstamp_s: u64, tstamp_mks: u16, chan: [u16; 6]) -> Pow {
|
||||
// Pow {
|
||||
// time_s: tstamp_s,
|
||||
// time_mks: tstamp_mks,
|
||||
// datetime: "".to_string(),
|
||||
// date: "".to_string(),
|
||||
// time: "".to_string(),
|
||||
// ch: chan,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
struct AsotrDataDesc {
|
||||
time_s: u64,
|
||||
time_mks: u32,
|
||||
datetime: String,
|
||||
date: String,
|
||||
time: String,
|
||||
data_type: AsotrDataType,
|
||||
kit: u8,
|
||||
}
|
||||
|
||||
impl AsotrDataDesc {
|
||||
pub fn new(time_s: u64, time_mks: u32,
|
||||
datetime: String, date: String, time: String,
|
||||
data_type: AsotrDataType, kit: u8) -> AsotrDataDesc {
|
||||
AsotrDataDesc { time_s, time_mks, datetime, date, time, data_type, kit}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_data_f32(buf: Vec<u8>) -> Result<[f32; 6], String> {
|
||||
let mut data = &buf[..];
|
||||
@ -58,7 +40,7 @@ pub mod asotr_data {
|
||||
ch[i] = match data.read_f32::<LittleEndian>() {
|
||||
Ok(val) => val,
|
||||
Err(msg) => {
|
||||
return Err(format!("failed parsing float32 data: {}", msg)); }
|
||||
return Err(format!("Error parsing file: failed parsing float32 data: {}", msg)); }
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,74 +55,108 @@ pub mod asotr_data {
|
||||
ch[i] = match data.read_u16::<LittleEndian>() {
|
||||
Ok(val) => val,
|
||||
Err(msg) => {
|
||||
return Err(format!("failed parsing float32 data: {}", msg)); }
|
||||
return Err(format!("Error parsing file: failed parsing float32 data: {}", msg)); }
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(ch);
|
||||
}
|
||||
|
||||
pub fn parse_data(path: String, filename: String) -> Result<String, String> {
|
||||
let ch_u16: [u16; 6];
|
||||
let ch_f32: [f32; 6];
|
||||
|
||||
fn parse_filename(filename_full: String) -> Result<(AsotrDataDesc, String), String> {
|
||||
let mut filename = String::new();
|
||||
|
||||
match filename_full.rfind('/') {
|
||||
Some(val) => { filename = (filename_full[val+1..filename_full.len()]).to_string(); }
|
||||
_ => { filename = filename_full.clone(); }
|
||||
}
|
||||
|
||||
if filename.len() != 32 { return Err(format!("Error parsing filename: Unsupported file ({})", filename)); }
|
||||
|
||||
let time_str_ = filename[0..10].parse::<u64>();
|
||||
let time_str = match &time_str_ {
|
||||
let time_unix_ = filename[0..10].parse::<u64>();
|
||||
let time_unix = match &time_unix_ {
|
||||
Ok(data) => data,
|
||||
Err(msg) => { return Err(format!("Error parsing filename: expected digits in timestamp part ({})", msg)); }
|
||||
Err(msg) => { return Err(format!("Error parsing filename: expected digits in timestamp sec part ({})", msg)); }
|
||||
};
|
||||
|
||||
let binding = filename[22..24].parse::<u8>();
|
||||
let data_type = match &binding {
|
||||
let data_type_ = filename[22..24].parse::<u8>();
|
||||
let data_type_u8 = match &data_type_ {
|
||||
Ok(data) => data,
|
||||
Err(msg) => { return Err(format!("Error parsing filename: expected digits in data type part ({})", msg)); }
|
||||
};
|
||||
|
||||
let _kit = &filename[30..32];
|
||||
let _time_str_mks = &filename[11..17];
|
||||
if *data_type_u8 == 1 || *data_type_u8 == 2 || *data_type_u8 == 6 { }
|
||||
else {
|
||||
return Err("unsupported file type! Parser supports data types: .data01.asotr01(02), data02.asotr01(02), data06.asotr01(02)".to_string());
|
||||
}
|
||||
|
||||
if *data_type == 1 || *data_type == 2 || *data_type == 6 {
|
||||
let data_type = match AsotrDataType::from_repr(*data_type_u8 as usize) {
|
||||
Some(value) => value,
|
||||
_ => return Err(format!("Error parsing filename d: expected digits in data type part"))
|
||||
};
|
||||
|
||||
let _kit = filename[30..32].parse::<u8>();
|
||||
let kit = match &_kit {
|
||||
Ok(data) => data,
|
||||
Err(msg) => { return Err(format!("Error parsing filename: expected digits in asotr kit part ({})", msg)); }
|
||||
};
|
||||
|
||||
let _time_str_mks = filename[11..17].parse::<u32>();
|
||||
let time_mks = match &_time_str_mks {
|
||||
Ok(data) => data,
|
||||
Err(msg) => { return Err(format!("Error parsing filename: expected digits in timestamp mks part ({})", msg)); }
|
||||
};
|
||||
|
||||
let time: SystemTime = UNIX_EPOCH + Duration::from_secs(*time_unix);
|
||||
let date_time = DateTime::<Utc>::from(time);
|
||||
let date_s = date_time.format("%d.%m.%Y").to_string();
|
||||
let time_s = date_time.format("%H:%M:%S").to_string();
|
||||
|
||||
let asotrDataHead = AsotrDataDesc::new(*time_unix, *time_mks,
|
||||
format!("{} {}", date_s, time_s),
|
||||
date_s, time_s, data_type, *kit);
|
||||
|
||||
return Ok((asotrDataHead, filename));
|
||||
}
|
||||
|
||||
pub fn parse_data(filename_full: String) -> Result<String, String> {
|
||||
let ch_u16: [u16; 6];
|
||||
let ch_f32: [f32; 6];
|
||||
|
||||
let (asotr_head, filename) = parse_filename(filename_full.clone())?;
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut out = String::new();
|
||||
|
||||
let time: SystemTime = UNIX_EPOCH + Duration::from_secs(*time_str);
|
||||
|
||||
let date_time = DateTime::<Utc>::from(time);
|
||||
|
||||
let date_s = date_time.format("%d.%m.%Y").to_string();
|
||||
let time_s = date_time.format("%H.%M.%S").to_string();
|
||||
|
||||
let mut data = match File::open(format!("{}{}", path, filename))
|
||||
let mut data = match File::open(filename_full.clone())
|
||||
{
|
||||
Ok(file) => file,
|
||||
Err(msg) => { return Err(format!("could not open data file ({}): {}", filename, msg)) }
|
||||
Err(msg) => { return Err(format!("Error opening data file {}: {}", filename, msg)) }
|
||||
};
|
||||
|
||||
match data.read_to_end(&mut buf) {
|
||||
Ok(stat) => stat,
|
||||
Err(msg) => { return Err(format!("could not read data file ({}): {}", filename, msg)) }
|
||||
Err(msg) => { return Err(format!("Error reading data file {}: {}", filename, msg)) }
|
||||
};
|
||||
|
||||
|
||||
|
||||
out.push_str(&time_str.to_string());
|
||||
out.push_str(&asotr_head.time_s.to_string());
|
||||
out.push(';');
|
||||
out.push_str(&date_s);
|
||||
out.push_str(&asotr_head.date);
|
||||
out.push(';');
|
||||
out.push_str(&time_s);
|
||||
out.push_str(&asotr_head.time);
|
||||
out.push(';');
|
||||
|
||||
if *data_type == 1 || *data_type == 6 {
|
||||
if asotr_head.data_type == AsotrDataType::Temp ||
|
||||
asotr_head.data_type == AsotrDataType::TempSet {
|
||||
ch_f32 = parse_data_f32(buf)?;
|
||||
for elem in ch_f32 {
|
||||
out.push_str(&elem.to_string());
|
||||
out.push(';');
|
||||
}
|
||||
}
|
||||
else if *data_type == 2 {
|
||||
else if asotr_head.data_type == AsotrDataType::Pow {
|
||||
ch_u16 = parse_data_u16(buf)?;
|
||||
for elem in ch_u16 {
|
||||
out.push_str(&elem.to_string());
|
||||
@ -151,36 +167,38 @@ pub mod asotr_data {
|
||||
out.remove(out.len() - 1);
|
||||
|
||||
return Ok(out);
|
||||
}
|
||||
else {
|
||||
return Err("unsupported file type! Parser supports data types: .data01.asotr01(02), data02.asotr01(02), data06.asotr01(02)".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fn main() {
|
||||
use crate::asotr_data::*;
|
||||
|
||||
let path = "../data/".to_string();
|
||||
// let fname = "1737354986_202730_data01.asotr02".to_string();
|
||||
let fname = "1737354361_630401_data06.asotr01".to_string();
|
||||
let arguments = Command::new("asotr_csv")
|
||||
.version("0.1.0")
|
||||
.author("Danila Gamkov <danila_gamkov@cosmos.ru>")
|
||||
.about("raw data ASOTR MVN parser")
|
||||
.arg(
|
||||
Arg::new("file")
|
||||
.short('f')
|
||||
.long("filename")
|
||||
.help("input ASOTR MVN data (.data01.asotr01(02), .data02.asotr01(02), .data06.asotr01(02))")
|
||||
.required(true),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let s = match parse_data(path, fname) {
|
||||
Ok(elem) => elem,
|
||||
Err(msg) => {
|
||||
println!("error: {}", msg);
|
||||
return;
|
||||
}
|
||||
let filename: &String = match arguments.get_one::<String>("file") {
|
||||
Some(fname) => fname,
|
||||
_ => { println!("Error: wrong argument!"); return; }
|
||||
};
|
||||
|
||||
let s = match parse_data(filename.clone()) {
|
||||
Ok(elem) => elem,
|
||||
Err(msg) => { println!("{}", msg); return; }
|
||||
};
|
||||
|
||||
println!("{}", s);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user