% makeRkwinCfgFiles
% 
% Vivek Nadkarni
% Trimble
% July 16, 2004

function makeRkwinCfgFiles

load gieTestSetupData; 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Create rkwin options files 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

PHASE = 1;
CODE = 2;
TRAJ = 3; 

fileP(PHASE) = fopen([directoryname 'rkwin_options_phase.txt'], 'wt');
fileP(CODE) = fopen([directoryname 'rkwin_options_code.txt'], 'wt');
fileP(TRAJ) = fopen([directoryname 'rkwin_options_traj.txt'], 'wt');

%% Carrier phase options file
% -e 0
% -r -2691521.391 -4301170.782 3851781.385
% -x -2691560.659 -4301144.164 3851781.333
% -m 3
% -o 1.0
% -z 0
% -u 0.120000 0.220000
% -v
% -w 4
% -h 0 0

% -d 5 -- for code residuals

% -m 2 -- for generating trajectory

% Cycle through common components
for fileType = 1:3
  
  fp = fileP(fileType); 

  % set elevation mask
  fprintf(fp, '# options file generated by makeRkwinCfgFiles.m\n');
  fprintf(fp, '-e 0\n');
  % set base position
  fprintf(fp, '-r %10.3f %10.3f %10.3f\n', ...
    surveyPos(BASE, 1), surveyPos(BASE, 2), surveyPos(BASE, 3));
  % set rover position
  fprintf(fp, '-x %10.3f %10.3f %10.3f\n', ...
    surveyPos(ROVER, 1), surveyPos(ROVER, 2), surveyPos(ROVER, 3));
  % Set remaining configuration
  fprintf(fp, '-o 1.0\n');
  fprintf(fp, '-z 0\n');
  fprintf(fp, '-u 0.120000 0.220000\n');
  fprintf(fp, '-v 3\n');
  fprintf(fp, '-w 4\n');
  fprintf(fp, '-h 0 0\n');

  if (fileType == TRAJ)
    fprintf(fp, '-m 2\n'); 
  else % fileType == CODE or PHASE
    % Compute residuals with respect to externally supplied baseline
    % trajectory.
    fprintf(fp, '-m 3\n');
    fprintf(fp, '--gie_extern_refsv\n');
    fprintf(fp, '--gie_extern_trajectory\n');
  end

  if (fileType == CODE)
    % Append code residual option to the code file.
    fprintf(fileP(2), '-d 5\n');
  end
  
  fclose(fp); 
  
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Create rkwin double difference residual batch file. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fp = fopen([directoryname 'run_rkwin.bat'], 'wt');

% C:\Vivek\Survey\code\RTKPC\rkwin\Debug\rkwin.exe sunn.dat Cont_29.dat -@ rkwin_options_sun2.txt
% del ddPhaseResidualTrueRover.txt
% ren ddPhaseResidualTrue.txt ddPhaseResidualTrueRover.txt
% 
% C:\Vivek\Survey\code\RTKPC\rkwin\Debug\rkwin.exe sunn.dat TestK_29.dat -@ rkwin_options_sun2.txt
% del ddPhaseResidualTrueGie.txt
% ren ddPhaseResidualTrue.txt ddPhaseResidualTrueGie.txt
fprintf(fp, 'REM batch file generated by makeRkwinCfgFiles.m\n'); 

fprintf(fp, '\nREM process Rover data\n');
fprintf(fp, '%s %s.dat %s.dat -@ rkwin_options_phase.txt\n', ...
  rkwinExe, fileNames{BASE}, fileNames{ROVER});
fprintf(fp, 'del ddPhaseResidualTrueRover.txt\n');
fprintf(fp, 'ren ddPhaseResidualTrue.txt ddPhaseResidualTrueRover.txt\n');

fprintf(fp, '\nREM process GIE data\n');
fprintf(fp, '%s %s.dat %s.dat -@ rkwin_options_phase.txt\n', ...
  rkwinExe, fileNames{BASE}, fileNames{GIE});
fprintf(fp, 'del ddPhaseResidualTrueGie.txt\n');
fprintf(fp, 'ren ddPhaseResidualTrue.txt ddPhaseResidualTrueGie.txt\n');

fprintf(fp, '\nREM process Rover Code data\n');
fprintf(fp, '%s %s.dat %s.dat -@ rkwin_options_code.txt\n', ...
  rkwinExe, fileNames{BASE}, fileNames{ROVER});
fprintf(fp, 'del ddCodeResidualTrueRover.txt\n');
fprintf(fp, 'ren ddCodeResidualTrue.txt ddCodeResidualTrueRover.txt\n');

fprintf(fp, '\nREM process GIE Code data\n');
fprintf(fp, '%s %s.dat %s.dat -@ rkwin_options_code.txt\n', ...
  rkwinExe, fileNames{BASE}, fileNames{GIE});
fprintf(fp, 'del ddCodeResidualTrueGie.txt\n');
fprintf(fp, 'ren ddCodeResidualTrue.txt ddCodeResidualTrueGie.txt\n');

fclose(fp);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Create rkwin trajectory batch file. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fp = fopen([directoryname 'run_rkwin_traj.bat'], 'wt');

fprintf(fp, 'REM batch file generated by makeRkwinCfgFiles.m\n'); 
fprintf(fp, '%s %s.dat %s.dat -@ rkwin_options_traj.txt\n', ...
  rkwinExe, fileNames{BASE}, fileNames{ROVER});
fprintf(fp, 'del rkwsTraj.pos\n'); 
fprintf(fp, 'ren rkws.pos rkwsTraj.pos\n');

fclose(fp); 


return; 
