#!/usr/bin/perl -w
#
# mocpro_level3.pl
# mocness tow data file manipulation routine - level 3
# For version, see $version

# October 17, 1997  rcg
# November 4, 1997.  Change to level 3.  rcg
# November 10, 1997.  Fix code for outputing comments.  rcg
# November 14, 1997 rcg Change output message to say displayed by.
# May 13, 1998 rcg Eliminate warning message when qualifier did not exist
# December 7, 1998. Display date and time correctly for old cruises.
# March 24, 1999.  Change location of files.  rcg.
# April 5, 1999.  Skip lines that have leading "#" character.  These
#	 are JGOFS-style comment lines, typically hand-edited in.  rcg
# April 14, 1999.  Make Ashjian enhancements the real one.  Change script
#	 location back to /data/rgroman/ctd_mocness.  rcg
# May 3, 1999. V1.3  Test for an empty (blank) line used briefly, then 
#	commented out.  Use field name time_local_alt for old format
#	to preserve time values when yearday not available.  rcg
# June 18, 1999.  V1.4.  Add code to look for and ignor header records
#	inside the "new formatted" data files.  RCG
# July 12, 1999.  V1.50.  Use sendmessage routine to handle error and
#	warning messages.  RCG
# January 28, 2000.  V1.51  Fix Y2K computation.  rcg
# December 24, 2001.  V1.52 Modified for ZooGene data.  Header records
#	starting with % and sometimes followed by blank.  rcg
# March 6, 2003. V1.53ZG  Modified March 6, 2003.  Change location of
# 	script file and file names to be /data1/zoogene_data/mocness from
# 	/data1/zoogene/Scripts/mocness.  rcg
# July 10, 2006 V1.54CM  Modified for CMarZ data.  mda
# July 22, 2008. V1.55  Modified to test for lat and lon values
#	equal to -999.000000 and set to "nd", and for temp or
#	sal equal to 50.000 and set to "nd".  If temp is bad,
#	then set values for theta, sigma and sal to 'nd'.  If
#	sal bad, then set sigma to 'nd'.  Warning:  Code depends
#	on the order of the data input to be in a "good"
#	order, otherwise bad values could be output before
#	they are set to "nd".  rcg
# July 25, 2008. V1.56.  Fix coding so that if temp value is bad
#	the sal value is also set as bad.  rcg

$version="V1.56/July 25, 2008";

# Looks at all the file $ARGV[2] and outputs level 3 data

# Passed parameters:
#	$ARGV[0] - data type
#	$ARGV[1] - file type
#	$ARGV[2] - data file to process

#some definitions
#$script_loc="/data/rgroman/ctd_mocness";   # not used
$| = 1;
$error="&x";
$warning="#";
$i_am_level = 3;
print STDOUT "#Displayed by $0, $version\n";

$fieldnames_level[3]="/data1/cmarz_data/mocness/fieldnames_level3";
$datafile=$ARGV[2];
print STDOUT "#   Using data file $datafile\n";

# Read fieldnames allowed and save for later use
if ($i_am_level != $#fieldnames_level) {
	$message0 = "At level $i_am_level rather than $#fieldnames_level";
	$message1 = "\tin routine $0.  Cannot continue.";
	&sendmessage ($error, $message0, $message1);
	exit 0;
}
for ($i=$i_am_level; $i <= $#fieldnames_level; $i++) {
#	print "STDOUT #**debug,  fieldnames_level[$i]=$fieldnames_level[$i]\n";
	unless (open FIELDNAMES, $fieldnames_level[$i]) {
		$message0 = "Could not get field names from $fieldnames_level[$i]";
		$message1 = "Error=$!";
		&sendmessage ($error, $message0, $message1);
		exit 0;
	}
	@fieldnames=<FIELDNAMES>;
	close FIELDNAMES;
	chomp (@fieldnames);
}

# Remove field name attributes if present
for ($i=0; $i <= $#fieldnames; $i=$i+1) {
	if (index ($fieldnames[$i], "#") >= 0 ) {  #comment line, drop out
		$fieldnames[$i]="";
		next;
	}
	$qualifier[$i]=$fieldnames[$i];
	$fieldnames[$i]=~ s/(^.*)\[.*/$1/;
	$qualifier[$i]=~ s/^.*(\[.*)/$1/;
	if ($fieldnames[$i] eq $qualifier[$i]) {$qualifier[$i]="";}
#	print STDOUT ("#**debug, fieldnames[$i]=",$fieldnames[$i], 
#		", qualifier[$i]=",$qualifier[$i], "\n");
}
# Read data file

open MOCDATA, $datafile or
	die "#  Could not open CTD from MOCNESS data file $datafile, $!";
$_=<MOCDATA>;
chomp;
@notuse=();
push @save_for_comments, $_ ;
#print STDOUT " **debug, array save_for_comments=@save_for_comments\n";
#print STDOUT "#**debug, before old/new determination _=$_\n";

if ( m/^\d{1,}.*/ ) {
#	old form found, with one field or value per line
	$format='old';
	$numbfields=$_ - 1;
#	print  STDOUT "# **debug, numbfields=$numbfields\n";
	for ($k=0; $k <= $numbfields; $k++) {
		$fields_in_data[$k] = <MOCDATA>;
		chomp $fields_in_data[$k];
#		print STDOUT ("# **debug, fields_in_data[",
#			$k,"]=$fields_in_data[$k]\n");
	}
	@save_for_comments=("#Note: local times may be in error.  12/8/98 rcg",
		"Thinks there are field names here");
}
else {
# 	new format with field names and data on per line basis
	$format="new";
	$numbfields=0;
	while ($numbfields == 0) {
		$_=<MOCDATA>;
		unless (defined $_ ) {
			$message0 = "Reached end of file in $datafile without";
			$message1 = "finding list of variables expected.";
			&sendmessage ($error, $message0, $message1);
			exit 0;
		}
		chomp;
		s/^ {0,1}% {0,1}//;
#		print STDOUT "# **debug, _=$_\n";
		push @save_for_comments, $_;
		@fields_in_data = split /\s+/, $_ ;
		for ($i=0; $i <= $#fields_in_data; $i++) {
			$fields_in_data[$i] =~ s/ //g;
#			print STDOUT 
#			  "**debug, fields_in_data[$i]=$fields_in_data[$i]\n";
		}
		if ($fields_in_data[0] eq "" ) {shift @fields_in_data; }
		if ($fields_in_data[0] eq "time" ) {
			$numbfields = <MOCDATA>;
			chomp $numbfields;
			$numbfields =~ s/^% {0,1}//;
			$numbfields = $numbfields -1;
			if ($numbfields != $#fields_in_data) {
				$message0 = "Datafile $datafile had $#fields_in_data fields.";
				$message1 = "Number of fields expected was $numbfields.";
				&sendmessage ($error, $message0, $message1);
				exit 0;
			}
		}
	}
}

for ($i=0; $i <= $#fields_in_data; $i=$i+1) {
	$fields_in_data[$i] =~ s/\s//g;
#	print STDOUT ("#**debug, redefine variable names, ", 
#		"fields_in_data[$i]=",$fields_in_data[$i], "\n");
	if ( $fields_in_data[$i] eq "time" and $format eq "new") { 
		$fields_in_data[$i] = "yrday_local"; }
	elsif ( $fields_in_data[$i] eq "time" and $format eq "old") {
		$fields_in_data[$i] = "time_local_alt"; }
	elsif ( $fields_in_data[$i] eq "pres" ) {
		$fields_in_data[$i] = "press"; }
	
	
}

# Output file comments if available (from new format only)
if ($#save_for_comments >= 0 ) {
	pop @save_for_comments;	#Remove line of field names
	foreach $comment (@save_for_comments) {
#		print STDOUT "# **debug, comment=$comment\n";
		print STDOUT ("#",$comment,"\n");
	}
}

# Determine field names that I know about and provide feedback if missing some
NAMES:
for ($i=0; $i <= $#fields_in_data; $i=$i+1) {
	foreach $name (@fieldnames) {
#		print STDOUT 
#		   "#**debug, compare # $i, $name with $fields_in_data[$i]\n";
		next NAMES if $name eq "";
		next NAMES if $name eq $fields_in_data[$i];
	}
	$message0 = "Cannot use field name $fields_in_data[$i].";
	$message1 = "The Data Management Office has been contacted.";
	&sendmessage ($warning, $message0, $message1);
	push @notuse, $i;
}
push @notuse, -1;	#In case all field names are okay

# Output valid field names
SKIP1:
for ($i=0; $i <= $#fields_in_data; $i++) {
	foreach $j (@notuse) {
		if ($j == $i) {next SKIP1; }
		print STDOUT ($fields_in_data[$i]);
		if ( defined $qualifier[$i] ) {print STDOUT ($qualifier[$i]); }
		print STDOUT ("\t");
		last;		#printed out valid field name, end inner loop
	}
}
print STDOUT "\n";

#Read data from file and output
$set_skip_internal_header = 0;

while (<MOCDATA>) {
	$temp_bad = 'no';
	$sal_bad = 'no';
	chomp;
	if ( m/^#/ ) { next; }
	if ( $set_skip_internal_header > 0) {
		$set_skip_internal_header = $set_skip_internal_header - 1;
		next;
	}
#	unless ( m/\w/ ) { next; }
#	print STDOUT "#**debug, format=$format, next data read=$_\n";
	if ($format eq "old") {
		$data[0]=$_;
		$data[0] =~ s/\s//g;
		for ($i=1; $i <= $numbfields; $i=$i+1) {
			$data[$i]=<MOCDATA>;
			$data[$i] =~ s/\s//g;
#			print STDOUT "#**debug -old fmt, data[$i]=$data[$i]\n";
		}
	}
	else {		# newformat
		if ( m/Tow:/ ) {
			$set_skip_internal_header = 6;
			print STDOUT ("#Internal header records ",
				"skipped in ", $datafile, "\n");
			next;
		}
		@data = split /\s+/, $_;
		for ($i=0; $i <= $#data; $i=$i+1) {
			$data[$i] =~ s/\s//g;
#			print STDOUT "#**debug -new fmt, data[$i]=$data[$i]\n";
		}
		if ($data[0] eq "" ) {shift @data; }		
	}
	
#	output only valid field values
SKIP2:	for ($i=0; $i <= $#data; $i=$i+1) {
		foreach $j (@notuse) {
			next SKIP2 if $j == $i ;
			if ($fields_in_data[$i] eq "time_local_alt" and  
				$format eq "old") {
				$hour = int($data[$i]);
				if ( $hour < 1 ) {$hour='00'; }
				elsif ($hour < 10)  {$hour='0' . $hour; }
				$min = ($data[$i] - $hour)*60 ;
				$min = sprintf "%6.2f", $min;
				$min =~ s/ //g;
				if ( $min < 10 ) {$min='0' . $min; } #has leading 0
#				print STDOUT ("#**debug, data[$i]=$data[$i]\n");
				$data[$i] = $hour . $min ;
#				print STDOUT ('#**debug, hour=', $hour,
#					' min=', $min, " data[$i]=", 
#					$data[$i], "\n");
				print STDOUT "$data[$i]\t";
			}
			elsif ($fields_in_data[$i] eq "lat" or
					$fields_in_data[$i] eq "lon") {
				$data[$i] =~ s/-999.000000/nd/;
				print STDOUT "$data[$i]\t";
			}
			elsif ($fields_in_data[$i] eq "temp") {
				if ($data[$i] =~ m/50.000/) {
					$data[$i] = 'nd';;
					$temp_bad = 'yes';
				}
				print STDOUT "$data[$i]\t";
			}
			elsif ($fields_in_data[$i] eq "sal") {
				if ($data[$i] =~ m/50.000/ or $temp_bad eq 'yes') {
					$data[$i] = 'nd';
					$sal_bad = 'yes';
				}
				print STDOUT "$data[$i]\t";
			}
			elsif ($fields_in_data[$i] eq "sigma" and
					$sal_bad eq 'yes') {
				$data[$i] = 'nd';
				print STDOUT "$data[$i]\t";
			}
			elsif ($temp_bad eq 'yes' and 
					($fields_in_data[$i] eq "theta" or 
					$fields_in_data[$i] eq "sal" or
					$fields_in_data[$i] eq "sigma") ) {
				$data[$i] = 'nd';
				print STDOUT "$data[$i]\t";
			}
			else { print STDOUT "$data[$i]\t"; }
			last;
		}
	}
	print STDOUT "\n";
}

close MOCDATA;

undef $error;
undef $warning;

exit 0;
	
#---------------------------------------------------------------------------

sub sendmessage {

#Send a message to the user.
#The message sent will be in the strings $_[1] and $_[2]
#The prefix string is in $_[0]

my ( @args, $mailfile, $message0, $message1, $prefix, $who);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);

$prefix=$_[0];
$message0=$_[1];
$message1=$_[2];
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
if ($year >= 100 and $year <1000) { $year = $year + 1900; }
$mon++;
if ($mon < 10) { $mon = "0" . $mon; }
if ($mday < 10) {$mday = "0" . $mday; }
if ($hour < 10) { $hour = "0" . $hour; }
if ($min < 10) { $min = "0" . $min; }
if ($sec < 10) { $sec = "0" . $sec; }
$mailfile=">/tmp/sendmess" . $year . $yday . $hour . $min . $sec . ".tmp";

if ( open TEMPFILE, $mailfile) {
	if ( exists $ENV{'REMOTE_HOST'} ) {$who=$ENV{'REMOTE_HOST'} ; }
	elsif (exists $ENV{'REMOTE_ADDR'} ) {$who=$ENV{'REMOTE_ADDR'} ; }
	else {$who="not available"; }
	print TEMPFILE ("Message from $0\n");
	print TEMPFILE (" Date of message: $year/$mon/$mday $hour:$min\n");
	print TEMPFILE (" From: $who\n");
	print TEMPFILE (" $message0\n");
	print TEMPFILE (" $message1\n");
	close TEMPFILE;
	`/bin/mail dmo\@globec.whoi.edu <$mailfile`;
	unlink $mailfile;
}
print STDOUT ($prefix,"Message from $0\n");
print STDOUT ($prefix," Date of message: $year/$mon/$mday $hour:$min\n");
print STDOUT ($prefix," $message0\n");
print STDOUT ($prefix," $message1\n");

return;
}	
