%% preprocessGieData
% 
% Vivek Nadkarni
% July 7, 2004
% Trimble

function preprocessGieData

load gieTestSetupData; 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% User should modify the following section:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% error('Are you sure? Remove this line from preprocessGieTest.m to continue. '); 

% Set these flags to 0 if the DAT-files have already been converted to
% MAT file format. Set the flags to 1 if you have not already run the
% conversion. The flags should normally be 1. 

convertDat2Txt = 1; 
convertTxt2Mat = 1; 

% The user should also enable or disable the runCombineFiles flag at the
% end of this script. Normally, the flag should be set to 1. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% End of section for user to modify.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Save the composite data in this file. 
testFileName = 'combinedTestData'; 

% Save the result plots to this directory
resultsdir = [directoryname 'results\'];

if (exist(resultsdir, 'dir') ~= 7)
  mkdir(directoryname, 'results');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Create keys for indexing into various types of files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% dkey is the key for indexing into the Matlab version of dat-files
dkey.TIME = 1;
dkey.CLOCKBIAS = 2;
dkey.SVID = 3;
dkey.AZ = 4;
dkey.ELV = 5;
dkey.PHASE = 6; 
dkey.RANGE = 7; 
dkey.L1CNZERO = 9;
dkey.VALID = 10; % CSLIP = +1, Invalid = -1, Good = 0
dkey.P2CODE = 11; % +1 if P2Code is bad, Good = 0
dkey.L2PHASE = 12; 
dkey.L2RANGE = 13; 
dkey.L2CNZERO = 14; 
dkey.L2SLIP = 15; % CSLIP = +1, Good = 0

% rkey is the key for indexing into double difference phase residual
% files
% 
% time, refsv, sv, dd resid, weight, ambStatus
rkey.TIME = 1; % timetag
rkey.REFSV = 2; % reference sv
rkey.SV = 3; % other sv for double difference
rkey.RESID = 4; % double difference residual
rkey.VAR = 5; % variance or weight
rkey.SLIP = 6; % ambiguity status or slip

rkey.RefSvElv = 7;
rkey.RefSvAz = 8;
rkey.TestSvElv = 9;
rkey.TestSvAz = 10;

rkey.BaseRefSvSNR = 11;
rkey.RovRefSvSNR = 12;
rkey.BaseTestSvSNR = 13;
rkey.RovTestSvSNR = 14;

% mkey is the key for indexing into mPOS Vehicle Navigation 
% Data files
mkey.TIME   = 1; % sec
mkey.LAT    = 2; % deg
mkey.LON    = 3; % deg
mkey.ALT    = 4; % meters
mkey.ROLL   = 5; % deg
mkey.PITCH  = 6; % deg
mkey.HDG    = 7; % deg from true North
mkey.VELN   = 8; % m/s North
mkey.VELE   = 9; % m/s East
mkey.VELD   = 10; % m/s Down
mkey.SPEED  = 11; % m/s
mkey.GSPEED = 12; % m/s Ground speed
mkey.ACCX   = 13; % X body accel in (g)
mkey.ACCY   = 14; % Y body accel in (g)
mkey.ACCZ   = 15; % Z body accel in (g)
mkey.ACC    = 16; % total accel
mkey.XRATE  = 17; % X Body Angular Rate (deg/sec)
mkey.YRATE  = 18; % Y Body Angular Rate (deg/sec)
mkey.ZRATE  = 19; % Z Body Angular Rate (deg/sec)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Convert DAT files to MAT files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Running conversion in'); 
disp(directoryname);

for idx = 1:3
  
  %% Convert dat file to text file
  if (convertDat2Txt)
    
    %     commandstr = ['viewdat -d17 -m ' directoryname fileNames{idx} ...
    %         '.dat > ' directoryname fileNames{idx} '.txt'];
    commandstr = [viewdatExe ' -d17 -m ' directoryname fileNames{idx} ...
        '.dat > ' directoryname fileNames{idx} '.txt']; 
    disp(commandstr);
    [s,w] = system(commandstr); 
    
  end
 
  %% Convert text file to mat file
  if (convertTxt2Mat)    
    disp('Loading text file');
    data = load([directoryname fileNames{idx} '.txt']);
        
    disp('Saving MAT file');
    save([directoryname fileNames{idx} '.mat'], 'data');
  else
    % Load the MAT-file directly
    load([directoryname fileNames{idx} '.mat']);    
  end
  
  % Save start and end times for chopping the files later
  dataStartTime(idx) = data(1,dkey.TIME);
  dataEndTime(idx) = data(end,dkey.TIME);
    
end

%% Select data time to be tested. 
startTime = max(dataStartTime); 
endTime = min(dataEndTime); 
duration = (endTime - startTime); 

disp('Conversion Complete'); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Clear temporary variables created
clear data idx s w commandstr convertDat2Txt convertTxt2Mat; 

% Save the expanded configuration file in the gieTools directory. 
save(cfgFileName); 

% Save the configuration file for all the tests in the directory in
% which the data is stored. This will allow us to recreate the
% results if we need to. 
save([directoryname cfgFileName]); 

% % Copy the temporary configuration file into the directory in which 
% % the data is being stored. This will allow us to recreate the 
% % results if we need to. 
% copyfile([cfgFileName '.mat'], cfgScriptDir); 

% Run the routine to combine the three data files into a single file for
% analyzing together. 
runCombineFiles = 1;
if (runCombineFiles)
  combineFiles; 
end

return; 
