function runRkwin
% Perform double difference residual computation using the rkwin
% engine. 
% 
% Vivek Nadkarni
% July 20, 2004
% Trimble

loadCfg; 


createRkwinFiles       = 1; 
createRefSvList        = 1;
createTrajFile         = 1; 
runRkwinBatchfile      = 1; 
combineRkwinPhaseFiles = 1; 
combineRkwinCodeFiles  = 1; 



if createRkwinFiles
  makeRkwinCfgFiles;
  disp('Rkwin configuration files created'); 
end

if createRefSvList
  makeRefSvList; 
  disp('Reference SV list created'); 
end

if createTrajFile
  % If the surveyType = 'survey' then the 
  % surveyPos(ROVER,:) should be the surveyed position 
  % of the rover. For other surveyTypes, e.g. 
  % 'dynamic' and 'static', the surveyPos(ROVER,:) should
  % be "close" to the rover position, i.e. say within
  % a hundred meters. 
  makeTrajFile(surveyType, 1, surveyPos(ROVER,:));
  disp(['Trajectory file created using method ' surveyType]);
end

if runRkwinBatchfile
  origDir = pwd; 
  cd(directoryname); 
  system('run_rkwin.bat');
  cd(origDir);
  disp('Rkwin batch file executed'); 
end

if combineRkwinPhaseFiles

  TsMsec = Ts*1000;
  ddprRovFile = 'ddPhaseResidualTrueRover.txt';
  ddprGieFile = 'ddPhaseResidualTrueGie.txt';

  ddprRov = load([directoryname ddprRovFile]);
  ddprRov(:,rkey.TIME) = ...
    TsMsec*round(ddprRov(:,rkey.TIME)/TsMsec);

  ddprGie = load([directoryname ddprGieFile]);
  ddprGie(:,rkey.TIME) = ...
    TsMsec*round(ddprGie(:,rkey.TIME)/TsMsec);
  
  save([directoryname 'combinedDdprData'], 'ddprRov', 'ddprGie');

  disp('Rkwin output files combined'); 
  
  clear ddprRov ddprGie; 
  
end

if combineRkwinCodeFiles

  TsMsec = Ts*1000;
  ddcrRovFile = 'ddCodeResidualTrueRover.txt';
  ddcrGieFile = 'ddCodeResidualTrueGie.txt';

  ddcrRov = load([directoryname ddcrRovFile]);
  ddcrRov(:,rkey.TIME) = ...
    TsMsec*round(ddcrRov(:,rkey.TIME)/TsMsec);

  ddcrGie = load([directoryname ddcrGieFile]);
  ddcrGie(:,rkey.TIME) = ...
    TsMsec*round(ddcrGie(:,rkey.TIME)/TsMsec);
  
  save([directoryname 'combinedDdcrData'], 'ddcrRov', 'ddcrGie');

  disp('Rkwin output files combined'); 

  clear ddcrRov ddcrGie;

end

return; 
