%Read WHOI and SCS Event and PSD files file
% Each Event is stopped and started to creat daily files (UTC)
% DEW LB 7/10/2013
disp('Need to run read_epicz to create 5 min all file');
disp('Read_SCS_WHOTS_2013');

clear
close all

cruise='WHOTS_2013';     % Cruise name, for example 'STRATUS_2009', 'TOTO_1969', etc...
ship='Hiialakai';                % Research vessel
data_drive='D:\';
Lacie_drive='G:\';
plotit = 1;

ev = input('Input SCS event number to read (011) ','s');

way_proc_data_flux = [data_drive cruise  '\' ship '\flux\Processed\'];     % Processed data path
way_images_flux = [data_drive cruise  '\' ship '\flux\Raw_Images\' ];

%Ludo's read
clear dir
dr=dir(['D:\WHOTS_2013\Hiialakai\SCS\Raw\WHOTS-OBS_' ev '.elg']);
e=dr(1).name;
disp(['Reading scs file ',e]);
flist=fopen(['D:\WHOTS_2013\Hiialakai\SCS\Raw\WHOTS-OBS_' ev '.elg']);
temp=[];scsdata=[];
if flist>0,                                 %if the file exists,
    while ~feof(flist)
        %             temp=textscan(flist,'%f %f %f %*c %f %f %f %*c %f %f %*c %f %f %*c %f %f %*c %f %f %*c %f %f %f %f %f  %*[^\n]','delimiter', ' ', 'treatAsEmpty', {'SBE-21','****','FWD','Temp'}, 'headerlines', 1,  'emptyvalue', NaN);
        temp=textscan(flist,'%2f/%2f/%4f %2f:%2f:%2f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*4c %f %f %f %*6c %f %*c %f %*c %*4c %f %f %f  %*[^\n]','delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN);
        scsdata=[scsdata cell2mat(temp)'];
    end;  %end while
end;      %     end if list
scs = scsdata;

% a=['D:\WHOTS_2013\Hiialakai\SCS\Raw\WHOTS-OBS_' bds '.dat'];
% scs = load(a);
% 1-3 Date
% 4-6 Time UTC
% 7 SAMOS-Lat-VALUE
% 8 SAMOS-Lon-VALUE
% 9 SAMOS-Gyro-VALUE
% 10 SAMOS-COG-VALUE
% 11 SAMOS-SOG-VALUE
% 12 SAMOS-AirTemp-VALUE                 **** RMYoung deg C
% 13 SAMOS-Barometer-VALUE             Bad RMYoung
% 14 SAMOS-RelHumidity-VALUE            **** RMY %
% 15 SAMOS-RelWind-Dir-VALUE
% 16 SAMOS-RelWind-Spd-VALUE
% 17 SAMOS-TrueWind-Dir-VALUE           *** Prop-Vane deg T
% 18 SAMOS-TrueWind-Spd-VALUE              *** Prop-Vane kts
% 19 SAMOS-TSG-Temp-VALUE              **** SST deg C
% 20 SAMOS-TSG-Cond-VALUE
% 21 SAMOS-TSG-Sal-VALUE
% 22 SAMOS-TSG-SV-VALUE
% 23 WINCH-PAYOUT
% 24 WINCH-SPD
% 25 WINCH-TENSION
% 26 WIND-85004-Ultrasonic-Anemometer   Rel dir deg
% 27 WIND-85004-Ultrasonic-Anemometer   Rel spd kts
% 28 Depth-Meters
% 29 Vaisala-Barometer--               Good Station pressure mb
% 30 Baro-Pressure  **Bad RMY

% Calculate Decimal year day
yd_scs=datenum(scsdata(3,:),scsdata(1,:),scsdata(2,:),scsdata(4,:),scsdata(5,:),scsdata(6,:))-datenum(scsdata(3,1),1,0);   %Day-Of-Year
bd = fix(yd_scs(1));
ed = bd + 1;

bds = sprintf('%03d',bd);
dds = sprintf('%02d',scsdata(2,1));
mms = sprintf('%02d',scsdata(1,1));


g=['D:\WHOTS_2013\Hiialakai\flux\Processed\WHOTS_2013_proc_met_1min_2013_' mms '_' dds '_' bds '.txt'];
met=load(g)';
% 1  jd_pc;  %decimal JD
% 2  pirm; %
% 3  pspm; %
% 4  Tc1;  %
% 5  Td1;  %
% 6  Tsea;
% 7  Tvais;    %
% 8  Rhvais;   %
% 9  org;  %
% 10  org_carrier; %
% 11  aspir_on;    %
% 12  press;    %

s=['D:\WHOTS_2013\Hiialakai\flux\Processed\WHOTS_2013_proc_son_1min_2013_' mms '_' dds '_' bds '.txt'];
son=load(s)';
% jd_son;	%decimal JD
% U;
% V;
% w;
% Tsonic;
% dir;

%  Load 5 min all file
b=['D:\WHOTS_2013\Hiialakai\flux\Processed\WHOTS_2013_PSD_flux_5min_all.txt'];
met5=load(b)';
% jdy  1  julian day to Matlab format
% U   2   true wind,PSD sonic (m/s)
% dir   3   true wind direction, PSD sonic (deg)
% tsnk   4   sea snake temperature,PSD, 0.05 m depth (C)
% ta=x(:,7);%air temperature, PSD (C) (z=15 m)
% press   31   PSD atmospheric pressure, mb
% RH  32    PSD RHvais, %

% Calculating Sea Level Pressure using Barometer height
% ESRL ht = 14.38m
% ship ht = 12.02m
% NOAA Tech training

%ESRL
Pz1 = met(12,:); % Barometer pressure
Ra = 287.05;  % Constant J/kgK
T1 = met(7,:);  % Temperature K
g = 9.81;  % gravity m/s
Z1 = 14.38;  % height of presure sensor in m
%Ps = ssealevel pressure
s1 = (9.81*Z1);
s2 = (Ra*(T1+274.15));
Ps1 = exp(s1./s2).*Pz1;
clear s1 s2
%Ship
Pz2 = scs(29,:); % Barometer pressure
Ra = 287.05;  % Constant J/kgK
T2 = scs(12,:);  % Temperature K
g = 9.81;  % gravity m/s
Z2 = 12.02;  % height of presure sensor in m
%Ps = ssealevel pressure

s1 = (9.81*Z2);
s2 = (Ra*(T2+274.15));
Ps2 = exp(s1./s2).*Pz2;

mPs2 = mean(Ps2);
minp = fix(min(Ps2));
maxp = fix(max(Ps2));
pp = find(Ps1 > mPs2-3 );
mPs1 = mean(Ps1(pp));

% figure
% plot(yd_scs,scs(29,:),'r')  %Vaisala-Barometer-
% hold
% plot(met(1,:),met(12,:),'k.')
% plot(yd_scs,Ps2,'g')  %Vaisala-Barometer-
% plot(met(1,pp),Ps1(pp),'b')
% ylabel('Pressure (mb)');
% xlabel('Year Day (UTC)')
% title('WHOTS 2013 Sea Level Pressure');
% grid
% xlim([ bd ed ]);
% ylim([ minp-2 maxp+2]);
% legend('SCS station','PSD station','SCS sea level','PSD sea level','location','best');
% 
% if plotit
%     print_buffer = [way_images_flux cruise '_SCS_PSD_Psealvl_' bds '.png'];
%     print('-dpng ', print_buffer);
% end
% %  Plot RMY and other pressures
% if 0
%     figure
%     plot(yd_scs,scs(:,35),'b') %Baro-Pressure
%     hold
%     plot(yd_scs,scs(:,34),'r') %Vaisala-Barometer-
%     plot(yd_scs,scs(:,13),'g')  %Samos-Barometer-Value
%     plot(met(1,:),met(12,:),'k.')  % PSD
%     ylabel('Pressure (mb)');
%     xlabel('Year Day (UTC)')
%     title('WHOTS 2013 Pressure');
%     grid
%     xlim([ bd ed ]);
%     legend('RMY','SCS','RMY','PSD','location','best');
% 
%     if plotit
%         print_buffer = [way_images_flux cruise '_SCS_PSD_Pall_' bds '.png'];
%         print('-dpng ', print_buffer);
%     end
% 
% end
% 
% figure
% plot(yd_scs,scs(19,:),'b') % Samos-TSG-Temp
% hold
% plot(met(1,:),met(6,:),'k')  % Seasnake
% ylabel('SST (C)');
% xlabel('Year Day (UTC)')
% title('WHOTS 2013 SST');
% grid
% xlim([ bd ed ]);
% legend('SCS','PSD','location','best');
% 
% if plotit
%     print_buffer = [way_images_flux cruise '_SCS_PSD_SST_' bds '.png'];
%     print('-dpng ', print_buffer);
% end
% 
% figure
% subplot(2,1,1),plot(yd_scs,scs(12,:),'b') % Samos-AirTemp
% hold
% subplot(2,1,1),plot(met(1,:),met(7,:),'r')  %PSD Vai Temp
% subplot(2,1,1),plot(son(1,:),son(5,:),'k')  %PSD Son Temp
% ylabel('Air T (C)');
% xlabel('Year Day (UTC)')
% title('WHOTS 2013 Air Temp');
% grid
% xlim([ bd ed ]);
% legend('SCS','PSD Vai','PSD Son','location','best','orientation','horizontal');
% 
% subplot(2,1,2),plot(yd_scs,scs(14,:),'b') % Samos-RH-Temp
% hold
% subplot(2,1,2),plot(met(1,:),met(8,:),'k') % PSD RH
% ylabel('RH(%)');
% xlabel('Year Day (UTC)')
% title('WHOTS 2013 RH');
% ylim([ 0 100]);
% grid
% xlim([ bd ed ]);
% legend('SCS','PSD Vai','location','best');
% 
% if plotit
%     print_buffer = [way_images_flux cruise '_SCS_PSD_TRH_' bds '.png'];
%     print('-dpng ', print_buffer);
% end
% 
% 
% sonspd = sqrt((son(2,:).^2)+(son(3,:).^2));
% 
% figure
% subplot(2,1,1),plot(yd_scs,scs(17,:),'b.') % Samos-TrueWind-Dir
% hold
% subplot(2,1,1),plot(met5(1,:),met5(3,:),'k.') % PSD Son
% ylabel('Dir (T)');
% xlabel('Year Day (UTC)')
% title('WHOTS 2013 Dir True');
% ylim([ 0 360]);
% grid
% xlim([ bd ed ]);
% legend('SCS P-V','PSD Son','location','best');
% 
% subplot(2,1,2),plot(yd_scs,scs(18,:)*.55,'b') % Samos TruWind Speed
% hold
% subplot(2,1,2),plot(met5(1,:),met5(2,:),'k.') % PSD TrueWind Speed
% ylabel('Spd (m/s)');
% xlabel('Year Day (UTC)')
% title('WHOTS 2013 Spd True');
% ylim([ 0 25]);
% xlim([ bd ed ]);
% grid
% 
% if plotit
%     print_buffer = [way_images_flux cruise '_SCS_PSD_DirSpdT_' bds '.png'];
%     print('-dpng ', print_buffer);
% end
% 
% 
% figure
% subplot(2,1,1),plot(yd_scs,scs(15,:),'b.') % Samos WindDir-Rel
% hold
% plot(yd_scs,scs(26,:),'r.')  % Ultra Sonic
% plot(son(1,:),son(6,:),'k.')  % PSD Son
% ylabel('Dir (Rel)');
% xlabel('Year Day (UTC)')
% title('WHOTS 2013 Dir Rel');
% ylim([ 0 360]);
% grid
% xlim([ bd ed ]);
% legend('SCS P-V','SCS Son','PSD Son','location','best');
% 
% subplot(2,1,2),plot(yd_scs,scs(16,:)*.55,'b') % Samos-WindRel Rel
% hold
% plot(yd_scs,scs(27,:)*.55,'r') % Ultra Sonic
% plot(son(1,:),sonspd,'k')  % PSD SOn
% ylabel('Spd (m/s)');
% xlabel('Year Day (UTC)')
% title('WHOTS 2013 Spd Rel');
% ylim([ 0 25]);
% grid
% xlim([ bd ed ]);
% 
% if plotit
%     print_buffer = [way_images_flux cruise '_SCS_PSD_DirSpdR_' bds '.png'];
%     print('-dpng ', print_buffer);
% end

% load and plot WHOI data Times are UTC 1 hr data...wind dir are
% oceanographic 180 out from met

whoi = load(['D:\WHOTS_2013\Hiialakai\WHOI\winds.dat']);
%col sys id yearday  bp    at   sst   wnde   wndn  rh   swr   lw     prc  cnd   wnds  wndd  hr
%0 2 06930 181.58 1015.0 23.66 35.96 00.00 00.00 078.0 004.0 394.0 28.0  00.00 00.00 000.0 14
% sys = buoy 1, 2 = old  3, 4 = new
% YD = 4 hr = 17
% True spd = 15 True Dir = 16
% P = 5 Ta = 6 RH = 10 SW = 11 LW - 12
b1 = find(whoi(:,2) == 1);
b2 = find(whoi(:,2) == 2);
b3 = find(whoi(:,2) == 3);
b4 = find(whoi(:,2) == 4);

% WHOI
%Ship
Pz3 = whoi(:,5); % Barometer pressure
Ra = 287.05;  % Constant J/kgK
T3 = whoi(:,6);  % Temperature K
g = 9.81;  % gravity m/s
Z3 = 3.5;  % height of presure sensor in m
%Ps = ssealevel pressure
s1 = (9.81*Z3);
s2 = (Ra*(T3+274.15));
Ps3 = exp(s1./s2).*Pz3;

whoi(:,16) = whoi(:,16)-180; % convert to meteorlogical

figure
plot(met5(1,:),met5(2,:),'k.') % PSD Son
hold
plot(whoi(b1,4),whoi(b1,15),'ro') % spd
plot(whoi(b2,4),whoi(b2,15),'b*') % spd
plot(whoi(b3,4),whoi(b3,15),'gd') % spd
plot(whoi(b4,4),whoi(b4,15),'c+') % spd
ylabel('Spd (m/s)');
xlabel('Year Day (UTC)')
title('WHOTS 2013 Spd True');
ylim([ 0 20]);
grid
 xlim([ bd ed ]);
legend('PSD','W1','W2','W3','W4','location','best','orientation','horizontal');
if plotit
    print_buffer = [way_images_flux cruise '_WHOI_SpdT_' bds '.png'];
    print('-dpng ', print_buffer);
end

figure
plot(met5(1,:),met5(3,:),'k') % PSD Son
hold
plot(whoi(b1,4),whoi(b1,16),'ro') % spd
plot(whoi(b2,4),whoi(b2,16),'b*') % spd
plot(whoi(b3,4),whoi(b3,16),'gd') % spd
plot(whoi(b4,4),whoi(b4,16),'c+') % spd
ylabel('Dir (T)');
xlabel('Year Day (UTC)')
title('WHOTS 2013 Dir True');
ylim([ 0 180]);
 xlim([ bd ed ]);
grid
legend('PSD','W1','W2','W3','W4','location','best','orientation','horizontal');

if plotit
    print_buffer = [way_images_flux cruise '_WHOI_DirT_' bds '.png'];
    print('-dpng ', print_buffer);
end

figure
plot(met(1,pp),Ps1(pp),'k')
hold
plot(whoi(b1,4),Ps3(b1),'ro') % spd
plot(whoi(b2,4),Ps3(b2),'b*') % spd
plot(whoi(b3,4),Ps3(b3),'gd') % spd
plot(whoi(b4,4),Ps3(b4),'c+') % spd
ylabel('Pressure (mb)');
xlabel('Year Day (UTC)')
title('WHOTS 2013 Sea Level Pressure');
grid
xlim([ bd ed ]);
ylim([ minp-2 maxp+2]);
legend('PSD','W1','W2','W3','W4','location','best','orientation','horizontal');
if plotit
    print_buffer = [way_images_flux cruise '_WHOI_SLP_' bds '.png'];
    print('-dpng ', print_buffer);
end

figure
plot(met(1,:),met(7,:),'k')  %PSD Vai Temp
hold
plot(whoi(b1,4),whoi(b1,6),'ro') % spd
plot(whoi(b2,4),whoi(b2,6),'b*') % spd
plot(whoi(b3,4),whoi(b3,6),'gd') % spd
plot(whoi(b4,4),whoi(b4,6),'c+') % spd
ylabel('Air T (C)');
xlabel('Year Day (UTC)')
title('WHOTS 2013 Air Temp');
grid
xlim([ bd ed ]);
legend('PSD','W1','W2','W3','W4','location','best','orientation','horizontal');
if plotit
    print_buffer = [way_images_flux cruise '_WHOI_T_' bds '.png'];
    print('-dpng ', print_buffer);
end

figure
plot(met(1,:),met(8,:),'k') % PSD RH
hold
plot(whoi(b1,4),whoi(b1,10),'ro') % spd
plot(whoi(b2,4),whoi(b2,10),'b*') % spd
plot(whoi(b3,4),whoi(b3,10),'gd') % spd
plot(whoi(b4,4),whoi(b4,10),'c+') % spd
ylabel('RH(%)');
xlabel('Year Day (UTC)')
title('WHOTS 2013 RH');
ylim([ 50 100]);
grid
xlim([ bd ed ]);
legend('PSD','W1','W2','W3','W4','location','best','orientation','horizontal');
if plotit
    print_buffer = [way_images_flux cruise '_WHOI_RH_' bds '.png'];
    print('-dpng ', print_buffer);
end

figure
plot(met(1,:),met(3,:),'k') % PSD SW
hold
plot(whoi(b1,4),whoi(b1,11),'ro') % spd
plot(whoi(b2,4),whoi(b2,11),'b*') % spd
plot(whoi(b3,4),whoi(b3,11),'gd') % spd
plot(whoi(b4,4),whoi(b4,11),'c+') % spd
ylabel('SW (Wm-2)');
xlabel('Year Day (UTC)')
title('WHOTS 2013 SW');
%ylim([ 50 100]);
grid
xlim([ bd ed ]);
legend('PSD','W1','W2','W3','W4','location','best','orientation','horizontal');
if plotit
    print_buffer = [way_images_flux cruise '_WHOI_SW_' bds '.png'];
    print('-dpng ', print_buffer);
end

figure
plot(met(1,:),met(2,:),'k') % PSD RH
hold
plot(whoi(b1,4),whoi(b1,12),'ro') % spd
plot(whoi(b2,4),whoi(b2,12),'b*') % spd
plot(whoi(b3,4),whoi(b3,12),'gd') % spd
plot(whoi(b4,4),whoi(b4,12),'c+') % spd
ylabel('LW (Wm-2)');
xlabel('Year Day (UTC)')
title('WHOTS 2013 LW');
%ylim([ 50 100]);
grid
xlim([ bd ed ]);
legend('PSD','W1','W2','W3','W4','location','best','orientation','horizontal');
if plotit
    print_buffer = [way_images_flux cruise '_WHOI_LW_' bds '.png'];
    print('-dpng ', print_buffer);
end

figure
plot(met(1,:),met(6,:),'k') % PSD RH
hold
plot(whoi(b1,4),whoi(b1,7),'ro') % spd
plot(whoi(b2,4),whoi(b2,7),'b*') % spd
plot(whoi(b3,4),whoi(b3,7),'gd') % spd
plot(whoi(b4,4),whoi(b4,7),'c+') % spd
ylabel('SST (C)');
xlabel('Year Day (UTC)')
title('WHOTS 2013 SST');
%ylim([ 50 100]);
grid
xlim([ bd ed ]);
legend('PSD','W1','W2','W3','W4','location','best','orientation','horizontal');
if plotit
    print_buffer = [way_images_flux cruise '_WHOI_SST_' bds '.png'];
    print('-dpng ', print_buffer);
end
