% loadParameterFile.m
% 
% Load parameters from an xml configuration file. 
% 

function [parameters] = loadParameterFile (reloadFile);

%% Initialize reloadFile if it isn't provided
if ( nargin == 0 )
  reloadFile = 0; 
end

%% Load the version structure
currentVersion = loadVersion; 

%% Load the configuration file
disp('Loading configuration (XML) file.');
configFileName = 'analyzePosConfig.xml';

if exist(configFileName, 'file')
  xDoc = xmlread(configFileName);
else
  error(['Could not find configuration file ' configFileName]);
end

%% Extract the filename
fileName = getElementFromXML(xDoc, 'fileName');

%% Get the fileType - t01 or extracted
% Run viewdat if t01 file 
% An extracted file is the text output of viewdat -d29 -h -m
% It may also be the text output loaded into a mat file in the future. 
fileTypeString = getElementFromXML(xDoc, 'fileType');

% initialize flags
extractT01File = 0; 
useExtractedFile = 0; 
if ( strcmp( fileTypeString, 't01' ) || strcmp( fileTypeString, 'T01' ) )
  extractT01File = 1; 
elseif ( strcmp( fileTypeString, 'extracted' ) )
  useExtractedFile = 1; 
else
  error('File Type is unknown. \n Specify "t01" or "extracted" for the fileType tag.');
end

  
%% Position record type string 'rec29' or 'rec11'
posRecString = getElementFromXML(xDoc, 'positionRecordType');
if ( strcmp( posRecString, 'rec29' ) )
  posRecType = 29;
elseif ( strcmp( posRecString, 'rec11' ) )
  posRecType = 11;
else
  error('Position Record Type is unknown. \n Specify "rec11" or "rec29" for the positionRecordType tag.');
end

%% Get the coordinates

% First try LLA
elementIsOptional = 1; 
[latStr,latPresent] = getElementFromXML(xDoc, 'latitude', elementIsOptional);
[lonStr,lonPresent] = getElementFromXML(xDoc, 'longitude', elementIsOptional);
[hgtStr,hgtPresent] = getElementFromXML(xDoc, 'height', elementIsOptional);

% Construct the reference LLA coordinates
if ( latPresent && lonPresent && hgtPresent )
  llaPresent = 1;
  refLLA = [str2num(latStr), str2num(lonStr), str2num(hgtStr)];
else
  llaPresent = 0; 
end

% Then try X,Y,Z if LLA is not present
if ~llaPresent
  [xStr,xPresent] = getElementFromXML(xDoc, 'ecefX', elementIsOptional);
  [yStr,yPresent] = getElementFromXML(xDoc, 'ecefY', elementIsOptional);
  [zStr,zPresent] = getElementFromXML(xDoc, 'ecefZ', elementIsOptional);
  
  if ( xPresent && yPresent && zPresent )
    xyzPresent = 1; 
    refLLA = xyz2geo( str2num(xStr), str2num(yStr), str2num(zStr) );    
  else
    % xyzPresent = 0;
    error ( 'Configuration file does not contain XYZ or LLA reference coordinates' );
  end
end


%% Figure out whether to save MAT file
saveMatFileStr = getElementFromXML(xDoc,'saveMatFile');
saveMatFile = str2num(saveMatFileStr);

%% Extract position limits
[posLimitStr, posLimitPresent] = ... 
    getElementFromXML(xDoc, 'posLimitMeters', elementIsOptional);
if ( posLimitPresent )
  posLimitMeters = str2num(posLimitStr);
else
  % Use default value
  posLimitMeters = 5.0; 
end

%% Extract velocity limits
[velLimitStr, velLimitPresent] = ... 
    getElementFromXML(xDoc, 'velLimitMetersPerSecond', elementIsOptional);
if ( velLimitPresent )
  velLimitMetersPerSecond = str2num(velLimitStr);
else
  % Use default value
  velLimitMetersPerSecond = 0.5;
end

%% Extract packageResults flag. 
[packageResultsStr, packageResultsPresent] = ... 
    getElementFromXML(xDoc, 'packageResults', elementIsOptional);
if ( packageResultsPresent )
  packageResultsFlag = str2num(packageResultsStr);
else
  % Use default value of TRUE
  packageResultsFlag = 1; 
end


%% Populate the parameters to be sent to the outside world.
parameters.fileName = fileName; 
parameters.extractT01File = extractT01File; 
parameters.useExtractedFile = useExtractedFile; 
parameters.posRecType = posRecType; 
parameters.refLLA = refLLA;
parameters.reloadFile = reloadFile; 
parameters.saveMatFile = saveMatFile;
parameters.posLimitMeters = posLimitMeters;
parameters.velLimitMetersPerSecond = velLimitMetersPerSecond; 
parameters.packageResults = packageResultsFlag; 
parameters.versionTag = currentVersion.versionTag;
parameters.releaseTag = currentVersion.releaseTag;
end


%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% getElementFromXML extracts the first element of the requested tag from
% an XML file if it exists. If the tag doesn't exist, getElementFromXML
% flags an error.  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [element,elementExists] = getElementFromXML(xDoc, tag, elementIsOptional)
  
  elementList = xDoc.getElementsByTagName(tag);
  
  if ( elementList.getLength > 0 )
    element = elementList.item(0).getFirstChild.getData; 
    % Convert Java strings to Matlab strings
    element = char(element);
    elementExists = 1; 
  else

    element = 'null'; 
    if ( nargin == 2 )
      elementIsOptional = 0; 
    end
    
    if elementIsOptional
      elementExists = 0;
    else 
      error(['The configuration XML file does not specify ' tag]);
    end
  end
  
end
