Re: [GENERAL] Creating web images from postgres data - Mailing list pgsql-general

From Michael A. Koerber SR
Subject Re: [GENERAL] Creating web images from postgres data
Date
Msg-id 199810302143.QAA04546@ll.mit.edu
Whole thread Raw
In response to Re: [GENERAL] Creating web images from postgres data  (Egon Schmid <eschmid@stuttgart.netsurf.de>)
List pgsql-general
Opps...

#!/usr/bin/perl -w

# $Source: /home/mak/CVS/misc_util/pp,v $
# $Author: mak $
# $Date: 1998/08/12 19:01:55 $
# $Revision: 1.5 $

# ----------------------------------------------------------------------
# Parse command line options
use Getopt::Std;
%opts = ();
getopts('t:x:y:l:f:q',\%opts);

# If the 'l' (legend) option exists, parse it based on the ':' character
@legend = split(':',$opts{l}) if exists($opts{l});

if (exists($opts{f})) {
    $out_base = $opts{f};
}
else {
    $out_base = 'gnuplot';
}

# ----------------------------------------------------------------------
# Begin parsing input
while (<>) {

    # --------------------------------------------------
    # strip out | characters
    s/\|/ /g;

    # --------------------------------------------------
    # work only on strings which are purely numeric.  This is a bit tricky
    # but we allow the columns of numbers to contain unit specifier and
    # just suck out the numeric data
    @line = ();
    while (m/\G[^-\d\s]*\s*([-+]*\d+\.?\d*[-+eE]*\d*)[\s\n]*[^-\d\s]*/g) {
push @line, $1;}

    if (@line) {
    $cols = @line unless $cols;

    # Testing line
    # print join(' : ',@line), "\n";

    # Open an output file for pairs of columns ($line[0,1],
$line[0,2], etc)

    unless (@filenames) {
        # This will only happen the first time through
        @filenames = map { "A$_" } (1..$#line);

        foreach $fn (@filenames) {
        $cmd = "open $fn, \">\$fn\" or die \"Unable to open file
\$fn\\n\"";
        eval($cmd);
        }
    }

    # Begin output of this line of data
    if (@line == $cols) {
        # Only output if the number of lines is consistant with the
first valid row
        @cmds = map { "$filenames[$_] \"$line[0] $line[$_+1] \n\"" }
(0..$#filenames);
        foreach (@cmds)  {
        # print "$_\n";
        eval("print $_ \n");
        }
    }
    else {
        warn "Number of columns changed.  Trashing this line:\n";
        warn "$_";
    }

    }
}

# ----------------------------------------------------------------------
# Close all the files
foreach $fn (@filenames) {
    $cmd = "close $fn";
    eval($cmd);
}

# ----------------------------------------------------------------------
# set up the gnuplot command file

$cmd = "plot ";
foreach (@filenames) {
    if (@legend) {
    $this_legend = shift @legend;
    $cmd .= qq('$_' using 1:2 title '$this_legend' with linespoints,
);

    }
    else {
    $cmd .= "'$_' using 1:2 with linespoints, ";
    }
}
chop $cmd;
chop $cmd;

open( OUT, ">$out_base.cmd") or die "Unable to open $out_base.cmd\n";


push @cmd_opts, qq(set title '$opts{"t"}') if exists($opts{"t"});
push @cmd_opts, qq(set xlabel '$opts{"x"}') if exists($opts{"x"});
push @cmd_opts, qq(set ylabel '$opts{"y"}') if exists($opts{"y"});
$cmd_list = join(';',@cmd_opts);


if (exists $opts{'q'}) {
    # Then quiet mode was selected.  Only output to a postscript file
print OUT <<END;
set terminal postscript landscape color
set output \'$out_base.ps\'
set grid
$cmd_list
$cmd

END

close OUT;
}
else {
    # Output to screen and to postscript file
print OUT <<END;
set grid
$cmd_list
$cmd
pause 5

set terminal postscript landscape color
set output \'$out_base.ps\'
replot

END

close OUT;
}

system(qq(gnuplot $out_base.cmd));

print "See $out_base.ps in current directory for last plot\n";



__END__

=head1 NAME

pp - Postgres Plotting Script

=head1 SYNOPSIS

C<pp [-t -x -y -l -f -q]>

=head1 DESCRIPTION

This script will take the tabular output of a Postgres Query and generate
a B<gnuplot> command file (F<gnuplot.cmd>) which is then used to generate
a
plot of the data.

The follow command line options are available

=over 4

=item B<-t title> places a title on the plot

=item B<-x xlabel> places a label on the x-axis

=item B<-y ylabel> places a label on the y-axis

=item B<-l legend_1:legend_2:...> places legends on the plot.  Note the
use
of the ':' for delimiting legends.  Extra legend will be ignored.  If
there are
not enough legends, the ones specified will be used.  The remaining
legends are
then F<gnuplot> defaults.

=item B<-f out_file_base_name> creates output files
F<out_file_base_name.cmd> and
F<out_file_base_name.ps>.  By default, F<gnuplot.[cmd|ps]> are used.

=item B<-q> is quiet mode.  If specified, then the postscript files are
generated
but no screen output is shown.  If not specified, then a screen output is
displayed
for 5 seconds, then the postscript file written.

=back


=head1 NOTES

$Source: /home/mak/CVS/misc_util/pp,v $
$Author: mak $
$Date: 1998/08/12 19:01:55 $
$Revision: 1.5 $



Dr Michael A. Koerber
MIT/LL
Radar Imaging Techniques


pgsql-general by date:

Previous
From: Christophe Pettus
Date:
Subject: Expensive query
Next
From: Dustin Sallings
Date:
Subject: Re: [GENERAL] Expensive query