function [Cn2, r0] = ProcessScidar(file);
%
% Batch file implementation of processing scripts
% No parameter look up as read from header txt files
%
% INPUTS: file= file containing data.
%
% Error checking should be performed by Matlab hence no error handles on function calls
% Mo 12/07/02 crw99@ic.ac.uk
% Set path
%cd C:\Scidar_data;
addpath /home/asmpredi/Scidar/matlab/Scripts
cd /home/asmpredi/Scidar/matlab/Temp
%regularisation parameter for the inversion [0,1]
%0=exact inverse solution (noisy, negative values, positivity not fullfilled), 
%1=max noise damping (smooth profile but large error) 
reg=0.05;
%instrumental constants
drstep=1/122.; %pupil sampling (m per pixel)
%


%
% if miska=0 the program use matlab'matrices and;
% if miska=1 the program uses miska's matrices
miska=1;
save miska miska;



i=0;
count=0;
Hfile='Empty';
for i=1:size(file,2)
    if file(1,i)=='_'
        count=i;
    end
end
Hfile=file(1,1:(count-1));
ext='.txt';
Hfile=strcat(Hfile,ext);
s = which (Hfile);
if (isempty(s))
    error('No header file found! Ensure you have copied the file across to local machine');
 end

daterun = (textread (Hfile,'%s',10));
%strdate=string(daterun)
type (Hfile);
fid=fopen(Hfile,'r');
i=0;
count=0;

for i=1:3
temp=fgetl(fid);
end

str=fgetl(fid);
fclose(fid);
for i=1:size(str,2)
    if str(1,i)==',';
        count=i;
    end
end


if ( isempty(str(count+1))) | count==0
    disp('Error in header file format, please enter the parameters manually: ')
    zenith=input('Angle from Zenith');
    sep=input('Angular separation of star');
    magdiff=input('Magnitude difference');
    conj=input('Conjugate height');
else
    strshort=str(count+2:size(str,2));
    clear str;
    %  [zenith,sep,magdiff,conj]=strread(strshort,'%f%f%f%f'); %only in matlab6
  [char,char2,zenith,sep,magdiff,conj]=textread(Hfile,'%s %s %f %f %f %f','headerlines',3);
  if (isempty(conj) | isempty(sep) | isempty(magdiff) | isempty(zenith))
     disp('Error in header file format, please enter parameters manually: ');
    zenith=input('Angle from Zenith');
    sep=input('Angular separation of star');
    magdiff=input('Magnitude difference');
    conj=input('Conjugate height');
	end
end
parameters=[zenith, sep, magdiff, drstep, -conj];

name_base=file(1:size(file,2)-4);


if miska==0
 ext1='_auto_2';
 ext2='_nrm_0_1';
else
 ext1='_auto_miska';
 ext2='_nrm_miska';
end
fileauto=strcat(name_base,ext1);
filenrm=strcat(name_base,ext2);

%   disp('Entering generalised mode');
	pause (1) %time for display info in the command window
   [Cn2,r0]=SCIDARProcessGen2ASCII(file,2000,fileauto,filenrm,parameters,reg,'empt');
   
   figure(3)
   text(1.8,1.5,daterun,'fontsize',8)
   
%   cd C:\SCIDAR\matlab\Results;
cd /home/asmpredi/Scidar/matlab/Temp
   figure(3)
   print ('-djpeg',name_base);
   
    
