#!/usr/bin/perl use strict; use Getopt::Long; use File::Basename; use FileHandle; use lib "."; use SAC; my $version = "2009.129"; #my $gnuplot = "gnuplot"; my $gnuplot = "/Applications/Gnuplot.app/Contents/Resources/bin/gnuplot"; my $usage = undef; my $verbose = undef; my $title = undef; my $outfile = undef; my $getoptsret = GetOptions ( 'help|usage|h|?' => \$usage, 'verbose|v+' => \$verbose, 'title|t=s' => \$title, 'outfile|o=s' => \$outfile, ); if ( ! $getoptsret || $#ARGV < 0 || $usage ) { &usage(); exit 1; } # Read each file and put references to SAC objects in @timeseries my @timeseries = (); foreach my $infile ( @ARGV ) { if ( ! -f "$infile" ) { print "Cannot find input file $infile\n"; next; } my $sac = new SAC->new ($infile); push (@timeseries, \$sac) if ( $sac ); } # Open pipe to gnuplot open GP, "|$gnuplot -persist" || die "Cannot open pipe to gnuplot ($gnuplot)\n"; GP->autoflush(1); # Set output to file if specified otherwise to X11 display if ( defined $outfile ) { print GP "set terminal png size 800,400\n"; print GP "set output \"$outfile\"\n"; } else { # Redefine STDIN as the terminal ouput, since gnuplot inherits # the STDIN filehandler from his parent, the perl script. open (STDIN, "print; my $srcname = $$sacref->sourceName; my $stime = $$sacref->startTimeDepoch; print "Gathering plot data for $srcname\n" if ( $verbose > 0 ); $plotcmd .= "'-' using (\$0*$$sacref->{delta}+$stime):1 title '$srcname' with lines,"; foreach my $sample ( @{$$sacref->{datasamples}} ) { $plotdata .= "$sample\n"; } #my $stime = $$sacref->startTimeDepoch; #foreach my $sample ( @{$$sacref->{datasamples}} ) { # $plotdata .= "$stime $sample\n"; # $stime += $$sacref->{delta}; #} $plotdata .= "e\n"; } # Replace last comma with semi-colon in plot command $plotcmd =~ s/,$/\n/; # Add plot data to plot command $plotcmd .= $plotdata; print "Sending plot data to gnuplot\n" if ( $verbose > 0 ); print GP "$plotcmd;"; #print "PLOTCMD:\n'$plotcmd'\n"; # If not priting to an output file pause after drawing to X11 display #if ( ! defined $outfile ) { # print GP "pause -1\n"; #} close GP; ## End of main sub usage { my $basename = basename $0; print STDERR "$basename version $version\n\n"; print STDERR "Plot time-series data in SAC format using gnuplot\n\n"; print STDERR "Usage: $basename [options] [inputfile]\n\n"; print STDERR " -h -? --help Print this help message\n"; print STDERR " -v --verbose Be verbose\n"; print STDERR "\n"; print STDERR "If no input file is specified, standard input is opened for, um, input.\n"; print STDERR "\n"; }