#!/usr/local/bin/perl -w # process_email.pl # Program to read in data sent in from the NOPP project and update # the necessary data files with the new data. # All necessary run-time and configuration values are read in from # a configuration file specified as the first argument in the command # line. $version="V1.00 October 4, 1999"; # October 4, 1999. V1.00 R. Groman $| = 1; $error = "&x"; $warning = "#"; $configuration_file = @ARGV[0]; print STDOUT ("\n#Program $0 Version: $version\n"); print STDOUT ("# configuration file is $configuration_file\n"); @required = ("archive_data_directory","new_data_directory", "catch_info_file", "catch_info_field_names", "tow_info_file", "tow_info_field_names", "vessel_info_file", "vessel_info_field_names", "vessel_stats_field_names", "vessel_stats_file", "vessel_code_file"); &read_configuration_file($configuratino_file); $okay = "yes"; for ($i=0; $i<=$#required; $i++) { unless (exists $$config_param{"$required[$i]"} ) { $okay="no"; &sendmessage ($error, "$required[$i] is missing from configuration ", "file=$configuration_file"); } } if ( $okay eq "no") { &sendmessage ($error, "One or more parameters are missing from the configuration file", "Cannot continue."); exit; } &process_data; undef $error; undef $warning; undef $version; exit; #------------------------------------------------------------------------ sub read_configuration_file { # Open and read configuration file specified as first passed parameter # $_[0]. Return the contents of the file as the hash array %config_param. # Lines beginning with "#" are treated as comments. It is assumed # that the indirect file contains lines as # parameter = value # and this information is stored as # $config_param{"parameter"} = value my $filename = $_[0]; my ($parameter, $value); print STDOUT ("#**debug, indirect filename=$filename\n"); unless (open CONFIG_FILE, $filename) { &sendmessage ($error, "Could not open indirect file=$filename", "Error code=$!. Cannot continue."); exit; } while () { if (m/^#/) { next;} ($parameter, $value) = split /=/; chomp $value; $parameter =~ s/^ //; $value =~ s/ $//; $config_param{$parameter} = $value; print STDOUT ("#**debug, config_param{$parameter}=", $config_param{$parameter}, "\n"); } close CONFIG_FILE; } #------------------------------------------------------------------------ sub sendmessage { #Send a message to the user. #The message sent will be in the strings $_[0] and $_[1] 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 > 97 and $year < 100 ) {$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) { print TEMPFILE ("Message from $0\n"); if ( exists $ENV{'REMOTE_HOST'} ) {$who=$ENV{'REMOTE_HOST'} ; } elsif (exists $ENV{'REMOTE_ADDR'} ) {$who=$ENV{'REMOTE_ADDR'} ; } else {$who="not available"; } 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; `/usr/bin/mail -w dmo\@globec.whoi.edu <$mailfile`; # unlink $mailfile; } print STDOUT ($prefix," $message0\n"); print STDOUT ($prefix," $message1\n"); print STDOUT ($prefix," Above message from $0\n"); print STDOUT ($prefix," Date of message: $year/$mon/$mday $hour:$min\n"); return 0; } #---------------------------------------------------------------------------- sub process_data { # Process the new data from the file directory specified in # $config_param{new_data_directory}. Processed data are moved to # $config_param{archive_data_directory}. # Assumptions: # 1. Hash %config_param contains the necessary run-time parameters. my ($file, @files_to_process); # Make list of files to process. If new data are added during this # processing run, newer files are skipped until next run of the program. @files_to_process = grep !/^\./, readdir $config_param{"new_data_directory"}; unless ($#files_to_process >= 0) { print STDOUT ("\nNo files to process this time.\n"); return; } # Start reading files; determine the type of file it is and go from there # Store field names and data in the hash array %data, but watch out for # duplicates. DATAFILE: foreach $file (@files_to_process) { unless (open DATA, "$config_param{"new_data_directory"}/$file") { print STDOUT ("Could not open data file\n\t", "$config_param{"new_data_directory"}/$file\n\t", "Continuing to next file.\n"); next; } $catch_counter = -1; # Read contents of file. Skip records unless they contain a # a "=:" string. This gets around other stuff for now. # A special case are the Athena data files. Check for those # first and process them separately. foreach $record () { if ($record =~ m/ATHENA V/ ) { &add_athena_data; next DATAFILE; } unless ($record =~ m/=:/ ) { next; } ($field_name, $value) = split /=:/, $record; chomp $value; $value =~ s/.* *$//; #strip trailing blanks $field_name = lc $field_name; $field_name =~ s/.* *$//; $field_name =~ s/^ *//; #strip leanding blanks $field_name =~ s/ /_/g; #Replace embedded blanks with _ $field_name =~ s/\//_/g; #Replace slashes with underscores $value =~ s/^ *//; if (exists $data("$field_name" and $field_name eq "catch") { $catch_counter++; $key = "catch_" . $catch_counter; $data{"$key} = $value; } elsif (exists $data("$field_name" and $data{"$field_name"} ne $value ) { print STDOUT ("$warning Duplicate field name, ", $field_name, ", found, with\n", "different values ", $data("$field_name", " and $value\n Will continue, ignoring newer ", "value.\n"); } else { $data{"$field_name"} = $value; } } # Determine type of data file to write or append to. if ($data{"file header"} eq "vessel_stats") { &add_data ("vessel_stats"); } elsif ($data{"File header"} eq "tow_header") { &add_data ("tow_header"); else { print STDOUT "$warning, Could not handle File header = ", $data{"file header"}, ". Skipping this data.\n\n"); } undef %data; } } #------------------------------------------------------------------------- sub add_athena_data { # Read in this data file as athena data and write it out to the data file(s). print STDOUT ("Routine add_athena_data - stub"); return; #------------------------------------------------------------------------- sub add_data { # Read in data and write it out to the appropriate data file(s). # Passed parameter: # $_[0] = file header string (defines file type), e.g. # vessel_stats or tow_info # Determine if data exists. If not, issue error message and return. my ($file_name, $file_type); $file_type = $_[0]; $file_name = $config_param{$file_type . "_file"}; if ( -e $file_name} { unless (open DATAFILEOUT ">>$file_name") { print STDOUT ("$warning, Could not open $file_name", " for writing. Error=$!\n. Continuing...\n\n"); return; } } else { return;} # Update Vessel Info file, if necessary, if getting vessel stats data if ($file_type eq "vessel_stats" ) { &update_vessel_info_file; &update_vessel_stats_file ($file_name); } elsif ($file_type eq "tow_info") { &create_tow_file ("nd"); else { print STDOUT ("$warning, Could not update $file_type data.\n\t", "Unknown data type. Continuing.\n\n"); } return; } #--------------------------------------------------------------------- sub update_vessel_info_file { # Update vessel info file specified by $config_param{"vessel_info_file"} # with the available information. my ($field_name, @field_names, $file_name, $present); $file_name = $config_param{"vessel_info_file"}; if ( -e $file_name} { unless (open VESSELINFO, ">>$file_name") { print STDOUT ("$warning, Could not open $file_name", " for writing. Error=$!\n. Continuing.\n\n"); return; } } else { print STDOUT ("$warning, Could not open $file_name", "Does not exist. Error=$!\n\n. Continuing.\n\n"); return; } unless (exists $data{"vessel_name"} and defined $data{"vessel_name"} ) { print STDOUT ("$warning, Vessel name not defined.\n", "\tCould not update vessel info file. Continuing.\n\n"); return; } # Check if record already exists. $present = `grep $data{"vessel_name"} $file_name`; if ( $present =~ m/$data{"vessel_name"}/ ) { return } @field_names = split /,/, $config_param{"vessel_info_field_names"}; foreach $field_name (@field_names) { if ( exists $data{$field_name} and defined $data{$field_name} ) { print VESSELINFO, ( $data{$field_name}, "\t"); } else { print VESSELINFO, ( "nd\t"); } } print VESSELINFO ( "\n"); close VESSELINFO; return; } #------------------------------------------------------------------------ sub update_vessel_stats_file { # Update vessel stats file using the already opened file handle called # DATAFILEOUT. The name of this file is passed as $_[0]. # Must check to see if vessel stats file has existing record # for this trip before adding new one. my (@date, $date, $file_name); $file_name = $_[0]; unless (exists $data{"vessel_name"} and defined $data{"vessel_name"} ) { print STDOUT ("$warning, Vessel name not defined.\n", "\tCould not update stats file. Continuing.\n\n"); return; } # Check if record already exists. If not, write it in. @date = split / /, $data{"time_date_of_departure"}; if ($#date < 4 ) { $date = scalar localtime; @date = split / /, $date; } $tow_filename = &getvesselcode . "_" . $date[4] . $date[1] . $date[2]; $present = `grep $data{"vessel_name"} $file_name | grep $tow_filename $file_name`; unless ( $present =~ m/$data{"vessel_name"}/ ) { @field_names = split /,/, $config_param{"vessel_stats_field_names"}; foreach $field_name (@field_names) { if ( exists $data{$field_name} and defined $data{$field_name} ) { print DATAFILEOUT, ( $data{$field_name}, "\t"); } else { print DATAFILEOUT, ( "nd\t"); } } print VESSELINFO ( $config_param{"tow_info_file"}, $file_name, "\n"); close VESSELINFO } # Create tow file, assuming there are data &create_tow_file ($config_param{"tow_info_file"} . $file_name); return; } #-------------------------------------------------------------------- sub &create_tow_file { # Create tow file. Passed parameter, if present, is the full file spec # of the tow file to create. If set to "nd" will be defined. my ($catch_file_name, @date, $date, $tow_file); @date = split / /, $data{"time_date_of_departure"}; if ($#date < 4 ) { $date = scalar localtime; @date = split / /, $date; } if (defined $_[0]) { $tow_file = $_[0]; else { $tow_file = "nd" } if ( $tow_file eq "nd" } { $tow_file = $config_param{"tow_info_file"} . &getvesselcode . "_" . $date[4] . $date[1] . $date[2]; } if ( -e $tow_file) { unless (open TOWFILE, ">>$tow_file" ) { print STDOUT ("$warning, Could not open tow file \n\t", $tow_file, " for appending. Continue.\n\n"); return; } else { unless ( open TOWFILE, ">$tow_file" ) { print STDOUT ("$warning, Could not open new tow ", "file \n\t", $tow_file, ". Continue.\n\n"); return; } } @field_names = split /,/, $config_param{"tow_info_field_names"}; foreach $field_name (@field_names) { if ( exists $data{$field_name} and defined $data{$field_name} ) { print TOWFILE, ( $data{$field_name}, "\t"); } else { print TOWFILE, ( "nd\t"); } } $catch_file_name = &getvesselcode . "_" . $date[4] . $date[1] . $date[2]; print TOWFILE ( $config_param{"catch_info_file"}, $catch_file_name, "\n"); # Create tow file, assuming there are data &create_catch_file ($config_param{"catch_info_file"} . $catch_file_name); close TOWFILE; return; } #------------------------------------------------------------------ sub &create_catch_file { # Create catch file. Passed parameter, if present, is the full file spec # of the catch file to create. If set to "nd" will be defined. my ($catch_file, @date, $date); @date = split / /, $data{"time_date_of_departure"}; if ($#date < 4 ) { $date = scalar localtime; @date = split / /, $date; } if (defined $_[0]) { $catch_file = $_[0]; else { $catch_file = "nd" } if ( $catch_file eq "nd" } { $catch_file = $config_param{"catch_info_file"} . &getvesselcode . "_" . $date[4] . $date[1] . $date[2]; } if ( -e $catch_file) { unless (open CATCHFILE, ">>$tow_file" ) { print STDOUT ("$warning, Could not open catch file \n\t", $catch_file, " for appending. Continue.\n\n"); return; } else { unless ( open CATCHFILE, ">$tow_file" ) { print STDOUT ("$warning, Could not open new catch ", "file \n\t", $catch_file, ". Continue.\n\n"); return; } } @field_names = split /,/, $config_param{"catch_info_field_names"}; foreach $field_name (@field_names) { if ( exists $data{$field_name} and defined $data{$field_name} ) { print CATCHFILE, ( $data{$field_name}, "\t"); } else { print CATCHFILE, ( "nd\t"); } } print CATCHFILE, ( "\n"); close CATCHFILE; return; } #---------------------------------------------------------------------- sub getvesselcode { # Looks up the vessel code in file defined # $config_param{"vessel_code_file"} # and returns it. Otherwise, returns the value "nd". my ($code, $platform); $code = `grep $data{"vessel_name"} $config_param{"vessel_code_file"}`; if ($code =~ m/$data{"vessel_name"}/ { ($platform, $code) = split /\t/, $code; } else { $platform = "nd"; $code = "nd"; } undef $platform; return $code; } #-----------------------------------------------------------------------