%
mpath='T:/RICO_2005/SEWARD_JOHNSON/flux/Raw/combined/';
rpath='T:/RICO_2005/SEWARD_JOHNSON/radar/profiler/Raw/MomHourly/';
%mpath='c:/src/CorrectedWinds/data/';
%rpath='c:/src/CorrectedWinds/data/';

kn2ms = 1.852 * 1000. / 3600.;
deg2rad = pi/180.;
azicor=0.;

dm = zeros(5,3610);
dr = zeros(100,6,100);
rn = zeros(100,1);
re = zeros(100,1);
dp = zeros(17,100);
psec = zeros(100,1);

%yr=4;
yr=5;
%for yd=364:366
%yr=10;
for yd=1:23
    for hr=0:23
        
        fnm=sprintf('%shed0%02d%03d%02d_raw.txt',mpath,yr,yd,hr);
        %fnr=sprintf('%satl%02d%03d%02d.raw',rpath,yr,yd,hr);
        fnr=sprintf('%sric%02d%03d%02d.raw',rpath,yr,yd,hr);
        %fnc=sprintf('%satl%02d%03d%02d.rew',rpath,yr,yd,hr);
        fnc=sprintf('%sric%02d%03d%02d.rew',rpath,yr,yd,hr);
        
        fidm = fopen(fnm,'r');
        if( fidm < 0 )
            fprintf(1,'No %s\n',fnm);
            continue;
        end
        fidr = fopen(fnr,'r');
        if( fidr < 0 )
            fprintf(1,'No %s\n',fnr);
            continue;
        end
        fidc = fopen(fnc,'w');
        if( fidc < 0 )
            fprintf(1,'Cannot open %s\n',fnc);
            continue;
        end
        
        fprintf(1,'Processing yd:%03d hr:%02d\n',yd,hr);
        
        %01/17/2005 12:00 Generic with ASCII Output (SN: 1234)
        %0001702 $PSAT,HPR,120001.00,74.30,2.09,,N*09
        %0001732 $GPVTG,0.00,T,2.0,M,0.00,N,0.00,K,D*23
        
        linem=fgetl(fidm);
        nm = 0;
        while ~feof(fidm)
            line=fgetl(fidm);
            data = sscanf(line,'%d $PSAT,HPR,%2d%2d%f,%f');
            nm = nm + 1;
            dm(1,nm) = data(2)*3600. + data(3)*60. + data(4);
            dm(2,nm) = data(5);
            line=fgetl(fidm);
            data = sscanf(line,'%d $GPVTG,%f,T,%f,M,%f');
            dm(3,nm) = data(2);
            dm(4,nm) = data(3);
            dm(5,nm) = data(4) * kn2ms;
        end
        fclose(fidm);
        
        liner=fgetl(fidr);
        np = 0;
        while ~feof(fidr)
            line=fgetl(fidr);
            data = sscanf(line,'%d%f%d%d%d%d%d%d');
            np = np + 1;
            dp(1:8,np) = data(1:8);
            psec(np) = data(6)*3600. + data(7)*60. + data(8);
            line=fgetl(fidr);
            data = sscanf(line,'%d%d%d%d%d%d%d%d%d');
            dp(9:17,np) = data(1:9);
            for ih=1:data(5)
                line=fgetl(fidr);
                data = sscanf(line,'%f%f%f%f%f%f');
                dr(ih,1:6,np) = data(1:6);
            end
        end
        fclose(fidr);
        
        fprintf(fidc,'%s\n',liner);
        
        ip=1;
        while ip+2 <= np
            %if dp(1,ip)==5 && dp(1,ip+1)==0 && dp(1,ip+2)==1
            if dp(1,ip)==2 && dp(1,ip+1)==1 && dp(1,ip+2)==0
                ibma = ip + 2;
                ibmp = ip + 1;
                ibmv = ip;
                %elseif dp(1,ip)==4 && dp(1,ip+1)==2 && dp(1,ip+2)==3
            elseif dp(1,ip)==3 && dp(1,ip+1)==4 && dp(1,ip+2)==5
                ibma = ip;
                ibmp = ip + 1;
                ibmv = ip + 2;
            else
                ip = ip + 1;
                continue
            end
            if ibma>np || ibmp>np || ibmv > np
                break;
            end
            
            seca = dp(6,ibma)*3600 + dp(7,ibma)*60 + dp(8,ibma);
            secp = dp(6,ibmp)*3600 + dp(7,ibmp)*60 + dp(8,ibmp);
            secv = dp(6,ibmv)*3600 + dp(7,ibmv)*60 + dp(8,ibmv);
            indxa=find( dm(1,:) >= seca & dm(1,:) < seca+10);
            indxp=find( dm(1,:) >= secp & dm(1,:) < secp+10);
            indxv=find( dm(1,:) >= secv & dm(1,:) < secv+10);
            lena=length(indxa);
            lenp=length(indxp);
            lenv=length(indxv);
            if lena==0 || lenp==0 || lenv==0
                ip = ip + 1;
                continue;
            end
            
            cn1 = dm(4,indxa(1)).*cos(dm(3,indxa(1))*deg2rad);
            cn2 = dm(4,indxp(1)).*cos(dm(3,indxp(1))*deg2rad);
            cn3 = dm(4,indxv(1)).*cos(dm(3,indxv(1))*deg2rad);
            ce1 = dm(4,indxa(1)).*sin(dm(3,indxa(1))*deg2rad);
            ce2 = dm(4,indxp(1)).*sin(dm(3,indxp(1))*deg2rad);
            ce3 = dm(4,indxv(1)).*sin(dm(3,indxv(1))*deg2rad);
            acn = (cn1+cn2+cn3)/3.0;
            ace = (ce1+ce2+ce3)/3.0;
            hn1 = cos(dm(2,indxa(1))*deg2rad);
            hn2 = cos(dm(2,indxp(1))*deg2rad);
            hn3 = cos(dm(2,indxv(1))*deg2rad);
            he1 = sin(dm(2,indxa(1))*deg2rad);
            he2 = sin(dm(2,indxp(1))*deg2rad);
            he3 = sin(dm(2,indxv(1))*deg2rad);
            ahn = (hn1+hn2+hn3)/3.0;
            ahe = (he1+he2+he3)/3.0;
            %acn = ahn;
            %ace = ahe;
            s=sqrt(acn*acn+ace*ace);
            c=atan2(ace,acn);
            h=atan2(ahe,ahn);
            tha=deg2rad*(dp(3,ibma)+azicor);
            thp=deg2rad*(dp(3,ibmp)+azicor);
            z=dp(2,ibmp)*deg2rad;
            rn(1:dp(13,ibma))=dr(1:dp(13,ibma),1,ibma).*cos(tha+h)-...
                dr(1:dp(13,ibmp),1,ibmp).*sin(tha+h)-...
                s*cos(c)*cos(z)-...
                (dr(1:dp(13,ibmv),1,ibmv).*sin(z).*cos(tha+h)-...
                dr(1:dp(13,ibmv),1,ibmv).*sin(z).*sin(tha+h)-...
                dr(1:dp(13,ibmv),1,ibmv).*sin(z));
            re(1:dp(13,ibmp))=dr(1:dp(13,ibma),1,ibma).*sin(tha+h)+...
                dr(1:dp(13,ibmp),1,ibmp).*cos(tha+h)-...
                s*sin(c).*cos(z)-...
                (dr(1:dp(13,ibmv),1,ibmv).*sin(z).*sin(tha+h)+...
                dr(1:dp(13,ibmv),1,ibmv).*sin(z).*cos(tha+h)-...
                dr(1:dp(13,ibmv),1,ibmv).*sin(z));
            
            fprintf(fidc,'%2d%3d%4d%3d%4d%3d%3d%3d\n',...
                dp(1,ibma),dp(2,ibma),0,dp(4,ibma),...
                dp(5,ibma),dp(6,ibma),dp(7,ibma),dp(8,ibma));
            fprintf(fidc,'%3d%5d%4d%4d%4d%4d%3d%5d%3d\n',...
                dp(9,ibma),dp(10,ibma),dp(11,ibma),dp(12,ibma),...
                dp(13,ibma),dp(14,ibma),dp(15,ibma),dp(16,ibma),dp(17,ibma));
            for ih=1:dp(13,ibma)
                fprintf(fidc,'%8.3f%8.3f%8.3f%8.3f%8.3f%2d\n',...
                    rn(ih),dr(ih,2,ibma),dr(ih,3,ibma),...
                    dr(ih,4,ibma),dr(ih,5,ibma),fix(dr(ih,6,ibma)));
            end
            fprintf(fidc,'%2d%3d%4d%3d%4d%3d%3d%3d\n',...
                dp(1,ibmp),dp(2,ibmp),90,dp(4,ibmp),...
                dp(5,ibmp),dp(6,ibmp),dp(7,ibmp),dp(8,ibmp));
            fprintf(fidc,'%3d%5d%4d%4d%4d%4d%3d%5d%3d\n',...
                dp(9,ibmp),dp(10,ibmp),dp(11,ibmp),dp(12,ibmp),...
                dp(13,ibmp),dp(14,ibmp),dp(15,ibmp),dp(16,ibmp),dp(17,ibmp));
            for ih=1:dp(13,ibmp)
                fprintf(fidc,'%8.3f%8.3f%8.3f%8.3f%8.3f%2d\n',...
                    re(ih),dr(ih,2,ibmp),dr(ih,3,ibmp),...
                    dr(ih,4,ibmp),dr(ih,5,ibmp),fix(dr(ih,6,ibmp)));
            end
            
            fprintf(fidc,'%2d%3d%4d%3d%4d%3d%3d%3d\n',...
                dp(1,ibmv),dp(2,ibmv),dp(3,ibmv),dp(4,ibmv),...
                dp(5,ibmv),dp(6,ibmv),dp(7,ibmv),dp(8,ibmv));
            fprintf(fidc,'%3d%5d%4d%4d%4d%4d%3d%5d%3d\n',...
                dp(9,ibmv),dp(10,ibmv),dp(11,ibmv),dp(12,ibmv),...
                dp(13,ibmv),dp(14,ibmv),dp(15,ibmv),dp(16,ibmv),dp(17,ibmv));
            for ih=1:dp(13,ibmv)
                fprintf(fidc,'%8.3f%8.3f%8.3f%8.3f%8.3f%2d\n',...
                    dr(ih,1,ibmv),dr(ih,2,ibmv),dr(ih,3,ibmv),...
                    dr(ih,4,ibmv),dr(ih,5,ibmv),dr(ih,6,ibmv));
            end
            
            ip=ip+3;
            
        end
        
    end
    
    fclose(fidc);
    
end
