function analyzeGie; 
% analyzeGie.m
% 
% Top level function to run the GIE data analysis. This file
% also contains instructions on how to run gieTools to analyze 
% GIE data. The instructions are structured as a tutorial. 
% 
% You can perform all the steps and manually run each m-file as 
% described in these instructions. Otherwise, you can go through 
% all the m-files, verify that you have done all the steps and
% then *run* analyzeGie.m at the Matlab prompt. In order to
% run the analyzeGie you need to set the runAnalyzeGie flag
% to 1 before trying to execute this function. By default this
% flag is set to zero. 
% 
% After these m-files are run, a directory called "results" will be
% created under the directory in which you have kept the DAT-files
% being used as inputs. This results directory will contain image
% files in the Portable Network Graphics (PNG) format. 
% 
% Most web browsers will read PNG. You can use also a PNG viewer 
% such as the freeware IrfanView (www.irfanview.com) under Windows, 
% which will allow you to page through the image files in a 
% directory as if it were a slideshow. 
% 
% Vivek Nadkarni
% Trimble
% July 13, 2004
% updated December 22, 2004

runAnalyzeGie = 0;
if ~runAnalyzeGie
  disp(' '); 
  disp('analyzeGie: This is the top level gieTools function. It ');
  disp('contains instructions on how run it. In short: '); 
  disp('  1. Follow the instructions on how to set up data files. ');
  disp('  2. Edit gieTestSetup.m to point to the data files. ');
  disp('  3. Verify that flags in the functions are set properly. ');
  disp('  4. Set the flag runAnalyzeGie = 1 in the analyzeGie.m file. ');
  disp('  5. Run analyzeGie from the Matlab command prompt.');
  disp(' '); 
  return; 
end

stTm = clock;
% 1. Create a directory for the data. Place the Base, Rover, and GIE
%    DAT-files in this directory. The Rover and GIE data should be split
%    off the same antenna. The Base receiver is typically a separate
%    receiver on a separate antenna. Let us say that the data files are
%      Rover = ContCut_28.dat
%      GIE = TestKCut_28.dat
%      Base = SUN2195.dat 
%    The base data was obtained from the SunnyvaleB8 antenna, by
%    concatenating the binary hourly DAT files obtained from the Tahoe
%    server. You can use "copy /b" in DOS to concatenate the binary
%    files, but make sure they are copied in alphabetical order.
%    The files are placed in the directory 
%      C:\Vivek\Data\Survey\gieAnalysis\2004-07-12\
% 
% 2. Copy the file gieCfgScript_Example.m into the data directory
%    specified above and rename it to gieCfgScript.m. Open this file
%    gieCfgScript.m and edit it.
%    a. Change the "directoryname" to the directory specified above. Make 
%       sure that the directoryname has a trailing backslash. (i.e. 
%       2004-07-12\ instead of just 2004-07-12).
%    b. Change the fileNames variables in the file to specify the 
%       base, rover and gie DAT files.  
%    c. Specify the surveyed in positions for the base and rover/gie 
%       antennas for the static rover test. The coordinates should
%       be specified as the [X, Y, Z] position of the antenna in 
%       ECEF coordinates. It doesn't matter if you use the WGS-84 or 
%       ITRF coordinate frame since they both use the same ellipsoid. 
%       However you must use the same coordinate frame for both 
%       the base and the rover/gie. For the dynamic rover test, use
%       the position of the base as the position of the rover/gie. 
%    d. Specify the surveyType for the test. This gives the source of
%       truth data i.e. the reference trajectory for the test. 
%         'dynamic' - rover trajectory generated by rkwin
%         'static'  - mean of the rover trajectory from rkwin
%         'survey'  - externally surveyed point. specify location in
%                     surveyPos(ROVER,:) and surveyPos(GIE,:)
%         'mpos'    - trajectory from mPOS
%         'ats'     - trajectory from ATS
%    e. Specify the mpos trajectory file in mposFileName and the ATS
%       trajectory file in atsFileName. 
%    f. If some SVs are not to be used as a reference SV, specify
%       them in purgeSvList.
%    g. Manually specifying Loop L information:
%       SVs going into Loop L along with the corresponding start and
%       end times for loop L can be specified using svLoopL, tStartLoopL,
%       tEndLoopL.  Similarly, events such as frequency errors can be 
%       specified manually using svEvent, tEvent and descEvent. 
%       If we are not using these manual specifications, define these
%       values but leave them as empty matrices. 
%    h. Automatically specifying Loop L information:
%       If the #Sw# (Loop Switching) diagnostics are being output in
%       the gielog.txt, you can run 
%       grep "#Sw#" gielog.txt | sed "s/#Sw#//" > gielog_switch.txt
%       The start and end times for loop L are grabbed from
%       gielog_switch.txt and used in the plots. 
%       If the #F# (frequency error) diagnostics are being output in
%       the gielog.txt, you can run 
%       grep "#F#" gielog.txt | sed "s/#F#//" > gielog_freq.txt
%       The times for frequency errors are grabbed from
%       gielog_freq.txt and used in the plots. 
%    i. Run gieTestSetup from the Matlab command prompt. 
if (gieTestSetup == 0)
  disp('Exiting ...');
  return;
end

% 3. Open the file preprocessGieData.m. Make sure that the following
%    flags are set to 1. 
%      convertDat2Txt = 1;
%      convertTxt2Mat = 1; 
%    Also make sure that at the bottom of the file, the following
%    flag is set to 1. 
%      runCombineFiles = 1; 
%    Run preprocessGieData.m. 
preprocessGieData;

% 4. Open the file runRkwin.m. Make sure that the following 
%    flags are set to 1. 
%      createRkwinFiles       = 1;
%      createRefSvList        = 1;
%      createTrajFile         = 1;
%      runRkwinBatchfile      = 1;
%      combineRkwinPhaseFiles = 1;
%      combineRkwinCodeFiles  = 1;
%    Run runRkwin.m from the Matlab command prompt.
runRkwin;

% 5. Open plotCNzero.m and ensure that the 
%    flags are set as follows. 
%      printPlots = 1; % save to PNG format
%      plotFigS = 1;   % generate individual plots by SV
%      plotFigC = 1;   % generate composite plot of all SVs
%    Run plotCNzero.m from the Matlab command prompt. 
plotCNzero;

% 6. Open plotDdResids.m. Ensure that the flags are as follows:
%      printPlots = 1; % save plots in PNG format
%      plotFigC   = 1; % combined figure 1
%      plotFigC2  = 1; % combined figure 2
%      plotFigS2  = 1; % separate figures by SV
%      plotFigH   = 1; % histogram
%      plotFigS   = 0; % old figure, no longer used
%    Run plotDdResids.m from the Matlab command prompt with the 
%    input parameter 'code'. Repeat for the input parameter 
%    'phase'. You can also specify the max value of the y-axis of 
%    the plot as a second parameter to plotDdResids. e.g.
%      plotDdResids('code', 20); % for y-axis of -20:20 meters or 
%      plotDdResids('phase', 2); % for y-axis of -2:2 L1 cycles
plotDdResids('code',20);
plotDdResids('phase',5);

% 7. Open plotDdResVsIndepVar. Ensure that the flags are as follows:
%      printPlots = 1; % save plots to PNG
%      plotBar    = 0; % create bar chart instead of plot
%    Run plotDdprVsCNzero.m from the Matlab command prompt. Use
%    the input parameters as shown below, to generate statistics
%    of both the code and the phase, as they vary with each
%    CN0, elevation and azimuth. 
%    independent parameter. 

% Double Differenced Code Residuals
plotDdResVsIndepVar('code', 'cn0');
plotDdResVsIndepVar('code', 'elv');
plotDdResVsIndepVar('code', 'az');

% Double Differenced Phase Residuals
plotDdResVsIndepVar('phase', 'cn0');
plotDdResVsIndepVar('phase', 'elv');
plotDdResVsIndepVar('phase', 'az');

% 8. All the plots generated are stored in the directory:
%      C:\Vivek\Data\Survey\gieAnalysis\2004-07-12\results\
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

endTm = clock;
elapsedTime = etime(endTm, stTm);
fprintf('Elapsed time = %.1f seconds\n', elapsedTime);

return; 
