#!/usr/bin/env perl # # plot-matrix - generate plots for 3-column CSV files (x, y, value) # # $Id$ # should share code with ./plot-metrics use warnings; use strict; use Getopt::Std; use File::Spec::Functions qw(abs2rel rel2abs splitpath catpath); use File::Path qw(mkpath); my $mydir; BEGIN { ($mydir) = ( $0 =~ m#(.*)[/\\]# ) or $mydir = '.'; } use lib "$mydir/../share"; use Squimp::R; my %opt; getopts( 'h?Hd:s:t:v:x:y:T:', \%opt ); $ENV{PATH} = '/cygdrive/c/ProgramFiles/R/R-2.11.1/bin:' . $ENV{PATH}; my $scaler = $opt{s} // ''; my $sep = $opt{t} // ','; my $imgext = $opt{T} // 'png'; my %ext2type = ( 'bmp' => 'bmp', 'fig' => 'xfig', 'jpg' => 'jpeg', 'pdf' => 'pdf', 'png' => 'png', 'ps' => 'postscript', 'wmf' => 'win.metafile' ); my $exts = join( ', ', sort keys %ext2type ); grep { /^[h?]/ } keys %opt and HELP_MESSAGE(); my $imgdevtype = $ext2type{$imgext} or die "invalid output filetype $imgext, must be one of: $exts\n"; if ( !@ARGV ) { @ARGV = ('-'); } else { @ARGV = map { if ( $_ ne '-' && ( !-e $_ || !-r $_ || -d $_ ) ) { warn "nonexistent or unusable, skipped: $_\n"; } else { $_; } } @ARGV; } die "no valid arguments left\n" if !@ARGV; @ARGV = map { $_ eq '-' ? '/dev/stdin' : $_ } @ARGV; my $r = new Squimp::R; $r->x( read_r_scripts("$mydir/../share/plot-matrix.R") ); foreach my $f (@ARGV) { $r->x( r_code_to_plot($f) ); } exit(0); sub HELP_MESSAGE { print < ); close($fh) or die "fatal error: cannot close R script, $f: $!\n"; } }; join( $/, @lines ); } sub r_code_to_plot { my ($f) = @_; my ($indir) = parent_dir($f); my ($outdir) = abs2rel( rel2abs( $opt{d} // '.', $indir ), '.' ); if ( !-d ($outdir) && !mkpath($outdir) ) { warn "cannot create output directory $outdir, skipping $f\n"; return -1; } my $g = do { my $finod = abs2rel( $f, $indir ); my $finod_base = $finod =~ /^(.*)\./ ? $1 : $finod; rel2abs( $finod_base, $outdir ); }; chomp( $f = `cygpath -w "$f"` ); $f =~ s#\\#\\\\#g; chomp( $g = `cygpath -w "$g"` ); $g =~ s#\\#\\\\#g; my $outfn_tmpl = sprintf( '%s-%s-at-%s-x-%s.%s', $g, '%v', '%x', '%y', $imgext ); warn "outfn_tmpl is $outfn_tmpl\n"; my $header = $opt{H} ? 'header=TRUE' : 'header=FALSE,col.names=c("x","y","v")'; <