#!/usr/bin/perl
# $Id: history-plot-electrode-1.0.pl,v 1.1 2011/06/14 11:47:10 sofc Exp $

use AFM;

if ( scalar(@ARGV) < 1 ) {
    print 'Use: history-plot.pl $rig [optional: $testno --version --debug], where $rig is the rig number' , "\n";
    print '$testno is the test number if an other test than the current is to be processed', "\n";
    exit 1;
}
my $VERSION = 0.3;
my $PROGRAM = 'history-plot.pl';
my $debug = 0;
if ( scalar(@ARGV) > 0 ) {
    for ( my $x = 0 ; $x < scalar(@ARGV) ; $x++ ) {
        if ( $ARGV[$x] =~ /--version/ ) {
            print "$PROGRAM is: $VERSION\n";
            exit 0;
        }
	if ( $ARGV[$x] =~ /--debug/)
	{
	    $debug = 1;
	}
    }
}



$GNUPLOT = '/usr/bin/gnuplot';

sub printgp {
    return unless ($plotcolumn);
    push(@psfiles, $filename);
    if ($plotcolumn)
    {
	print GRAPH <<END;

reset
set noytics
set xla "$xla"
#set bmargin 3
set nokey
set title \'$title\'
set yr$yrange
set ytics $ytics
    f(x)=$fx;
set yla '$ylabel'
set term post portrait enhanced color
set size $pssize
set out '$pslocation/$filename.eps'
plot '$plotfile' u 1:(f(\$$plotcolumn)) w $pspoints
END
    }
}

#open( GRAPH, "| $GNUPLOT 2> /dev/null" ) || die;



$pngpoints = "points pointtype 15 pointsize 0.25";
$pspoints  = "points pointtype 15 pointsize 0.25";

$xla     = "Hours";
$xcolumn = 1;
$pngsize = 0.7;

# 0.5;
$pssize = "1,0.5";
$rigno  = $ARGV[0];
if ( !&AFM::check_rig_nr($rigno) ) {
    print "Rig $rigno is not valid!\n";
    exit 1;
}

$voltagerange = &AFM::get_usercv($rigno,'userinterface','pO2_voltagerange');
$voltagerange='-100,1400' unless ($voltagerange);
my ($vmin,$vmax) = split(/\s*\,\s*/,$voltagerange);

my @ticarr = ();
for (my $x = $vmin; $x <= 0; $x+= 200)
{
    push @ticarr, '"'.-$x.'" '.$x;
}
push @ticarr, '"0" 0';
my $xmin = (@ticarr-1)*200+$vmin;
for (my $x = $xmin;$x <= $vmax; $x+=200)
{
    push @ticarr, '"'.-$x.'" '.$x;
}
my $yticstr = '('.join(',',@ticarr).')';

$awklocation = '/usr/local/bin/awk/';
my $testno = 0;
if ( scalar(@ARGV) > 1 ) {
    $ttestno = $ARGV[1];
    $ttestno =~ /(\d+)/;
    $testno = $1;
}
else {
    $testno = &AFM::get_test_nr($rigno);
}
$htmllocation =
  "/home/http/html/rig" . $rigno . "/" . $rigno . "test" . $testno;
$htmlfile = $htmllocation."/history.html";
$plotfile = $htmllocation . "/jdata";
if ( !( -e ($plotfile) ) ) {
    print "$plotfile not found!!\n";
    exit 1;
}
$gpfile=$htmllocation."/history.gp";
open( GRAPH, "> $gpfile" ) || die "Could not open $gpfile\n";
$line = `head -1 $plotfile`;
@Fld  = split ( ' ', $line, 9999 );

for ( $i = 0 ; $i < $#Fld ; $i += 2 ) {
    $name{$Fld[$i] } = $Fld[ $i ];
    $data{ $Fld[$i] } = $Fld[ $i + 1 ];
    $col{ $Fld[$i] }  = $i + 2

}

$area = &AFM::get_usercv_test($rigno,$testno,'main','cell_area');
$area = &AFM::get_config_value_old( $rigno, 'area' ,$testno) unless ($area);
$area = 1 unless ($area);

$fx = "(abs(x)<2000?x:1/0)";

$pnglocation = $htmllocation . "/png";

$pslocation  = $htmllocation . "/ps";
$cstr="mkdir $pslocation";
print $cstr,"\n" if ($debug);
system($cstr) if (!-e($pslocation));
$cstr="mkdir $pnglocation";
print $cstr,"\n" if ($debug);
system($cstr) if (!-e($pnglocation));


$pngtype = "png";


$filename   = "bottle_temp";
$ylabel     = "Temperature, ^oC";
$title      = "Bottle Temperature";
$yrange     = "[0:50]";
$ytics      = " 0,10,50";
$plotcolumn = $col{$filename};

printgp();

$filename   = "bottle_temp_1";
$ylabel     = "Temperature, ^oC";
$title      = "Bottle Temperature 1";
$yrange     = "[0:50]";
$ytics      = " 0,10,50";
$plotcolumn = $col{$filename};

printgp();

$filename   = "bottle_temp_2";
$ylabel     = "Temperature, ^oC";
$title      = "Bottle Temperature 2";
$yrange     = "[0:50]";
$ytics      = " 0,10,50";
$plotcolumn = $col{$filename};

printgp();

$filename   = "T_furnace";
$title      = "T furnace";
$yrange     = "[0:1100]";
$ytics      = "0,200,1000";
$plotcolumn = $col{$filename};

printgp();

$filename = "T_chamber";
$title    = "T chamber";

$plotcolumn = $col{$filename};

printgp();

$filename = "T_pO2";
$title    = "T pO2";

$plotcolumn = $col{$filename};

printgp();

$filename = "T_flange";
$title    = "T flange";
$yrange     = "[0:200]";
$ytics      = "0,20,200";

$plotcolumn = $col{$filename};
printgp();

$filename = "T_rig";
$title    = "T rig";
$yrange     = "[0:100]";
$ytics      = "0,10,100";

$plotcolumn = $col{$filename};
printgp();

$filename = "T_box";
$title    = "T box";
$yrange     = "[0:100]";
$ytics      = "0,10,100";

$plotcolumn = $col{$filename};
printgp();

$filename = "pO2";
$title    = "pO_2";
$ylabel     = "Potential, mV";
$fx       = "((abs(x)<2000?-x:1/0))";
$yrange   = "[$vmin:$vmax]";
$ytics = $yticstr;

$plotcolumn = $col{$filename};
printgp();

close(GRAPH);
$cstr="$GNUPLOT $gpfile";
system($cstr);

open(HTML,"> $htmlfile") or die "Could not open $htmlfile";
print $htmlfile,"\n" if ($debug);

print HTML <<END;
<html>
<head>
<title>Plot</title>
</head>
<body>
<table>
<tbody>
<tr>
END


    $ncol=2;
for ($i=0;$i<=$#psfiles;$i++) { 
        if ( ( ( $i - 2 ) % ($ncol) == 0 ) ) { print HTML "</tr>\n<tr>" };
        print HTML "<td><img src=\"png/$psfiles[$i].png\" alt=\"image\"></a></td>\n";

$cstr =
  "/usr/local/bin/ps2png  $pslocation"."/".$psfiles[$i].".eps $pnglocation"."/".$psfiles[$i].".png";
print $cstr,"\n" if ($debug);
system($cstr);

    }
print HTML <<END;
</tr></table>
</body>
</html>
END

close(HTML);
