#!/usr/bin/perl -w
#
# mocpro_level1_conf.pl  (uses conf file)

# mocness tow data file processing routine - level 1

## October 3, 2001. V2.07so. Southern Ocean version of script used
#	originally in GB program.    Field names
#	also come from original location, but patterns are "local".
#	rcg
# December 24, 2001.  V1.08ZG Modified for ZooGene data.  It won't find the
#	cruises in the globec inventory.  Either update it or
#	make an inventory for the zoogene project.  For now,
#	don't access this information since we'll get an error
#	message. rcg
# July 10, 2006  V1.10CM  Modified for CMarZ data  mda
#
# Dec. 12, 2011. V1.11BC Modified for BCO-DMO data. Scripts 
#       on dmoserv2 at /data/Scripts/ctd_mocness  mda + rcg
# Jan. 30, 2012.  V2.0  Modified to use a config file for data on dmoserv2
# 	at /data/Scripts/ctd_mocness  mda+ rcg
# February 13, 2012. V2.01 Divide comment so it fits on two lines. rcg
#
# November 7, 2012.  V2.1.  Modified to accept 'Date' in a different format and to
#	insert some debug lines. Also to get cruise_id from data header correctly.  mda + rcg
#
# All parameters now come from single config file.
#
# February 21, 2014. V2.1DM3. Copied version to dmoserv3 in order to process ctd_mocness files.
#                              mda
# April 11, 2014. V2.20 Add require statement to find the make_log_entry.pl routine. rcg

### YES: Passed parameter
#	$ARGV[1] = MOC1, or MOC10 or MOC.25 specifying which MOCNESS
#			data type to view
#	$ARGV[2] = file type to process, e.g. pro, tab, raw  (note:
#			the code will have to change to accommodate
#			other than pro format.)

# YES:Passed parameter
#	$ARGV[0] = configuration file

$version=" V2.20/April 11, 2014";


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

use lib '/data1/www/bco-dmo/perlmodule';
	require ('sendmessage.pl');
	require ('get_config_file_hash.pl');
	require ('make_log_entry.pl');

$configuration_file = $ARGV[0];

$i_am_level = 1;

@required = (
	'fieldnames_level_0', 'fieldnames_level_1', 'fieldnames_level_2',
        'fieldnames_level_3', 'level_2_script', 'data_directories',
	 'moc10patterns', 'moc1patterns', 'mocpoint25patterns'

);

$config_param_ref=&get_config_file_hash($configuration_file);
unless(defined $config_param_ref){
	print STDOUT ("#**error, could not get configuration file contents\n");
	exit;
}

%config_param=%{$config_param_ref};

$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;
}
my $debug = 'false';
if (exists $config_param{'debug'} and defined $config_param{'debug'}) {
	if ($config_param{'debug'} =~ m/^t/i or $config_param{'debug'} =~ m/^1/
			or $config_param{'debug'} =~ m/^y/i){
		$debug = 'true';
	}
}

#$inventory_object = "/globec/gb/inventory";
#$inventory_object = "/globec/unaffiliated/inventory_lmg0602";

$error="&x";
$warning="#";

$datatype=uc $ARGV[1];
$filetype=lc $ARGV[2];
$filetypeuc=uc $filetype;

$| = 1;		#set to flush output

$moc10patterns = $config_param{'moc10patterns'};
$moc1patterns = $config_param{'moc1patterns'};
$mocpoint25patterns = $config_param{'mocpoint25patterns'};

$level_2_script = $config_param{'level_2_script'};

print STDOUT ("#Displayed by $0 \n#  Version: $version\n",
	"#  For $datatype data, with filetype=$filetype\n");

if ($datatype eq "MOC10" ) {
	open PATTERN, $moc10patterns or 
		die "#$0 could not read $moc10patterns patterns file, $!";
	@pattern=<PATTERN>;
	chomp @pattern;
#	print STDOUT "#**debug, numb patterns=$#pattern, patterns=@pattern\n";
	close PATTERN;
}elsif ($datatype eq "MOC1" ) {
	open PATTERN, $moc1patterns or 
		die "#$0 could not read $moc1patterns patterns file, $!";
	@pattern=<PATTERN>;
	chomp @pattern;
#	print STDOUT "#**debug, numb patterns=$#pattern, patterns=@pattern\n";
	close PATTERN;
}elsif ($datatype eq "MOC.25" ) {
	open PATTERN, $mocpoint25patterns or 
		die "#$0 could not read $mocpoint25patterns patterns file, $!";
	@pattern=<PATTERN>;
	chomp @pattern;
#	print STDOUT "#**debug, numb patterns=$#pattern, patterns=@pattern\n";
	close PATTERN;
} else {
	print STDOUT "#$0 cannot recognize datatype=$datatype";
	exit 0;
}

if ($filetype eq "pro" ) {
#	yes, we can handle this format
}
else {
	print STDOUT "#Sorry, but I cannot handle filetype=$filetype";
}

$data = ();

$data_directories=$config_param{'data_directories'};
@top_datadir = split /,/,$data_directories;
for ($i=0; $i<=$#top_datadir; $i++) {
	$top_datadir[$i]=~ s/^\s+//;
	$top_datadir[$i]=~ s/\s+$//;
#print STDOUT ("\n#**debug, top_datadir = $top_datadir[$i]\n");
}

foreach $top_datadir (@top_datadir) {
#	print STDOUT ("\n#**debug, top_datadir2=$top_datadir\n");
	chdir($top_datadir) or die "#Cannot change to $top_datadir directory";
	opendir DATADIR, $top_datadir or 
		die "#Cannot open directory $top_datadir";
	@cruise_dir = grep -d, readdir DATADIR;
	foreach $cruise_dir (@cruise_dir)   { # get filenames
		$cruise_id = uc $cruise_dir;
#		print STDOUT ("#**debug, cruise_id from cruise_dir = $cruise_id\n")
#			if $debug eq 'true';   
#		print STDOUT "\n#**debug,cruise_dir=$cruise_dir\n";
		$moc_dir = $top_datadir . "/" . $cruise_dir . "/moc";
		if (! -e $moc_dir or ! -d $moc_dir )   {
			$moc_dir =~ s/\/moc$/\/mocness/;
			if (! -e $moc_dir or ! -d $moc_dir ) {next;}
		}
#		print STDOUT "#**debug,moc=$moc_dir\n";
		@datafile=<$moc_dir/*.$filetype>;
		push @datafile, <$moc_dir/*.$filetypeuc>;
		if ( $#datafile < 0 ) {next;}
		foreach $checkfile (@datafile) {
			$temp=$checkfile;     #used with debug line only
#         		print STDOUT "#**debug, checkfile: $checkfile\n";
			foreach $pattern (@pattern) {
				$_=$checkfile;

				if ( m/$pattern/ ) {
					$datafile=$checkfile;
					if ( $datafile =~ m/\n/ ) {
						$datafile =~  s/.*\n(.*)/$1/;
					}
					goto FILEOKAY;
				}
			}
		}
#		print STDOUT "#**debug, checkfile=$temp does not match pattern\n";
		next;	#skip this directory, not of interest
FILEOKAY:	
		unless (open(PROFILE, $datafile) ) { 
			print STDOUT ("#***debug, datafile=$datafile\n");
			print STDOUT ("#Could not open data file $datafile",
				" error=$!\n");
			next;
		}
		print STDOUT "\n#**get_config_file_hash.pldebug, opening file=$datafile\n";
		
		$cruise=$cruise_id;
		print STDOUT ("#**debug, cruise from cruise_id = $cruise\n")
			if $debug eq 'true'; 
		$year_gmt="nd";
		$ship= substr $cruise_id, 0, 2;
READDATA:	while(<PROFILE>) {
			$record=$_;
			chomp ;
			if( m/Tow/ )  {
				@towline = split;
				$cruise = uc $towline[$#towline];
				print STDOUT ("#**debug, cruise from towline = $cruise\n")
					if $debug eq 'true'; 

			}
			elsif( m/Date:/ ) {
				if ((index $record, "/" ) > 0) {
					my $datestring = $record;
					$datestring =~ s!.*date.*(\d{1,2}/\d{1,2}/\d{1,4})!$1!i;
					
					@dateline = split /\//, $datestring;
					unless ($#dateline == 2){
						$message0= "Unexpected date format found = $record" .
						" in file " . $datafile;
						#message1= "Record read=" . $_;
						&sendmessage ($warning, $message0, $message1);
						next;
					} 
					@cruisedate = @dateline; 
						
					$year_gmt = $cruisedate[2];
				}
				else {
					@cruisedate = 
						split /\s/, $record;
					$year_gmt = $cruisedate[$#cruisedate];
				}
				if (! defined $year_gmt) {$year_gmt="nd";}
				last READDATA;
			}
			elsif ( m/^\d\d.*/ )  {#old style format
				if ($ship eq "AL" or $ship eq "SJ") {
					$year_gmt=substr $cruise_id, 2, 2;
				}
				else {
					$year_gmt = "nd";
				}				
				last READDATA;
			}
			else {
				$message0= "Unexpected data format found" .
					" in file " . $datafile;
				#message1= "Record read=" . $_;
				&sendmessage ($warning, $message0, $message1);
				$year_gmt="nd";
				last READDATA;
			}
		}

		close(PROFILE);
		if (($ship eq "AL" or $ship eq "SJ") and $year_gmt eq "nd") {
			$year_gmt=substr $cruise_id, 2, 2;
		}
		if ($cruise ne $cruise_id) {
			$message0="#Something wrong with file=" .
				 $datafile;
			$message1="#Cruise id's do not match: " . 
				$cruise . " versus " .
				$cruise_id;
			&sendmessage ($warning, $message0, $message1);
			$cruise=$cruise_id; #Use value from directory name
		}
#		print STDOUT "\n#**debug ship=$ship\n";
#		print STDOUT "\n#**debug year_gmt=$year_gmt\n";
		if (($ship eq "AL" or $ship eq "SJ") and $year_gmt ne "nd") {
			if($year_gmt > 1000 ) {
				$testyear=$year_gmt - (int($year_gmt/100))*100;
			}
			else {
				$testyear=$year_gmt;
			}
			$yearfromdir=substr $cruise_id, 2, 2;
			if ($testyear != $yearfromdir) {
				$message0="Something wrong with the year " .
					"in " .
					$cruise . " for " .
				 	$datafile;
				 $message1="  Years do not match. " . 
				 	$testyear . " versus " .
					$yearfromdir;
				&sendmessage ($warning, $message0, $message1);
				$year_gmt=$yearfromdir;
			}
		}
		if ($year_gmt ne "nd" ) { 
			if ($year_gmt < 30 ) {
				$year_gmt+=2000;
			}
			elsif ($year_gmt < 100 ) {
				$year_gmt+=1900;
			}
		}	          
		$string = "(" . $level_2_script . " " . $configuration_file .
				" " . $moc_dir . " " . $datatype . 
				" " . $filetype . " " . 
				" " . uc $cruise . " " . $year_gmt . ")";
#		$field_values{"project"} = uc $cruise;
#		$projection{"brief_desc"} = "nd";
#		&get_first_data_from_object ($inventory_object);
		if ($year_gmt eq 'nd' and defined ($config_param{"data_year"})){
			$year_gmt=$config_param{"data_year"};
		}
				
		$dataline = $cruise . "\t" . $year_gmt. "\t" . 
			  $string . "\n";	
		push @data, $dataline;
    	}
}

# Set up fieldname lists
for ($j=0; $j<=9; $j++) {
	$level = 'fieldnames_level_'.$j;
	unless(exists ($config_param{$level}) and
		defined($config_param{$level})) {
		last;
	}
	$fieldnames_level[$j]=$config_param{$level};
	#print STDOUT (" **fieldnames_level[$j]=$fieldnames_level[$j] \n");
}


# variable list - from configuration file

for ($i=$i_am_level; $i <= $#fieldnames_level; $i=$i+1) {
	print STDOUT ($fieldnames_level[$i],"\n");
}

foreach $line (sort @data) {
	print STDOUT $line;
}

undef $error;
undef $warning;
exit 0;

