% computeCslipsInLoopL.m
% 
% Generate statistics from the record type 17s of the DAT file 
% and print them to a file. 
% 
% Vivek Nadkarni
% Trimble
% December 17, 2004

loadData;

svid = gieData(:,dkey.SVID);  
cn0 = gieData(:,dkey.L1CNZERO);
valid = gieData(:,dkey.VALID);

svidR = roverData(:,dkey.SVID);
cn0R = roverData(:,dkey.L1CNZERO);
validR = roverData(:,dkey.VALID);

totalG = 0;
cslipG = 0;
invalidG = 0;
validG = 0;

totalR = 0;
cslipR = 0;
invalidR = 0;
validR = 0;

totalB = 0;
cslipB = 0;
invalidB = 0;
validB = 0;

cn0Mask = 60; 

fid = fopen([directoryname 'results\cslipsInLoopL' num2str(cn0Mask) '.txt'], 'w');
fprintf(fid, '%% %s \r\n', directoryname);
fprintf(fid, '%% Using epochs under %2d dbHz as a proxy for loop L epochs\r\n', cn0Mask); 
fprintf(fid, '%% gie Loop L \t\t\t\t\t\t rover Loop B\r\n');
fprintf(fid, '%%sv, epochs,  cslips, invalid,   valid\t\t\t sv, epochs,  cslips, invalid,   valid\r\n');
for sv = 1:32
  seg = find(svid == sv);
  segseg = find(cn0(seg) < cn0Mask);
  
  total = length(segseg); 
  cslipCnt = length(find(valid(seg(segseg)) == 1)); 
  invalidCnt = length(find(valid(seg(segseg)) == -1)); 
  validCnt = total - (cslipCnt + invalidCnt);
  fprintf(fid, '%2d % 8d % 8d % 8d % 8d\t\t\t', ...
    sv, total, cslipCnt, invalidCnt, validCnt);
   
  totalG = totalG + total; 
  cslipG = cslipG + cslipCnt;
  invalidG = invalidG + invalidCnt; 
  validG = validG + validCnt; 
     
  seg = find(svidR == sv);
  segseg = find(cn0R(seg) < cn0Mask);
  
  total = length(segseg); 
  cslipCnt = length(find(valid(seg(segseg)) == 1)); 
  invalidCnt = length(find(valid(seg(segseg)) == -1)); 
  validCnt = total - (cslipCnt + invalidCnt);
  fprintf(fid, '%2d % 8d % 8d % 8d % 8d\r\n', ...
    sv, total, cslipCnt, invalidCnt, validCnt);

  totalR = totalR + total; 
  cslipR = cslipR + cslipCnt;
  invalidR = invalidR + invalidCnt; 
  validR = validR + validCnt; 
  
end

fprintf(fid, '\r\n');
fprintf(fid, '%%   totalG,  totalR,  validG,  validR \r\n');
fprintf(fid, '%% % 8d % 8d % 8d % 8d \r\n', ...
  totalG, totalR, validG, validR);
fprintf(fid, '%% total percent Gie/Rov, valid percent Gie/Rov\r\n');
fprintf(fid, '%% % 3.2f %%,          % 3.2f %%\r\n', ...
  100*totalG/totalR, 100*validG/validR);

fclose(fid);
