#!/usr/bin/perl
# $Id: jdata_plot.pl,v 1.2 2016/03/10 15:08:40 sqko Exp $

use strict;
use warnings;
use AFM;
my $GNUPLOT = '/usr/local/bin/gnuplot';


my $VERSION = 0.01;
my $PROGRAM = 'jdata_plot.pl';
for my $string (@ARGV)
{
    if ($string =~ /--version/)
    {
        print "$PROGRAM is: $VERSION\n";
        exit;
    }
}

if ( scalar(@ARGV) < 3 )
{
    print "\n\n     Program for plotting jdata files\n\n";
    print "\n";
    print 'Usage: '."$PROGRAM \$rig \$test \"xvalues\" [\"yvalues\"] . . . [optional --xrange \$xrange  --yrange \$yrange] [optional --debug  --BV  --points],\n".'where $rig is the rig number';
    print ' $test is the test number'."\n";
    print "The \"xvalue\" and \"yvalues\" is the colum names of the values to be plotted.\n";
    print "The --BV option is used if the plot must be black and white (color otherwise).\n";
    print "The --points option is used for plots with points (default is lines).\n";
    print "The --debug option is used to print debug information (and the gnuplot file) to standard out. This is usefull if more controll of the plot is required than the default options allow.\n";
    print "Use the --xrange and --yrange options to controll the scaling.\n";
    print "The syntax for these options are: '--xrange 100:300' which would plot data from 100 to 300.\n\n\n";
    exit;
}
my $debug = 0;
my $bv = 0;
my $points = 0;

my $maxl = scalar(@ARGV) -1;
if ($ARGV[$maxl] =~ /--debug/)
{
    $debug = 1;
    pop @ARGV;
}
$maxl = scalar(@ARGV) -1;
if ($ARGV[$maxl] =~ /--BV/)
{
    $bv = 1;
    pop @ARGV;
}
$maxl = scalar(@ARGV) -1;
if ($ARGV[$maxl] =~ /--points/)
{
    $points = 1;
    pop @ARGV;
}
$maxl = scalar(@ARGV) -1;
if ($ARGV[$maxl] =~ /--BV/)
{
    $bv = 1;
    pop @ARGV;
}
$maxl = scalar(@ARGV) -1;
if ($ARGV[$maxl] =~ /--debug/)
{
    $debug = 1;
    pop @ARGV;
}


my $rig = $ARGV[0];
&check_run($rig);
my $testno = 0;
if (scalar(@ARGV) > 1)
{
    my $string = $ARGV[1];
    $string =~ /(\d+)/;
    my $ttest = $1;
    if (defined($ttest))
    {
        $testno = $ttest+0;
    }
}
my $xvaluename = $ARGV[2];
my @source_name= ();
for (my $x = 3; $x < scalar(@ARGV); $x++)
{
    push @source_name, $ARGV[$x] unless (($ARGV[$x] =~ /^--/) || ($ARGV[$x-1] =~ /^--/));
}

my $xrange = undef;
my $yrange = undef;
for (my $x = 3; $x < scalar(@ARGV)-1; $x++)
{
    if ($ARGV[$x] =~ /--xrange/i)
    {
	$xrange = $ARGV[$x+1];
    }
    if ($ARGV[$x] =~ /--yrange/i)
    {
	$yrange = $ARGV[$x+1];
    }
}

#### Indsaet conversion af jdata fil ####
my $dir = '/home/http/html/rig'.$rig.'/'.$rig.'test'.$testno.'/';
my $jfile = $dir .'jdata';
my $headerstr = `/usr/bin/head -1 $jfile`;
my @list = split(/\s+/,$headerstr);

#### Finding collum-id for source ###
my @sourceid = ();
my $xvalues = 0;
my $timeid = 0;
my $maxcol = scalar(@list);
for (my $x = 0; $x < $maxcol; $x++)
{ 
    if ($list[$x] eq $xvaluename)
    {
	$xvalues = $x+2;
    }
    for (my $idcount = 0; $idcount < scalar(@source_name); $idcount++)
    {
	if ($list[$x] eq $source_name[$idcount])
	{
	    $sourceid[$idcount] = $x+2;
	}
    }
}
if ($xvaluename eq 'time')
{
    $xvalues = 1;
}


my $gpfile = '/tmp/'.$$.'plot.gp';

my $pspoints  = "lines";
if ($points){$pspoints  = "points pointsize 0.25"};

my $homedir =  $AFM::publicdir . 'rig' . $rig . "/" . $rig . "test" . $testno  . "/";
my $psdir  = $homedir . "ps";
my $pngdir = $homedir . "png";
if ( !( -d $psdir ) )  { system("mkdir $psdir") };
if ( !( -d $pngdir ) ) { system("mkdir $pngdir") };
my $outputname = '';
for my $str (@source_name)
{
    $outputname = $outputname.$str;
}
$outputname = $outputname.'_vs_'.$xvaluename.'.ps';
my $term = &AFM::get_config_global( "GNUPLOT", 'postportterm' );
if ($bv){$term =~ s/color//;}
open GP, "> $gpfile" or die "could not open $gpfile\n";
print GP "set term ", $term, "\n";
print GP "set size ". &AFM::get_config_global( "GNUPLOT", 'postportsize' )."\n";
print GP "set out '".$psdir.'/'.$outputname."'\n";
$xvaluename =~ s/_/ /g;
print GP "set xla '$xvaluename'\n";
if (defined($xrange))
{
    print GP 'set xr ['.$xrange.']'."\n";
}
if (defined($yrange))
{
    print GP 'set yr ['.$yrange.']'."\n";
}
print GP "plot ";
for (my $x = 0; $x < scalar(@sourceid); $x++)
{
    my $tit = $source_name[$x];
    $tit =~ s/_/ /g;
    print GP "'$jfile' u $xvalues:$sourceid[$x] tit '$tit' w $pspoints";
    print GP ',' unless ($x > scalar(@sourceid)-2);
}
print GP "\n";

close GP;
if($debug)
{
    my @l = `/bin/cat $gpfile`;
    print @l;
}
my $cmd = $GNUPLOT.' '.$gpfile;
system($cmd);
unlink $gpfile;
