%Read SCS Event file
% Each Event is stopped and started to creat daily files (UTC)
%
disp('Read_SCS_STRATUS_2019');

clear all
close all

cruise='STRATUS_2019';     % Cruise name, for example 'STRATUS_2009', 'TOTO_1969', etc...
ship='CabodeHornos';                % Research vessel
data_drive='D:\';
Lacie_drive='G:\';
stjd = 94;  % start day
endjd = 94; % end day
yyyy = '2019';
plotit = true; % for doing plots.
prtit = true;  % for saving plots--plotit must also be true
graphformat = '.png';  % select graphics format files
graphdevice = '-dpng'; % select graphic device
cruise = 'STRATUS_2019'; % Cruise name
ship = 'CaboDeHornos';    % Research vessel
data_drive = 'D:\Data\';
% way_proc_data_flux = [data_drive cruise  '\' ship '\flux\Processed\'];     % Processed data path
%way_images_flux = [data_drive cruise  '\' ship '\flux\Raw_Images\' ];
path_images = fullfile('D:\Data\STRATUS_2018\CabodeHornos\flux\Processed_Images');
%ev = input('Input SCS event number to read (yyyymmdd) ','s');

%Ludo's read
clear dir
dr=dir(['D:\Data\STRATUS_2019\CabodeHornos\SCS\Raw\']);
e='v20180404.asc';
disp(['Reading scs file ',e]);
flist=fopen(['D:\Data\STRATUS_2019\CabodeHornos\SCS\Raw\v20180404.asc']);
temp=[];scsdata=[];
if flist>0,                                 %if the file exists,
    while ~feof(flist)
     temp=textscan(flist,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*[^\n]','delimiter', ' ', '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
% 8 Heading
% 9 Speed Over Ground * 0.5144 to m/s
% 11 Atmospheric Pressure hPa
% 12 Atmospheric Pressure at SL
% 14 RelHumidity-VALUE
% 15 Atmospheric Temperature
% 18 RelWind-Dir-VALUE
% 21 RelWind-Spd-VALUE
% 37 Latitude
% 38 Longitude

% Calculate Decimal year day

yd_scs=datenum(scsdata(1,:),scsdata(2,:),scsdata(3,:),scsdata(4,:),scsdata(5,:),scsdata(6,:))-datenum(scsdata(1,:),scsdata(2,:),scsdata(3,:));   %Day-Of-Year
bd = (stjd)+ yd_scs;
bds = num2str(bd);

figure
plot(bd ,scs(:,12),'b')  %Vaisala-Barometer-
ylabel('Pressure (hPa)');
xlabel('Year Day (UTC)')
title('SCS Stratus 2019 Pressure');
grid
%xlim([ bd ed ]);
ylim([ 1010 1020]);
legend('SCS','location','best');
 if prtit
 ppath = fullfile(path_images,[cruise,'_SCS_Pressure_',sprintf('%02i_%02i_%04i',scs(1,2),scs(1,3),scs(1,1)),graphformat]);
 print(graphdevice,ppath);
 end
   
figure

subplot(2,1,1),plot(bd,scs(:,8),'b') %Heading
ylabel('Heading (deg)');
xlabel('Year Day (UTC)')
title('SCS Stratus 2018 Ship Heading');
legend('SOG');
grid
ylim([ 0 360]);
subplot(2,1,2),plot(bd,scs(:,9)* 0.5144,'r') %Ship Speed
ylabel('Speed (m/s)');
xlabel('Year Day (UTC)')
title('SCS Stratus 2019 Ship Speed');
legend ('spd');
grid
ylim([ -1 25]);
 if prtit
 ppath = fullfile(path_images,[cruise,'_SPD_SOG_',sprintf('%02i_%02i_%04i',scs(1,2),scs(1,3),scs(1,1)),graphformat]);
 print(graphdevice,ppath);
 end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
figure
subplot(2,1,1),plot(bd,scs(:,21)* 0.5144,'b') % Wind speed
ylabel('m/s');
xlabel('Year Day (UTC)')
title('SCS Stratus 2019 Wind Speed');
ylim([ 0 12 ]);
legend('ws');
grid
subplot(2,1,2),plot(bd,scs(:,18),'b') % Wind Dir
ylabel('deg');
xlabel('Year Day (UTC)')
title('SCS Stratus 2019 Wind Direction');
grid
ylim([ 0 360 ]);
legend('wd');
 if prtit
 ppath = fullfile(path_images,[cruise,'_SCS_WSpeed_WDirection_',sprintf('%02i_%02i_%04i',scs(1,2),scs(1,3),scs(1,1)),graphformat]);
 print(graphdevice,ppath);
 end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
subplot(2,1,1),plot(bd,scs(:,15),'b') % AirTemp
ylabel('Air T (C)');
xlabel('Year Day (UTC)')
title('SCS Stratus 2019 Air Temp');
grid
ylim([ 14 27 ]);
legend('AirT');
subplot(2,1,2),plot(bd,scs(:,14),'b') % RH
ylabel('RH(%)');
xlabel('Year Day (UTC)')
title('SCS Stratus 2019 RH');
ylim([ 0 100]);
grid
legend('RH');
 if prtit
 ppath = fullfile(path_images,[cruise,'_SCS_AirTemp_RH',sprintf('%02i_%02i_%04i',scs(1,2),scs(1,3),scs(1,1)),graphformat]);
 print(graphdevice,ppath);
 end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% figure
% subplot(2,1,1),plot(bd,scs(:,37),'b') % Lat
% ylabel('Latitude');
% xlabel('Year Day (UTC)')
% title('Stratus 2018 LAT');
% grid
% %xlim([ bd ed ]);
% legend('LAT');
% subplot(2,1,2),plot(bd,scs(:,38),'b') % RH
% ylabel('Longitude');
% xlabel('Year Day (UTC)')
% title('Stratus 2018 LON');
% %ylim([ 0 100]);
% grid
% %xlim([ bd ed ]);
% legend('LON');
%  if prtit
%  ppath = fullfile(path_images,[cruise,'_SCS_LAT/LON',sprintf('%02i_%02i_%04i',scs(1,2),scs(1,3),scs(1,1)),graphformat]);
%  print(graphdevice,ppath);
%  end