#!/usr/bin/perl -wT
use strict;
use warnings;
use AFM;
exit unless (@ARGV > 2);
my $rig = shift;
my $test = shift;
if ($rig =~ /(\d+)/)
{
    $rig = $1;
}
else
{
    $rig = 0;
}
if ($test =~ /(\d+)/)
{
    $test = $1;
}
else
{
    $test = 0;
}
my $tag = shift;
if ($tag =~ /(\w+)/)
{
    $tag = $1;
}
else
{
    $tag = undef;
}
exit unless ($test && $rig && $tag);
my $rejectmin = undef;
if (@ARGV)
{
    $rejectmin = shift;
    if ($rejectmin =~/([\d\.\-]+)/)
    {
        $rejectmin = $1;
    }
    else
    {
        $rejectmin = undef;
    }
}
my $rejectmax = undef;
if (@ARGV)
{
    $rejectmax = shift;
    if ($rejectmax =~/([\d\.\-]+)/)
    {
        $rejectmax = $1;
    }
    else
    {
        $rejectmax = undef;
    }
}
my $f = '/home/http/html/rig'.$rig.'/'.$rig.'test'.$test.'/jdata';
open MYIN,'<',$f or die "Could not open file $f for reading $!\n";
my $line = <MYIN>;
close MYIN;
my $col;
my @list = split(/[\s\t]/,$line);
for (my $x = 0; $x < @list; $x++)
{
    next unless ($list[$x] =~ /^$tag$/);
    $col = $x+2;
    last;
}
unless ($col)
{
    print "-32768\n-32768\n";
    exit 1;
}
my $cmd = "awk 'BEGIN {min = 1000000} { if (\$$col < min) min=\$$col } END {print min}' ".$f;
$cmd = "awk 'BEGIN {min = 1000000} { if (\$$col < min && \$$col >= $rejectmin) min=\$$col } END {print min}' ".$f if defined($rejectmin);
my $min = `$cmd`;
$cmd = "awk 'BEGIN {max = -1000000} { if (\$$col > max) max=\$$col } END {print max}' ".$f;
$cmd = "awk 'BEGIN {max = -1000000} { if (\$$col > max && \$$col <= $rejectmax) max=\$$col } END {print max}' ".$f if defined ($rejectmax);
my $max = `$cmd`;
print $min, ' ',$max;

