%function [sweep_data]=corrected_vr
files=dir(['C:\Documents and Settings\lorsolo\My Documents\research\rita\data\iwrap\*CH1*1806*.nc']);
PRF=20000;
for fil=1%:length(files)
    sweeps=files(fil).name
    ncload(['C:\Documents and Settings\lorsolo\My Documents\research\rita\data\iwrap\',sweeps]);
    [s,a,r]=size(di);
    T=1/PRF;
    bin_rez=30;
    channel=sweeps(4:6);
    
    if channel=='CH1'
        f=12.87e9;
        inc_iwrap=29.49;
    elseif channel=='CH2'
        f=13.25e9;
        inc_iwrap=39.29;
    end
    
    c=3e8;
    rge=15:30:199*30+15;
    Vny=PRF*c/(f*4);
    lambda=c/(f);

    
    for swp=29%1:s
        u_bad_data=find(time(swp,:)==-999);
        
        power(swp,u_bad_data,:)=NaN;
        
        I=squeeze(di(swp,:,:))';
        I(:,u_bad_data)=NaN;
        
        Q=squeeze(dq(swp,:,:))';
        Q(:,u_bad_data)=NaN;
        
        V_measured=(lambda/(4*pi*T))*atan2((-Q),I);
        
        
        encoder(swp,u_bad_data)=NaN;
        az_a=90-encoder(swp,:);
        u_neg=find(az_a<0);
        az_a(u_neg)=az_a(u_neg)+360;
        
        navigationalDataset(swp,u_bad_data,:)=NaN;
        
        pitch=navigationalDataset(swp,:,10);
        roll=navigationalDataset(swp,:,11);
        track1=navigationalDataset(swp,:,12);
        drift=navigationalDataset(swp,:,8);
        VGx=navigationalDataset(swp,:,6);
        VGy=navigationalDataset(swp,:,5);
        VGz=navigationalDataset(swp,:,7);
        VG=(VGx.^2 + VGy.^2).^0.5;
        
        
        [inc_iwrap_t, az_N]=aircraft2track( inc_iwrap, az_a, pitch, roll, drift, track1);
        sweep_data(swp).az_N=az_N;
        sweep_data(swp).inc_iwrap_t=inc_iwrap_t;
        
        inc_t=180-inc_iwrap_t;
        inc_t=deg2rad(inc_t);
        for rg=1:length(rge)
            temp(rg,:)=V_measured(rg,:)+VG.*cos(deg2rad(az_N-track1)).*sin(inc_t)+VGz.*cos(inc_t);
        end
        
        sweep_data(swp).V_corrected=temp;
        vm(swp,:,:)=V_measured;
%         plot(az_N, V_measured(70,:))
%         hold on
        
%         u_good_data=find(time(swp,:)~=-999);
%         if length(u_good_data)==0
%             disp(['file ' sweeps ' has no data for sweep ' num2str(swp)])
%             sweep_data(swp).range=[NaN;NaN];       
%         else
%            
%             p=10*log10(squeeze(power(swp,u_good_data(1),:)));
%             
%             radar_altitude=navigationalDataset(swp,u_good_data(1),15);
%             limit_range=find(p>=55)
%             dif=diff(limit_range);
%             u_dif=find(dif>20)
%             if length(u_dif)==0
%                 disp(['unable to find surface in sweep ' num2str(swp)])
%                 plot(p)
%                 sweep_data(swp).range=[NaN;NaN]; 
%             else
%                 sfc_range=limit_range(u_dif(1)+1);
%                 bins_in_ray=fix((radar_altitude/cos(deg2rad(inc_iwrap_t(u_good_data(1)))))/bin_rez);
%                 first_bin=sfc_range-bins_in_ray+1;
%             end
%             sweep_data(swp).range=[first_bin;sfc_range];
%         end
        time(swp,u_bad_data)=NaN;
        sweep_data(swp).time=time(swp,:);
    end
    
end
