#!/usr/bin/perl
use strict;
use warnings;
use AFM;
my $GNUPLOT = '/usr/local/bin/gnuplot';
my $rig = undef;
my $tmpstr = shift;
if ( $tmpstr =~ /^(\d+)$/ ) { $rig = $1 };
my $usernumber = $<;
my $username = &AFM::get_user_name($usernumber);
my $testusername = 'rig'.$rig;
if ($username ne $testusername)
{
    warn "wrong user: $username !\n";
    warn "To access data for rig $rig you must be user rig$rig!\n";
    exit 1;
}
if ( !&AFM::check_rig_nr($rig) )
{
    print "Rig $rig is not valid!\n";
    exit 1;
}
my $test = undef;
$tmpstr = shift;
if ( $tmpstr =~ /^(\d+)$/ ) { $test = $1 };
my $dir = '/home/http/html/rig'.$rig.'/'.$rig.'test'.$test.'/impedance/';
my $pngdir = $dir.'png/';
my $psdir = $dir.'ps/';
if ( !( -d $psdir ) )  { system("mkdir $psdir") }
if ( !( -d $pngdir ) ) { system("mkdir $pngdir") }
my $gpfile = '/tmp/diffimp'.$$ . ".gp";
my $datafile = '/tmp/diffimp'.$$ . ".txt";
my $count = 0;
my $basefile = shift;
my $fileid = '';
$basefile =~ /(\w+)\.\w+\s*$/;
$fileid = $fileid . $1;
my $area = &AFM::get_config_value_old($rig,'area',$test);

my @basedata = ();
open IN ,'<', $basefile or die "Could not open file $basefile for reading!\n";
while (my $st = <IN>)
{
    $count++;
    push @basedata,$st if (($count > 7) && ($st =~ /\d+/));
}
close IN;
my @freq = ();
my @real = ();
my @fcheck = ();
for my $st (@basedata)
{
    $st =~ s/^\s*//;
    my @l = split(/\s+/,$st);
    push @freq, &mylog($l[0])/&mylog(10);
    push @real, $l[1];
    push @fcheck,$l[0];
}
my @difreal = ();
for (my $x = 1; $x < @freq-1; $x++)
{
    $difreal[$x] = ($real[$x+1] - $real[$x-1])/($freq[$x-1] - $freq[$x+1]);
}
my @outdata = ();
for (my $x = 1; $x < @freq-1; $x++)
{
    $outdata[$x] = join("\t",$freq[$x],$difreal[$x]);
}
exit unless (@ARGV);
my $basetit = shift;
my $bv = 0;
exit unless (@ARGV);
if ($ARGV[@ARGV-1] =~ /--BV/i)
{
    pop @ARGV;
    $bv = 1;
}
my @title = ();
while (@ARGV)
{
    my $file = shift;
    $file =~ /(\w+)\.\w+\s*$/;
    $fileid = $fileid . $1;
    my $tit = '';
    $tit = shift if (@ARGV);
    push @title,$tit;
    my @data = ();
    open IN ,'<', $file or die "Could not open file $file for reading!\n";
    $count = 0;
    while (my $st = <IN>)
    {
	$count++;
	push @data,$st if (($count > 7) && ($st =~ /\d+/));
    }
    close IN;
    my @r = ();
    for (my $x = 0; $x < @data; $x++)
    {
	$data[$x] =~ s/^\s*//;
	my @l = split(/\s+/,$data[$x]);
	die "Frequency ranges does not match!\n" unless ($l[0] == $fcheck[$x]);
	push @r, $l[1];
    }
    my @difreal = ();
    for (my $x = 1; $x < @freq-1; $x++)
    {
	$difreal[$x] = ($r[$x+1] - $r[$x-1])/($freq[$x-1] - $freq[$x+1]);
    }
    for (my $x = 1; $x < @freq-1; $x++)
    {
	$outdata[$x] = join("\t",$outdata[$x],$difreal[$x]);
    }
}

open OUT,'>',$datafile or die "Could not open file $datafile for writing!\n";
for (my $x = 1; $x < @outdata;$x++)
{
    print OUT $outdata[$x],"\n";
}
close OUT;
open GP,'>',$gpfile or die "Could not open gnuplot file $gpfile for writing!\n";
print GP "set xlabel 'log frequency(Hz)'\n";
my $label = '{{/Symbol W}cm^2}';
print GP "set ylabel '$label'\n";
my $term = &AFM::get_config_global( "GNUPLOT", 'postportterm' );
if ($bv)
{
    $term =~ s/color\s*//;
}
print GP "set term ", $term, "\n";
print GP "set size 1,0.42\n";
print GP "set out \'$psdir" , $fileid , "-diffplot.ps\'\n";
print GP "plot '$datafile' u 1:(\$3-\$2)*$area tit '$title[0] - $basetit' w lines";
for (my $x = 1; $x < @title; $x++)
{
    my $y = $x+3;
    print GP ", '' u 1:(\$$y-\$2)*$area tit '$title[$x] - $basetit' w lines";
}
print GP "\n";
close GP;
system("$GNUPLOT $gpfile");
unlink $gpfile;
unlink $datafile;
my $cstr = '/usr/local/bin/ps2png '.$psdir.$fileid.'-diffplot.ps '.$pngdir.$fileid.'-diffplot.png';
system($cstr);


exit;

sub mylog($)
{
    my $x = shift;
    return undef unless defined($x);
    my $res = `/usr/local/bin/mylog $x`;
    chomp $res;
    return $res;
}
