#!/usr/bin/perl
# $Id: make_iv_flowvar.pl,v 1.5 2015/04/15 10:57:05 sqko Exp $
use AFM;

$VERSION = 0.2;
$PROGRAM = 'make_iv_flowvar.pl';

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

$rigno = $ARGV[0];
&check_run($rigno);

$awklocation  = '/usr/local/bin/awk/';
$perllocation = '/usr/local/bin/';
$GNUPLOT      = '/usr/bin/gnuplot';

my $testno = &AFM::get_test_nr($rigno);
$testno = $ARGV[1] if ($ARGV[1]);

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

#$testlocation = "/home/rig" . $rigno . "/" . $rigno . "test" . $testno;
$htmllocation = $AFM::publicdir.'rig' . $rigno . "/" . $rigno . "test" . $testno;
$ivlocation =  $htmllocation . "/iv/";
if ( !( -d $ivlocation ) ) {
    die "No iV curves found!\n";
}
#$datafile             = $htmllocation . "/jdata";
#$ivcurvetimeslocation = $testlocation . "/ivcurvetimes";
&lock_rig($rigno);
open IN,'<', $ivlocation.'/ivtable' or die "Could not open ivtable for reading!\n";
my @lines = ();
while (<IN>)
{
    chomp;
    push @lines,$_ unless ($_ =~ /ivnr\s+\d+/);
}
close IN;
if (@lines)
{
    open OUT ,'>', $ivlocation.'/ivtable' or die "could not open ivtable for writing!\n";
    for (my $x = 0; $x < @lines; $x++)
    {
	my $y = $x+1;
	$lines[$x] .= ' ivnr '.$y."\n";
	print OUT $lines[$x];
    }
    close OUT;
}
&unlock_rig($rigno);

# make subtables, H_2 and Air variation, manual editing
unless ($replot)
{
    $cstr =
	"awk -f $awklocation"
	. "/sortflowtime.awk  $ivlocation"
	. "/ivtable > "
	. $ivlocation
	. "/ivtable_sort_H_2_constant";
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr =
	"awk -f $awklocation"
	. "/sortflowtime.awk H_2=0  $ivlocation"
	. "ivtable > "
	. $ivlocation
	. "ivtable_sort_Air_constant";
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr = "emacs -nw " . $ivlocation . "ivtable_sort_Air_constant";
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr = "/bin/rm -f ".$ivlocation . 'ivtable_sort_Air_constant~';
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr = "emacs -nw " . $ivlocation . "ivtable_sort_H_2_constant";
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr = "/bin/rm -f ".$ivlocation . 'ivtable_sort_H_2_constant~';
    print $cstr, "\n" if ($debug);
    system($cstr);
}
# pick  data from edited files
    $cstr =
	"for n in \$(awk '{print \$NF}' $ivlocation"
	. "ivtable_sort_H_2_constant); do awk 'NR==n' n=\$n $ivlocation"
	. "ivtable; done > $ivlocation"
	. "ivtable_Air_variation";
    
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr = "cat $ivlocation" . "ivtable_sort_H_2_constant";
    my $const_H2flow = `$cstr`;
    $const_H2flow =~ /H_2\s+(\d+) /;
    $const_H2flow = $1;
    print "Hydrogen flow for air var: $const_H2flow\n" if ($debug);
    
    $cstr =
	"for n in \$(awk '{print \$NF}' $ivlocation"
	. "ivtable_sort_Air_constant); do awk 'NR==n' n=\$n $ivlocation"
	. "ivtable; done > $ivlocation"
	. "ivtable_H_2_variation";
    
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr = "cat $ivlocation" . "ivtable_sort_Air_constant";
    my $const_Airflow = `$cstr`;
    $const_Airflow =~ /Air\s+(\d+)/;
    $const_Airflow = $1;
    print "Air flow for H2 var: $const_Airflow\n" if ($debug);
    
# make plots and parameter files
    $cstr =
	$perllocation
	. "ivplot.pl $rigno H $testno <  $ivlocation"
	. "/ivtable_H_2_variation";
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr =
	$perllocation
	. "ivplot.pl $rigno Air $testno <  $ivlocation"
	. "ivtable_Air_variation";
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr =
	$perllocation . "fd.pl $rigno H $testno | sort >   $ivlocation" . "jd-H";
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr =
	$perllocation . "fd.pl $rigno Air $testno | sort >   $ivlocation" . "jd-Air";
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr =
	"awk '{print \$2,\$0}' $ivlocation"
	. "ivtable_H_2_variation | sort | join - $ivlocation"
	. "jd-H  >  $ivlocation"
	. "ivtable_H_2_variation.par";
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr =
	"awk '{print \$2,\$0}' $ivlocation"
	. "ivtable_Air_variation | sort | join - $ivlocation"
	. "jd-Air  >  $ivlocation"
	. "ivtable_Air_variation.par";
    print $cstr, "\n" if ($debug);
    system($cstr);
    $cstr =
	"$perllocation"
	. "voltage_vs_flow_plot.pl Air $ivlocation"
	. "ivtable_Air_variation.par > $ivlocation"
	. "ivtable_Air_variation.gp";
    
    print $cstr, "\n" if ($debug);
    system($cstr);


$cstr = "$GNUPLOT $ivlocation" . "ivtable_Air_variation.gp";
print $cstr, "\n" if ($debug);
system($cstr) if (-e $ivlocation.'ivtable_Air_variation.gp');
$psfile     = $ivlocation . "ivtable_Air_variation.par.ps";
$outpngfile = $ivlocation . "png/Air_var_H2flow" . $const_H2flow . ".par.png";

$cstr =
"echo \"quit\" | /usr/bin/gs -q -sDEVICE=png256 -sOutputFile=$outpngfile  $psfile > /dev/null";
print $cstr, "\n" if ($debug);
system($cstr);

$cstr =
  "mv $ivlocation"
  . "ivtable_Air_variation.par.ps $ivlocation"
  . "ps/Air_var_H2flow"
  . $const_H2flow . ".ps";
print $cstr, "\n" if ($debug);
system($cstr);
$cstr =
  "$perllocation"
  . "voltage_vs_flow_plot.pl H_2 $ivlocation"
  . "ivtable_H_2_variation.par > $ivlocation"
  . "ivtable_H_2_variation.gp";

print $cstr, "\n" if ($debug);
system($cstr);

$cstr = "$GNUPLOT $ivlocation" . "ivtable_H_2_variation.gp";

print $cstr, "\n" if ($debug);
system($cstr) if (-e $ivlocation.'ivtable_H_2_variation.gp');

$psfile     = $ivlocation . "ivtable_H_2_variation.par.ps";
$outpngfile =
  $ivlocation . "png/H_2_var_Airflow" . $const_Airflow . ".par.png";

$cstr =
"echo \"quit\" | /usr/bin/gs -q -sDEVICE=png256 -sOutputFile=$outpngfile  $psfile > /dev/null";
print $cstr, "\n" if ($debug);
system($cstr);

$cstr =
  "mv $ivlocation"
  . "ivtable_H_2_variation.par.ps $ivlocation"
  . "ps/H_2_var_Airflow"
  . $const_Airflow . ".ps";
print $cstr, "\n" if ($debug);
system($cstr);

$cstr =
  "mv $ivlocation"
  . "ps/iv-Air.eps $ivlocation"
  . "ps/iv-Air-H2flow$const_H2flow" . ".ps";
print $cstr, "\n" if ($debug);
system($cstr);
$cstr =
  "mv $ivlocation"
  . "ps/iv-H_2.eps $ivlocation"
  . "ps/iv-H_2-Airflow$const_Airflow" . ".ps";
print $cstr, "\n" if ($debug);
system($cstr);
