%**************************************************************************
% CLUSTER project: System check                    Autor: Christian Maszl * 
% Unit System = [SI]                  Mail: e0025754@student.tuwien.ac.at *
%**************************************************************************
%
% This loads the configfile.m and check if the values in the
% configfile are valid. It also converts the mode variable of the 
% configfile to the proper input parameters for the subroutine
% getData. Example> fn -> bfgm

errorflag=0;

% Error messages
error0=['Please restart the program!'];
error1=['Directory for the outputdata not found! Please check the' ...
        ' entry in configfile.m or create it (mkdir)!'];
error2=['Directory of the inputdata not found! Please check the' ...
        ' entry in configfile.m or make sure that the disk is' ...
        ' mounted! '];
error3=['Temporary directory not found! Please check the entry in' ...
        ' configfile.m or create it (mkdir)!'];
error4=['Logfile directory not found! Please check the entry in' ...
        ' configfile.m or create it (mkdir)!'];
error5=['Please enter -mat or -ascii for the' ...
        ' variable datatype in configfile.m (between primes)!'];
error6=['Please enter * for the  variable extension' ...
        ' in configfile.m (between primes)!'];
error7=['Please enter an integer value with TWO digits for the year!' ...
        ' Examples: 01,02,03 ... '];
error8=['Please enter an INTEGER value with two digits for the year!' ...
        ' Examples: 01,02,03 ... '];
error9=['Please enter for mode in configfile.m 2 characters between' ...
        ' primes! Example: fn'];
error10=['Configfile not loaded or incomplete! There are maybe deleted' ...
        ' entries!'];
error11=['Please check if all variable assingments in configfile.m' ...
	 ' are between primes! [Exceptions: y0 and advanced] and'];
error12=['Unknown mode! Please check the variable mode in' ...
	 ' configfile.m!'];
error13=['Please correct the variable advanced in configfile.m!' ...
	 ' Example: [hh mm ss duration]'];
error14=['Please correct the variable advanced in configfile.m! Use' ...
	 ' only integer values! Example: [00 00 00 86400]'];
error15=['Please check that the components of advanced are only' ...
	 ' integer!'];
error16=['Please correct the induction constant y0 to 1.256637061E-6' ...
         ' Vs/Am. Unit System: SI'];
% Load the configfile
try
   configfile;
catch
   disp(error11);
   disp(error15);
   disp(error0);
   break;
end

%**************************************************************************
% Conversion to satellite instrument
if (mode=='fn')
    instrument='bfgm';
else
% If mode is unknwon display error message!
    disp(error12);
    errorflag=1;
end
%**************************************************************************

try
% Check directories
if (isdir(outputspath)==1)
else
    disp(error1);
    errorflag=1;
end
if (isdir(datapath)==1)
else
    disp(error2);
    errorflag=1;
end
if (isdir(logfilepath)==1)
else
    disp(error4);
    errorflag=1;
end
if (isdir(tempdirpath)==1)
else
    disp(error3);
    errorflag=1;
end
if (isdir(logfilepath)==1)
else
    disp(error4);
    errorflag=1;
end
catch
    disp(error10);
end
% Check for typos
if (size(datatype,2)==4)
    if (datatype=='-mat')
    else
        disp(error5);
        errorflag=1;
    end
elseif(size(datatype,2)==6)
    if (datatype=='-ascii')
    else
	disp(error5);
        errorflag=1;
    end
else
    disp(error5);
    errorflag=1;
end
    
try
    if (extension=='*')
    else
    disp(error6);
    errorflag=1;
    end
catch
    disp(error6);
    errorflag=1;
end
if (isempty(str2num(year))==1)
    disp(error8);
    errorflag=1;
else
    if (size(year,2)~=2)
        disp(error7);
        errorflag=1;
    else
    end
end
if (size(mode,2)~=2)
    disp(error9);
    errorflag=1;
else
end
if (size(advanced,2)~=4)
    disp(error13);
    errorflag=1;
else
end
try
    if (isempty(str2num(advanced))==1)
        disp(error14);
        errorflag=1;
    else
    end
catch    
    disp('');
end
if (y0~=1.256637061E-6)
    disp(error16);
    errorflag=1;
else
end
% Check if all path variables end with /
l=size(datapath,2);
if (datapath(1,l)~='/')
    datapath(1,l+1)='/';
else
end
l=size(logfilepath,2);
if (logfilepath(1,l)~='/')
    logfilepath(1,l+1)='/';
else
end
l=size(outputspath,2);
if (outputspath(1,l)~='/')
    outputspath(1,l+1)='/';
else
end
l=size(tempdirpath,2);
if (tempdirpath(1,l)~='/')
    tempdirpath(1,l+1)='/';
else
end
% Display restart message
if (errorflag==1)
  disp(error0);
  break;
else
end
clear l error*;



