summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornatrixaeria <upezu@student.kit.edu>2019-12-13 03:29:00 +0100
committernatrixaeria <upezu@student.kit.edu>2019-12-13 03:29:00 +0100
commitc7f0faf6752de3655f2b9ac866c9cfb39f0ffb8f (patch)
tree785ff002495189e244c018e4ae84cd815a9b668f
parent1daf4f35a5dd2f500d95541797661c518f4e2713 (diff)
Tidy gnuplot interface
-rw-r--r--src/plot/gnuplot.rs91
1 files changed, 47 insertions, 44 deletions
diff --git a/src/plot/gnuplot.rs b/src/plot/gnuplot.rs
index ea52a18..8bb5ac6 100644
--- a/src/plot/gnuplot.rs
+++ b/src/plot/gnuplot.rs
@@ -1,11 +1,36 @@
-use std::io;
-use std::io::Write;
+use std::io::{self, Write};
use std::path::PathBuf;
-use std::process;
-use std::process::{Command, Stdio};
+use std::process::{self, Command, Stdio};
pub struct Gnuplot;
+const GNUPLOT_COMMAND: &str = "gnuplot";
+const OUTPUT_FILE: &str = "data";
+
+fn create_gnuplot_process(path: &str, chunk_size: u32, start: usize, end: usize)
+ -> Result<process::Child, io::Error> {
+ Command::new(GNUPLOT_COMMAND)
+ .args(&[
+ "-e",
+ format!("states={}; start={}; end={}", chunk_size, start, end).as_ref(),
+ "-c",
+ path,
+ ])
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .spawn()
+}
+
+fn write_data_file(path: &str, data: &[[f64; 6]]) -> Result<(), io::Error> {
+ let mut file = std::fs::File::create(OUTPUT_FILE)?;
+ for line in data {
+ file.write_fmt(format_args!(
+ "{}; {}; {}; {}; {}; {}\n",
+ line[0], line[1], line[2], line[3], line[4], line[5]
+ ))?;
+ }
+}
+
impl Gnuplot {
fn exec_plot(
data: &[[f64; 6]],
@@ -14,59 +39,37 @@ impl Gnuplot {
end: usize,
script: &str,
) -> Result<process::Child, io::Error> {
- let mut path = PathBuf::from("gnuplot");
- path.push(script);
- let mut file = std::fs::File::create("data").expect("could not create data file");
- let process = Command::new("gnuplot")
- .args(&[
- "-e",
- format!("states={}; start={}; end={}", chunk_size, start, end).as_ref(),
- "-c",
- path.to_str().unwrap_or(script),
- ])
- .stdin(process::Stdio::piped())
- .stdout(Stdio::piped())
- .spawn()
- .expect("Failed to spawn child process");
+ let path: PathBuf = ["gnuplot/", script].iter().collect();
+ let path = path.to_str().unwrap_or(script);
- //let stdin = process.stdin.as_mut().expect("Failed to open stdin");
- for line in data {
- let line = format!(
- "{}; {}; {}; {}; {}; {}\n",
- line[0], line[1], line[2], line[3], line[4], line[5]
- );
- file.write_all(line.as_bytes())
- .expect("Failed to write to stdin");
- }
- Ok(process)
+ let process = create_gnuplot_process(path, chunk_size, start, end)?;
+ write_data_file(path, data).map(|_| process)
+ }
+}
+
+fn plot_with(data: &[[f64; 6]], chunk_size: u32, start: usize, end: usize, name: &str) {
+ match Gnuplot::exec_plot(data, chunk_size, start, end, name) {
+ Ok(mut process) => println!("opening gnuplot {:?}", process.wait()),
+ Err(err) => println!("failed to start gnuplot: {}", err),
}
}
impl super::Graph for Gnuplot {
fn plot3d(data: &[[f64; 6]], chunk_size: u32) {
let iterations = data.len() / chunk_size as usize;
- match Gnuplot::exec_plot(data, chunk_size, 1, iterations, "anim3d.plt") {
- Ok(mut process) => println!("opening gnuplot {:?}", process.wait()),
- Err(err) => println!("failed to start gnuplot: {}", err),
- }
+ plot_with(data, chunk_size, 1, iterations, "anim3d.plt");
}
+
fn animate3d(data: &[[f64; 6]], chunk_size: u32, start: usize, end: usize) {
- match Gnuplot::exec_plot(data, chunk_size, start, end, "anim3d.plt") {
- Ok(mut process) => println!("opening gnuplot {:?}", process.wait()),
- Err(err) => println!("failed to start gnuplot: {}", err),
- }
+ plot_with(data, chunk_size, start, end, "anim3d.plt")
}
+
fn plot2d(data: &[[f64; 6]], chunk_size: u32) {
let iterations = data.len() / chunk_size as usize;
- match Gnuplot::exec_plot(data, chunk_size, 1, iterations, "anim2d.plt") {
- Ok(mut process) => println!("opening gnuplot {:?}", process.wait()),
- Err(err) => println!("failed to start gnuplot: {}", err),
- }
+ plot_with(data, chunk_size, 1, iterations, "anim2d.plt")
}
+
fn animate2d(data: &[[f64; 6]], chunk_size: u32, start: usize, end: usize) {
- match Gnuplot::exec_plot(data, chunk_size, start, end, "anim2d.plt") {
- Ok(mut process) => println!("opening gnuplot {:?}", process.wait()),
- Err(err) => println!("failed to start gnuplot: {}", err),
- }
+ plot_with(data, chunk_size, start, end, "anim2d.plt")
}
}