function [evla2,evlo2,error,torigin]=cal_loc0(stla,stlo,time,baz,refgvel)

% cal_loc; calculate location by using three arrival times and back azimuths.
%     Reference array is automatically determined by using arrival times.
%     2nd far array from the source is selected as a reference array.
% Usage; [eve_loc,error]=cal_loc(stla,stlo,time,baz,refgvel)

error = 0;

% defining initial points for location.


inilat = -90:5:90;
inilon = -180:5:180;

len1=length(inilat);
len2=length(inilon);

for i = 1:len1
    for j = 1:len2
        potini((i-1)*len2+j,1)=inilat(i);
        potini((i-1)*len2+j,2)=inilon(j);
    end
end


len_ini = size(potini);

for i = 1 : len_ini(1)
    [ini_error(i),ini_temp(i)] = ...
    cal_errorinloc1(stla,stlo,time,baz,potini(i,1),potini(i,2),refgvel);
end

[dumm,imin0] = min(ini_error');
evla2 = potini(imin0,1);
evlo2 = potini(imin0,2);
torigin = ini_temp(imin0);
error = dumm;
