#!/export/opt1/perl/bin/perl -w
#DOC#################################################
#DOC
#DOC program creator.pl
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Calculate predicted SKS arrival times and enter requests in suitable table
#DOC
#DOC variables : @hyp - List of hypocenter IDs
#DOC             @network - List of concatonated data centers and networks (e.g. IRIS_DMC::IU)
#DOC             @oked - Array containing concatonated data of suitable waveforms
#DOC             @oked2 - Subset of elements of @oked, (each with the same request ID)
#DOC             @sta - List of station codes
#DOC             @temp - Temporary array, used to store output from 'identify_stations'
#DOC             @temp2 - Temporary array, used to process each element of @temp
#DOC             %sta_lat - Hash containing station latitudes, (key = station code, $sta)
#DOC             %sta_lon - Hash containing station longitudes, (key = station code, $sta)
#DOC             $ar_size - Number of elements in @oked
#DOC             $center - Data center
#DOC             $chan - FDSN channel code
#DOC             $check - Output from 'inventory_check' (from 'shared.pl')
#DOC             $count - Number of individual data requests
#DOC             $delta - Distance between envent and station, (degrees)
#DOC             $del_check - Output from 'check_delta', (0 or 1)
#DOC             $hyp - Hypocenter ID
#DOC             $hyp_begin - Start date of requests
#DOC             $hyp_date - Hypocenter origin time to the nearest second
#DOC             $hyp_depth - Hypocenter depth (km)
#DOC             $hyp_end - End date of requests
#DOC             $hyp_lat - Hypocenter latitude (degrees)
#DOC             $hyp_lon - Hypocenter longitude (degrees)
#DOC             $i - Loop variable, used in conjunction with $ar_size and @oked
#DOC             $log - Log file name
#DOC             $net - Network code
#DOC             $net_name - Network name
#DOC             $num - Number of elements in @oked2
#DOC             $num_req - Maximum size of (@oked2), (maximum number of stations per request)
#DOC             $request - SQL request
#DOC             $seaz - Station to event azimuth (degrees)
#DOC             $sta - Station code
#DOC             $temp - Temporary variable, used to loop over each element in @network
#DOC             $wf_begin - Start time of waveform
#DOC             $wf_end - End time of waveform
#DOC
#DOC environment variables : $ENV{EVENT} - Table containing event data
#DOC                         $ENV{HYPOCENTER} - Table containing hypocenter data
#DOC                         $ENV{INVENTORY} - Table containing inventory data
#DOC                         $ENV{LAST_DATE} - Table tracking progress of ISC editors
#DOC                         $ENV{LDA} - Set on logging into Oracle
#DOC                         $ENV{NETMAG} - Table containing magnitude data
#DOC                         $ENV{SITE} - Table containing station data
#DOC                         $ENV{SPLIT} - Table containing split requests
#DOC                         $ENV{WFARS} - Table containing waveform requests
#DOC                         $ENV{USER} - Username for $ENV{WFARS}, (should be 'SPLIT')
#DOC
#DOC calls subroutines : calculate_delta
#DOC                     calculate_seaz
#DOC                     calculate_times
#DOC                     check_delta
#DOC                     identify_hyps
#DOC                     identify_networks
#DOC                     identify_stations
#DOC                     identify_times
#DOC                     inventory_check (from 'shared.pl')
#DOC                     make_request
#DOC                     oracle_login (from 'shared.pl')
#DOC                     oracle_logout (from 'shared.pl')
#DOC                     oracle_request (from 'shared.pl')
#DOC                     sort_by_delta
#DOC
#DOC other subroutines : assign_reqid (called by 'make_request')
#DOC                     date2number (from 'shared.pl', called by 'inventory_check')
#DOC                     oracle_command (from 'shared.pl', called by 'make_request')
#DOC                     
#DOC other programs : '/export/dev/ops/bin/ttimes' (called by 'make_request')
#DOC                     
#DOC#################################################

use Oraperl;
use strict;

do 'shared.pl';

my (@hyp, @network, @oked, @oked2, @sta, @temp, @temp2);
my (%sta_lat, %sta_lon);
my ($ar_size, $center, $chan, $check, $count, $delta, $del_check);
my ($hyp, $hyp_begin, $hyp_date, $hyp_depth, $hyp_end, $hyp_lat);
my ($hyp_lon, $i, $net, $num, $num_req, $request, $seaz, $sta);
my ($temp, $wf_begin, $wf_end, $month, $year);

my ($log, $net_name);

$center      = "IRIS_BF";
$net_name    = "CI";
$hyp_begin   = "01-01-1994 00:00:00";
$hyp_end     = "01-01-1995 00:00:00";

$ENV{EVENT}      = 'ISCDBADM.EVENT';
$ENV{HYPOCENTER} = 'ISCDBADM.HYPOCENTER';
$ENV{INVENTORY}  = 'MATT.INV2';
$ENV{LAST_DATE}  = 'ISCDBADM.LAST_DATE';
$ENV{NETMAG}     = 'ISCDBADM.NETMAG';
$ENV{SITE}       = 'MATT.STATION';
$ENV{SPLIT}      = 'MATT.'.$net_name.'_REQUEST';
$ENV{WFARS}      = 'MATT.WFARS_REQUEST';
$ENV{USER}       = "SPLIT";

### Login to oracle ###

($ENV{LDA})      = oracle_login();

### Identify start and end times ###

#($hyp_begin, $hyp_end) = identify_times();

#print "$hyp_begin, $hyp_end\n";
#exit;

### Number of events per request ###

$num_req = 40;
$chan    = "BHE";
$count   = 0;

### Sort out log file ###

$month = substr($hyp_begin, 3, 2);
$year  = substr($hyp_begin, 6, 4);
$log   ="/export/home/matt/dummy_wfars/".lc($net_name)."_requests/$year-$month";

open (LOG, ">>$log");

### Sort out network data (make work quicker) ###

#@network = identify_networks();

undef @network;

$network[0] = join("::", $center, $net_name);

### Identify hypocenters ###

(@hyp) = identify_hyps($hyp_begin, $hyp_end);

foreach $temp (@network) {

    #print "$temp\n";

    ($center, $net) = split(/::/,$temp);

    undef @sta;
    undef %sta_lat;
    undef %sta_lon;


    (@temp) = identify_stations($center, $net, $chan);

    foreach (@temp) {
    
        chomp ($_);
    
        (@temp2) = split(/::/,$_);
    
        $sta           = $temp2[0];
        $sta_lat{$sta} = $temp2[1];
        $sta_lon{$sta} = $temp2[2];
    
        push (@sta, $sta);
    
    }  

    
    foreach (@hyp) { 
    
        chomp($_);
    
        ($hyp, $hyp_lat, $hyp_lon, $hyp_depth, $hyp_date) = split(/::/,$_);
        
        if ($hyp_depth eq "") {
        
            $request = "select DEPDP from $ENV{HYPOCENTER}
                        where HYPID = '$hyp'";
                        
            ($hyp_depth) = oracle_request($request);
            
            print "Depth substitution performed\n";
            print LOG "Depth substitution performed\n";
        
        }
    
        #
        ##
        ###
        ####
        print "$hyp_lat, $hyp_lon, $hyp_depth, $hyp_date\n";
        print LOG "$hyp_lat, $hyp_lon, $hyp_depth, $hyp_date\n";
        ####
        ###
        ##
        #

        undef @oked;

        
        ### Loop over each station ###
        
        
        
        
        foreach $sta (@sta) {
            
            
            ### Calculate delta ###
            
            ($delta) = calculate_delta($hyp_lat, $hyp_lon, $sta_lat{$sta}, $sta_lon{$sta});
            
            $del_check = check_delta($delta);
            
            if ($del_check == 1) {
                    
                ($wf_begin, $wf_end) = calculate_times($delta, $hyp_depth, $hyp_date, $center);
                ($seaz)              = calculate_seaz($hyp_lat, $hyp_lon, $sta_lat{$sta}, $sta_lon{$sta});
                    
                    
                ($check) = inventory_check($center, $net, $sta, $chan, $wf_begin, $wf_end);
                        
                
                if ($check eq "R") {
                    push (@oked,join("::", $center, $net, $sta, substr($chan,0,2)."*", $wf_begin, $wf_end, $delta, $seaz));
                }
            
            
            } # end $del_check if
        
        
        } # End sta loop
        
        
        
        if (defined @oked) {
        
            ### Sort by delta ###
            
            (@oked) = sort_by_delta(@oked);
            
            ### Split into units of $num_req ###
            
            $ar_size = @oked;
            
            undef @oked2;
            $num = 0;
            
            for ($i=0;$i<$ar_size;$i++){
            
                
                if ($num == $num_req) {
                    () = make_request($hyp, @oked2);
                    undef @oked2;
                    $num = 0;
                    $count++;
                    
                }

                push (@oked2, $oked[$i]);
                $num++;
            
            
            }
            
            make_request($hyp, @oked2);
            $count++;
            
        
        
        }
        
    } # End hypocenter loop
    

} # End network loop


print "Number of requests: $count\n";
print LOG "Number of requests: $count\n";

oracle_logout();

close LOG;

exit(0);

#DOC#################################################
#DOC
#DOC Subroutines defined:
#DOC
#DOC assign reqid
#DOC calculate_delta
#DOC calculate_times
#DOC check_delta   
#DOC identify_hyps
#DOC identify_networks
#DOC identify_stations
#DOC identify_times
#DOC make_request
#DOC sort_by_delta
#DOC
#DOC#################################################

#DOC#################################################
#DOC
#DOC subroutine assign_reqid
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Assign new request ID as required
#DOC
#DOC input : none
#DOC
#DOC output: $reqid - Request ID
#DOC
#DOC internal : $request - SQL request
#DOC
#DOC environment variables : none
#DOC
#DOC calls subroutines : oracle_request (from 'shared.pl')
#DOC
#DOC#################################################


sub assign_reqid {

    my ($reqid, $request);

    $request = "SELECT iscdbadm.reqid.NEXTVAL FROM DUAL";
    ($reqid) = oracle_request($request);
    
    return ($reqid);

}

#DOC#################################################
#DOC
#DOC subroutine calculate_delta
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Calculate angular distance between station and event
#DOC
#DOC input : $hyp_lat - Hypocenter latitude
#DOC         $hyp_lon - Hypocenter longitude
#DOC         $sta_lat - Station latitude
#DOC         $sta_lon - Station longitude
#DOC
#DOC output : $delta - Station to event azimuth
#DOC
#DOC internal : $request - SQL request
#DOC
#DOC environment variables : none
#DOC
#DOC calls subroutines : oracle_request (from 'shared.pl')
#DOC
#DOC#################################################

sub calculate_delta {

    my ($sta_lat, $sta_lon, $hyp_lat, $hyp_lon) = @_;
    my ($request, $delta);
    
    $request = "SELECT ELLIPSOID.DELTA($sta_lat, $sta_lon, $hyp_lat, $hyp_lon) FROM DUAL";

    ($delta) = oracle_request($request);

    $delta = sprintf "%6.3f", $delta;
    
    return ($delta);


}

#DOC#################################################
#DOC
#DOC subroutine calculate_seaz
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Calculate station to event azimuth
#DOC
#DOC input : $hyp_lat - Hypocenter latitude
#DOC         $hyp_lon - Hypocenter longitude
#DOC         $sta_lat - Station latitude
#DOC         $sta_lon - Station longitude
#DOC
#DOC output : $seaz - Station to event azimuth
#DOC
#DOC internal : $request - SQL request
#DOC
#DOC environment variables : none
#DOC
#DOC calls subroutines : oracle_request (from 'shared.pl')
#DOC
#DOC#################################################

sub calculate_seaz {

    my ($hyp_lat, $hyp_lon, $sta_lat, $sta_lon) = @_;
    my ($seaz, $request);
    
    $request = "SELECT ELLIPSOID.AZIMUTH($hyp_lat, $hyp_lon, $sta_lat, $sta_lon) FROM DUAL";

    ($seaz) = oracle_request($request);

    $seaz = sprintf "%6.3f", $seaz;
    
    return ($seaz);


}

#DOC#################################################
#DOC
#DOC subroutine calculate_times
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Calculate window start and end times based on predicted SKS arrivals
#DOC
#DOC input : $delta - Station to event distance (degrees)
#DOC         $hyp_depth - Hypocenter depth (km)
#DOC         $hyp_date - Event time
#DOC         $center - Data center
#DOC
#DOC output : $win_begin - Window start time
#DOC          $win_end - Window end time
#DOC
#DOC internal : @data - Output from 'ttimes'
#DOC            $command - Command that executes 'ttimes'
#DOC            $sks_delay - Predicted SKS delay
#DOC            $skks_delay - Predicted SKKS delay
#DOC            $request - SQL request
#DOC
#DOC environment variables : none
#DOC
#DOC calls subroutines : find_phase_delay
#DOC                     oracle_request (from 'shared.pl')
#DOC
#DOC#################################################


sub calculate_times {

    my ($delta, $hyp_depth, $hyp_date, $center) = @_;
    my ($win_begin, $win_end);
    my (@data, $command, $sks_delay, $skks_delay, $request);
    
    $command = "/export/dev/ops/bin/ttimes <<EOF\nALL\n\n$hyp_depth\n$delta\n-1\n-1\nEOF\n";

    @data = `$command`;
    
    
    ($sks_delay)  = find_phase_delay("SKSac",@data);
    ($skks_delay) = find_phase_delay("SKKSac",@data);
    
    if ($center eq "IRIS_DMC") {
    
        $sks_delay  = int($sks_delay-180);
        $skks_delay = int($skks_delay+100);
        
    } else {
    
        $sks_delay  = int($sks_delay-60);
        $skks_delay = int($skks_delay+60);
    
    }

    $request = "select (to_date('$hyp_date') +$sks_delay/86400) from dual";
    ($win_begin) = oracle_request($request);
    $request = "select (to_date('$hyp_date') +$skks_delay/86400) from dual";
    ($win_end)   = oracle_request($request);
    
    return ($win_begin, $win_end);


}

#DOC#################################################
#DOC
#DOC subroutine check_delta
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Check station is at suitable distance (degrees)
#DOC
#DOC input : $delta - Station to event distance (degrees)
#DOC
#DOC output : $del_check - '1' if acceptable, '0' if rejected
#DOC
#DOC internal : $del_min - Minimum distance (degrees)
#DOC            $del_max - Maximum distance (degrees)
#DOC
#DOC environment variables : none
#DOC
#DOC calls subroutines : none
#DOC
#DOC#################################################

sub check_delta {

    my ($delta) = @_;
    my ($del_check, $del_min, $del_max);
    
    $del_min = 90;
    $del_max = 130;
    $del_check = 0;
    
    
    if ($delta > $del_min) {
    
        if ($delta < $del_max) {
            $del_check = 1;
        }
    
    }
    


    return ($del_check);

}

#DOC#################################################
#DOC
#DOC subroutine find_phase_delay
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Find predicted phase arrival from 'ttimes' output
#DOC
#DOC input : $phase - Relevant phase
#DOC         @data - Output from 'ttimes'
#DOC
#DOC output : $delay - Predicted offset from event origin time in seconds
#DOC
#DOC internal : @temp - Used to split each line
#DOC            $i - Loop variable
#DOC            $size - Number of elements in @data
#DOC
#DOC environment variables : none
#DOC
#DOC calls subroutines : none
#DOC
#DOC#################################################

sub find_phase_delay {

    my ($phase, @data) = @_;
    my (@temp, $i, $size, $delay);
    
    $size = @data;
    
    
    for ($i=28;$i<$size;$i++) {
    
        if ($data[$i] =~ $phase) {
        
            @temp  = split(/ +/,$data[$i]);
            $delay = $temp[3];
            
            last;
        
        }
    
    
    }
    
    return $delay;    

}

#DOC#################################################
#DOC
#DOC subroutine identify_hyps
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Identify suitable primary hypocenters
#DOC
#DOC input : $begin - Start of time window
#DOC         $end - End of time window
#DOC
#DOC output : @hyp - List of hypocenter ID numbers
#DOC
#DOC internal : $request - String containing SQL request
#DOC
#DOC environment variables : $ENV{EVENT} - Table containing event data
#DOC                         $ENV{HYPOCENTER} - Table containing hypocenter data
#DOC                         $ENV{NETMAG} - Table containing magnitude for an event
#DOC
#DOC calls subroutines : oracle_request (from 'shared.pl')
#DOC
#DOC#################################################

sub identify_hyps {

    my ($begin, $end) = @_;
    my ($request, @hyp);
    
    $request = "select hypid, lat, lon, depth, day from $ENV{HYPOCENTER}
                where hypid in
                  (select DISTINCT(e.prime_hyp)
                  from $ENV{EVENT} e
                  WHERE e.evid IN 
                    (SELECT h.isc_evid
                    FROM $ENV{HYPOCENTER} h, $ENV{NETMAG} n
                    where h.day >  '$begin'
                    and   h.day < '$end'
                    and   h.hypid = h.pref_hypid
                    and   n.hypid = h.hypid
                    and   UPPER(n.magtype) = 'MW'
                    AND   n.magnitude > '5.8'))
                order by day";
                
    (@hyp) = oracle_request($request);
    
    return (@hyp);

}

#DOC#################################################
#DOC
#DOC subroutine identify_networks
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Identify networks from which data is available
#DOC
#DOC input : none
#DOC
#DOC output : @networks - Array containing concatonation of center name and network code
#DOC                       (e.g. 'CNDC::CN')
#DOC
#DOC internal : $request - String containing SQL request
#DOC
#DOC environment variables : $ENV{INVENTORY} - Table identifying available waveform data
#DOC
#DOC calls subroutines : oracle_request (from 'shared.pl')
#DOC
#DOC#################################################

sub identify_networks {

    my ($request, @networks);
    
    $request = "select distinct CENTER, NET
                from $ENV{INVENTORY}";
                
                                
    (@networks) = oracle_request($request);
    

    return (@networks);

}

#DOC#################################################
#DOC
#DOC subroutine identify_stations
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Identify suitable stations from inventory table
#DOC
#DOC input : $center - Data center
#DOC         $net - Two digit network code
#DOC         $chan - Channel code 
#DOC
#DOC output : @sta - Concatonations of station, lat and lon, (e.g. EDM::53.2217::-113.35)
#DOC
#DOC internal : $request - String containing SQL request
#DOC
#DOC environment variables : $ENV{INVENTORY} - Table identifying available waveform data
#DOC                         $ENV{SITE} - Table contianing station data
#DOC
#DOC calls subroutines : oracle_request (from 'shared.pl')
#DOC
#DOC#################################################


sub identify_stations {

    my ($center, $net, $chan) = @_;
    my ($request, @sta);
    
    $request = "select sta, lat, lon from $ENV{SITE} where net = '$net' and sta in (
                select distinct sta from $ENV{INVENTORY}
                where CENTER = '$center' 
                and NET = '$net' 
                and PRIMARY_DATA = 'P'
                and CHAN = '$chan')";
                
                                
    (@sta) = oracle_request($request);
    
    return (@sta);

}

#DOC#################################################
#DOC
#DOC subroutine identify_times
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Identify month for which data will be requested 
#DOC
#DOC input : none
#DOC
#DOC output : $begin - Start time of current month (e.g. '01-11-2000 00:00:00')
#DOC          $end - End time of current month (e.g. '01-12-2000 00:00:00')
#DOC
#DOC internal : $request - String containing SQL request
#DOC
#DOC environment variables : $ENV{LAST_DATE} - Table tracking progress of ISC editors
#DOC
#DOC calls subroutines : oracle_request (from 'shared.pl')
#DOC
#DOC#################################################

sub identify_times {

    my ($request, $begin, $end);
    
    $request = "select MAX(add_months(LAST_DATE,-1)) 
                from $ENV{LAST_DATE}
                where REINT_DATE is not null";
                
    ($begin) = oracle_request($request);

    $request = "select MAX (LAST_DATE)
                from $ENV{LAST_DATE}
                where REINT_DATE is not null";
                
    ($end) = oracle_request($request);
    


    return ($begin, $end);

}

#DOC#################################################
#DOC
#DOC subroutine make_request
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Make request entries into appropriate table
#DOC
#DOC input : $hyp - Primary key to Hypocenter table
#DOC         @oked - Concatonation of station specific data, (one line per station)
#DOC
#DOC output : none
#DOC
#DOC internal : $temp - Used to loop over each value of '@oked'
#DOC            $center - Data center
#DOC            $net - Two digit network code
#DOC            $sta - Station code
#DOC            $chan - Channel code ('BH*')
#DOC            $wf_begin - Start time of individual request
#DOC            $wf_end - End time of individual request
#DOC            $delta - Station to event distance (degrees)
#DOC            $seaz - Station to event azimtuh
#DOC            $delta1 - Station to event distance (degrees)
#DOC            $temp1 - Temporary variable used to swap elements in @oked
#DOC            $command - String containing SQL command
#DOC            $reqid - Request ID number
#DOC            $earliest - 'Rectangularized' first begin time, (passed into database) 
#DOC            $latest - 'Rectangularized' last end time, (passed into database) 
#DOC
#DOC environment variables : $ENV{SPLIT} - Table containing split requests
#DOC                         $ENV{USER} - Username, (in all cases 'SPLIT')
#DOC                         $ENV{WFARS} - Waveform request table.
#DOC
#DOC calls subroutines : assign_reqid
#DOC                     oracle_command (from 'shared.pl')
#DOC
#DOC#################################################

sub make_request {

    my ($hyp, @oked) = @_;
    my ($temp, $center, $net, $sta, $chan, $wf_begin, $wf_end, $delta, $seaz);
    my ($command, $reqid, $earliest, $latest);

    ($reqid) = assign_reqid();
    
    ### Rectangularise time window ###
    
    ($center, $net, $sta, $chan, $earliest, $wf_end, $delta, $seaz) = split(/::/,$oked[0]);
    ($center, $net, $sta, $chan, $wf_begin, $latest, $delta, $seaz) = split(/::/,$oked[-1]);


    foreach $temp (@oked) { 
            
            
        ($center, $net, $sta, $chan, $wf_begin, $wf_end, $delta, $seaz) = split(/::/,$temp);
                
        $command = "INSERT INTO $ENV{WFARS} values (
                    '$reqid', '$ENV{USER}', '$hyp', '$center', '$net', '$sta', '$chan', 
                    '$earliest', '$latest', 'SAC', '')";
         
        #print "$command\n";
                           
        oracle_command($command);
                
                #
                ##
                ###
                ####
                print "$reqid $earliest $latest $hyp $center $net $sta $chan\n";
                print LOG "$reqid $earliest $latest $hyp $center $net $sta $chan\n";
                ####
                ###
                ##
                #
                 
                 
        $command = "INSERT INTO $ENV{SPLIT} values (
                    '$reqid', '$hyp', '$net', '$sta', '$chan', '$delta', '$seaz','','','')";
                    
        #print "$command\n";
        
        oracle_command($command);
               
            
    }

}

#DOC#################################################
#DOC
#DOC subroutine sort_by_delta
#DOC
#DOC author - M Evans
#DOC date - October 2003
#DOC
#DOC purpose : Order stations in network by distance from event
#DOC           Allows 'rectangularisation' which makes requests more
#DOC           efficient for data centers.
#DOC
#DOC input : @oked - Concatonation of station specific data, (one line per station)
#DOC
#DOC output : @oked - As above, sorted by distance
#DOC
#DOC internal : $max - Size of @oked
#DOC            $i - First loop variable
#DOC            $j - Second loop variable
#DOC            $center - Data center
#DOC            $net - Two digit network code
#DOC            $sta - Station code
#DOC            $chan - Channel code ('BH*')
#DOC            $wf_begin - Start time of request
#DOC            $wf_end - End time of request
#DOC            $delta - Station to event distance (degrees)
#DOC            $seaz - Station to event azimtuh
#DOC            $delta1 - Station to event distance (degrees)
#DOC            $temp1 - Temporary variable used to swap elements in @oked
#DOC            
#DOC environment variables : none
#DOC
#DOC calls subroutines : none
#DOC
#DOC#################################################

sub sort_by_delta {

    my (@oked) = @_;
    my ($max, $i, $j, $center, $net, $sta, $chan, $wf_begin, $wf_end, $delta, $seaz);
    my ($delta1, $temp1);

    $max = @oked;
            
    for ($i=0;$i<$max;$i++) {

        for ($j=$i+1;$j<$max;$j++) {

            ($center, $net, $sta, $chan, $wf_begin, $wf_end, $delta, $seaz) = split(/::/,$oked[$i]);
            ($center, $net, $sta, $chan, $wf_begin, $wf_end, $delta1, $seaz) = split(/::/,$oked[$j]);

            if ($delta1 < $delta) {
        
                $temp1      = $oked[$j];
                $oked[$j] = $oked[$i];
                $oked[$i] = $temp1;
        
            } # End 'if'

        } # End for $j
               
    } # End for $i

    return (@oked);
    
}

#DOC#
#DOC##
#DOC### EOF
#DOC##
#DOC#
