#!C:\Perl\bin\perl

#########
#### DESCRIPTION:
#########

# This program is designed to be used with the .txt files downloadable from Alfred using the Output Format: Observations by Real-Time Period. The datafile, then, should consist of four columns and be tab-delimited. The output is a .csv that the Matlab routine realtimeobservationdatamaker.m can work on (the "rfm" means ready-for-matlab).

# Author: Seth Pruitt, 1/21/07
# sjpruitt@ucsd.edu



#########
#### PROGRAM
#########

use Cwd;
$cd = getcwd();

print "* You have started realtimeobservationdatamaker.pl\n";
print "* Would you like to change the working directory (y/n)?\n";
$change = <stdin>;
if ($change =~ m/y/i) {
    print "* You have chosen to change the working directory\n";
    print "> Please input the path (from root) of the\n  working directory you would like\n";
    $change2 = 0;
    while ($change2 == 0) {
        $nd = <stdin>;
        chomp($wd);
        if (opendir(INDIR,$wd)) {
            print "\n* The path you entered was successfully found and opened\n";
            $change2 = 1;
        } else {
            print "\n!! The path you entered could not be found;\n  please check your input or choose another\n";
            $change2 = 0;
        }
    }
} else {
    print "\n* You have chosen to remain in the current directory\n";
    $wd = $cd;
    opendir(INDIR,$wd);
}
print "\n   ********** \n";

@wdfiles = grep -T, readdir INDIR; #makes list of text files in the directory
@wdtxts = ();
print "\n* These are the possibly-data .txt files in the directory\n\n";
foreach $f (@wdfiles) {
    if ($f =~ m/(.*)\.txt/i & $f !~ m/README/i) {
    print "$1.txt\n";
        push(@wdtxts,$1);
    }
}
print "\n* Which of these would you like to transform?\n  (if you say 'none' this program will quit):\n";
$picked = 0;
$fnd = 0;
while ($fnd == 0) {
    $picked = <stdin>;
    chomp($picked);
    unless ($picked eq 'none') {
        $picked =~ m/(.*)\.txt/i;
        $picked = $1;

        $ct=1;
        while (<@wdtxts>) {
            unless ($picked =~ m/$_/gi) {
            } else {
                $fnd = 1;
            }
        }

        if ($fnd == 0) {
            print "\n!! The choice did not match any of the listed files;\n  please choose again:\n";
        } else {
        }
    } else {
        die "!!**!! You chose to make the program quit !!**!!\n";
    }
}

print "\n* Now I will read $picked.txt and create $picked\_rfm.csv\n";
$inname = $wd . '/' . $picked . '.txt';
$outname = $wd . '/' . $picked . '_rfm.csv';

print "\n   ********** \n";

if (-e $inname) {
  #Open input file and create output file
  open (INDATA, $inname) or die "Cannot open $inname : $!\n";#To debug if can't open the data file for some reason
  open (OUTDATA, ">$outname");#Create/replace document of output
  print "\n* NOTE: if the following goes past time 1, there is a problem!\n";
  while ($line = <INDATA>) {
    chomp($line);
    ($date,$name,$start,$end) = split("\t",$line);
    @pcs = split/-/, $date;
    @spc = split/-/, $start;
    @epc = split/-/, $end;
    if ($name eq ".") {
        $name = "" ;
    }
    if ($spc[0] eq ".") {
        $spc[0] = "" ;
    }
    if ($epc[0] eq ".") {
        $epc[0] = "" ;
    }
    $ct = 1;
    if (!exists($pcs[1]) & !exists($pcs[2]) & !exists($spc[1]) & !exists($spc[2]) & !exists($epc[1]) & !exists($epc[2])){
        print "* Time \# $ct realtimeobservationdatamaker found a line\n    that appears to be a header\n";
        $ct = $ct + 1;
        print OUTDATA "$pcs[0]_year,$pcs[0]_month,$pcs[0]_day,$name,$spc[0]_year,$spc[0]_month,$spc[0]_day,$epc[0]_year,$epc[0]_month,$epc[0]_day\n";
    } else {
    print OUTDATA "$pcs[0],$pcs[1],$pcs[2],$name,$spc[0],$spc[1],$spc[2],$epc[0],$epc[1],$epc[2]\n";
    }
  }

  print "\n   ********** \n";
  unless ($ct > 2) {
    print "\n* SUCCESS\n  $outname is created\n";
  } else {
    print "\n* FAILURE\n  $outname is created\n  BUT we found more than one header in the data, indicating a problem\n";
  }
} else {#to debug if program can't find file
  print "$inname did not exist as far as this program could see.\n";
}


