function [fintime1,finamp1,finbaz1] = ...
          cal_pick1(time,maxamp,maxbaz,amp_fac)

% CAL_PICK1  Picking code for amplitude equaliziation data set.



% Select maximum and minimum. index max(1), min(-1), nothing(0)
mm0 = size(maxamp);
ii=1;
for i =1 : mm0(1)-2;
    amp1 = maxamp(i);
    amp2 = maxamp(i+1);
    amp3 = maxamp(i+2);
    amp12 = amp1 - amp2;
    amp23 = amp2 - amp3;
    amp4 = amp12 * amp23;
    if (amp4 < 0) 
       finamp(ii)=maxamp(i+1);
       fintime(ii)=time(i+1);
       finbaz(ii)=maxbaz(i+1);
       if (amp12 < 0)
          index(ii) = 1;
       else
          index(ii) = -1;
       end
       ii = ii + 1;
    end  
end

mm1 = size(finamp);

% Determin initial value.
if (index(1) == 1)
i1 = 1;
end

if (index(1) == -1)
index(1) = 0;
i1 = 2;
end
ii = i1;
for i = i1:2:mm1(2)-2
    amp1 = finamp(ii); % maximum1
    amp2 = finamp(i+1); % minimum1
    amp3 = finamp(i+2); % maximum2
    rat1 = amp1/amp2;
    rat2 = amp3/amp2;
    if (rat1 > amp_fac)
       if (rat2 > amp_fac) % all satisfies.
           ii = i + 2;
           index(ii)=1;
           index(i+1)=1;
       else 
           index(i+2) = 0;
           index(ii) = 1;
       end
     else
       if (rat2 > amp_fac) 
          ii = i + 2;
          index(ii) = 0;
          index(i+2) = 1;
       elseif (rat1 < rat2)
          ii = i + 2;
          index(ii) = 0;            
       end
     end
end

jj = 1;
for j = 1 : mm1(2);
    if (index(j) == 1)
       fintime1(jj) = fintime(j);
       finamp1(jj) = finamp(j);
       finbaz1(jj) = finbaz(j);
       jj = jj + 1;
    end
end

