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

    inpath = 'D:\CALNEX_2010\Atlantis\radar\profiler\Raw\MC\';
    outpath  = 'D:\CALNEX_2010\Atlantis\radar\profiler\Raw_Images\';

%cd(inpath);

%if( nargin == 0 )
    bys = '2010';  %input('Input year to process (2007) ','s');
    bms = input('Input the month to process (1-12) ','s');
    bds = input('Input the beginning day to process (1-31) ','s');
    eys = '2010';  %input('Input year to process (2007) ','s');
    ems = 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
ic1 = 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 = [ inpath '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(4,j);
        mctim(j,2) = times(5,j);
        mctim(j,3)=  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,4) = rpar(5);
        line=fgetl(fid);
        line=fgetl(fid);
        line=fgetl(fid);
        
        for i = 1:nhts(j)
            line=fgetl(fid);
%     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
        %   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(:,4) == 400);
    mc7 = find(mctim(:,4) == 700);
            
    l1 = length(mc4);
    h1 = length(mc7);
    
    xct = 0;
    for x = 1:l1
        xct = xct + 1;
        mcsl(:,xct) = s(:,mc4(x));
        mcdl(:,xct) = d(:,mc4(x));
        jdl(:,xct) = jd(mc4(x));
        mcr1l(:,xct) = r1(:,mc4(x));
        mcr2l(:,xct) = r2(:,mc4(x));
        mcr3l(:,xct) = r3(:,mc4(x));
        mcc1l(:,xct) = c1(:,mc4(x));
        mcc2l(:,xct) = c2(:,mc4(x));
        mcc3l(:,xct) = c3(:,mc4(x));
        mcsnr1l(:,xct) = snr1(:,mc4(x));
        mcsnr2l(:,xct) = snr2(:,mc4(x));
        mcsnr3l(:,xct) = snr3(:,mc4(x));        
    end
    
    xct = 0;
    for x = 1:h1
        xct = xct + 1;
        mcsh(:,xct) = s(:,mc7(x));
        mcdh(:,xct) = d(:,mc7(x));
        jdh(:,xct) = jd(mc7(x));
        mcr1h(:,xct) = r1(:,mc7(x));
        mcr2h(:,xct) = r2(:,mc7(x));
        mcr3h(:,xct) = r3(:,mc7(x));
        mcc1h(:,xct) = c1(:,mc4(x));
        mcc2h(:,xct) = c2(:,mc4(x));
        mcc3h(:,xct) = c3(:,mc4(x));
        mcsnr1h(:,xct) = snr1(:,mc4(x));
        mcsnr2h(:,xct) = snr2(:,mc4(x));
        mcsnr3h(:,xct) = snr3(:,mc4(x));        
    end
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('CALNEX Profiler low mode mc');
ylabel('Ht-agl (km)');
%xlabel('Time (UTC)');

slim = [0 30];
subplot(2,1,2),imagesc(jdl,htl(1:36),mcsl(1:36,:));
colorbar, axis xy, caxis(slim);
%title('CALNEX Profiler low mode');
ylabel('Ht-agl (km)');
xlabel('Year Day (UTC)');
if pngflg
   print_buffer = [outpath 'CALNEX_Profiler_SDl_mc_' bys ' ' bms '-' bds '.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('CALNEX Profiler high mode mc');
ylabel('Ht-agl (km)');

slim = [0 30];
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 'CALNEX_Profiler_SDh_mc_' bys ' ' bms '-' bds '.png'];
        print('-dpng ', print_buffer);
end

if 0  % skip everything but the spd and dir plots
%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('CALNEX 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 'CALNEX_Profiler_radl_mc_' bys ' ' bms '-' bds '.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('CALNEX 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 'CALNEX_Profiler_CTl_mc_' bys ' ' bms '-' bds '.png'];
        print('-dpng ', print_buffer);
end

%plot snr
figure
snrlim = [-15 25];
subplot(3,1,1),imagesc(jdl,htl(1:36),mcsnr1l(1:36,:));
colorbar, axis xy, caxis(snrlim);
title('CALNEX 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 'CALNEX_Profiler_SNRl_mc_' bys ' ' bms '-' bds '.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('CALNEX 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 'CALNEX_Profiler_radh_mc_' bys ' ' bms '-' bds '.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('CALNEX 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 'CALNEX_Profiler_CTh_mc_' bys ' ' bms '-' bds '.png'];
        print('-dpng ', print_buffer);
end

%plot snr
figure
snrlim = [-15 25];
subplot(3,1,1),imagesc(jdh,hth(1:36),mcsnr1h(1:36,:));
colorbar, axis xy, caxis(snrlim);
title('CALNEX 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 'CALNEX_Profiler_SNRh_mc' bys ' ' bms '-' bds '.png'];
        print('-dpng ', print_buffer);
end
end
