#!/export/opt1/perl/bin/perl
#
# Subroutines defined:
# 
# pick_check
# stalta_picker
#
####################################

sub pick_check {

# Name: pick_check
# Description: Searches output from stalta picker for suitable arrivals
# Called by: main program
# Calls subroutines: NONE
# Input: $sks_time, @stalta_out
# Output: $best_pick, $best_stalta, $best
# Environment variables: NONE
# Variables: 
# @stalta_out = Output from 'stalta_no_files', passed back in by main program
# $best = Current closest value
# $best_pick = Time of closest value
# $best_stalta = Ratio of closest value
# $diff = Difference between $sks_time and $pick
# $junk = Just junk
# $line = Variable used to loop over @stalta_out
# $pick = Pick time, (s)
# $stalta = STALTA ratio 
# $sks_time = Predicted arrival time from IASP91
# END

    my ($sks_time, @stalta_out) = @_;
    my ($line, $junk, $pick, $stalta, $diff);
    my ($best, $best_pick, $best_stalta);
    
    $best = "1000000000";
    
    
    foreach $line (@stalta_out) {
        
        chomp ($line);
            
        ($junk, $pick, $stalta) = split(/ +/,$line);
        
        $diff = $pick - $sks_time;
            
        if (abs($diff) < $best) {
            $best = abs($diff);
            $best_pick = $pick;
            $best_stalta = $stalta;
                
            
        }
        
    
    }
    
    return ($best_pick, $best_stalta, $best);


}


####################################

sub stalta_picker {

# Name: stalta_picker
# Description: Runs sta/lta picker. Returns 'NOISE' if no pick, or time and sta/lta ratio if picks made
# Called by: main program
# Calls subroutines: NONE, (but runs F77 code 'stalta')
# Input: $n_file, $e_file, $staverage, $ltaverage, $cutoff, $nenvfil, $hanfil
# Output: @output
# Environment variables: $ENV{TEMP_DIR}
# Variables: 
# @output = Output from '/export/home/matt/stalta/stalta'
# $cutoff = minimum value of sta/lta ratio to register a pick (threshold value)
# $e_file = Input east component
# $hanfil = Half length of the Hanning filter (s)
# $ltaverage = Length of long term average (s)
# $n_file = Input north component
# $nenvfil = Half-length of filter for envelope function (samples)
# $staverage = Length of short term average (s)
# END


    my ($n_file, $e_file, $sks_start) = @_;
    my ($staverage, $ltaverage, $cutoff, $nenvfil, $hanfil);
    my (@output);
    
    undef(@output);
    
    $staverage = "1";
    $ltaverage = "15";
    $cutoff    = "2";
    $nenvfil   = "50";
    $hanfil    = "10";

    $tolerance = "12";

    $staverage = sprintf("%6.3f",$staverage);
    $ltaverage = sprintf("%6.3f",$ltaverage);
    $cutoff    = sprintf("%6.3f",$cutoff);
    $nenvfil   = sprintf("%3d",$nenvfil);
    $hanfil    = sprintf("%6.3f",$hanfil);

    $command  = "/export/home/matt/stalta/stalta <<EOF\n";
    $command .= "$n_file\n$e_file\n$ENV{TEMP_DIR}/\n$staverage\n$ltaverage\n$cutoff\n$nenvfil\n$hanfil\nEOF\n";

    @output = `$command`;
    
    print "@output\n";
    
    ### Check to see if measurement should be made ###

    if (defined @output) {

        ($best_pick, $best_stalta, $best) = pick_check($sks_start, @output);
        
        if (abs($best) <= $tolerance) {
        
            return ("1", $best_pick, $best_stalta);
        
        } else { # Pick not close enough
        
            return ("0","0","0");
        
        }

    } else { # No pick made

        return ("0","0","0");

    }

}

