#!/usr/local/bin/perl -w # # mocpro_level2.pl # mocness tow data file processing routine - level 2 # 23June97/srg # Further modified August 11, 1997 rcg # Modified Nov. 4, 1997 Change to level 2. rcg # Modified Nov. 10, 1997. Change sorting of tow names to be case insensitive. # Also added a space after # in output lines. rcg # November 14, 1997 rcg Change output message to say displayed by. # November 30, 1997. rcg Add date test for string month in day field. # December 3, 1998. rcg Redirect messages to DMO mail, rather than as comments, # add cruiseid and year to incoming parameters and cruiseid # to next level script. # December 4, 1998. Add local year as expected passed parameter; Look # at eventlog for old cruises to get day and month values. # December 8, 1998. Add test if comment field is defined. # December 17, 1998. Add host name or number to mailed error message. # January 28, 1999. Fix misspelling of MAILFILE (was TEMPFILE) in # message subroutine. rcg # Febrary 18, 1999. Display tow values with underscores (_) changed # to minus signs. rcg # March 24, 1999. Add station and standard station fields from eventlog # file when available. rcg # # Looks at all the MOC files in the /data2/gbdata and other places # processing those with a "moc" subdir and then # the files found with the file type specified at run-time. # # sets up info required for mocpro_level2.pl based # on results of files lists and initial header data # in the data files # # Passed parameter # $ARGV[0] = data directory for these data # $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.) # $ARGV[3] = cruiseid # $ARGV[4] = local four digit year $version="v2.00/Mar. 24, 1999"; #some definitions $script_loc="/data/cashjian"; # script location dir $script_name="mocpro_level3.pl"; # next script name $i_am_level=2; $fieldnames_level[0]="/data/cashjian/fieldnames_level0"; $fieldnames_level[1]="/data/cashjian/fieldnames_level1"; $fieldnames_level[2]="/data/cashjian/fieldnames_level2"; $fieldnames_level[3]="/data/cashjian/fieldnames_level3"; $moc1patterns="/data/cashjian/moc1patterns"; $moc10patterns="/data/cashjian/moc10patterns"; $mocpoint25patterns="/data/cashjian/mocpoint25patterns"; ($moc_dir, $datatype, $filetype, $cruiseid, $year) = @ARGV; $filetypeuc=uc $filetype; print STDOUT ("#Displayed by $0, $version\n", "# For $moc_dir, $datatype, $filetype, $cruiseid, $year\n"); $datatype = uc $datatype; if ($datatype eq "MOC10" ) { open PATTERN, $moc10patterns or die "# $0 could not read $moc10patterns patterns file, $!"; @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=; 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=; 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 "# $0 cannot handle filetype=$filetype"; exit 0; } $index=-1; $data=(); @datafile = <$moc_dir/*.$filetype>; # get the data files push @datafile, <$moc_dir/*.$filetypeuc>; foreach $checkfile (@datafile) { # print STDOUT "\n**debug, checkfile: $checkfile\n"; foreach $pattern (@pattern) { $_=$checkfile; if ( m/$pattern/ ) { $datafile=$checkfile; # print STDOUT ("#**debug: fileokay, pattern=$pattern, ", # " datafile=$datafile\n"); goto FILEOKAY; } } next; #skip this file, not of interest FILEOKAY:open(PROFILE, $datafile) or die "# Could not open data file $datafile, $!"; # print STDOUT "\n**debug, opening file=$datafile\n"; $index++; $tow[$index]="nd"; $month_local[$index]=0; $day_local[$index]=0; $datafile[$index]=$datafile; READDATA:while() { $record=$_; chomp; if( m/Tow/ ) { @towline = split; $tow[$index] = $towline[1]; } elsif( m/Date:/ ) { if ((index $record, "/" ) > 0) { @dateline = split; $dateline[1]=~s/ //g; @cruisedate = split(/\//,$dateline[1]); # print STDOUT ("#**debug, file=$datafile ", # "dateline[1]=$dateline[1] ", # "cruisedate[0]=$cruisedate[0] ", # "cruisedate[1]=$cruisedate[1]\n"); $month_local[$index] = $cruisedate[1]; $day_local[$index]=$cruisedate[0]; } else { @cruisedate = split /\s/, $record; $month_local[$index]=$cruisedate[$#cruisedate-2]; $day_local[$index]=$cruisedate[$#cruisedate-1]; $test=&monthtonum ($day_local[$index]); if ( $test <= 12 and $test >= 1) { $day_local[$index]=$month_local[$index]; $month_local[$index]=$test; } } if (! defined $month_local[$index]) { $month_local[$index]=0; } if (! defined $day_local[$index]) { $day_local[$index]=0; } if ($month_local[$index] < 1 or $month_local[$index] > 12 ) { $message0="Bad month value of " . $month_local[$index]; $message1="Data file=$datafile"; &sendmessage ($message0, $message1); } if ($day_local[$index] < 1 or $day_local[$index] > 31 ) { $message0="Bad day value of $day_local[$index]"; $message1="Data file=$datafile"; &sendmessage ($message0, $message1); } last READDATA; } elsif ( m/^\d\d.*/ ) { $tow[$index] = $datafile; $tow[$index] =~ s/^.*-(\d{1,})\.$filetype$/$1/; &get_some_data_from_eventlog ( $cruiseid, $datatype, $tow[$index], $day_local[$index], $month_local[$index],$dummy1,$dummy2); unless (defined $month_local[$index]) { $month_local[$index] = "nd"; } unless (defined $day_local[$index] ) { $day_local[$index]= "nd"; } last READDATA; } else { $message0= "Unexpected data format found" . " in file " . $datafile[$index]; #message1= "Record read=" . $_; &sendmessage ($message0, $message1); last READDATA; } } close(PROFILE); $i=$index; $string= "(" . $script_loc . "/" . $script_name . " " . $datatype . " " . $filetype . " " . $datafile[$i] . " " . $cruiseid . ")"; $townumb = $tow[$i]; $townumb =~ s/_/-/g; if ( $townumb =~ m/[-Mm]/ ) { $townumb =~ s/.*-(\d+)$/$1/; } &get_some_data_from_eventlog ($cruiseid, $datatype, $townumb, $station,$station_std); $dataline = $townumb . "\t" . $day_local[$i] . "\t" . $month_local[$i] . "\t" . $station . "\t" . $station_std . "\t" . $string . "\n"; push @data, $dataline; } #print STDOUT "#**debug, length of fieldnames_level=$#fieldnames_level\n"; for ($i=$i_am_level; $i <= $#fieldnames_level; $i++) { # print STDOUT "#**debug, fieldnames_level[$i]=$fieldnames_level[$i]\n"; open FIELDNAMES, $fieldnames_level[$i] or die "# Could not get field names from $fieldnames_level[$i], $!"; @fieldnames=; close FIELDNAMES; chomp (@fieldnames); foreach $variable (@fieldnames) { print STDOUT "$variable\t" unless (index $variable, "#") >= 0; } print STDOUT "\n"; } #print STDOUT ("#**debug, last index=$index\n#\t", # "Lengths of tow, day_local, month_local=$#tow, ", # "$#day_local, $#month_local\n"); foreach $line (sort numalpha @data) { print STDOUT $line; } undef $dummy1; undef $dummy2; exit 0; #--------------------------------------------------------------------------- sub sendmessage { #Send a message to the DMO via e-mail. (revised 12/2/1998) #The message sent will be in the strings $_[0] and $_[1] my ( @args, $mailfile, $message0, $message1, $who); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst); $message0=$_[0]; $message1=$_[1]; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); $mailfile="/tmp/sendmess" . $year . $yday . $hour . $min . $sec . ".tmp"; #print STDOUT ("#**debug, mailfile=$mailfile\n"); if ( open MAILFILE, ">$mailfile") { print MAILFILE ("#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 MAILFILE ("# Date of message: $year/$mon/$mday $hour:$min\n"); print MAILFILE (" From: $who\n"); print MAILFILE ("# $message0\n"); print MAILFILE ("# $message1\n"); close MAILFILE; `/usr/bin/mail -w dmo\@globec.whoi.edu <$mailfile`; unlink $mailfile; } else { print STDOUT ("#Message from $0\n"); print STDOUT ("# Date of message: $year/$mon/$mday $hour:$min\n"); print STDOUT ("# $message0\n"); print STDOUT ("# $message1\n"); print STDOUT ("# Could not send e-mail, error=$!\n", "#\tmailfile=$mailfile"); } return 0; } #--------------------------------------------------------------------------- sub numalpha { my ($a1, $b1, @rest, $type); ($a1, @rest)=split /\t/, $a; ($b1, @rest)=split /\t/, $b; $a1=lc $a1; $b1=lc $b1; #print STDOUT "#**debug, a1=$a1, b1=$b1, "; $type="n"; $_=$a1; if ( m/.*^\D.*/ ) { $type="l";} $_=$b1; if ( m/.*^\D.*/ ) { $type="l";} #print STDOUT " type=$type\n"; if ($type eq "n") { if ( ($a1 <=> $b1) == -1) { return -1; } elsif( ($a1 <=> $b1) == 0) { return 0; } else { return 1; } } else { if ( ($a1 cmp $b1) == -1) { return -1; } elsif( ($a1 cmp $b1) == 0) { return 0; } else { return 1; } } } #--------------------------------------------------------------------------- sub monthtonum { #Given the text string for the month, $_[0], , return the month number, 1 - 12 my %month = ( jan => 1, january => 1, feb => 2, february => 2, mar => 3, march => 3, apr => 4, april => 4, may => 5, jun => 6, june => 6, jul => 7, july => 7, aug => 8, august => 8, sep => 9, sept => 9, september => 9, "oct" => 10, october => 10, nov => 11, november => 11, dec => 12, december => 12, ); if (defined $month{lc $_[0]}) { return $month{lc $_[0]}; } else { return "nd"; } } #--------------------------------------------------------------------------- sub get_some_data_from_eventlog { # Use the event log object to get at day and month for old format data # Arguments passed are: # $_[0] = cruiseid # $_[1] = datatype # $_[2] = tow # Values returned are: # $_[3] = day_local # $_[4] = month_local # $_[5] = station # $_[6] - station_std my ($cast, $comment, $cruiseid, $datatype, $day, $input, $listgb, $listgb_options, $message0, $message1, $month, $object_spec, $station, $station_std, $tow ); $cruiseid = $_[0]; $datatype = uc $_[1]; $tow = $_[2]; #print STDOUT ("#**debug, cruiseid=$cruiseid, datatype=$datatype, tow=$tow\n"); $listgb = "/data5/globec/bin/listgb "; $listgb_options = "-c -n -b -t "; $object_spec = "\"/globec/gb/eventlogs(cruiseid=$cruiseid,inst=$datatype," . "cast,day_local,month_local,station,station_std,comments)\""; $input = $listgb . $listgb_options . $object_spec . "|"; $_[3] = "nd"; $_[4] = "nd"; $_[5] = "nd"; $_[6] = "nd"; unless ( open EVENTLOG, $input) { $message0="Could not open eventlog to get day and month"; $message1="open=$input"; &sendmessage ($message0, $message1); return; } # Search for correct tow number in comment field and use day and month values while () { ($cast, $day, $month, $station, $station_std, $comment) = split /\t/; if (defined $cast and ($cast=~m/\d+/) and ($tow=~m/\d+/) ) { if ( $cast == $tow ) { $_[3] = $day; $_[4] = $month; $_[5] = $station; $_[6] = $station_std; last; } } elsif (defined $comment and ($comment=~m/\d+/) and ($tow=~m/\d+/) ) { if ( $comment == $tow ) { $_[3] = $day; $_[4] = $month; $_[5] = $station; $_[6] = $station_std; last; } } elsif (defined $cast) { if ( $cast =~ m/$tow/ ) { $_[3] = $day; $_[4] = $month; $_[5] = $station; $_[6] = $station_std; last; } } elsif (defined $comment) { if ( $comment =~ m/$tow/ ) { $_[3] = $day; $_[4] = $month; $_[5] = $station; $_[6] = $station_std; last; } } } close EVENTLOG; return; }