%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Quick look LiDAR script for DOMEX
%
% by: Neil Lareau
% created on: 5-6-11
%
% Provides a look at legs 1L,2L and 5L with flight level data overlaid
% The goal is to observed structures within the boundary layer
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

close all
clear all

datadir='/Users/neil/Documents/DOMEX_DATA/WCL/';
cd(datadir);
flightdir='/Users/neil/Documents/DOMEX_DATA/FLIGHT_DATA/';

cases=dir('*rf06*');
for i=1:1%length(cases)
    path=[datadir cases(i).name];
    cd(path);
    RF_num=cases(i).name;
    files=dir('*.nc');
    count=0;
    for j=1:length(files)
        file=files(j).name;
        yr=file(13:16);
        mn=file(17:18);
        dy=file(19:20);
        hh=file(21:22);
        mm=file(23:24);
        today=datenum(str2double(yr),str2double(mn),str2double(dy));
        ss='00';
        ffile=[flightdir yr mn dy '.c1.nc']; % location of the flight data
        timestamp=str2double([hh mm ss]);
        [leg,hilo]=rflookup_lidar(RF_num,timestamp);
        sdate=datenum(str2double(yr),str2double(mn),str2double(dy),str2double(hh),str2double(mm),00);
        edate=datenum(str2double(yr),str2double(mn),str2double(dy),str2double(hh),str2double(mm)+10,00);
        
        if ismember(leg,[1]);% 2 5])
            %then it is a leg we want
            
            if strcmp('low',hilo)
                if length(leg)>1 %sometimes we get a double match
                    leg =leg(1,1) %just go with the first time period
                end
                leg
                hilo
                %then it is a low leg, which we want
                [copol,cross,depol,height,alt,timemx]=lidarload(file);
                time=timemx(1,:);
                timer=double(time)./(86400) + today;
                time2=double(timemx)./86400 +today;
                
                if std(height(1,:))<50
                    
                    %load LCL0405leg1.mat
                    %load LCL0419leg1.mat
                    load LCL0415leg1.mat
                    LCL=LCL0415leg1;
                    count=count+1;
                    [lcl,ftime,flag]=lcl_to_lidar_load(LCL,timer);
                    [w,mr,aero,rh,theta,flag]=ka1sec_to_lidar_load(ffile,timer); %screen_size = get(0, 'ScreenSize');

                    
                    f1 = figure(1);
                    
                    %set(f1, 'Position', [0 0 screen_size(3) screen_size(4) ] );
                    hold on
                    pcolor(time2,height,copol);shading flat;
                    ax1=gca;
                    caxis([0 10])
                    ylim([height(1,1) 900])
                    %xlim([min(ftime) max(ftime)]);
                    datetick('x','dd.HH.MM');
                    xlim([datenum(2011,04,15,14,30,0) datenum(2011,04,15,14,42,0)])
                    
                    colorbar

                    %now add the flight data on top
                    a1=plot(ax1,time2(1,:),lcl+permute(alt,[2 1]),'r--','linewidth',2);
                    [x,y]=size(copol);
                    %now add the flight data on top
                    thetav=theta.*(1+.61*mr.*10^-3);
                    tvplot=250.*(thetav-nanmean(thetav))+400;
                    wplot=100.*(w-nanmean(w))+450;%+mean(alt);
                    aerop=4.*(aero - nanmean(aero))+400;%+mean(alt);
                    mrprime=100.*(mr-nanmean(mr))+400;%+mean(alt);
                    rhp=10.*(rh-nanmean(rh))+450;
                    rh80=repmat(nanmean(rh),y,1)+400;
                    tplot=250.*(theta-nanmean(theta))+400;
                    %%a2=plot(ax1,time2(1,:),wplot,'k--','linewidth',1);
                    %plot(ax1,timemx(1,:),aerop,'k','linewidth',1);
                    %plot(ax1,timemx(1,:),mrprime,'--','linewidth',2,'color','c')
                    %%a3=plot(ax1,time2(1,:),rhp,'c','linewidth',1)
                    %plot(ax1,time2(1,:),rh80,'c--','linewidth',2)
                    %plot(ax1,timemx(1,:),tplot,'r','linewidth',2)
                    %%4=plot(ax1,time2(1,:),tvplot,'m--','linewidth',1);
                    %legend([a1 a2 a3 a4],'LCL','W','RH','TV')
                    ylabel('Height [m]')
                    xlabel('date [dd.HH.MM]')
                    title('Lidar and LCL Comparison');
                                       % xlim([min(ftime) max(ftime)]);

                end
                
            end
            
            
        end
        
        
        
    end
end

return;

