function [data,failed] = find_slips (data)
% find_slips: find cycle slips
failed=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% build an index of all cycle slips
j=0;
data.GPS.tracking_prn=[];
data.GPS.tracking_prn{1}=' ';
L1_valid=[];
L1_slip=[];
L2_valid=[];
L2_slip=[];
data.GPS.observables.cycle_slip.L1_valid=[];
data.GPS.observables.cycle_slip.L1_slip=[];
data.GPS.observables.cycle_slip.L2_valid=[];
data.GPS.observables.cycle_slip.L2_slip=[];
data.GPS.observables.cycle_slip.slips{1}=[];
data.GPS.observables.cycle_slip.slips{2}=[];
data.GPS.observables.cycle_slip.slip_count=[];
for (i=1:size(data.GPS.observables.SV,2))
   L1_slip=[];
   L2_slip=[];
   if (isempty(data.GPS.observables.SV{i})==0)
      j=j+1;
      elev_min=min(data.GPS.observables.SV{i}(find(data.GPS.observables.SV{i}(:,1)>0),1));
      elev_max=max(data.GPS.observables.SV{i}(:,1));
      if (elev_min==elev_max)
         data.GPS.tracking_prn{j+1}=sprintf('%.0f (%.0f)',i,elev_min);
      elseif (isempty(elev_min)==1)
         data.GPS.tracking_prn{j+1}=sprintf('%.0f (%.0f)',i,elev_max);
      elseif (isempty(elev_max)==1)
         data.GPS.tracking_prn{j+1}=sprintf('%.0f (%.0f)',i,elev_min);
      else
         data.GPS.tracking_prn{j+1}=sprintf('%.0f (%.0f-%.0f)',i,elev_min,elev_max);
      end;
      
      L1_valid=find(data.GPS.observables.SV{i}(:,7)==5);
      if (isempty(L1_valid)==0)
         data.GPS.observables.cycle_slip.L1_valid((end+1):(end+size(L1_valid,1)),1:2)=[data.GPS.observables.time(L1_valid,1) (j+0.1)*ones(size(L1_valid,1),1)];
      end;
      
      L1_slip=find(data.GPS.observables.SV{i}(:,7)==4);
      if (isempty(L1_slip)==0)
         data.GPS.observables.cycle_slip.slips{1}((end+1):(end+size(L1_slip,1)),:)=[data.GPS.observables.time(L1_slip,1) i*ones(size(L1_slip,1),1) L1_slip data.GPS.observables.SV{i}(L1_slip,1) data.GPS.observables.SV{i}(L1_slip,2)];
         data.GPS.observables.cycle_slip.L1_slip((end+1):(end+size(L1_slip,1)),1:3)=[data.GPS.observables.time(L1_slip,1) (j+0.1)*ones(size(L1_slip,1),1) data.GPS.observables.SV{i}(L1_slip,1)];
      end;
      
      L2_valid=find(data.GPS.observables.SV{i}(:,12)==2);
      if (isempty(L2_valid)==0)
         data.GPS.observables.cycle_slip.L2_valid((end+1):(end+size(L2_valid,1)),1:2)=[data.GPS.observables.time(L2_valid,1) (j-0.1)*ones(size(L2_valid,1),1)];
      end;
      
      L2_slip=find(data.GPS.observables.SV{i}(:,12)==1);
      if (isempty(L2_slip)==0)
         data.GPS.observables.cycle_slip.slips{2}((end+1):(end+size(L2_slip,1)),:)=[data.GPS.observables.time(L2_slip,1) i*ones(size(L2_slip,1),1) L2_slip data.GPS.observables.SV{i}(L2_slip,1) data.GPS.observables.SV{i}(L2_slip,2)];
         data.GPS.observables.cycle_slip.L2_slip((end+1):(end+size(L2_slip,1)),1:3)=[data.GPS.observables.time(L2_slip,1) (j-0.1)*ones(size(L2_slip,1),1) data.GPS.observables.SV{i}(L2_slip,1)];
      end;
   end;
end;
data.GPS.tracking_prn{end+1}=' ';

% build a vector of cycle slip counts
data.GPS.observables.cycle_slip.slip_count=[data.GPS.observables.time(:,1)  zeros(size(data.GPS.observables.time,1),2)];
if (isempty(data.GPS.observables.cycle_slip.slips{1})==0)
   L1_slip_times=unique(data.GPS.observables.cycle_slip.slips{1}(:,1));
   for (i=1:size(L1_slip_times,1))
      j01=find(data.GPS.observables.cycle_slip.slips{1}(:,1)==L1_slip_times(i,1));
      data.GPS.observables.cycle_slip.slip_count(data.GPS.observables.cycle_slip.slips{1}(j01(1),3),2)=size(j01,1);
   end;
end;
if (isempty(data.GPS.observables.cycle_slip.slips{2})==0)
   L2_slip_times=unique(data.GPS.observables.cycle_slip.slips{2}(:,1));
   for (i=1:size(L2_slip_times,1))
      j01=find(data.GPS.observables.cycle_slip.slips{2}(:,1)==L2_slip_times(i,1));
      data.GPS.observables.cycle_slip.slip_count(data.GPS.observables.cycle_slip.slips{2}(j01(1),3),3)=size(j01,1);
   end;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
failed=0;
return;
