#!/usr/local/bin/perl -w # # nepctd_level0.pl # Serve NEP ctd data. Based on mocness_ctd scripts. # Sets up level 1 for CTD data object. # Bob Groman # October 19, 1999. V1.0 $version="v1.00/October 19, 1999"; # Passed parameter # $ARGV[0] = data directory for these data # $ARGV[1] = datatype, e.g. NEP1 # $ARGV[3] = cruiseid # $ARGV[4] = local four digit year $| = 1; #some definitions $error="&x"; $warning="#"; $script_loc="/data/NEP/Scripts"; # script location dir $script_name="nepctd_level2.pl"; # next script name $i_am_level=1; $fieldnames_level[0]="/data/NEP/Support/fieldnames_level0"; $fieldnames_level[1]="/data/NEP/Support/fieldnames_level1"; $fieldnames_level[2]="/data/NEP/Support/fieldnames_level2"; ($ctd_dir, $datatype, $cruiseid, $year) = @ARGV; print STDOUT ("#Displayed by $0, $version\n", "# For $ctd_dir, $datatype, $cruiseid, $year\n"); $datatype = uc $datatype; if ($datatype eq "NEP1" ) { # 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; } else { print STDOUT "# $0 cannot recognize datatype=$datatype\n"; exit 0; } $index=-1; @data=(); @datafile = <$ctd_dir/*>; # get the data files #print STDOUT ("#**debug, number of data files=$#datafile\n"); foreach $datafile (@datafile) { if ( $datafile =~ m/^\./ ) { next;} unless (open(PROFILE, $datafile) ) { sendmessage ($warning, "Could not open data file $datafile", "Error=$!, File skipped."); next; } # print STDOUT "\n**debug, opening file=$datafile\n"; $index++; $station[$index] = "nd"; $station_name[$index] = "nd"; $month_local[$index] = "nd"; $day_local[$index] = "nd"; $time_local[$index] = "nd"; $lat[$index] = "nd"; $lon[$index] = "nd"; $depth_w[$index] = "nd"; $datafile[$index] = $datafile; READDATA:while() { $record=$_; chomp; unless( m/^h/ or m/^#/ ) { last READDATA } if ( m/^h Time/ ) { $time_local[$index] = $record; $time_local[$index] =~ s/^h Time +(\d{1,4}) +(\d{1,2})\/(\d{1,2})\/.*/$1/; $day_local[$index] = $2; $month_local[$index] = $3; $time_local[$index] =~ s/\s//g; chomp $time_local[$index]; # print STDOUT ("#**debug, time_local[$index]=", # $time_local[$index], "\n"); } elsif ( m/^h Lat/ ) { $lat[$index] = $record; $lat[$index] =~ s/^h Lat +(.*)$/$1/; ($deg,$min) = split /\s/, $lat[$index]; if ($deg < 0) { $lat[$index] = $deg - ($min/60); } else { $lat[$index] = $deg + ($min/60); } $lat[$index] = sprintf "%8.4f", $lat[$index]; # print STDOUT ("#**debug, lat[$index]=", # $lat[$index], "\n"); } elsif ( m/^h Lon/ ) { $lon[$index] = $record; $lon[$index] =~ s/^h Lon +(.*)$/$1/; ($deg,$min) = split /\s/, $lon[$index]; if ($deg < 0) { $lon[$index] = $deg - ($min/60); } else { $lon[$index] = $deg + ($min/60); } $lon[$index] = sprintf "%8.4f", $lon[$index]; } elsif ( m/^h Filename/ ) { # Watch out. In some files the keyword is spelled wrong $temp = $record; $temp =~ s/\s+$//; $temp =~ s/h Filename.* (.*)/$1/; $temp =~ s/\s//g; $temp1 = $datafile; $temp1 =~ s/^.*\/(.*)$/$1/; $temp1 =~ s/\s//g; unless ( $temp1 eq $temp ) { $message0="Expected file $datafile"; $message1="But found $temp"; &sendmessage($warning,$message0,$message1); } } elsif ( m/^h Bottom depth/ ) { $depth_w[$index] = $record; $depth_w[$index] =~ s/^h Bottom depth *(.*)/$1/; $depth_w[$index] =~ s/\s//g; unless ($depth_w[$index] =~ m/\w+/ ) { $depth_w[$index] = "nd"; } } elsif ( m/^h Stn number/ ) { $station[$index] = $record; $station[$index] =~ s/^h Stn number *(.*)/$1/; $station[$index] =~ s/\s//g; unless ($station[$index] =~ m/\w+/ ) { $station[$index] = "nd"; } } elsif ( m/^h Stn name/ ) { $station_name[$index] = $record; $station_name[$index] =~ s/^h Stn name *(.*)/$1/; $station_name[$index] =~ s/\s//g; unless ($station_name[$index] =~ m/\w+/ ) { $station_name[$index] = "nd"; } } } close(PROFILE); $i = $index; $string = "(" . $script_loc . "/" . $script_name . " " . $datatype . " " . $datafile[$i] . " " . $cruiseid . ")"; $dataline = $station[$i] . "\t" . $station_name[$i] . "\t" . $day_local[$i] . "\t" . $month_local[$i] . "\t" . $time_local[$i] . "\t" . $depth_w[$i] . "\t" . $lat[$i] . "\t" . $lon[$i] . "\t" . $string . "\n"; push @data, $dataline; # print STDOUT ("#**debug, dataline=$dataline\n"); } #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, size of data array=$#data\n"); foreach $line (sort numalpha @data) { print STDOUT $line; } undef $error; undef $warning; exit 0; #--------------------------------------------------------------------------- sub sendmessage { #Send a message to the user. #The message sent will be in the strings $_[1] and $_[2] #The prefix string is in $_[0] 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) { if ( exists $ENV{'REMOTE_HOST'} ) {$who=$ENV{'REMOTE_HOST'} ; } elsif (exists $ENV{'REMOTE_ADDR'} ) {$who=$ENV{'REMOTE_ADDR'} ; } else {$who="not available"; } print TEMPFILE ("Message from $0\n"); 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,"Message from $0\n"); print STDOUT ($prefix," Date of message: $year/$mon/$mday $hour:$min\n"); print STDOUT ($prefix," $message0\n"); print STDOUT ($prefix," $message1\n"); return; } #--------------------------------------------------------------------------- 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; } } } #---------------------------------------------------------------------------