Advertisement
mpettis

Gnuplot-Perl-Code-Generator

Jan 11th, 2012
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.93 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use Data::Dumper;
  4. use Getopt::Long;
  5.  
  6.     #...................................................................
  7.     # Config
  8.     #...................................................................
  9.  
  10. my $input;
  11. my $output;
  12. my $title;
  13. my $ylabel;
  14.  
  15. GetOptions(
  16.     "input=s"   => \$input,
  17.     "output=s"  => \$output,
  18.     "title=s"   => \$title,
  19.     "ylabel=s"  => \$ylabel,
  20. );
  21.  
  22. #print "input is [$input]", "\n";
  23. #print "output is [$output]", "\n";
  24. #print "title is [$title]", "\n";
  25.  
  26.     # Start of the contents for the gnuplot (.gp) file
  27. my $file_gp=<<EOF;
  28. set terminal png size 800 600
  29. set output "$output"
  30. set xtics nomirror rotate -90
  31. set xlabel "DateTime"
  32. set ylabel "$ylabel"
  33. set decimal locale
  34. set format y "%'g"
  35. set title "$title"
  36. set key rmargin
  37. EOF
  38.  
  39. #print $file_gp;
  40.  
  41.  
  42.  
  43.     #...................................................................
  44.     # Read Data from input file, determine number of index sets and their SeriesName
  45.     #...................................................................
  46. my %series;
  47. open my $fh_in, '<', "$input" or die "cannot open $input: $!\n";
  48. while (my $line = <$fh_in>) {
  49.     next unless ($line =~ /\#Series=/);
  50.     my ($series, $idx) = $line =~ /Series=(\S+) Index=(\d+)/;
  51. #    print $line;
  52. #    print "Series is [$series], Index is [$idx]\n";
  53.     $series{$idx} = $series;
  54. }
  55. close $fh_in;
  56.  
  57. #print Dumper(\%series), "\n";
  58.  
  59.  
  60.  
  61.     #...................................................................
  62.     # Create the `plot' statement based on the different series
  63.     #...................................................................
  64. my @series;
  65. foreach my $idx (sort keys %series) {
  66.     my $str = sprintf('"%s" index %s using 3:xtic(2) title "%s" with lines', $input, $idx, $series{$idx});
  67. #    print $str, "\n";
  68.     push @series, $str;
  69. }
  70. #print Dumper(\@series), "\n";
  71.  
  72. $file_gp .= "plot " . join(", \\\n", @series) . "\n";
  73.  
  74. print $file_gp;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement