% HiWinGS 2013 :: 2013-09 :: LB
% reads daily SCS ship data system files.
disp(['read_scs_day: ', cruise]);

jd=sprintf('%03i',ddd);
                                                 %10-min index
jax=1;                              %line counter
%declare arrays
nr2=38;scsdata=ones(nr2,60*24)*NaN;

dr=dir([way_raw_data_flux, 'KN' yyyy(3:4) sprintf('%02i%02i',Vdate(2),Vdate(3)) '_*.csv']);
e=dr(1).name;

disp(['Reading scs file ',e]);
flist=fopen([way_raw_data_flux e],'r');
temp=[];stx=[];
if flist>0,                                 %if the file exists,
    while ~feof(flist)
        try
%             Date	 Time_GMT	 Dec_Lat	 Dec_Lon	 SPD	 HDT	 COG	 SOG	 WXTP_Ta	 WXTS_Ta	 WXTP_Pa	 WXTS_Pa	 WXTP_Ri	 WXTS_Ri	 WXTP_Rc	 WXTS_Rc	 WXTP_Dm	 WXTS_Dm	 WXTP_Sm	 WXTS_Sm	 WXTP_Ua	 WXTS_Ua	 WXTP_TS	 WXTS_TS	 WXTP_TD	 WXTS_TD	 SWR	 LWR	 SBE45S	 SBE48T	 FLR	 SSV	 Depth12	 Depth35	 HdChkSum=00 
%             8/11/2013	0:00:01	41.52395	-70.672264	-0.01	24.6	138.8	0.02	22.6	22.5	1014.4	1014.4	0	0	18.86	6.03	226	231	5.5	4.3	77.1	78.2	5.21	3.91	259.61	268.6	12.092	358.9	0.2427	22.979	164.6	1493.09	nan	nan	
            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 %f %f %f %f %f %f %f %*c  %*[^\n]','delimiter', ',', 'headerlines', 2,  'emptyvalue', NaN);
            stx=[stx cell2mat(temp)'];
        catch
            [lnr2,cnr2]=size(temp{1,nr2});
            for ll=1:nr2
                 if length(temp{1,ll})~=lnr2
                     temp{1,ll}(lnr2+1)=[];
                 end;
            end;
            if isempty(temp{1,1}); continue; end
%                 if isempty(temp{1,1});
%                     temp{1,nr2}=temp{1,nr2}';
%                 end;
         stx=[stx cell2mat(temp)'];
        end;
    end;  %end while

    [nr1,nl1]=size(stx);
    scsdata(:,jax:jax+nl1-1)=stx;
end;      %     end if list


jd_scs=datenum(scsdata(1:6,:)')-datenum(Vdate(1),1,0);   %Day-Of-Year
Dec_Lat=scsdata(7,:); %Latitude	deg	C-Nav GPS receiver
Dec_Lon=scsdata(8,:); %Longitude	deg	C-Nav GPS receiver
SPD=scsdata(9,:);
HDT=scsdata(10,:);
COG=scsdata(11,:); %Course Over Ground	deg	C-Nav GPS receiver
SOG=scsdata(12,:); %Speed Over Ground	knots	C-Nav GPS receiver
WXTP_Ta=scsdata(13,:); %Air Temp	C	Port VaisalaWXT520
WXTS_Ta=scsdata(14,:); %Air Temp	C	Starboard VaisalaWXT520
WXTP_Pa=scsdata(15,:); %Pressure	hPa	Port VaisalaWXT520, uncorrected for height
WXTS_Pa=scsdata(16,:); %Pressure	hPa	Starboard VaisalaWXT520, uncorrected for height
WXTP_Ri=scsdata(17,:); %Rainfall Intensity	mm/h	Port VaisalaWXT520
WXTS_Ri=scsdata(18,:); %Rainfall Intensity	mm/h	Starboard VaisalaWXT520
WXTP_Rc=scsdata(19,:); %Cumulative Rainfall	mm	Port VaisalaWXT520
WXTS_Rc=scsdata(20,:); %Cumulative Rainfall	mm	Starboard VaisalaWXT520
WXTP_Dm=scsdata(21,:); %Relative Wind Direction	deg	Port VaisalaWXT520
WXTS_Dm=scsdata(22,:); %Relative Wind Direction	deg	Starboard VaisalaWXT520
WXTP_Sm=scsdata(23,:); %Relative Wind Speed	m/s	Port VaisalaWXT520
WXTS_Sm=scsdata(24,:); %Relative Wind Speed	m/s	Starboard VaisalaWXT520
WXTP_Ua=scsdata(25,:); %Relative Humidity	%	Port VaisalaWXT520
WXTS_Ua=scsdata(26,:); %Relative Humidity	%	Starboard VaisalaWXT520
WXTP_TS=scsdata(27,:); %True Wind Speed	m/s	Port VaisalaWXT520, corrected for ship's gyro, SOG, and COG
WXTS_TS=scsdata(28,:); %True Wind Speed	m/s	Starboard VaisalaWXT520, corrected for ship's gyro, SOG, and COG
WXTP_TD=scsdata(29,:); %True Wind Direction	deg	port VaisalaWXT520, corrected for ship's gyro, SOG, and COG
WXTS_TD=scsdata(30,:); %True Wind Direction	deg	Starboard VaisalaWXT520, corrected for ship's gyro, SOG, and COG
SWR=scsdata(31,:); %Short Wave Radiation	W/m2	IMET (Eppley Laboratories, Inc. pyronometer)
LWR=scsdata(32,:); %Long Wave Radiation	W/m2	IMET (Eppley Laboratories, Inc. pyronometer)
SBE45S=scsdata(33,:);
SBE48T=scsdata(34,:);
FLR=scsdata(35,:); %Fluorescence	mV	WetLabs Wet-StarWS3S-901P
SSV=scsdata(36,:); %Surface Sound Velocity	m/s	Calculated from SSTMP and SSCND
Depth12=scsdata(37,:);
Depth35=scsdata(38,:);
% HdChkSum=00 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%save dat in one ascii file
if saveit;
    np=length(jd_scs);
    eval(['prt_jas_scs_' currentMname]);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




