#!/usr/local/bin/perl -w
#
# ctd_so_level1.pl

# CTD data file processing routine - level 1

$version = "V1.00/December 20, 2002";

# December 20, 2002 Robert C. Groman

# Looks at all the CTD files specified in subdirectory $ARGV[1],
# processing those with the specified patterns specified at run-time
# via the configuration file specified as $ARGV[0].

# Sets up info required for the next level based
# on results of files founde matrix (vector) being read.
#
# Passed parameter
#	$ARGV[0] = configuration file
#	$ARGV[1] = subdirectory of files to process

# Asuumptions
#	1. Program code must match the number and type of arguments specified in
# the level 1 field names from the configuration file.
#	2. Field name key words are specified as ID_<field_name> within
# the configuration file.  For example, cast is ID_cast.  Use these key words
# to search for the value within the data file header.
#	3. Assumes <ctd_event_file> is located below the ship name
# directory, one level higher than the subdirectory passed.  We get 
# all the date, time, position information from here.

unshift (@INC, "/data/rgroman/perllib");
        require ('sendmessage.pl');
        require ('make_log_entry.pl');
        require ('read_configuration_file.pl');
        require ('bob_setup.pl');
        require ('bob_unsetup.pl');

&bob_setup();

$i_am_level=1;
$max_levels = 10;

%months = ('Jan', 1,
	'Feb', 2,
	'Mar', 3,
	'Apr', 4,
	'May', 5,
	'Jun', 6,
	'Jul', 7,
	'Aug', 8,
	'Sep', 9,
	'Oct', 10,
	'Nov', 11,
	'Dec', 12);

unless ( defined $ARGV[0] ) {
	print STDOUT ("$error Displayed by $0, $version\n",
		"$error Required configuration file not specified.\n");
	exit;
}

unless (defined $ARGV[0] ) {
	&make_log_entry("Configuration file not passed along");
	&sendmessage($error, "Required configuration file name not passed along",
		"Cannot continue");
	exit;
}
$configuration_file = $ARGV[0];

@required = (
	'field_names_level_0', 'field_names_level_1', 'field_names_level_2', 
	'cast_pattern',
	'ctd_event_file', 'data_file_type', 'data_file_pattern', 
	'next_script', 'next_configuration_file'

);

&read_configuration_file($configuration_file);

$debug = 'no';
if (exists $config_param{'debug'} and defined $config_param{'debug'} ) {
	if ($config_param{'debug'} =~ m/^y/i or 
	   $config_param{'debug'} =~ m/^1/ or 
	   $config_param{'debug'} =~ m/^t/i) {;
		$debug = "yes";
	}
	elsif ($config_param{'debug'} =~ m/^keep/i ) {
		$debug = "keep";
	}
}

&make_log_entry (">>>>>>>Begin Program=$0",
	"Date of run=$date\tVersion=$version",
	"Configuration file=$configuration_file");


print STDOUT ("#Displayed by $0, $version\n");
unless (defined $ARGV[1] ) {
	&make_log_entry("Data file directory not passed along");
	&sendmessage($error, "Required data file directory not passed along",
		"Cannot continue");
	exit;
}
$data_subdirectory = $ARGV[1];
print STDOUT ("#  Data subdirectory is $data_subdirectory\n");

$status = ' ';
foreach (@required) {
	unless (exists $config_param{$_} and defined $config_param{$_} ) {
		$status = $status . ' ' . $_;
	}
}

unless ($status eq ' ') {
	&make_log_entry(
		"Required configuration parameters not defined=$status");
	&sendmessage($error, "Required configuration parameters $status",
		"not defined.");
	exit;
}

#Determine number of levels
for ($i=0; $i<=$max_levels; $i++) {
	$last_fieldname_level = $i-1;
	unless (defined $config_param{"field_names_level_$i"} and
		exists$config_param{"field_names_level_$i"} ) {last} ;
}

if ($last_fieldname_level >= ($max_levels-1) ) {
	&make_log_entry("Maximum number of levels exceeded=$max_levels");
	&sendmessage($error, "Maximum number of levels exceeded",
		"at $max_levels   Cannot continue");
	exit;
}

$data_file_type = $config_param{'data_file_type'}; 
$data_file_type_uc = uc $config_param{'data_file_type'}; 
$data_file_pattern = $config_param{'header_file_pattern'}; 
$data_file_pattern_uc = uc $config_param{'header_file_pattern'};

$ctd_event_file = $data_subdirectory . '/' . $config_param{'ctd_event_file'};
    
$cast_pattern = $config_param{'cast_pattern'};

$next_script = $config_param{'next_script'};
$next_config_file = $config_param{'next_configuration_file'};

# Output field names

for ($i=$i_am_level; $i <= $last_fieldname_level; $i=$i+1) {
	unless (defined $config_param{"field_names_level_$i"} and
			exists $config_param{"field_names_level_$i"} ) {last;}
	@fieldnames = split /,/, $config_param{"field_names_level_$i"};
	if ($i == $i_am_level) {
		@current_field_names = @fieldnames;
	}
	foreach $variable (@fieldnames) {
		$variable =~ s/^\s+//;
		$variable =~ s/\s+$//;
		print STDOUT ("$variable\t");
	}
	print STDOUT "\n";
}

@headerfiles = <$data_subdirectory/$data_file_pattern\.$datar_file_type>;
@headerfiles_uc = <$data_subdirectory/$data_file_pattern_uc\.$data_file_type_uc>;
push @headerfiles, @headerfiles_uc;

if ($#headerfiles < 0 ) {
	&make_log_entry("Expected header files in directory=$data_subdirectory",
		"With header type=$header_file_type");
	&sendmessage($error,"No header files of type $header_file_type found in",
		"directory $data_subdirectory");
	exit;
}

foreach $file (sort @headerfiles)   { 
	if ($file eq '.' or $file eq '..') {next;}
	$datafile = $file;
	$datafile =~ s/\.$header_file_type/\.$data_file_type/;
	print STDOUT ("#***debug, datafile=$datafile\n") if $debug eq 'yes';
	unless (-e $datafile and $file ne $datafile) {
		$datafile =~ s/\.$header_file_type_uc/\.$data_file_type_uc/;
		print STDOUT ("#***debug, uc datafile=$datafile\n") 
				if $debug eq 'yes';
		unless (-e $datafile) {
			&make_log_entry("No data file for header=$file");
			next;
		}
	}
	
	&make_log_entry("Processing header file=$file");
	print STDOUT ("#***debug, file=$file\n") if $debug eq 'yes';
	unless (open HEADERDATA, $file ) {
		$status = $!;
		&make_log_entry("Could not open header file=$file",
			"Status=$status");
		&sendmessage($error,
			"Could not open header file $file",
			"Status was $status");
		exit;
	}
	
# Read header file and extract out level 1 data

	@records = <HEADERDATA>;
	close HEADERDATA;
	print STDOUT ("#***debug, current_field_names=@current_field_names\n")
		if $debug eq 'yes';

	foreach $fieldname (@current_field_names) {		
		if ($fieldname eq 'year') { 
			$key = 'ID_year';
		}
		elsif ($fieldname eq 'month' or $fieldname eq 'day' or
				$fieldname eq 'time' or $fieldname eq '>') {
			next;
		}
		else {
			$key = 'ID_' . $fieldname;
		}		
		unless (exists $config_param{$key} and defined $config_param{$key} ) {
			&make_log_entry("Key word not defined for=$key");
			&sendmessage($error,
			"Key word not defined for key $key",
			"Cannot continue.  Contact the DMO.");
			exit;
		}
		$key_equiv = $config_param{$key};
		$key_equiv =~ s/%/ /g;
		$value = &find_key_value_in_records ($key_equiv, @records);
		print STDOUT ("#***debug, key=$key, key_equiv=$key_equiv, value=$value\n")
			if $debug eq 'yes';
		if ($key eq 'ID_year' ) {
			if ($value eq 'nd') {
				$fieldname{'year'} = 'nd';
				$fieldname{'month'} = 'nd';
				$fieldname{'day'} = 'nd';
				$fieldname{'time'} = 'nd';				
				next;
			}
			$month = $value;
			$month =~ s/(\w\w\w) (\d\d) (\d\d\d\d) (\d\d):(\d\d):(\d\d)/$1/;
			unless (defined $month ) {$month = 'nd';}
			if ( exists $months{$month} and defined $months{$month} ) {
				$month = $months{$month};
			}
			else {
				$fieldname{'year'} = 'nd';
				$fieldname{'month'} = 'nd';
				$fieldname{'day'} = 'nd';
				$fieldname{'time'} = 'nd';				
				next;
			}
			$fieldname{'month'} = $month;
			unless (defined $2 ) {
				$fieldname{'day'} = 'nd';
			}
			else {
				$fieldname{'day'} = $2;
			}
			unless (defined $3 ) {
				$fieldname{'year'} = 'nd';
			}
			else {
				$fieldname{'year'}  = $3;
			}
			unless (defined $5 and defined $6 ) {
				$fieldname{'time'} = 'nd';
			}
			else {
				$minutes = $5 + ($6/60);
				if ($minutes < 10) {$minutes = '0' . $minutes;	}		
				$fieldname{'time'} = $4 . $minutes;
				$fieldname{'time'} = sprintf '%7.2f', $fieldname{'time'};
			}		
			next;
		}
		elsif ($key eq 'ID_lat' or $key eq 'ID_lon') {
			($deg,$minute,$direction) = split / /, $value;
			unless (defined $deg and defined $minute and defined 
					$direction) {
				$fieldname{$fieldname} = 'nd';
				next;
			}
			if ($direction eq 'W' or $direction eq 'S') {
				$direction = -1;
			}
			elsif ($direction eq 'E' or $direction eq 'N') {
				$direction = 1;
			}
			else {
				$fieldname{$fieldname} = 'nd';
				next;
			}
			$value = $direction * ($deg + ($minute/60));
			$width = 9;
			if ($key eq "ID_lon" ) {$width =10;}
			$value = sprintf "%${width}.4f", $value;
			$fieldname{$fieldname} = $value;
		}
		else {
			$value =~ s/ /_/g;
			unless (defined $value ) {$value = 'nd';}
			if ($value eq "") {$value = 'nd';}
			$fieldname{$fieldname} = $value;
		}
	}
	foreach $fieldname (@current_field_names) {
		if ($fieldname eq ">" ) {next;}
		unless (exists $fieldname{$fieldname} and defined $fieldname{$fieldname} ) {
			&make_log_entry("Value not defined for fieldname=$fieldname");
			&sendmessage($warning,
				"Value not defined for fieldname $fieldname",
				"Will use nd value.");
			print STDOUT ("nd\t");
		}
		else {		
			print STDOUT ($fieldname{$fieldname},"\t");
		}
	}

	print STDOUT ("($next_script $next_config_file $datafile)\n");
}		

&make_log_entry ("<<<<<<<End Program=$0");

&bob_unsetup();
exit 0;

#-------------------------------------------------------------------------

sub find_key_value_in_records {

# Find key $_[0] with the records passed along as $_[n], n=1,2,3,...
# Return the value found with % characters converted to spaces or
# the value of "nd" if not found.

my ($i, $key, $record, $value);

unless (defined $_[0]) {return "nd";}
$key = $_[0];

#print STDOUT ("#***debug, key value=$key\n") if $debug eq "yes";
for ($i=1; $i <= $#_; $i++) {
	$record = $_[$i];
	chomp $record;
#	print STDOUT ("#***debug, record=$record\n") if $debug eq "yes";
	if ($record =~ m/$key/ ) {
		$value = $record;
		$value =~ s/.*$key(.*)/$1/;
		$value =~ s/^\s+//;
		$value =~ s/\s+$//;
		unless (defined $value) {$value = "nd";}
		return $value;
	}
}
return "nd";
}
