#!/usr/bin/perl
# $Id: jdata_derivative.pl,v 1.1 2011/07/05 08:08:34 sofc Exp $

use strict;
use warnings;
use AFM;

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

if ( scalar(@ARGV) < 3 )
{
    print "\n\n     Program for calculating derivatives from jdata files\n\n";
    print "The derived values gets a new collumn named d_\$source_dt\n";
    print "\n";
    print 'Usage: '."$PROGRAM \$rig \$test \"source\"  [optional --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";
    print "The \"source\" is the colum names of the source values\n\n";
    exit;
}
my $debug = 0;

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

my $usernumber = $<;
my $username = &AFM::get_user_name($usernumber);
my $testusername = 'rig'.$ARGV[0];
if ($username ne $testusername)
{
    
    die "wrong user: $username !\nTo access data for rig $ARGV[0] you must be user rig$ARGV[0]!\n";
}

my $rig = $ARGV[0];
if ( !&AFM::check_rig_nr($rig) )
{
    die "Rig $rig is not valid!\n";
}
my $testno = 0;
if (scalar(@ARGV) > 1)
{
    my $string = $ARGV[1];
    $string =~ /(\d+)/;
    my $ttest = $1;
    if (defined($ttest))
    {
        $testno = $ttest+0;
    }
}

my @source_name= ();
for (my $x = 2; $x < scalar(@ARGV); $x++)
{
    push @source_name, $ARGV[$x];
}


#### Indsaet conversion af jdata fil ####
my $dir = '/home/http/html/rig'.$rig.'/'.$rig.'test'.$testno.'/';
my $jfile = $dir .'jdata';
my @list = `/bin/cat $jfile`;

#### Finding collum-id for source ###
my @sourceid = ();
my $timeid = 0;
my @l = split(/\s+/,$list[0]);
my $maxcol = scalar(@l);
for (my $x = 0; $x < $maxcol; $x++)
{
    for (my $idcount = 0; $idcount < scalar(@source_name); $idcount++)
    {
	if ($l[$x] eq $source_name[$idcount])
	{
	    $sourceid[$idcount] = $x+1;
	}
    }
}
my @xcarry = ();

my $line = 0;
for my $str (@list)
{
    $line++;
    my @l = split(/\s+/,$str);
    my @xval = ();
    for (my $x = 0; $x < scalar(@sourceid); $x++)
    {
	$xval[$x] = $l[$sourceid[$x]]+0;
    }
    my @t = &convert($l[0],@xval);
    for (my $x = 0; $x < scalar(@sourceid); $x++)
    {
	push @l, 'd_'.$source_name[$x].'_dt';
	push @l, $t[$x];
    }
    for my $st (@l)
    {
	print $st,' ';
    }
    print "\n";
}
exit;



sub convert()
{
    my @res = ();
    my $time = shift(@_);
    my @xval = @_;
    if (scalar(@xcarry) > 1)
    {
	my $oldtime = $xcarry[0]+0;
	for (my $x =0; $x < scalar(@xval); $x++)
	{
	    my $dev = '';
	    if ($time != $oldtime)
	    {
		$dev = ($xval[$x] - $xcarry[$x+1]) / ($time - $oldtime);
		if ($dev =~ /(\-*\d+\.\d\d\d\d)/)
		{
		    $dev = $1+0;
		}
	    }
	    else
	    {
		$dev = '#####';
	    }
	    warn "Value $x = $dev\n" if ($debug);
	    push @res, $dev;
	}
    }
    else
    {
	for (my $x =0; $x < scalar(@xval); $x++)
	{
	    push @res, '#####';
	}
    }
    $xcarry[0] = $time;
    for (my $x = 0; $x < scalar(@xval); $x++)
    {
	$xcarry[$x+1] = $xval[$x]; 
    } 
    warn "\n" if ($debug);
    return @res;
}



