% Plot UM bas logger data
% 10m sonic 12, 22m sonic 4
% UMass sonic 2D Model 81000
% u is E/W + fm E
% v is N/S + fm N
% w -s up/dn + is up
% on N boom +v = 334
% on S boom +v = 154

clear
close all

read_parameters_UM; %Reading parameter files flux conf

pltit = 1;

load z:\BAO\Tower\umass\processed\sonic_022n_20070827.dat  %1 day 1 sec averaged data 22m north boom
st22 = sonic_022n_20070827;
load z:\BAO\Tower\umass\processed\sonic_010n_20070827.dat  %1 day 1 sec averaged data 10m north boom
st10 = sonic_010n_20070827;
tit = 'BAO 2007';
% yy  mm dd hh mn ss    u       v     w       t     npt bat
%2007 08 20 00 00 00    3.89   0.50  -0.40   27.10  32 11.1
%2007 08 20 00 00 01    3.51   0.85  -0.17   27.09  32 11.1

%%%%%%% PSD 10m data
%1    2   3   4     5     6      7      8       9      10        11     12     13    14
%ID  YR  YD  hhmm  T      RH     WSv   WSs     Dir    WDt       Bat    press  precip WDT
%10,2007,165,1405,15.095,50.858,1.808,-999.000,238.61,-999.0000,13.515,843.01,0.000,212.610

st22(:,13) = datenum(st22(:,1),st22(:,2),st22(:,3),st22(:,4),st22(:,5),st22(:,6))-datenum(st22(:,1)-1,12,31);
st10(:,13) = datenum(st10(:,1),st10(:,2),st10(:,3),st10(:,4),st10(:,5),st10(:,6))-datenum(st10(:,1)-1,12,31);

u10 = st10(:,7);
v10 = st10(:,8);
w10 = st10(:,9);
t10 = st10(:,10);
yys10 = num2str(st10(1,1));
mms10 = num2str(st10(1,2));
dds10 = num2str(st10(1,3));
hhs10 = num2str(st10(1,4));
mns10 = num2str(st10(1,5));
sss10 = num2str(st10(1,6));

u22 = st22(:,7);
v22 = st22(:,8);
w22 = st22(:,9);
t22 = st22(:,10);
yys22 = num2str(st22(1,1));
mms22 = num2str(st22(1,2));
dds22 = num2str(st22(1,3));
hhs22 = num2str(st22(1,4));
mns22 = num2str(st22(1,5));
sss22 = num2str(st22(1,6));


%  Plot Temperatures
figure(1)
plot(st10(:,13),st10(:,10),'k')
hold
plot(st22(:,13),st22(:,10),'r')
legend('10','22')
ylabel('T (C)')
xlabel('Year Day')
mx = max(st10(:,13));
xlim([st10(1,13) mx])
ylim([min(st10(:,10))-5 max(st10(:,10)+5)])
title(tit);
if pltit
    print_buffer = [way_images_sonic 'UMPSD_T_' yys10 mms10 dds10 '_10_22.png'];
    print('-dpng ', print_buffer);
end

S10=sqrt(u10.*u10+v10.*v10);
%dir=(atan2(-v,u)*180/pi) - 26;
dir10=(atan2(u10,v10)*180/pi) - 26;
for j = 1:length(dir10)
    if dir10(j) < 0 & dir10(j) >= -180
        dir10(j) = 360 + dir10(j);
    end
end

S22=sqrt(u22.*u22+v22.*v22);
%dir=(atan2(-v,u)*180/pi) - 26;
dir22=(atan2(u22,v22)*180/pi) - 26;
for j = 1:length(dir22)
    if dir22(j) < 0 & dir22(j) >= -180
        dir22(j) = 360 + dir22(j);
    end
end

% on N boom +v = 334
% on S boom +v = 154
% 10m is on N boom so we need to add 26 degrees to directions

figure(2)
subplot(2,1,1),plot(st10(:,13),S10,'k')
hold
subplot(2,1,1),plot(st22(:,13),S22,'r')

legend('10','22')
ylabel('Spd (m/s)')
xlabel('Year Day')
xlim([st10(1,13) mx])
ylim([0 20])
title(tit);

subplot(2,1,2),plot(st10(:,13),dir10,'k')
hold
subplot(2,1,2),plot(st22(:,13),dir22,'r')
ylabel('Direction')
xlabel('Year Day')
xlim([st10(1,13) mx])
ylim([0 360])
if pltit
    print_buffer = [way_images_sonic 'UMPSD_SPDDIR_' yys10 mms10 dds10 '_10_22.png'];
    print('-dpng ', print_buffer);
end


figure(3)
plot(st10(:,13),st10(:,7:9))
legend('u','v','w')
ylabel('Spd (m/s)')
xlabel('Year Day')
xlim([st10(1,13) mx])
ylim([-10 10])
title('10');
if pltit
    print_buffer = [way_images_sonic 'UMPSD_uvw_' yys10 mms10 dds10 '_10.png'];
    print('-dpng ', print_buffer);
end
figure(4)
plot(st22(:,13),st22(:,7:9))
legend('u','v','w')
ylabel('Spd (m/s)')
xlabel('Year Day')
xlim([st22(1,13) mx])
ylim([-10 10])
title('22');
if pltit
    print_buffer = [way_images_sonic 'UMPSD_uvw_' yys22 mms22 dds22 '_22.png'];
    print('-dpng ', print_buffer);
end

figure(6)
plot(st10(:,13),st10(:,11),'k.')
hold
plot(st22(:,13),st22(:,11),'r.')
legend('10','22','Location','Southwest')
title('BAO 2007 UMass sonic npts');
ylim([0 34]);