function slip_report (data,type)
% slip_report: generates a cycle slip report
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if (data.GPS.loaded==0)
   err_text=sprintf('Please load GPS data!\n');
   fault(err_text);
   return;
end;

% add wait bar
whan=waitbar(0.10,'Generating Cycle Slip Reports, Please wait...',...
   'Name','Plotting',...
   'CloseRequestFcn','closereq',...
   'WindowStyle','Normal',...
   'Pointer','watch');
drawnow;

% file name string
j01=findstr(data.GPS.GPS_file,'.');
filename=data.GPS.GPS_file(1:(j01(end)-1));
j02=findstr(filename,'\');

% build a vector of cycle slips
slips=[data.GPS.observables.cycle_slip.slips{1} ones(size(data.GPS.observables.cycle_slip.slips{1},1),1)
   data.GPS.observables.cycle_slip.slips{2} 2*ones(size(data.GPS.observables.cycle_slip.slips{2},1),1)];
slips=sortrows(slips,1);

if (type==1)
   % build slip report (by time)
   slip_report=strcat(filename,'.cycle slip report (by time).txt');
   fid=fopen(slip_report,'w');
   fprintf(fid,'Cycle Slip Report (by Time)\r\nGPS file: %s\r\n\r\n',filename((j02(end)+1):end));
   fprintf(fid,'  Time     PRN Chan  Elev  Az\r\n');
   for (i=1:size(slips,1))
      if ((i==1)|(slips(i,1)>slips(i-1,1)))
         fprintf(fid,'%10.1f  %2.0f   L%1.0f  %4.1f  %5.1f\r\n',slips(i,1),slips(i,2),slips(i,6),slips(i,4),slips(i,5));
      else
         fprintf(fid,'%14.0f   L%1.0f  %4.1f  %5.1f\r\n',slips(i,2),slips(i,6),slips(i,4),slips(i,5));
      end;
   end;
   fclose(fid);
elseif (type==2)
   % build slip report (by Elevation)
   slip_report=strcat(filename,'.cycle slip report (by elev).txt');
   fid=fopen(slip_report,'w');
   fprintf(fid,'Cycle Slip Report (by Elevation)\r\nGPS file: %s\r\n\r\n',filename((j02(end)+1):end));
   fprintf(fid,'  Elev PRN Chan  Time      Az\r\n');
   
   if (isempty(slips)==0)
      slips=sortrows(slips,4);
      for (i=size(slips,1):-1:1)
         if ((i==size(slips,1))|(slips(i,1)~=slips(i+1,1)))    %&(slips(i,2)~=slips(i+1,2)))
            %fprintf(fid,'%10.1f  %2.0f   L%1.0f  %4.1f  %5.1f\r\n',slips(i,1),slips(i,2),slips(i,6),slips(i,4),slips(i,5));
            fprintf(fid,'  %4.1f  %2.0f   L%1.0f  %8.1f  %5.1f\r\n',slips(i,4),slips(i,2),slips(i,6),slips(i,1),slips(i,5));
            %elseif (slips(i,2)~=slips(i+1,2))
            %fprintf(fid,'  %4.1f  %2.0f   L%1.0f            %5.1f\r\n',slips(i,4),slips(i,2),slips(i,6),slips(i,5));
            %elseif (slips(i,1)~=slips(i+1,1))
            %fprintf(fid,'  %4.1f       L%1.0f  %8.1f  %5.1f\r\n',slips(i,4),slips(i,6),slips(i,1),slips(i,5));
         else
            %fprintf(fid,'  %4.1f       L%1.0f            %5.1f\r\n',slips(i,4),slips(i,6),slips(i,5));
            fprintf(fid,'  %4.1f  %2.0f   L%1.0f            %5.1f\r\n',slips(i,4),slips(i,2),slips(i,6),slips(i,5));
         end;
      end;
   end;
   fclose(fid);
else
   return;
end;

% open slip report
if (exist(slip_report)==2)
   % print slip report (if applicable)
   if (data.config.print==1)
      whan2=waitbar(0.10,'Printing Report, Please wait...',...
         'Name','Printing',...
         'CloseRequestFcn','closereq',...
         'WindowStyle','Modal',...
         'Pointer','watch');
      fprintf('printing Report: %s\r\n',slip_report);
      dos(sprintf('write.exe "%s" /p',slip_report));
      for (i=1:5)
         waitbar(i/5,whan2);
         drawnow;
         pause(1);
      end;
      set(whan2,'Pointer','arrow');
      delete(whan2);
   end;
   
   % open slip report
   if (data.config.auto_close==0)
      dos(sprintf('write.exe "%s" &',slip_report));
   end;
else
   err_text=sprintf('Cycle Slip Report does not exist!\r\n');
   fault(err_text);
   return;
end;

% remove wait bar
set(whan,'Pointer','arrow');
delete(whan);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;
