#!/usr/athena/bin/perl

print("Welcome to PerlScatterPlot.\n");
print("What file is the data in?\n");
$filename = &input();
if(!(-e $filename)) { print("No such file.  Try Again.\n");exit;}
open(DATA, $filename);
for($i=0;$i<15;$i++){
	$_ = <DATA>;
	split;
	if($prev != $#_){
		$incons++;
	    }
	$prev = $#_;
    }
close(DATA);
if($incons>1){
    print("Inconsistent File Format\n");
    print("At line 15, $prev columns\n");
    print("\nUse that value only?   (ignore header)\n");
    if(&input() eq "n"){
	print("I can't deal with this.  I'm only a perl script...\n");
	exit;
    }
}

$cols = $prev;
print("Ok, since I'm rather picky, we must make sure the data is clean.\n");
print("As far as I know the data looks like this:\n$_\n");
print("\nWhich column is going to be the X axis?\n");
$xaxis = int(&answer());
print("\nWhich column is going to be the Y (data) axis?\n");
$yaxis = int(&answer());
print("\n\nNow for cleaning.  Which format is the X axis in?\n");
$xclean = &menu();
print("Which format is the Y axis in?\n");
$yclean = &menu();
$xmin = 100000000;
$ymin = 100000000;
open(DATA, $filename);
print("O boy, looking through this data and storing it.\n");
while(<DATA>){
    split;
    if($#_ == $cols){
	$xcol = $_[$xaxis];
	$ycol = $_[$yaxis];
	$xval = &$xclean($xcol);
	$yval = &$yclean($ycol);
	$xmax = ($xval > $xmax) ? $xval:$xmax;
	$ymax = ($yval > $ymax) ? $yval:$ymax;
	$xmin = ($xval < $xmin) ? $xval:$xmin;
	$ymin = ($yval < $ymin) ? $yval:$ymin;
	$values[$xval] = $yval;
	
    }
}
$xscale = 60/($xmax - $xmin);
$yscale = 60/($ymax - $ymin);

foreach $i ($xmin..$xmax){
    if(defined($values[$i]){
	if(defined($picture[int(($values[$i]-$ymin)*$yscale)])){
	    $picture[int(($values[$i]-$ymin)*$yscale)]++;
	}
	else
{
    $picture[int(($values[$i]-$ymin)*$yscale)] = "0";
}

sub input {
	local($foo);
	print(">>>");
	$foo = <STDIN>;
	chop($foo);
	$foo;
}

sub menu {
    local($type);

    print("1) Clean integer\n");
    print("2) Dirty integer (,.)\n");
    print("3) Time hh:mm:ss\n");
    print("4) Date\n");
    print("5) Other fielded number\n");
    print("\n");
    $type = int(&input());

    if($type == 1){
	$clean = 'integerf';
	return;
    }
    elsif($type==2){
	print("\nOk, what is the dirt?\n");
	$dirt = &input();
	$clean = 'dirtyf';
	return;}
    elsif($type==3){
	print("Ok, what is the format?  1 = hh:mm:ss, 2= hh:mm 3 = mm:ss\n");
	$timeformat = &input();
	$clean = 'timef';
	return;
    }
    elsif($type==4){
	  print("Ok, format for this? eg mm/dd/yy\n");
	  $dateformat = &input();
	  $clean = 'datef';
      }
    elsif($type==5){
	print("Oops, I don't really know how to do this.\n");
	exit;}
}

sub integerf {
    local($integ) = @_;
    $ret = int($integ);
    return;
}

sub dirtyf {
    local($val) = @_;
    $val =~ s/[$dirt]//g;
    $ret = int($val);
    return;
}
    
sub timef {
    local($val) = @_;
    $val =~ /(\d\d):(\d\d):(\d\d)/;
    $hours = $1;
    $minutes = $2;
    $seconds = $3;
    $ret = $hours*3600 + $minutes*60 + $seconds;
    }



