% read hyd6 ini file

function[sigma_array]=read_hyd6_cal(fname)

fid=fopen(fname,'r');
channel_number=0;


%init array

sigma_array=struct('ch_name','','sigma1',0,'sigma2',0,'sigma3',0);


% identify cal file version
string_read=fscanf(fid,'%s',[1]);
	% remove blank char
	string_read_length=size(string_read,2);
   if string_read_length>1

   j=0;
   while string_read(1,string_read_length-j)==' '
      string_read=string_read(1,1:string_read_length-j-1);
   	j=j+1;   
   end
   string_read_length=size(string_read,2);
	string_read=lower(string_read);
   if strcmp(string_read,'[start]')==1  | strcmp(string_read,'makeini')==1
      version=1;
   else
      version=2;
   end
   end
   
   
% Read cal file according to version detected    
   
if version==1
while (feof(fid)==0)
   
   string_read=fscanf(fid,'%s',[1]);

% remove blank char
	string_read_length=size(string_read,2);
   if string_read_length>4

   j=0;
   while string_read(1,string_read_length-j)==' '
      string_read=string_read(1,1:string_read_length-j-1);
   	j=j+1;   
   end
   string_read_length=size(string_read,2);
   
 %
 	string_read=lower(string_read);
    
    
   if string_read(1:4)=='[cha'
      channel_number=str2num(string_read(9));
   end
   
   if string_read(1:4)=='name'
   	channel_name=string_read(6:string_read_length);
   end   
   
   if channel_number~=0
      if string_read(1:5)=='sigma'
         sigma_number=str2num(string_read(6));
   	   sigma=str2num(string_read(8:string_read_length));
         sigma_array(channel_number).ch_name=channel_name;
         eval(['sigma_array(' num2str(channel_number) ').sigma' num2str(sigma_number) '=' num2str(sigma) ';']);
      end
   end
	end   
end
end

if version==2
while (feof(fid)==0)
   
   string_read=fscanf(fid,'%s',[1]);

% remove blank char
	string_read_length=size(string_read,2);
   if string_read_length>4

   j=0;
   while string_read(1,string_read_length-j)==' '
      string_read=string_read(1,1:string_read_length-j-1);
   	j=j+1;   
   end
   string_read_length=size(string_read,2);
   
 %
 	string_read=lower(string_read);
    
    
   if string_read(1:4)=='[cha'
      channel_number=fscanf(fid,'%d');
   end
   
   if string_read(1:4)=='name'
   	channel_name=string_read(6:string_read_length);
   end   
   
   if channel_number~=0
      if string_read(1:5)=='sigma'
         sigma_number=str2num(string_read(6));
   	   sigma=str2num(string_read(8:string_read_length));
         sigma_array(channel_number).ch_name=channel_name;
         eval(['sigma_array(' num2str(channel_number) ').sigma' num2str(sigma_number) '=' num2str(sigma) ';']);
      end
   end
	end   
end
end

fclose(fid);
return
