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

# CTD data processing routine for John Klinck data - level 0

$version = "V1.00/March 27, 2003";

# March 27, 2003 Robert C. Groman
# Copyright 2003, Woods Hole Oceanographic Institution

# Looks at all the John Klinck CTD files in the specified directories,
# processing those with the specified patterns specified at run-time.

# Sets up info required for the next level based
# on results of files found, initial data found
# in the data files, and run time parameters contained in the
# configuration file.
#
# Passed parameter
#	$ARGV[0] = configuration file

# Asuumptions
#	1. Field names for the last level will be supplied by the configuration
# files until the last level at which point it will be "data driven" and
# matched up.  It is left to the method to do a "transvar' (field name 
# translation) if desired.

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=0;
$max_levels = 10;

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

$configuration_file = $ARGV[0];

@required = (
	'data_directories', 'vessel_subdirectory_pattern',
	'subdirectory_path',
	'field_names_level_0', 'field_names_level_1', 'field_names_level_2', 
	'data_file_pattern', 'data_file_type',
	'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");

$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.");
	goto DONE;
}
	
# top level data directories
@very_top_datadir = split /,/, $config_param{'data_directories'};

@vessel_names = ();

#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");
	goto DONE;
}

  
$data_file_type = $config_param{'data_file_type'}; 
$data_file_type_uc = uc $config_param{'data_file_type'}; 
  
$data_file_pattern = $config_param{'data_file_pattern'}; 
$data_file_pattern_uc = uc $config_param{'data_file_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"};
	foreach $variable (@fieldnames) {
		$variable =~ s/^\s+//;
		$variable =~ s/\s+$//;
		print STDOUT ("$variable\t");
	}
	print STDOUT "\n";
}


foreach $top_dir (@very_top_datadir) {
	print STDOUT ("#***debug, top_dir=$top_dir\n") if $debug eq 'yes';
	unless (opendir DIRS, $top_dir) {
		&make_log_entry("Could not open directory=$top_dir");
		&sendmessage($warning,"Could not open directory $top_dir",
			"Continuing with next directory");
		next;
	}
	@sub_dirs = grep !/^\./, readdir DIRS;
	foreach $file (@sub_dirs) {
		print STDOUT ("#***debug, file=$file\n") if $debug eq 'yes';
		$pattern = $config_param{'vessel_subdirectory_pattern'};
		unless 
		  ($file =~ m/$pattern/ ) {
		  next;}
		$full_file_spec = $top_dir . '/' . $file . '/' .
					$config_param{'subdirectory_path'};
		print STDOUT ("#***debug, full_file_spec=$full_file_spec\n") 
			if $debug eq 'yes';
		unless ( -e $full_file_spec and -d $full_file_spec ) {
#			&make_log_entry(
#			 "Could not find directory=$full_file_spec");
			next;
		}
		&make_log_entry("Working on subdirectory=$file") if $debug eq 'yes';
		@datafiles = 
		  <$full_file_spec/$data_file_pattern.$data_file_type>;
		@datafiles_uc = 
		  <$full_file_spec/$data_file_pattern_uc.$data_file_type_uc>;
		push @datafiles, @datafiles_uc;
		if ($#datafiles < 0 ) {
			&make_log_entry("No ctd data for directory=$file");
			next;
		}
		print STDOUT ("#***debug, datafiles=$datafiles[0]\n")
			if $debug eq 'yes';
		$vessel_name = $file;
		
		$vessel_name =~ 
			s/.*($pattern).*/$1/;

		push @vessel_names, $vessel_name;
		$specific_cruise_directory{$vessel_name} = $full_file_spec;
	}
}

close DIRS;


foreach $vessel_name (sort @vessel_names) {
	print STDOUT (uc $vessel_name, "\t(",
		$next_script, " ", $next_config_file, " ",
		$specific_cruise_directory{$vessel_name},
		")\n");
}

DONE:

&make_log_entry ("<<<<<<<End Program=$0");
&bob_unsetup();
undef $warning;
undef $date;

exit 0;

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

