%
path='T:\NAME_2004\ALTAIR\radar\profiler\Raw\Motion/';
yr=4;
%yr=5;
%yd=17;

d3 = zeros(3610,3);
d7 = zeros(3610,9);

%for yd=364:366
for yd=183:203
    for hr=0:23
        
        fn3=sprintf('%s%02d%03d%02d.P02',path,yr,yd,hr);
        fn7=sprintf('%s%02d%03d%02d.P01',path,yr,yd,hr);
        
        fid3 = fopen(fn3,'r');
        if( fid3 < 0 )
            fprintf(1,'No %s\n',fn3);
            continue;
        end
        fid7 = fopen(fn7,'r');
        if( fid7 < 0 )
            fprintf(1,'No %s\n',fn7);
            continue;
        end
        fprintf(1,'Processing yd:%03d hr:%02d\n',yd,hr);
        
        %12750393600046,$HEHDT,080.4,T*23
        n3 = 0;
        line=fgetl(fid3);
        while ~feof(fid3)
            line=fgetl(fid3);
            %if length(line) ~= 32
            %    continue
            %end
            data = sscanf(line,'%4d%3d $HCHDM,%f,*%4s');
            n3 = n3 + 1;
            d3(n3,1:3) = data(1:3);
        end
        fclose(fid3);
        
        %12750454801062,$GPRMC,170001,A,1757.8130,N,06135.3240,W,002.9,080.7,170105,014.6,W*7E
        n7 = 0;
        while ~feof(fid7)
            line=fgetl(fid7);
            if length(line) < 60
                continue
            end
            data = sscanf(line,...
                '%4d%3d $GPRMC,%2d%2d%2d,A,%f,N,%f,W,%f,%f,*%d,*%f,*%4s');
            n7 = n7 + 1;
            d7(n7,1:9) = data(1:9);
        end
        fclose(fid7);
        
        fno=sprintf('%shed0%02d%03d%02d_raw.txt',path,yr,yd,hr);
        fido = fopen(fno,'w');
        fprintf(fido,...
            '01/17/2005 %02d:00 Generic with ASCII Output (SN: 1234)\n',hr);
        
        for i=1:n7
            indx=find(d3(:,1) == d7(i,1) );
            si = size(indx,1);
            if si > 0
                hd = d3(indx(si),3);
                spn = d7(i,8);
                spk = spn * 1.852;
                hour = d7(i,3);
                mn = d7(i,4);
                sc = d7(i,5);
                fprintf(fido,...
                    '%02d%02d702 $PSAT,HPR,%02d%02d%02d.00,%.2f,2.09,,N*09\n',...
                    mn,sc,hour,mn,sc,hd);
                fprintf(fido,...
                    '%02d%02d732 $GPVTG,%.2f,T,2.0,M,%.2f,N,%.2f,K,D*23\n',...
                    mn,sc,d7(i,9),spn,spk);
            end
        end
        fclose(fido);
    end
    
end