%
mpath='T:/CALNEX_2010/Atlantis/radar/profiler/Raw/ShipHeading/';
%rpath='T:/NAME_2004/ALTAIR/radar/profiler/Raw/Motion/';
%mpath='c:/src/CorrectedWinds/data/';
%rpath='c:/src/CorrectedWinds/data/';

kn2ms = 1.852 * 1000. / 3600.;

yr=10;
sd=[2010 5 12 0 0 0];
%sd=[2010 6 7 0 0 0];
ed=[2010 6 7 0 0 0];
bdn = datenum(sd);
edn = datenum(ed);
%yr=5;
%for yd=364:366
%yr=10;
for dn=bdn:edn
    dm = zeros(5,86500) + NaN;
    dv = datevec( dn );
    yd = md2yd(dv(1), dv(2), dv(3) );
    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);
        
        fidm = fopen(fnm,'r');
        if( fidm < 0 )
            fprintf(1,'No %s\n',fnm);
            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);
        while ~feof(fidm)
            line=fgetl(fidm);
            data = sscanf(line,'%d $PSAT,HPR,%2d%2d%f,%f');
            if data(2) ~= hr
                line=fgetl(fidm);
                continue
            end
            %if hr == 0 && data(2) ~= hr
            %    line=fgetl(fidm);
            %    continue
            %end
            %if hr == 23 && data(2) ~= hr
            %    line=fgetl(fidm);
            %    continue
            %end

            nm = data(2)*3600. + data(3)*60. + data(4) + 1;
            dm(1,nm) = datenum(dv(1),dv(2),dv(3),data(2),data(3),fix(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);
    end
    
    subplot(2,1,1),plot(dm(1,1:100:nm),dm(5,1:100:nm),'r');
    xlim([fix(dm(1,nm)) fix(dm(1,nm))+1]);
    ylim([0,max(dm(5,1:nm))+2])
    stitle=sprintf('CALNEX 20%02d YearDay %03d', yr, yd);
    title(stitle);
    ylabel('Spd (m/s)')
    xlabel('Hour (UTC)')
    datetick('x','HH','keeplimits');
    
    subplot(2,1,2),plot(dm(1,1:100:nm),dm(2,1:100:nm),'rx','MarkerSize',4);
    hold
    subplot(2,1,2),plot(dm(1,1:100:nm),dm(3,1:100:nm),'kx','MarkerSize',4');
    %legend('Heading','Course',...
    %       'Orientation','Horizontal','Location','Best');
    xlim([fix(dm(1,nm)) fix(dm(1,nm))+1]);
    ylim([0 360]);
    ylabel('Direction (deg)');
    xlabel('Hour (UTC)')
    datetick('x','HH','keeplimits');
    
    print_buffer=sprintf('%shead_20%02d%03d.png',mpath,yr,yd);
    print('-dpng ', print_buffer);
    close all
    clear dm nm
    
end
