%function scan=spect_process
year=2005;
filename=dir(['C:\Documents and Settings\lorsolette\My Documents\research\rita\data\KU*']); 
fid=fopen(['C:\Documents and Settings\lorsolette\My Documents\research\rita\data\',filename(1).name],'r','l');
sweeps=filename(1).name;
eofstat=feof(fid);
fseek(fid, 0, 'eof');
length_of_packets = 4268;
length_of_file=ftell(fid);
total_number_of_packets=fix(length_of_file/length_of_packets);
fseek(fid,0,'bof');
fclose(fid);
max_encoder=511;
scan_rate=30;
PRF=20e3;
T=1/PRF;
channel=sweeps(4:6);

if channel=='CH1'
    freq=12.87e9;
    inc_iwrap=29.49;
elseif channel=='CH2'
    freq=13.25e9;
    inc_iwrap=39.29;
end

c=3e8;
Vny=PRF*c/(freq*4);
lambda=c/(freq);
M=512;
[tout_encoder]=read_encoder(filename, total_number_of_packets, 0);
u_zero=find(tout_encoder==0);
start_position=(u_zero(1)-1)*length_of_packets;
d=diff(tout_encoder);
u_debut_scan=find(d==-511);

for n=1:length(u_debut_scan)-1
    number_of_packets= u_debut_scan(n+1)-(u_debut_scan(n)+1)+1;
    [encoder, timehi, timelow, eofstat, end_position,nav_data,Vraw]=read_raw_iwrap(filename, number_of_packets, start_position);
    start_position=end_position;
    
    section=0:6:max_encoder;
    [encoder,full_encoder,az]=az_calcul(encoder, 315, section);
    
    % work on the encoder to get ~156 az of 2.3deg worth of data
    az_a=90-az;
    u_neg=find(az_a<0);
    az_a(u_neg)=az_a(u_neg)+360;
    
    
    
    for azi=1:length(section)-1
        nav_az=find(encoder >= section(azi) & encoder < section(azi+1));
        pitch(azi)=mean(nav_data(10,nav_az));
        roll(azi)=mean(nav_data(11,nav_az));
        track1(azi)=mean(nav_data(12,nav_az));
        drift(azi)=mean(nav_data(8,nav_az));
        VGx(azi)=mean(nav_data(6,nav_az));
        VGy(azi)=mean(nav_data(5,nav_az));
        VGz(azi)=mean(nav_data(7,nav_az));
        altit(azi)=mean(nav_data(15,nav_az));
         VG(azi)=(VGx(azi).^2 + VGy(azi).^2).^0.5;
    end
    
    
%         [inc_iwrap_t, az_N]=aircraft2track( inc_iwrap, az_a, pitch, roll, drift, track1);
%         inc_t=180-inc_iwrap_t;
%         Ground_speed=-VG.*cos(deg2rad(az_N-track1)).*sin(deg2rad(inc_t))-VGz.*cos(deg2rad(inc_t));        
%         w=hann(M)';
%         
%         for azi=1:length(section)-1
%             u_az=find(full_encoder >= section(azi) & full_encoder < section(azi+1))
%             V_temp=Vraw(:,u_az);
%             for i=1:250
%                 for j=1:length(u_az)
%                     window test=0;
%                     ind_w=1;
%                     P_temp2=[];
%                     Vr_temp2=[];
%                     dyn_noise=0;
%                     CSR=[];
%                     R1=[];
%                     
%                     
%                     
%                     
%                     
%                     while window_test==0
%                         P_temp=0;
%                         Vr_temp=0;
%                         spect_width_seed=0.7;
%                         xwin=w.*V_temp;
%                         F=fft(xwin,M);
%                         
%                         
%                         S_temp=(abs(F).^2)*T/M;
%                         S=[S_temp((M/2)+1:M) S_temp(1:M/2)];
%                         f=-PRF/2:PRF/M:PRF/2;
%                         f=f(1:M);
%                         v=-f*(lambda/2);
%                         bin=S;
%                         %---------Noise removal-------------
%                         if dyn_noise == 1
%                             [B, ordre]=sort(S);
%                             f_asc=f(ordre);
%                             
%                             
%                             S_input=bin_unoised;
%                             %------Clutter Filtering
%                             dP = 1;
%                             dV=Vny*0.8;
%                             F_filt=F;
%                             V_filt=V_temp;
%                             while dP > 0.2 | dV/Vny > 0.5
%                                 
%                                 [S_filt]=clutter_filt(S_input,f,spect_width_seed,noise_level);
%                                 
%                                 F_filt=sqrt(F_filt)*M/T;
%                                 V_filt=ifft(F_filt,256);
%                                 
%                                 Pf=sum(V_filt.*conj(V_filt))/M;
%                                 R11=(1/(M-1))*sum(V_filt(2:M).*conj(V_filt(1:M-1)));
%                                 I=real(R11);
%                                 Q=imag(R11);
%                                 vr_test=(lambda/(4*pi*T))*atan2((-Q),I);
%                                 dP=abs(10*log10(Pf)-10*log10(Ptemp));
%                                 dV=abs(vr_test-Vr_temp);
%                                 if dP > 0.2 | dV/Vny >0.5
%                                     P_temp=Pf;
%                                     Vr_temp=vr_test;
%                                     S_input=S_filt;
%                                     spect_width_seed=spect_width_seed - 0.1;
%                                 end
%                                 
%                                 
%                             end
%                             
%                             P_temp2(ind_w)=Pf;
%                             Vr_temp2(ind_w)=vr_test;
%                             Pu=sum(bin_unoised*(f(2)-f(1)));
%                             CSR(ind_w)=(10*log10(Pu)-10*log10(Pf))/10*log10(Pf);
%                             R1(ind_w)=R11;
%                             
%                             %-------Window test
%                             if CSR(ind_w) > 40 & ind_w==1
%                                 w=blackman(M)';
%                                 window_test=0;
%                                 ind_w=ind_w+1;
%                                 dyn_noise=1;
%                                 
%                                 
%                             elseif CSR(ind_w) > 20 & CSR(ind_w) <= 40 & ind_w==1
%                                 w=blackman(M)';
%                                 window_test=0;
%                                 ind_w=ind_w+1;
%                                 
%                             elseif CSR(ind_w) > 25 & ind_w==2
%                                 Ptot(i,j)=P_temp2(ind_w);
%                                 Vr(i,j)=Vr_temp2(ind_w);
%                                 spect_width(i,j)=(lambda/(2*sqrt(2)*pi*T)*sqrt(log(abs((Ptot(i,j)-P_noise)/abs(R1(ind_w)))));
%                                 window_test=1;
%                                 
%                             elseif CSR(ind_w) > 2.5 & CSR(ind_w) <=20 & ind_w==1
%                                 window_test=1;
%                                 Ptot(i,j)=P_temp2(ind_w);
%                                 Vr(i,j)=Vr_temp2(ind_w);
%                                 spect_width(i,j)=(lambda/(2*sqrt(2)*pi*T)*sqrt(log(abs((Ptot(i,j)-P_noise)/abs(R1(ind_w)))));
%                                 
%                                 
%                             elseif CSR(ind_w) <=2.5  & ind_w==1
%                                 w=rectwin(M);
%                                 window_test=0;
%                                 ind_w=ind_w+1;
%                                 
%                             elseif CSR(ind_w) <=1 & ind_w==2
%                                 Ptot(i,j)=P_temp2(ind_w);
%                                 Vr(i,j)=Vr_temp2(ind_w);
%                                 spect_width(i,j)=(lambda/(2*sqrt(2)*pi*T)*sqrt(log(abs((Ptot(i,j)-P_noise)/abs(R1(ind_w)))));
%                                 window_test=1;
%                                 
%                             elseif CSR(ind_w) > 1 & CSR(ind_w) <=25 & ind_w==2
%                                 
%                                 Ptot(i,j)=P_temp2(ind_w-1);
%                                 Vr(i,j)=Vr_temp2(ind_w-1);
%                                 spect_width(i,j)=(lambda/(2*sqrt(2)*pi*T)*sqrt(log(abs((Ptot(i,j)-P_noise)/abs(R1(ind_w-1)))));
%                                 window_test=1;
%                                 
%                             elseif ind_w==2 & dyn_noise==1
%                                 Ptot(i,j)=P_temp2(ind_w);
%                                 Vr(i,j)=Vr_temp2(ind_w);
%                                 spect_width(i,j)=(lambda/(2*sqrt(2)*pi*T)*sqrt(log(abs((Ptot(i,j)-P_noise)/abs(R1(ind_w)))));
%                                 window_test=1;
%                             end
%                             
%                             
%                             
%                             
%                         end
%                     end
%                     
%                     
%                     
%                     
%                     
%                     
%                     
%                     
%                     scan(n).Ptot=Ptot;
%                     scan(n).Vr=Vr;
%                     scan(n).spect_width=spect_width;
%                     
%                     
%                     
%                     [Time]=irig2unix(year, timelo, timehi);
%                     
%                     
%                     
%                     
%                     if n==number_scan_in_file & eofstat~=1
%                         fclose(fid);
%                     end
%                 end
            end
