Manage gnuplot with IPC::Open3, so we allow fancy output filenames, with spaces!

This commit is contained in:
Timothy Allen 2017-12-17 11:06:14 +02:00
parent a43b67fe7e
commit 410b1363b6
1 changed files with 41 additions and 12 deletions

View File

@ -3,6 +3,7 @@
use strict;
use warnings;
use Getopt::Long;
use IPC::Open3;
use Time::Piece;
my $error = "Usage: $0 --input <CSV file> --output <output PDF> [--max <n.n>] [--low <n.n>] [--high <n.n>]\n";
@ -27,19 +28,17 @@ GetOptions ("input=s" => \$input, # The name of the CSV file from whic
"low:f" => \$min_glucose, # The low end of your target blood glucose level
"max:i" => \$graph_max, # The highest displayed glucose level on each graph
"graphs:i" => \$days_per_page) # The number of days printed on each page
or die $error;
or die $error;
die $error unless ( defined $input and defined $output );
open( my $fh, '<:encoding(UTF-8)', $input )
open( my $ifh, '<:encoding(UTF-8)', $input )
or die "Could not open file '$input' $!";
while ( my $row = <$fh> ) {
while ( my $row = <$ifh> ) {
chomp( $row );
push @lines, $row;
}
close( $fh )
close( $ifh )
or warn "close failed: $!";
# Set up basic gnuplot options for reading the CSV data
@ -98,7 +97,7 @@ reset
set datafile separator whitespace
set terminal pdf size 29.7cm,21.0cm enhanced font 'Calibri,14' linewidth 1
set output '$output'
#set output '$output'
set key off
set style data lines
set xdata time
@ -134,7 +133,7 @@ foreach my $d ( sort keys %seen ) {
push @data, qq(
set title "Daily Glucose Summary for $title" font "Calibri,18"
set xlabel "Time" offset 0,1
set xlabel "Time" offset 0,-0.25
set ylabel "Blood glucose"
set xtics left tc rgb "#000000"
set ytics 2 tc rgb "#000000"
@ -152,7 +151,7 @@ undefine \$SmoothData$label
# Add an x grid
set multiplot previous
set title " "
set xlabel " " offset 0,1
set xlabel " " offset 0,-0.25
set ylabel " "
set xtics tc rgb "#ffffff00"
set ytics tc rgb "#ffffff00"
@ -173,9 +172,39 @@ push @data, qq(
unset multiplot
);
# output the gnuplot data
# run the data through gnuplot
$gnuplot_data = join "\n", @data;
print $gnuplot_data;
#gnuplot(@data);
open( my $ofh, '>', $output )
or die "Could not open file '$output' $!";
my ( $pid, $stdin, $stdout, $stderr );
use Symbol 'gensym';
$stderr = gensym;
$pid = open3( $stdin, $stdout, $stderr, 'gnuplot' );
print $stdin $gnuplot_data;
close( $stdin );
while ( <$stdout> ) {
print $ofh "$_";
}
while ( <$stderr> ) {
warn $_;
}
close($stdout);
close($stderr);
waitpid( $pid, 0 );
my $child_exit_status = $? >> 8;
close( $ofh )
or warn "close failed: $!";
#open (GNUPLOT, "|gnuplot");
#print GNUPLOT $gnuplot_data;
# vim: set expandtab shiftwidth=4 softtabstop=4 :