function [encoder, timehi, timelo, eofstat, end_position, nav_data,V]=read_raw_iwrap(filename, number_of_packets, start_position)
%[V,encoder, timehi, timelo, eofstat, end_position, nav_data]=read_raw_iwrap(filename, number_of_packets, start_position)
fid=fopen(['C:\Documents and Settings\lorsolo\My Documents\research\rita\data\',filename(1).name],'r','l');
eofstat=feof(fid);
fseek(fid, 0, 'eof');
length_of_packets = 4268;
length_of_file=ftell(fid)
check=strcmp('KU',filename(1).name(1:2));
V=zeros(250,number_of_packets*4);
nav_data=zeros(34,number_of_packets);
timehi=[];
timelo=[];
encoder=[];
fseek(fid, start_position, 'bof');
%---------------------------------------Number of headers------------------------%
for iter=1:number_of_packets
    header=fread(fid,8,'char');
    Header=char(header');
    %disp(Header);
    header_counter=fread(fid,1,'int32');
    status=fread(fid,1,'int32');
    timehi=[timehi;fread(fid,1,'uint32')];
    timelo=[timelo;fread(fid,1,'uint32')];
    LI=fread(fid,1,'uint32');
    I=fread(fid,1024,'int16=>int16');
    LQ=fread(fid,1,'uint32');
    
    Q=fread(fid,1024,'int16=>int16');
    
    I=reshape(I(1:1000)',250,4);
    Q=reshape(Q(1:1000)',250,4);
    V(:,(iter-1)*4+1:iter*4)=complex(I,Q);
    encoderC=fread(fid,1,'uint16');
    encoderKU=fread(fid,1,'uint16');
    if check==1
        encoder=[encoder;encoderKU];
    else
        encoder=[encoder;encoderC];
    end
   nav_data(:,iter)=fread(fid,34,'float32');
    if length_of_file <=(length_of_packets*iter)
        fseek(fid, 0, 'eof');
        disp('reach end of file')
        break
    else
        fseek(fid, (start_position+length_of_packets*iter), 'bof');
    end
    
end


end_position=ftell(fid)
eofstat=feof(fid);
if eofstat==1
    fclose(fid);
end


