% makeRefSvList.m
% 
% Create a list of timetags and corresponding SV to use as a reference
% SV for double differences. 
% 
% Vivek Nadkarni
% Trimble
% June 24, 2004
function makeRefSvList()
%% Load files
loadData; 

VBN_DBG = 0;

if (VBN_DBG)
  warning('debugging on: reference SVs chosen badly');
end

%% Step through all the data files, to find the highest common SV. 

% baseStart = dataStartTime(BASE);
baseEndTime = dataEndTime(BASE);

baseIdx = 1;
roverIdx = 1;
gieIdx = 1;

baseEndIdx = length(baseData);
roverEndIdx = length(roverData);
gieEndIdx = length(gieData);

[nrows,ncols] = size(baseData);
refTime = baseData(baseIdx,dkey.TIME);
NSVS = 32; 

refIdx = 1; 

while (refTime <= baseEndTime)
  
 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %% Fill up the Epoch Matrices
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  %% Loop through the baseData
  [baseEpoch, baseIdx] = ...
    createEpochMatrix(baseData, baseIdx, baseEndIdx, refTime, ...
    ncols, NSVS, dkey.TIME, dkey.SVID);

  %% Loop through the rover data
  [roverEpoch, roverIdx] = ...
    createEpochMatrix(roverData, roverIdx, roverEndIdx, refTime, ...
    ncols, NSVS, dkey.TIME, dkey.SVID);

  %% Loop through the gie data
  [gieEpoch, gieIdx] = ...
    createEpochMatrix(gieData, gieIdx, gieEndIdx, refTime, ...
    ncols, NSVS, dkey.TIME, dkey.SVID);

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % End of filling the epoch matrix
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
  %% Find all common satellites
  svsPresent = ...
    (baseEpoch(:,dkey.SVID)>0) + ...
    (roverEpoch(:,dkey.SVID)>0) + ...
    (gieEpoch(:,dkey.SVID)>0);
  baseSvsPresent = (baseEpoch(:,dkey.SVID)>0);

  
  if (max(svsPresent) == 3)
    % Normal case. We will have multiple SVs which are seen by all three
    % receivers. These are our candidate reference SVs. 
    refSvsIdx = (svsPresent == 3);
  elseif (any(baseSvsPresent))
    % Case in which the rover or gie SV are missing. In this case,
    % all of the base SVs are candidate reference SVs. 
    refSvsIdx = (baseSvsPresent == 1); 
  else
    % Base SV is not present. This record is going to be skipped by 
    % the rtk engine anyway. Our candidate list of reference SVs
    % is empty. 
    refSvsIdx = zeros(size(svsPresent)); 
  end


  % Check if we have any candidate reference SVs. Otherwise set
  % the reference SV to zero. 
  if (any(refSvsIdx))

    %% Select candidate SV with max base elevation
    baseElev = baseEpoch(:, dkey.ELV); 
    
    % Could select sv's based on other characteristics here. 
    
    % Remove the satellites that are in the purgeSvList. The 
    % purgeSvList contains satellites that you don't want to 
    % have as the reference SV. It is created in gieTestSetup.m
    for pidx = 1:length(purgeSvList)
      refSvsIdx(purgeSvList(pidx)) = 0;
    end

    % If satellites are not in the useSvList, then don't 
    % use them as reference SVs. 
    useSvMask = zeros(32,1); 
    for pidx = 1:length(useSvList)
      useSvMask(useSvList(pidx)) = 1;
    end
    % Mask out SVs that are not in the useSvList. 
    refSvsIdx = refSvsIdx.*useSvMask;     
    
    % Set the elevations of non-chosen satellites to zero. 
    baseElev = baseElev.*refSvsIdx; 
    
    % Choose the reference SV
    refSv = find(baseElev == max(baseElev));
    
    % If more than one match, arbitrarily choose the first one. 
    refSv = refSv(1); 
    
    if (VBN_DBG)
      %%%%%%%% DEBUG RefSvList ONLY
      refSv = find(refSvsIdx);
      refSv = refSv(1);
    end

  else
    refSv = 0; 
  end
  
  %% Save the time and the reference SV
  % Round time to the closest multiple of Ts, and then convert
  % to milliseconds.
  refSvList(refIdx, 1) = 1000*Ts*round(refTime/Ts); 
  refSvList(refIdx, 2) = refSv; 
  
  refIdx = refIdx + 1;
  if (baseIdx < baseEndIdx)
    refTime = baseData(baseIdx,dkey.TIME);
  else
    refTime = baseEndTime + 1;
  end

end % while (refTime <= baseEndTime)

%% Create an output file
outputFileName = [directoryname 'refSvList.txt'];
fid = fopen(outputFileName,'w'); 
fprintf(fid, '%d %d\r\n', refSvList.'); 
fclose(fid); 

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [epochMatrix, newIdx] = ...
  createEpochMatrix(data, startIdx, endIdx, refTime, ncols, NSVS, TIME, SVID)

%% Initialize epochMatrix
epochMatrix = zeros(NSVS, ncols);

idx = startIdx; 

%% Loop through the gie data
time = data(idx,TIME);
while (time < refTime)
  idx = idx + 1;
  time = data(idx,TIME);
end
while(time == refTime)
  sv = data(idx,SVID);
  epochMatrix(sv,:) = data(idx,:);

  % Get time from next record
  idx = idx + 1;
  if (idx<=endIdx)
    time = data(idx,TIME);
  else
    % Exit from loop.
    time = refTime + 1; 
  end
end

% Return the new idx
newIdx = idx; 

end
