%%%% READ Profiler data from NAME 60m mode 55 hts...
%%%% COMPOSITE TIME-HEIGHT DISPLAY ....
%%   DEW 12/11/2004

clear all;
warning off MATLAB:divideByZero  % turn off warning message

% Thesedata were created running name2004_read_RWPqc1.m
load c:\data\name04\results\name04_rwp1

ymax= 3.5;
pb2 = 1;
%Convert spd and dir to U/V
%%%
for i = 1:length(trwp1)
    % initialize u and v values
    trwp1(i,4) = -950;
    trwp1(i,5) = -950;
    if trwp1(i,2) >= 0  % test for good spd and dir data
        dir=trwp1(i,3);
        theta=trwp1(i,3);
        theta(dir>=0 & dir<=90)=(-1*(theta(dir>=0 & theta<=90)+90));
        theta(dir<0 | dir>90)=270-theta(dir<0 | theta>90);
        trwp1(i,4) = cos(theta*pi/180).* trwp1(i,2); % U
        trwp1(i,5) = sin(theta*pi/180).* trwp1(i,2); % V
    end
end

%%%%


np = length(trwp1)/55;
rwp1h(1:55,1) = trwp1(1:55,1);
% reformat data files so it can be displayed as a color image (Spd and Dir)
for k = 1:np
    pe2 = pb2 + 54;
    rwp1s(1:55,k) = trwp1(pb2:pe2,2);
    rwp1d(1:55,k) = trwp1(pb2:pe2,3);
    rwp1u(1:55,k) = trwp1(pb2:pe2,4);
    rwp1v(1:55,k) = trwp1(pb2:pe2,5);
    pb2 = pe2 + 1;
end
save c:\data\NAME04\results\NAME04_RWP1 trwp1 jul_day_rwp1 year month day hour minutes rwp1s rwp1d rwp1u rwp1v rwp1h;

if 1  % skip color contour plots if 0
    %%% JULIAN DAY 188 = JULY 6!!
    % j = find(rwp1s(:,2)> 0 & rrwp1s(:,2)< 30);
    % rwp1 = trwp1(j,:);

    figure(1)
    set(gcf,'DefaultAxesFonts',16);
    imagesc(jul_day_rwp1,[rwp1h(1:55,1)],(rwp1d(1:55,:)),[0 360]);
    axis xy; ylim([0 ymax]);
    hc = colorbar;
    xlabel('Day','FontName','times','FontSize',18);
    ylabel('Height (m)','FontName','times','FontSize',18);
    title('NAME 2004, 60m Direction','FontName','times','FontSize',18);
    print_buffer = ['c:\data\NAME04\results\NAME_2004_DIR60.jpg'];
    print('-djpeg90 ', print_buffer);


    figure(2)
    set(gcf,'DefaultAxesFonts',16);
    imagesc(jul_day_rwp1,[rwp1h(1:55,1)],(rwp1s(1:55,:)),[0 15]);
    axis xy; ylim([0 ymax]);
    hc = colorbar;
    xlabel('Day','FontName','times','FontSize',18);
    ylabel('Height (m)','FontName','times','FontSize',18);
    title('Name 2004, 60m Wind Speed (m/s)','FontName','times','FontSize',18);
    print_buffer = ['c:\data\NAME04\results\NAME_2004_SPD60.jpg'];
    print('-djpeg90 ', print_buffer);

    figure(3)
    set(gcf,'DefaultAxesFonts',16);
    imagesc(jul_day_rwp1,[rwp1h(1:55,1)],(rwp1u(1:55,:)),[-15 15]);
    axis xy; ylim([0 ymax]);
    hc = colorbar;
    xlabel('Day','FontName','times','FontSize',18);
    ylabel('Height (m)','FontName','times','FontSize',18);
    title('NAME 2004, 60m U m/s','FontName','times','FontSize',18);
    print_buffer = ['c:\data\NAME04\results\NAME_2004_U60.jpg'];
    print('-djpeg90 ', print_buffer);


    figure(4)
    set(gcf,'DefaultAxesFonts',16);
    imagesc(jul_day_rwp1,[rwp1h(1:55,1)],(rwp1v(1:55,:)),[-15 15]);
    axis xy; ylim([0 ymax]);
    hc = colorbar;
    xlabel('Day','FontName','times','FontSize',18);
    ylabel('Height (m)','FontName','times','FontSize',18);
    title('Name 2004, 60m V (m/s)','FontName','times','FontSize',18);
    print_buffer = ['c:\data\NAME04\results\NAME_2004_V60.jpg'];
    print('-djpeg90 ', print_buffer);


end % skip color contour plots

if 1
    % Calculate diurnal sounding profiles
    % Stratus soundings taken at 0,6,12,18 12/05 to 12/11
    % While at WHOI site 0,4,8,12,16,20 launches
    % 4=.1667 6=.25 8=.333 12=.5 16=.667 18=.75 20=.833  30 min = .0208
    % 23=.958 19=.792 17=.708 15=.625 11=.458 7=.292 3=.125
    for t = 1:length(jul_day_rwp1)
        tim(t) = jul_day_rwp1(t) - floor(jul_day_rwp1(t));
    end
    %%%%
    % calculate composites for each hour
    degrad = (pi/180);
    h = 0;
    for hh =0:23
        h = hh + 1;
        tt = hh/24;
        for z = 1:(length(rwp1h)) % mean of all common times at same ht
            % +/- 30min of 3
            j = find(rwp1s(z,:) > 0 & tim>(tt - .0208) & tim<(tt + .0208));
            d_s(z,h) = mean(rwp1s(z,j));
            d_u(z,h) = mean(rwp1u(z,j));
            d_v(z,h) = mean(rwp1v(z,j));
            %Convert u/v back to dir in degrees
            dtmp = atan2(-d_v(z,h),-d_u(z,h)); % negate meteorological U and V
            d_d(z,h) = (1/degrad)*dtmp;
            if d_d(z,h) < 0
                d_d(z,h) = d_d(z,h) + 360;
            end
            clear j
        end
    end

    if 0
        %Plot the compostie profiles
        % First offset profiles by 5 m/s each
        for s = 2:8
            %d_int(:,s:8) = d_int(:,s:8) + 5;
        end
        figure(5)
        clf
        plot(d_s,rwp1h)
        axis([0,20,0,3.5])
        title('Hourly Composite Profiles')
        ylabel('Ht (km)')
        xlabel('Wind Spd (m/s)')
        ylim ([0 5])
        %legend('3','5','7','11','15','17','19','23','Location','NorthEast')
        %print_buffer = ['c:\data\STRATUS04\mw21\results\STRATUS_2004_diurnal_profiles.jpg'];
        %print('-djpeg90 ', print_buffer);
    end

    if 0

        % plot the number of points in each durnal profile
        figure(20)
        plot(npts,h_int/1000,'.')
        title('Launches/Diurnal Profile')
        ylabel('Ht (km)')
        xlabel('# of launches')
        ylim ([0 5])
        legend('3','5','7','11','15','17','19','23','Location','NorthWestoutside')

    end

    %save c:\data\SNAME04\results\d_int
   % save c:\data\name04\name04_hrly_comp60   d_s d_d d_u d_v rwp1h 

end % skip mean processing and plots