%**************************************************************************
% CLUSTER project: System check                    Autor: Christian Maszl 
% Unit System = [SI]                  Mail: e0025754@student.tuwien.ac.at 
%**************************************************************************
%
% Version 1.0 - 20.08.2004
%
% This program loads the configfile.m and check if the values in
% the configfile are valid and perform several write tests . It
% also  converts the mode variable of the configfile to the proper
% input parameters for the subroutine getData. 
%
% Example: nr to b (normal mode), hr to bfgm (high res. mode)

errorflag=0;

% Error messages
error0=['Please restart the program!'];
error1=['Directory for 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 make sure that the components of advanced are only' ...
	 ' integer!'];
error16=['Please correct the induction constant y0 to 1.256637061E-6' ...
         ' Vs/Am. Unit System: SI'];
error17=['Please correct the varibale opmode in configfile.m to either' ...
	 ' a for AUTOMATIC or m for MANUAL!'];
error18=['Please correct the varibale nolog in configfile.m to either' ...
	 ' y = YES or n = NO logfiles!'];
error19=['You do not have write permission for the directory!'];
error20=['Please change the path or use the chmod command to alter' ...
	' the permission status!'];
error21=['Please correct the varibale plot_mode_normal in configfile.m '...
	 'to either y = YES or n = NO!'];
error22=['Please correct the varibale plot_mode_parnor in configfile.m '...
	 'to either y = YES or n = NO!'];
error23=['Please correct the varibale legend_on in configfile.m to either' ...
	 ' y = YES or n = NO!'];

% Load the configfile
try
   configfile;
catch
   disp(error11);
   disp(error15);
   disp(error0);
   break;
end

%**************************************************************************
% Conversion to satellite instrument
%**************************************************************************
op_mode=lower(op_mode);
% hr = high resolution mode - 45msec data
if     (op_mode=='hr')
        % Mode is the parameter in the filenames
        mode      ='fb';
        instrument='bfgm';
	% Prevet Yuri's programm from storing data to the HD
	option    ='nosave';
        % Set the time accuracy for the sorting routine
        timeacc   =8.00e-6; % in [h] - ca. 29msec
	timeres   ='45m';
	datasrc   ='fgm';
	dataname  ='"high resolution mode"';
	varname   ='B';
% nr = normal resolution mode -  4sec data
elseif (op_mode=='nr')
        mode      ='fn';
        instrument='b';
	option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-4; % in [h] - ca.  2sec
	timeres   ='4';
	datasrc   ='fgm';
	dataname  ='"normal mode(pp)"';
	varname   ='B';
elseif (op_mode=='vi')
        mode='notavailable';
        instrument='vcis';
	option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-4; % in [h] - ca.  2sec
	timeres   ='4';
	datasrc   ='vcis - cis';
	dataname  ='"normal mode(pp)"';
	varname   ='X';
elseif (op_mode=='ve')
        mode='notavailable';
        instrument='r';
	% Instrument use in the following order vcis - b
	% - vce. Therefore start with r. Have a look at the 
	% mainprogram!
	option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-4; % in [h] - ca.  2sec
	timeres   ='4';
	datasrc   ='vce - cis';
	dataname  ='"normal mode(pp)"';
	varname   ='X';
	% Note in order to use this parameter don't set the
	% option 'nosave' for yuri.m
elseif (op_mode=='ni')
        mode='notavailable';
        instrument='ncis';
	option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-4; % in [h] - ca.  2sec
	timeres   ='4';
	datasrc   ='ncis - cis';
	dataname  ='"normal mode(pp)"';
	varname   ='X';
elseif (op_mode=='ed')
        mode='notavailable';
        instrument='edi';
	option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-4; % in [h] - ca.  2sec
	timeres   ='4';
	datasrc   ='edi';
	dataname  ='"normal mode(pp)"';
	varname   ='X';	
elseif (op_mode=='sp')
        mode='notavailable';
        instrument='p';
	option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-4; % in [h] - ca.  2sec
	timeres   ='4';
	datasrc   ='aspoc';
	dataname  ='"normal mode(pp)"';	
	varname   ='X';		
elseif (op_mode=='ef')
        mode='notavailable';
        instrument='e';
        option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-6; % in [h] - ca.  2sec
	timeres   ='40m';
	datasrc   ='efw';
	dataname  ='"normal mode(pp)"';	
	varname   ='X';		
elseif (op_mode=='sa')
         mode='notavailable';
        instrument='sax';
        option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-4; % in [h] - ca.  2sec
	timeres   ='4';
	datasrc   ='ephemeris spin axis orientation';
	dataname  ='"normal mode(pp)"';		
	varname   ='X';		
elseif (op_mode=='wp')
        mode='notavailable';
        instrument='whip';
        option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-4; % in [h] - ca.  2sec
	timeres   ='4';
	datasrc   ='whisper';
	dataname  ='"normal mode(pp)"';		
	varname   ='X';		
elseif (op_mode=='ph')
        mode='notavailable';
        instrument='a';
        option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-4; % in [h] - ca.  2sec
	timeres   ='4';
	datasrc   ='ephemeris phase';
	dataname  ='"normal mode(pp)"';		
	varname   ='X';		
elseif (op_mode=='lo')
        mode='notavailable';
        instrument='r';
        option    ='nosave';
	% Set the time accuracy for the sorting routine
        timeacc   =5.50e-4; % in [h] - ca.  2sec
	timeres   ='4';
	datasrc   ='ephemeris position';
	dataname  ='"normal mode(pp)"';		
	varname   ='r';		
else
% If mode is unknown display error message!
    disp(error12);
    errorflag=1;
end
mode      =lower(mode);
instrument=lower(instrument);
% The time accuracy "timeacc" effect the sorting routine a lot! A to
% small accuracy lead to high peaks in jav. A to high accuracy cancels
% all the data!
%**************************************************************************

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
datatype=lower(datatype);
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(op_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
compmode=lower(compmode);
if (compmode=='a'| compmode=='m')
else
    disp(error17);
    errorflag=1;
end
nolog =lower(nolog);
if (nolog =='y'| nolog =='n')
else
    disp(error18);
    errorflag=1;
end
plot_mode_normal =lower(plot_mode_normal);
if (plot_mode_normal =='y'| plot_mode_normal =='n')
else
    disp(error21);
    errorflag=1;
end
plot_mode_parnor =lower(plot_mode_parnor);
if (plot_mode_parnor =='y'| plot_mode_parnor =='n')
else
    disp(error22);
    errorflag=1;
end
legend_on =lower(legend_on);
if (legend_on =='y'| legend_on =='n')
else
    disp(error23);
    errorflag=1;
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
% Check for write permission in temp, log, and outputdata
command_string=['touch ' logfilepath 'write_test'];
if (unix(command_string)==0)
    delete([logfilepath 'write_test']);
else
    message=[error19 logfilepath ' !'];
    disp(message);
    disp(error20);
    errorflag=1;
end
command_string=['touch ' outputspath 'write_test'];
if (unix(command_string)==0)
    delete([outputspath 'write_test']);
else
    message=[error19 outputspath ' !'];
    disp(message);
    disp(error20);
    errorflag=1;
end
command_string=['touch ' tempdirpath 'write_test'];
if (unix(command_string)==0)
    delete([tempdirpath 'write_test']);
else
    message=[error19 tempdirpath ' !'];
    disp(message);
    disp(error20);
    errorflag=1;
end
% Display restart message
if (errorflag==1)
  disp(error0);
  break;
else
end
clear l error* message command_string;



