disp('Profiler_loop');

%Plot profiler data
% DEW 4/16/2010
% Plot data from GMD 300m BAO data loggers
clear
close all

pltflg = 1; %flag to plot the data: 1=yes, 0=no
pngflg = 1; %flag to make a png of the plot: 1=yes, 0=no

if ispc
    inpath = 'z:\bao\Tower\Processed\daily\';
    outpath  = 'w:/data/calnex/images/';
else
    inpath = 'z:\BAO\Tower\Processed\daily\';
    outpath  = 'w:/data/calnex/images/';
end

%cd(inpath);

%if( nargin == 0 )
    bys = '2005';  %input('Input year to process (2007) ','s');
    bms = '1';  %input('Input the month to process (1-12) ','s');
    bds = input('Input the beginning day to process (1-31) ','s');
    eys = '2005';  %input('Input year to process (2007) ','s');
    ems = '1';  %input('Input the month to process (1-12) ','s');
    eds = input('Input end day to process (1-31) ','s');
    by = str2double( bys );
    bm = str2double( bms );
    bd = str2double( bds );
    ey = str2double( eys );
    em = str2double( ems );
    ed = str2double( eds );
%end

bdn = datenum([by bm bd 0 0 0]);
edn = datenum([ey em ed 0 0 0]);

nmax = 3000;

%after motion correction
xct1 = 0;
xct2 = 0;
for i = bd:ed

%     times = zeros(6,48);
%     nhts = zeros(48,1);
%     delta = zeros(48,1);
%     ht = zeros(40,48);
%     s = zeros(40,48);
%     d = zeros(40,48);
    
    yd = datenum(by,bm,i,0,0,0)-datenum(by-1,12,31);

    yds = sprintf('%03d',yd);
    fname = ['Z:\BAO\Tower\Raw\profiler\MotionComp/W' bys(3:4) yds '.cns']
    fid = fopen(fname, 'r');
    line=fgetl(fid);

    j = 0;
    while ~feof(fid)
        j = j + 1;
        line=fgetl(fid);
        line=fgetl(fid);
        line=fgetl(fid);
        line=fgetl(fid);
        time = sscanf(line,'%d %d %d %d %d %d');
        times(1:6,j) = time(1:6);
        mctim(j,1) = times(1,j);
        mctim(j,2) = times(2,j);
        mctim(j,3)=  times(3,j);        
        mctim(j,4) = times(4,j);
        mctim(j,5) = times(5,j);
        mctim(j,6)=  times(6,j);
        line=fgetl(fid);
        nht = sscanf(line,'%d %d %d');
        nhts(j) = nht(3);
        line=fgetl(fid);
        line=fgetl(fid);
        rpar = sscanf(line,'%d %d %d %d %d');
        mctim(j,7) = rpar(5);
        line=fgetl(fid);
        line=fgetl(fid);
        line=fgetl(fid);  % Data header line
        
        for i = 1:nhts(j)
            if j == 1
                length(line);
            end
            line=fgetl(fid);
                        if length(line) > 110
                %     HT      SPD      DIR    U    V   W   RAD      RAD      RAD      CNT      CNT      CNT      SNR      SNR      SNR
                data = sscanf(line,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f');
                ht(i,j) = data(1);
                s(i,j) = data(2);
                d(i,j) = data(3);
                %             U(i,j) = data(4);
                %             V(i,j) = data(5);
                %             W(i,j) = data(6);
                r1(i,j) = data(7);
                r2(i,j) = data(8);
                r3(i,j) = data(9);
                c1(i,j) = data(10);
                c2(i,j) = data(11);
                c3(i,j) = data(12);
                snr1(i,j) = data(13);
                snr2(i,j) = data(14);
                snr3(i,j) = data(15);
                        end

            if length(line) < 110
%     HT      SPD      DIR    RAD      RAD      RAD      CNT      CNT      CNT      SNR      SNR      SNR
            data = sscanf(line,'%f %f %f %f %f %f %f %f %f %f %f %f');
            ht(i,j) = data(1);
            s(i,j) = data(2);
            d(i,j) = data(3);
%             U(i,j) = data(4);
%             V(i,j) = data(5);
%             W(i,j) = data(6);            
            r1(i,j) = data(4);
            r2(i,j) = data(5);
            r3(i,j) = data(6);
            c1(i,j) = data(7);
            c2(i,j) = data(8);
            c3(i,j) = data(9);
            snr1(i,j) = data(10);
            snr2(i,j) = data(11);
            snr3(i,j) = data(12);            
            end
        end
        %   read in the last line '$'
        line=fgetl(fid);
    end
    
    times(1,1:j)=times(1,1:j)+2000;
    yd(1:j)=datenum(times(1,1:j),times(2,1:j),times(3,1:j),times(4,1:j),times(5,1:j),times(6,1:j));
    jd(1:j)=datenum(times(1,1:j),times(2,1:j),times(3,1:j),times(4,1:j),times(5,1:j),times(6,1:j))-datenum(times(1,1:j)-1,12,31);
    
    htl = ht(:,1);
    hth = ht(:,2);
    
    % new code to sort out modes
        % sort through arrays since they are not in the same order
    mc4 = find(mctim(:,7) == 400);
    mc7 = find(mctim(:,7) == 700);
            
    l1 = length(mc4);
    h1 = length(mc7);
    

    for x = 1:l1
        xct1 = xct1 + 1;
        mcsl(:,xct1) = s(:,mc4(x));
        mcdl(:,xct1) = d(:,mc4(x));
        jdl(:,xct1) = jd(mc4(x));
        mcr1l(:,xct1) = r1(:,mc4(x));
        mcr2l(:,xct1) = r2(:,mc4(x));
        mcr3l(:,xct1) = r3(:,mc4(x));
        mcc1l(:,xct1) = c1(:,mc4(x));
        mcc2l(:,xct1) = c2(:,mc4(x));
        mcc3l(:,xct1) = c3(:,mc4(x));
        mcsnr1l(:,xct1) = snr1(:,mc4(x));
        mcsnr2l(:,xct1) = snr2(:,mc4(x));
        mcsnr3l(:,xct1) = snr3(:,mc4(x));        
    end
    
    for x = 1:h1
        xct2 = xct2 + 1;
        mcsh(:,xct2) = s(:,mc7(x));
        mcdh(:,xct2) = d(:,mc7(x));
        jdh(:,xct2) = jd(mc7(x));
        mcr1h(:,xct2) = r1(:,mc7(x));
        mcr2h(:,xct2) = r2(:,mc7(x));
        mcr3h(:,xct2) = r3(:,mc7(x));
        mcc1h(:,xct2) = c1(:,mc4(x));
        mcc2h(:,xct2) = c2(:,mc4(x));
        mcc3h(:,xct2) = c3(:,mc4(x));
        mcsnr1h(:,xct2) = snr1(:,mc4(x));
        mcsnr2h(:,xct2) = snr2(:,mc4(x));
        mcsnr3h(:,xct2) = snr3(:,mc4(x));        
    end
   clear mctim
end % loop though each day

figure
dlim = [0 360];
subplot(2,1,1),imagesc(jdl,htl(1:36),mcdl(1:36,:));
colorbar, axis xy, caxis(dlim);
title('RICO Profiler low mode mc');
ylabel('Ht-agl (km)');
%xlabel('Time (UTC)');

slim = [0 15];
subplot(2,1,2),imagesc(jdl,htl(1:36),mcsl(1:36,:));
colorbar, axis xy, caxis(slim);
%title('RICO Profiler low mode');
ylabel('Ht-agl (km)');
xlabel('Year Day (UTC)');
if pngflg
   print_buffer = [outpath 'RICO_Profiler_SDl_' bys ' ' bds '-' eds '.png'];
        print('-dpng ', print_buffer);
end

figure
dlim = [0 360];
subplot(2,1,1),imagesc(jdh,hth(1:36),mcdh(1:36,:));
colorbar, axis xy, caxis(dlim);
title('RICO Profiler high mode mc');
ylabel('Ht-agl (km)');

slim = [0 15];
subplot(2,1,2),imagesc(jdh,hth(1:36),mcsh(1:36,:));
axis xy, colorbar, caxis(slim);
ylabel('Ht-agl (km)');
xlabel('Year Day (UTC)');
if pngflg
    print_buffer = [outpath 'RICO_Profiler_SDh_' bys ' ' bds '-' eds '.png'];
        print('-dpng ', print_buffer);
end

%Low mode
%Plot radials
figure
wlim = [-5 5];
subplot(3,1,1),imagesc(jdl,htl(1:36),mcr1l(1:36,:));
colorbar, axis xy, caxis(wlim);
title('RICO Profiler Radials low mode mc');
ylabel('Ht-agl (km)');

rlim = [-10 10];
subplot(3,1,2),imagesc(jdl,htl(1:36),mcr2l(1:36,:));
axis xy, colorbar, caxis(rlim);
ylabel('Ht-agl (km)');

subplot(3,1,3),imagesc(jdl,htl(1:36),mcr3l(1:36,:));
axis xy, colorbar, caxis(rlim);
ylabel('Ht-agl (km)');
xlabel('Year Day (UTC)');
if pngflg
    print_buffer = [outpath 'RICO_Profiler_radl_' bys ' ' bds '-' eds '.png'];
        print('-dpng ', print_buffer);
end

% plot counts
figure
clim = [0 15];
subplot(3,1,1),imagesc(jdl,htl(1:36),mcc1l(1:36,:));
colorbar, axis xy, caxis(clim);
title('RICO Profiler Cts low mode');
ylabel('Ht-agl (km)');

subplot(3,1,2),imagesc(jdl,htl(1:36),mcc2l(1:36,:));
axis xy, colorbar, caxis(clim);
ylabel('Ht-agl (km)');

subplot(3,1,3),imagesc(jdl,htl(1:36),mcc3l(1:36,:));
axis xy, colorbar, caxis(clim);
ylabel('Ht-agl (km)');
xlabel('Year Day (UTC)');
if pngflg
    print_buffer = [outpath 'RICO_Profiler_CTl_' bys ' ' bds '-' eds '.png'];
        print('-dpng ', print_buffer);
end

%plot snr
figure
snrlim = [-25 15];
subplot(3,1,1),imagesc(jdl,htl(1:36),mcsnr1l(1:36,:));
colorbar, axis xy, caxis(snrlim);
title('RICO Profiler SNR low mode mc');
ylabel('Ht-agl (km)');

subplot(3,1,2),imagesc(jdl,htl(1:36),mcsnr2l(1:36,:));
axis xy, colorbar, caxis(snrlim);
ylabel('Ht-agl (km)');

subplot(3,1,3),imagesc(jdl,htl(1:36),mcsnr3l(1:36,:));
axis xy, colorbar, caxis(snrlim);
ylabel('Ht-agl (km)');
xlabel('Year Day (UTC)');
if pngflg
    print_buffer = [outpath 'RICO_Profiler_SNRl_' bys ' ' bds '-' eds '.png'];
        print('-dpng ', print_buffer);
end

%High mode
%Plot radials
figure
wlim = [-5 5];
subplot(3,1,1),imagesc(jdh,hth(1:36),mcr1h(1:36,:));
colorbar, axis xy, caxis(wlim);
title('RICO Profiler Radials high mode mc');
ylabel('Ht-agl (km)');

rlim = [-10 10];
subplot(3,1,2),imagesc(jdh,hth(1:36),mcr2h(1:36,:));
axis xy, colorbar, caxis(rlim);
ylabel('Ht-agl (km)');

subplot(3,1,3),imagesc(jdh,hth(1:36),mcr3h(1:36,:));
axis xy, colorbar, caxis(rlim);
ylabel('Ht-agl (km)');
xlabel('Year Day (UTC)');
if pngflg
    print_buffer = [outpath 'RICO_Profiler_radh_' bys ' ' bds '-' eds '.png'];
        print('-dpng ', print_buffer);
end

% plot counts
figure
clim = [0 15];
subplot(3,1,1),imagesc(jdh,hth(1:36),mcc1h(1:36,:));
colorbar, axis xy, caxis(clim);
title('RICO Profiler Cts high mode mc');
ylabel('Ht-agl (km)');

subplot(3,1,2),imagesc(jdl,htl(1:36),mcc2l(1:36,:));
axis xy, colorbar, caxis(clim);
ylabel('Ht-agl (km)');

subplot(3,1,3),imagesc(jdl,htl(1:36),mcc3l(1:36,:));
axis xy, colorbar, caxis(clim);
ylabel('Ht-agl (km)');
xlabel('Year Day (UTC)');
if pngflg
    print_buffer = [outpath 'RICO_Profiler_CTh_' bys ' ' bds '-' eds '.png'];
        print('-dpng ', print_buffer);
end

%plot snr
figure
snrlim = [-25 15];
subplot(3,1,1),imagesc(jdh,hth(1:36),mcsnr1h(1:36,:));
colorbar, axis xy, caxis(snrlim);
title('RICO Profiler SNR high mode mc');
ylabel('Ht-agl (km)');

subplot(3,1,2),imagesc(jdh,hth(1:36),mcsnr2h(1:36,:));
axis xy, colorbar, caxis(snrlim);
ylabel('Ht-agl (km)');

subplot(3,1,3),imagesc(jdh,hth(1:36),mcsnr3h(1:36,:));
axis xy, colorbar, caxis(snrlim);
ylabel('Ht-agl (km)');
xlabel('Year Day (UTC)');
if pngflg
    print_buffer = [outpath 'RICO_Profiler_SNRh_mc' bys ' ' bds '-' eds '.png'];
        print('-dpng ', print_buffer);
end
