Files
iris_format/rust/iris_rust/src/lib.rs

141 lines
5.0 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

pub mod structures;
pub mod read;
pub mod write;
pub mod examples;
use std::vec;
use serde::{Deserialize, Serialize};
use serde_json::json;
pub use structures::{TimeStruct, SpectralData, ImageInfo, OneIRISData};
pub use read::{read_time, read_spectral_data, read_image_info, read_iris_file};
pub use write::*;
pub use examples::{spectral_data_roundtrip};
pub fn explortirisdatatojson(path:&String)->(){
let irisdata=read_iris_file(path).unwrap();
// let irisjson=serde_json::to_string(&irisdata).unwrap(); 转字符串是用两个空格
let irisjson = serde_json::to_string_pretty(&irisdata).unwrap();
//将irisjson写入到path中 后缀iris改成.json
let json_path = path.replace(".iris", ".json");
std::fs::write(json_path, irisjson).expect("Unable to write JSON file");
println!("IRIS data exported to JSON successfully!");
}
pub fn explortirisdatatocsv(path:&String)->(){
let irisdata=read_iris_file(path).unwrap();
//将irisdata转换为csv格式
let mut csv_data = String::new();
let mut envermentinfo=serde_json::json!({});
let mut devinfo=Vec::new();
for spectral_info in &irisdata.spectral_info_section {
if spectral_info["info_type"] == "environment" {
envermentinfo = spectral_info.clone();
} else {
devinfo.push(spectral_info.clone());
}
}
// 添加环境信息 第一行
csv_data.push_str("EnvironmentalContext");
//获取环境信息的所有键和值
for (key, value) in envermentinfo.as_object().unwrap() {
if key == "info_type" {
continue; // 跳过info_type
}
if key == "date"{
//将 yyyy-mm-dd hh:mm:ss 转换为 yyyy_mm_dd hh:mm:ss
let date_str = value.as_str().unwrap_or("");
let formatted_date = date_str.replace("-", "_");
csv_data.push_str(&format!(",{},{}", key, formatted_date));
continue; // 跳过date
}
//如果value是object类型则将其转换为字符串
if value.is_object() {
for (key1,value1) in value.as_object().unwrap() {
csv_data.push_str(&format!(",{}", key1));
csv_data.push_str(&format!(",{}", value1));
}
continue; // 跳过object类型的value
}
csv_data.push_str(&format!(",{},{}", key, value));
}
csv_data.push_str("\n");
// 添加设备信息
let mut indexofdevinfo = 1;
for info in devinfo {
csv_data.push_str(&format!("FS{}_Info", indexofdevinfo));
indexofdevinfo += 1;
//获取设备信息的所有键和值
for (key, value) in info.as_object().unwrap() {
if key == "info_type" {
continue; // 跳过info_type
}
if key=="wave_coeff" {
continue; // 跳过wave_coeff
}
if value.is_object() {
// 如果value是object类型则将其转换为字符串
for (key1, value1) in value.as_object().unwrap() {
csv_data.push_str(&format!(",{}", key1));
csv_data.push_str(&format!(",{}", value1));
}
continue; // 跳过object类型的value
}
csv_data.push_str(&format!(",{},{}", key, value));
}
csv_data.push_str("\n");
let wavecoeff1=info["wave_coeff"]["a1"].as_f64().unwrap_or(0.0);
let wavecoeff2=info["wave_coeff"]["a2"].as_f64().unwrap_or(0.0);
let wavecoeff3=info["wave_coeff"]["a3"].as_f64().unwrap_or(0.0);
let wavecoeff4=info["wave_coeff"]["a4"].as_f64().unwrap_or(0.0);
let bandnum= info["bandnum"].as_u64().unwrap_or(0);
let wavelist: Vec<f64> = (0..bandnum)
.map(|i| wavecoeff4 + wavecoeff3 * (i as f64) + wavecoeff2 * (i as f64).powi(2) + wavecoeff1 * (i as f64).powi(3))
.collect();
// 添加波长信息
csv_data.push_str(&format!("Wavelength"));
for (i, wave) in wavelist.iter().enumerate() {
csv_data.push_str(&format!(",{}" ,wave));
}
csv_data.push_str("\n");
}
// 添加光谱数据
csv_data.push_str("Data Section\n");
for SpectralData in &irisdata.spectral_data_section {
csv_data.push_str(&format!("{}_P{}", SpectralData.sensor_id,SpectralData.fiber_id));
if SpectralData.valid_flag== 1 {
csv_data.push_str(",Valid");
} else {
csv_data.push_str(",Invalid");
}
csv_data.push_str(&format!(",{}", SpectralData.exposure));
let vector_size = SpectralData.Get_Spectral_Data();
for value in &vector_size {
csv_data.push_str(&format!(",{}", value));
}
csv_data.push_str("\n");
}
//将csv_data写入到path中 后缀iris改成.csv
let csv_path = path.replace(".iris", ".csv");
std::fs::write(csv_path, csv_data).expect("Unable to write CSV file");
println!("IRIS data exported to CSV successfully!");
}