#! /usr/bin/perl
#  read daily ascii simrad data on NBP0204
#use Date::Calc qw(Day_of_Week Week_Number Day_of_Week_to_Text);
## modify again to work for either every line tc'd or just first
## switches on day 208; starts saving files on day 206 ...
use Time::Local;
$knot=-999; ##  is the default value for stuff you don't want in your file.
#########  ENTER DAYS TO PROCESS in simnowplotter.m or day.dat file #############
open (SIMDAY, "/home/gallagsc/simrad/day.dat") or die "can't in simday\n";
while ($linen = <SIMDAY>){
print $linen, "\n";

@dayyouwant=$linen;
}

foreach $dayyouwant (@dayyouwant) { 
print $dayyouwant, "\n";

# set up for 3 frequencies; #$2freq should equal 4;
$freq=4;
$day=$dayyouwant;
chomp($day);
$daytoprocess=$day;
print $day, "\n";
$datacount1=0;$datacount2=0;$datacount3=0;$datacount=0;
system("date");
#unlink("/home/karen/NBP/NBP0104sim.prc");
#unlink("/home/karen/NBP/NBP0104sim.prc.gz");
### system("cp /rawdata/rtuw/NBP0104sim1.d$day /home/karen/NBP/NBP0104sim.prc");
####system("cp /mnt/rvdas/current_cruise/rvdas/uw/NBP0104sim1.d$day /home/karen/NBP/NBP0104sim.prc");
#system("cp /mnt/rvdas/current_cruise/rvdas/uw/NBP0104sim1.d$day.gz /home/karen/NBP/NBP0104sim.prc.gz");
#system("gunzip /home/karen/NBP/NBP0104sim.prc.gz");
open (SIMREAD, "/home/gallagsc/simrad/NBP0204sim1.d$day") or die "can't in\n";
open (SIMOUT1, ">/home/gallagsc/simrad/SIMf$day.out1") or die "can't out1\n";
open (SIMOUT2, ">/home/gallagsc/simrad/SIMf$day.out2") or die "can't out2\n";
open (SIMOUT3, ">/home/gallagsc/simrad/SIMf$day.out3") or die "can't out3\n";
open (SIMTIME1, ">/home/gallagsc/simrad/SIMt$day.time1") or die "can't out11\n";
open (SIMTIME2, ">/home/gallagsc/simrad/SIMt$day.time2") or die "can't out22\n";
open (SIMTIME3, ">/home/gallagsc/simrad/SIMt$day.time3") or die "can't out33\n";
#}
# Set up start markers to avoid leading returns
$startmark1=0; $startmark2=0; $startmark3=0; $timesthrough=0; $recordcount1=0; $recordcount2=0; $recordcount3=0;
#  Main loop through the entire rvdas generated file
while($line = <SIMREAD>){
$timesthrough++;
chomp($line);
$a=0; $b=0; $c=0; $d=0; $e=0; $f=0; $g=0; $h=0; $i=0; $j=0; $k=0;
	#  Determine the frequency, which determines which file to put data into
	if ($line =~ /Q1/){
	$freq=1;
		if ($startmark1>0){
		$stopthis=25-$recordcount1;
			for ($m=1;$m<=$stopthis;$m++){
			print SIMOUT1 "0 0 0 0 0 0 0 0 0 0 ";
			}
		print SIMOUT1 "\n";
		}
	$startmark1=1;
   	( $time, $o)=split /\s+/, $line;
	($year, $rtime)=split /\+/, $time;
	($day, $hour, $min, $sec)=split /:/, $rtime;
$min ||= $knot; #  If $b false, replace with $knot; 
$day ||= $knot; #  If $b false, replace with $knot; 
$hour ||= $knot; #  If $b false, replace with $knot; 
	$daytime= $day+($hour/24+($min/(24*60))+($sec/(24*60*60)));
	print SIMTIME1 $daytime, " ", $hour," ", $min, "\n";
		$recordcount1=0; $datacount=0;
}
	elsif ($line =~ /Q2/){
	$freq=2;

		if ($startmark2>0){
		$stopthis=25-$recordcount2;
			for ($m=1;$m<=$stopthis;$m++){
			print SIMOUT2 "0 0 0 0 0 0 0 0 0 0 ";
			}
		print SIMOUT2 "\n";
		}
	$startmark2=1;
   	( $time, $o)=split /\s+/, $line;
	($year, $rtime)=split /\+/, $time;
	($day, $hour, $min, $sec)=split /:/, $rtime;
$min ||= $knot; #  If $b false, replace with $knot; 
$day ||= $knot; #  If $b false, replace with $knot; 
$hour ||= $knot; #  If $b false, replace with $knot; 
	$daytime= $day+($hour/24+($min/(24*60))+($sec/(24*60*60)));
	print SIMTIME2 $daytime," ", $hour," ", $min, "\n";
		$recordcount2=0; $datacount=0;
}
	elsif ($line =~ /Q3/){
	$freq=3;
		if ($startmark3>0){
		$stopthis=25-$recordcount3;
			for ($m=1;$m<=$stopthis;$m++){
			print SIMOUT3 "0 0 0 0 0 0 0 0 0 0 ";
			}
		print SIMOUT3 "\n";
		}	$startmark3=1;
   	( $time, $o)=split /\s+/, $line;
	($year, $rtime)=split /\+/, $time;
	($day, $hour, $min, $sec)=split /:/, $rtime;
$min ||= $knot; #  If $b false, replace with $knot; 
$day ||= $knot; #  If $b false, replace with $knot; 
$hour ||= $knot; #  If $b false, replace with $knot; 
	$daytime= $day+($hour/24+($min/(24*60))+($sec/(24*60*60)));
	print SIMTIME3 $daytime, " ", $hour," ", $min, "\n";
		$recordcount3=0; $datacount=0;
}

else {

if ($datacount <=240){
unless ($line =~ /[a-zA-Z%*\_\/\^]/) {
## WARNING EXCLUDES LONELY -'s and changes undef, "", 0 vals to -999 ...##

# try to find dashes
#######     $line =~ s/[\d \b \,]\-[\b \, \- \s \e]/$knot/;
$line =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\b+\./$knot/g;
# \a alarm \n newline \r carriage ret. \t tab \f form feed \e escape \d digit
# \D nondigit \w word boundary \W non-word bound. \s white space (\t\n\r\f) 
# \S not white space * either 0 or 1 times... \Z at end of string ;)

##############SPLIT AND TEST AREA #######################
#####   unless defined($k)  $k=$knot;
#######  $b =~ s/undef/2/g;
#######   unless defined($b=$knot);
#######  unless($b) $b=0;	
##########@SIMLINE= split /,/, $line;
##########$simrec=$#SIMLINE; # get last index in the line
if ($line =~ /01+/){
	( $xb, $c, $d, $e, $f, $g, $h, $i, $j, $k)=split /,/, $line;
   	( $fore, $b)=split /\s+/, $xb;
}
else{
	( $b, $c, $d, $e, $f, $g, $h, $i, $j, $k)=split /,/, $line;
}
$b ||= $knot; #  If $b false, replace with $knot; 
$c ||= $knot;
$d ||= $knot;
$e ||= $knot;
$f ||= $knot;
$g ||= $knot;
$h ||= $knot;
$i ||= $knot;
$j ||= $knot;
$k ||= $knot;
$b =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\-\-/$knot/g;
$c =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\-\-/$knot/g;
$d =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\-\-/$knot/g;
$e =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\-\-/$knot/g;
$f =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\-\-/$knot/g;
$g =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\-\-/$knot/g;
$h =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\-\-/$knot/g;
$i =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\-\-/$knot/g;
$j =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\-\-/$knot/g;
$k =~ s/\-\D|\-\Z|\d+\-+\d+|\.\d+\.|\-\-/$knot/g;
$b =~ s/^[^\d-\s]+$/$knot/g;
$c =~ s/^[^\d-\s]+$/$knot/g;
$d =~ s/^[^\d-\s]+$/$knot/g;
$e =~ s/^[^\d-\s]+$/$knot/g;
$f =~ s/^[^\d-\s]+$/$knot/g;
$g =~ s/^[^\d-\s]+$/$knot/g;
$h =~ s/^[^\d-\s]+$/$knot/g;
$i =~ s/^[^\d-\s]+$/$knot/g;
$j =~ s/^[^\d-\s]+$/$knot/g;
$k =~ s/^[^\d-\s]+$/$knot/g;

#############################START PRINTING FREQ DATA HERE ###############
########### Q1 RECORD STUFF ####################
if ($freq<2) {
	$recordcount1++;
	print SIMOUT1  $b,' ', $c,' ', $d,' ', $e,' ', $f,' ', $g,' ', $h,' ', $i,' ', $j,' ', $k,' ';
$datacount+=10;
	}
########### Q2 RECORD STUFF ####################
	elsif($freq<3 && $freq >1) {
	$recordcount2++;
	print SIMOUT2  $b,' ', $c,' ', $d,' ', $e,' ', $f,' ', $g,' ', $h,' ', $i,' ', $j,' ', $k,' ';
$datacount+=10;
	}


########### Q3 RECORD STUFF ####################
	elsif($freq<4 && $freq >2) {
	$recordcount3++;
	print SIMOUT3  $b,' ', $c,' ', $d,' ', $e,' ', $f,' ', $g,' ', $h,' ', $i,' ', $j,' ', $k,' ';
$datacount+=10;
	}
}
}
}
}

###################### WRITE LAST RECORD ########################
	#  Determine the frequency, which determines which file to put data into
	if($freq==1){
		if ($startmark1>0){
		$stopthis=25-$recordcount1;
			for ($m=1;$m<=$stopthis;$m++){
			print SIMOUT1 "0 0 0 0 0 0 0 0 0 0 ";
			}
		print SIMOUT1 "\n";
		}

	$startmark1=1;
   	( $time, $o)=split /\s+/, $line;
	($year, $rtime)=split /\+/, $time;
	($day, $hour, $min, $sec)=split /:/, $rtime;
$min ||= $knot; #  If $b false, replace with $knot; 
$day ||= $knot; #  If $b false, replace with $knot; 
$hour ||= $knot; #  If $b false, replace with $knot; 
	#print $wkday, $month, $day, $time, $year, '\n';
	$daytime= $day+($hour/24+($min/(24*60))+($sec/(24*60*60)));
	print SIMTIME1 $daytime," ", $hour," ", $min, "\n";
		$recordcount1=0;
	}
	elsif ( $freq==2){
		if ($startmark2>0){
		$stopthis=25-$recordcount2;
			for ($m=1;$m<=$stopthis;$m++){
			print SIMOUT2 "0 0 0 0 0 0 0 0 0 0 ";
			}
		print SIMOUT2 "\n";
		}
	$startmark2=1;
   	( $time, $o)=split /\s+/, $line;
	($year, $rtime)=split /\+/, $time;
	($day, $hour, $min, $sec)=split /:/, $rtime;
$min ||= $knot; #  If $b false, replace with $knot; 
$day ||= $knot; #  If $b false, replace with $knot; 
$hour ||= $knot; #  If $b false, replace with $knot; 
	#print $wkday, $month, $day, $time, $year, '\n';
	$daytime= $day+($hour/24+($min/(24*60))+($sec/(24*60*60)));
	print SIMTIME2 $daytime, " ",$hour," ", $min, "\n";
		$recordcount2=0;
	}
	elsif ($freq==3){
		if ($startmark3>0){
		$stopthis=25-$recordcount3;
			for ($m=1;$m<=$stopthis;$m++){
			print SIMOUT3 "0 0 0 0 0 0 0 0 0 0 ";
			}
		print SIMOUT3 "\n";
		}
	$startmark3=1;
   	( $time, $o)=split /\s+/, $line;
	($year, $rtime)=split /\+/, $time;
	($day, $hour, $min, $sec)=split /:/, $rtime;
$min ||= $knot; #  If $b false, replace with $knot; 
$day ||= $knot; #  If $b false, replace with $knot; 
$hour ||= $knot; #  If $b false, replace with $knot; 
	#print $wkday, $month, $day, $time, $year, '\n';
	$daytime= $day+($hour/24+($min/(24*60))+($sec/(24*60*60)));
	print SIMTIME3 $daytime, " ",$hour," ", $min, "\n";
		$recordcount3=0;
	}
system("date");
}
#system("gzip /home/karen/NBP/SIMf2*");
#system("mv /home/karen/NBP/SIMf2* /home/karen/NBP/SIMsaves");
