#!/usr/bin/perl -wT
# $Id: make_iv_curves.pl,v 1.10 2024/01/04 09:56:11 sqko Exp $
use strict;
use warnings;
use AFM;
my $recalculate = 0;

if (!@ARGV)
{
    print "Usage: make_iv_curves.pl \$rig \$test [opt. \$predominant_current, default 1A/cm\^2]\n";
    print "If the test number is also omitted, the program uses the curent test as default\n";
    exit;
}

### Extract switches (arguments beginning with -- )###
my @argvs = ();
foreach (@ARGV)
{
    unless (/\-\-/)
    {
	push @argvs,$_;
	next;
    }
    $recalculate = 1 if (/recalculate/);
}
@ARGV = @argvs;

my $rig = shift;
my $test = shift;
unless ($test)
{
    $test = &AFM::get_test_nr($rig);
}
my $curr = undef;
if (@ARGV)
{
    $curr = shift;
}
else
{
    print "Using default predominant current of 1 A/cm^!\n";
}
unless ($rig && $test)
{
    die "Illegal rig or test attempted!\n";
}
$rig =~ /^(\d*)/;
$rig = $1;
$test =~ /(\d*)/;
$test = $1;
unless ($rig && $test)
{
    die "Illegal rig or test attempted!\n";
}
&check_run($rig);

my $dir = $AFM::publicdir.'rig'.$rig.'/'.$rig.'test'.$test.'/';
my $finfile = $dir.'report.fin';
if (-e $finfile && !$recalculate)
{
    warn "This test is marked as having a finished report!\nIf the report is to be rerun, remove the file '$finfile' and retry\n";
    exit 1;
}
my $psdir = $dir.'ps/';
my $pngdir = $dir.'png/';
my $txtdir = $dir.'txt/';
###### Remove old plotdata #####
unless ($recalculate)
{
    system("/bin/rm -f $dir*.gp");
    system("/bin/rm -rf $psdir") if (-d $psdir);
    system("/bin/rm -rf $pngdir") if (-d $pngdir);
    system("/bin/rm -rf $txtdir") if (-d $txtdir);
}
my $ivdir = $dir.'iv/';
### Remove old iv data ##
eval 
{
    unless ($recalculate)
    {
	system("/bin/rm -rf $ivdir") if (-d $ivdir);
    }
};
if ($@)
{
    &errorlog($@);
    die $@;
}
### Create fresh iv-dir ####
system("/bin/mkdir $ivdir") unless (-d $ivdir);
my $homedir = '/home/rig'.$rig.'/'.$rig.'test'.$test.'/';
my $ivcurvetimes = $homedir.'ivcurvetimes';
my $jdata = $dir.'jdata';
exit unless (-e $ivcurvetimes);
#### Create i-v curve file from ivcurvetimes file ####
my $cstr = '/usr/bin/perl /usr/local/bin/iv1.pl '.$ivcurvetimes.' '. $jdata.' '.$rig.' '.$test.' > '.$ivdir.'ivtable';
system($cstr);
#### Run all calculations using resistance-calc_3.0.pl and OCV_corr ###
$cstr = '/usr/local/bin/resistance_calc_3.0.pl '.$rig.' '.$test;
if (($curr) && ($curr =~ /(\-*\d*\.*\d+)/))
{
    $cstr .= ' '.$1; 
    my $file = $ivdir.'predominant_current.txt';
    open MYOUT,'>',$file or die "Could not open file $file for writing!\n";
    print MYOUT $1,"\n";
    close MYOUT;
}
else
{
    $cstr .= ' 1'; 
    my $file = $ivdir.'predominant_current.txt';
    open MYOUT,'>',$file or die "Could not open file $file for writing!\n";
    print MYOUT "1\n";
    close MYOUT;
}
system($cstr);
#### Generate all iv-curve figures for all iv-curves ####
my $command = '/bin/ls -d1 '.$ivdir.'ivdatadir*';
my @iv = `$command`;
for my $ivnr (@iv)
{
    if ($ivnr =~ /datadir\-(\d+)/)
    {
	$ivnr = $1;
	print "## Processing iv curve $ivnr ###\n";
	my $command = '/usr/local/bin/ivdataplot-3.0.pl '.$rig.' '.$test.' '.$ivnr;
	system($command);
    }
}
#### Generate overview figure ####
$command = '/usr/local/bin/ivhtml_tex.pl '.$rig.' '.$test;
system($command);
exit;
