clear all
fclose('all');

Speed_surface_minimum = 2;
coefficient = 1.3;

if (ispc)
    files_directory_cns = 'C:\Data\2011\DOE\DataSet\CasiStudio\vlc\consensusWW\';
    files_directory_cns_new = 'C:\Data\2011\DOE\DataSet\CasiStudio\vlc\consensusWW_Clutter_removed\';
    files_directory_surface = 'C:\Data\2011\DOE\DataSet\CasiStudio\vlc\surface\';
else
    ipathw = '/psd2data/obs/realtime/Radar449/WwWind/vlc/';
    ipaths = '/psd2data/obs/realtime/CsiDatalogger/SurfaceMet/vlc/';
    opathw = '/psd3data/bao/wfip/psd/';
end

site='vlc';

dd = clock;
dn=datenum(dd(1),dd(2),dd(3),dd(4)-1,dd(5),dd(6));
dv=datevec(dn);
yd = md2yd(dv(1), dv(2), dv(3));

fniw = sprintf('%s%s%2d%03d.%02dw',ipathw,site,dv(1)-2000,yd,dv(4) );
fidiw = fopen( fniw, 'r' );
if( fidiw > 0 )
    
    fnow = sprintf('%s%s%2d%03d.%02dw',opathw,site,dv(1)-2000,yd,dv(4) );
    fidow = fopen( fnow, 'w');

        fnis = sprintf('%s%s%2d%03d.%02dm',ipaths,site,dv(1)-2000,yd,dv(4) );
    fidis = fopen( fnis, 'r' );
    if( fidis > 0 )
        
        while ~feof(fidiw)
            
            line1_mode1 = fgetl(fidiw);
            line2_mode1 = fgetl(fidiw);
            line3_mode1 = fgetl(fidiw);
            line4_mode1 = fgetl(fidiw);
            line5_mode1 = fgetl(fidiw);
            Line_time_mode1 = str2num(line5_mode1);
            hour_cns = Line_time_mode1(4);
            line6_mode1 = fgetl(fidiw);
            line7_mode1 = fgetl(fidiw);
            line8_mode1 = fgetl(fidiw);
            line9_mode1 = fgetl(fidiw);
            Line_pwidth_mode1 = str2num(line9_mode1);
            line10_mode1 = fgetl(fidiw);
            line11_mode1 = fgetl(fidiw);
            
            for i = 1:Line_pwidth_mode1(end-2)
                
                D_mode_str1(i,:) = fgetl(fidiw);
                D_mode1(i,:) = str2num(D_mode_str1(i,:));
                
            end
            
            Height_mode1 = D_mode1(:,1)';
            Speed_mode1 = D_mode1(:,2)';
            Direction_mode1 = D_mode1(:,3)';
            MET_QC_mode1 = D_mode1(:,4)';
            
            line1_mode2 = fgetl(fidiw);
            line2_mode2 = fgetl(fidiw);
            if isempty(line2_mode2) break; end
            line3_mode2 = fgetl(fidiw);
            line4_mode2 = fgetl(fidiw);
            line5_mode2 = fgetl(fidiw);
            Line_time_mode2 = str2num(line5_mode2);
            hour_cns = Line_time_mode2(4);
            line6_mode2 = fgetl(fidiw);
            line7_mode2 = fgetl(fidiw);
            line8_mode2 = fgetl(fidiw);
            line9_mode2 = fgetl(fidiw);
            Line_pwidth_mode2 = str2num(line9_mode2);
            line10_mode2 = fgetl(fidiw);
            line11_mode2 = fgetl(fidiw);
            
            for i = 1:Line_pwidth_mode2(end-2)
                
                D_mode_str2(i,:) = fgetl(fidiw);
                D_mode2(i,:) = str2num(D_mode_str2(i,:));
                
            end
            
            Height_mode2 = D_mode2(:,1)';
            Speed_mode2 = D_mode2(:,2)';
            Direction_mode2 = D_mode2(:,3)';
            MET_QC_mode2 = D_mode2(:,4)';
            
            line12_mode2 = fgetl(fidiw);
            break;
            %         if isempty(line12_mode2) break; end
            
        end
        
        Line_surface = 0;
        
        k = 0;
        
        while ~feof(fidis)
            
            k = k+1;
            [Line_surface,count] = fscanf(fidis,'%i,%i,%i,%i,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f',[1,16]);
            
            if(isempty(Line_surface)) break; end
            
            Speed_surface(k) = Line_surface(9);
            
        end
        
        Speed_surface_average = mean(Speed_surface);
        
        %delete strcat(files_directory_cns_new,f2open_cns);
                
        fprintf(fidow,'%s\r\n',line1_mode1);
        fprintf(fidow,'%s\r\n',line2_mode1);
        fprintf(fidow,'%s\r\n',line3_mode1);
        fprintf(fidow,'%s\r\n',line4_mode1);
        fprintf(fidow,'%s\r\n',line5_mode1);
        fprintf(fidow,'%s\r\n',line6_mode1);
        fprintf(fidow,'%s\r\n',line7_mode1);
        fprintf(fidow,'%s\r\n',line8_mode1);
        fprintf(fidow,'%s\r\n',line9_mode1);
        fprintf(fidow,'%s\r\n',line10_mode1);
        fprintf(fidow,'%s\r\n',line11_mode1);
        
        for i = 1:size(D_mode1,1)
            
            D_mode1 = str2num(D_mode_str1(i,:));
            
            if (Speed_surface_average <= Speed_surface_minimum || (D_mode1(2) >= coefficient*Speed_surface_average) || (i > 1 && modified_mode1(i-1) == 0))
                
                fprintf(fidow,'%s\r\n',D_mode_str1(i,:));
                modified_mode1(i) = 0;
                
            elseif (Speed_surface_average > Speed_surface_minimum && D_mode1(2) < coefficient*Speed_surface_average && ((i > 1 && modified_mode1(i-1) ~= 0) || i == 1))
                
                D_mode_str1_modified = D_mode_str1(i,:);
                D_mode_str1_modified(32:33) = num2str(10);
                
                fprintf(fidow,'%s\r\n',D_mode_str1_modified);
                modified_mode1(i) = 1;
                
            end
            
        end
        
        if(~isempty(D_mode2))
            
            fprintf(fidow,'%s\r\n',line1_mode2);
            fprintf(fidow,'%s\r\n',line2_mode2);
            fprintf(fidow,'%s\r\n',line3_mode2);
            fprintf(fidow,'%s\r\n',line4_mode2);
            fprintf(fidow,'%s\r\n',line5_mode2);
            fprintf(fidow,'%s\r\n',line6_mode2);
            fprintf(fidow,'%s\r\n',line7_mode2);
            fprintf(fidow,'%s\r\n',line8_mode2);
            fprintf(fidow,'%s\r\n',line9_mode2);
            fprintf(fidow,'%s\r\n',line10_mode2);
            fprintf(fidow,'%s\r\n',line11_mode2);
            
            for j = 1:size(D_mode2,1)
                
                D_mode2 = str2num(D_mode_str2(j,:));
                
                if (Speed_surface_average <= Speed_surface_minimum || (D_mode2(2) >= coefficient*Speed_surface_average) || (j > 1 && modified_mode2(j-1) == 0))
                    
                    fprintf(fidow,'%s\r\n',D_mode_str2(j,:));
                    modified_mode2(j) = 0;
                    
                elseif (Speed_surface_average > Speed_surface_minimum && D_mode2(2) < coefficient*Speed_surface_average && ((j > 1 && modified_mode2(j-1) ~= 0) || j == 1))
                    
                    D_mode_str2_modified = D_mode_str2(j,:);
                    D_mode_str2_modified(32:33) = num2str(10);
                    
                    fprintf(fidow,'%s\r\n',D_mode_str2_modified);
                    modified_mode2(j) = 1;
                    
                end
                
            end
            
            fprintf(fidow,'%s\r\n',line12_mode2);
            
        end
        
    else
        
        copyfile(fniw,fnow)
        
    end
    
    
    fclose('all');
    
end % if have wind file

exit

