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
    ipath = '/psd2data/obs/realtime/';
    opath = '/psd3data/bao/wfip/psd/';
    %opath = '/home/dwelsh/src/Matlab/';
end

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));

wtype = 'WwWind';

sinst='CsiDatalogger';
stype='SurfaceMet';

for is = 1:8
    switch is
        case 1
            %site = 'anw';
            continue;
        case 2
            site = 'dst';
            %continue;
        case 3
            site = 'lds';
            %continue;
        case 4
            site = 'mbg';
            %continue;
        case 5
            site = 'jyg';
            %continue;
        case 6
            site = 'wfc';
            %continue;
        case 7
            %site = 'bfl';
            continue;
        case 8
            site = 'vlc';
            %continue;
    end
    
    if is < 7
        winst = 'Radar915';
    else
        winst = 'Radar449';
    end
    
    
    pathw=sprintf('%s/%s/%s/%s/',ipath,winst,wtype,site);
    fniw = sprintf('%s%s%2d%03d.%02dw',pathw,site,dv(1)-2000,yd,dv(4) );
    fidiw = fopen( fniw, 'r' );
    if( fidiw > 0 )
        
        fnow = sprintf('%s%s%2d%03d.%02dw',opath,site,dv(1)-2000,yd,dv(4) );
        fidow = fopen( fnow, 'w');
        
        paths=sprintf('%s/%s/%s/%s/',ipath,sinst,stype,site);
        fnis = sprintf('%s%s%2d%03d.%02dm',paths,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);
                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
                
                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);
                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
                
                line12_mode2 = fgetl(fidiw);
                break;
                %         if isempty(line12_mode2) break; end
                
            end
            
            Line_surface = 0;
            
            k = 0;
            
            while ~feof(fidis)
                
                k = k+1;
                temp=fgetl(fidis);
                [Line_surface,count] = sscanf(temp,'%i,%i,%i,%i,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f',[1,16]);
                
                if(isempty(Line_surface))
                    break;
                end
                
                if is == 5 || is == 6
                    Speed_surface(k) = Line_surface(12);
                else
                    Speed_surface(k) = Line_surface(9);
                end
                
            end
            
            Speed_surface_average = mean(Speed_surface);
            if Speed_surface_average < 8.0
                Speed_surface_check = Speed_surface_average * coefficient;
            else
                Speed_surface_check = Speed_surface_average;
            end
            clear Speed_surface Line_surface
            
            %delete strcat(files_directory_cns_new,f2open_cns);
            
            fnow = sprintf('%s%s%2d%03d.%02dw',opath,site,dv(1)-2000,yd,dv(4) );
            fidow = fopen( fnow, 'w');
            
            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_mode_str1,1)
                
                D_mode1 = str2num(D_mode_str1(i,:));
                
                if ( D_mode1(4) > 2)
                    fprintf(fidow,'%s\r\n',D_mode_str1(i,:));
                    modified_mode1(i) = 1;
                    if (i > 1 && modified_mode1(i-1) == 0)
                        modified_mode1(i) = 0;
                    end
                    continue;
                end
                
                if (Speed_surface_average <= Speed_surface_minimum || (D_mode1(2) >= Speed_surface_check) || (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) < Speed_surface_check && ((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
            
            clear D_mode_str1 modified_mode1
            
            if(~isempty(D_mode_str2))
                
                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_mode_str2,1)
                    
                    D_mode2 = str2num(D_mode_str2(j,:));
                    
                    if (D_mode2(4) > 2 )
                        fprintf(fidow,'%s\r\n',D_mode_str2(j,:));
                        modified_mode2(j) = 1;
                        if (j > 1 && modified_mode2(j-1) == 0)
                            modified_mode2(j) = 0;
                        end
                         continue;
                    end
                    
                    if (Speed_surface_average <= Speed_surface_minimum || (D_mode2(2) >= Speed_surface_check) || (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) < Speed_surface_check && ((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);
                clear D_mode_str2 modified_mode2
                
            end
            
        else
            
            copyfile(fniw,fnow)
            
        end
        
        fclose('all');
        
    end % if have wind file
end % loop through all sites

exit

