disp('read_shp_hiwings2013.m')
fclose('all');

% 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=43;scsdata=ones(nr2,360)*NaN;

hr=sprintf('%02i',hhh);
% e=[way_raw_data_flux, yyyy(3:4),jd,'\scs0', yyyy(3:4) jd hr '_raw.txt'];
e=[way_raw_data_flux, yyyy(3:4),jd,'/scs0', yyyy(3:4) jd hr '_raw.txt'];% Mac file system
disp(['Reading scs file from doy ',jd,'  hour ',hr]);
flist=fopen(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,'%2f%2f%3f %*6c %4f/%2f/%2f %2f:%2f:%2f %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 %*[^\n]','delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN);
            stx=[stx cell2mat(temp)'];
        catch
            nr2=length(temp);
            [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});
                disp('Should never do this!')
                temp{1,7}=temp{1,7}';
            end;
         stx=[stx cell2mat(temp)'];
        end;
    end;  %end while
    stx(end+1,:)=stx(1,:)*0+hhh;
    [nr1,nl1]=size(stx);
    scsdata(:,jax:jax+nl1-1)=stx;
    jax=jax+nl1;
else
    nr2=43;
    stx=ones(nr2,360)*NaN;
    [nr1,nl1]=size(stx);
    scsdata(:,jax:jax+nl1-1)=stx;
    jax=jax+nl1;
end;      %     end if list

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

clear stx; stx=scsdata;
% stx(:,any(isnan(stx),1))=[]; %remove lines containing NaNs (bad lines, too long...)
% stx(2,:)=[];

% timegps = stx(1,:);
latx =   scsdata(10,:); %Latitude	deg	C-Nav GPS receiver
lonx =    scsdata(11,:); %Longitude	deg	C-Nav GPS receiver
cog =     scsdata(14,:); %Course Over Ground	deg	C-Nav GPS receiver
sog =     scsdata(15,:)*0.514; %Speed Over Ground	knots	C-Nav GPS receiver   %converted from knot to m/s
tsg =     scsdata(38,:);%Sea Surface Temperature	C	SeaBird SBE48
tss =     scsdata(37,:);
rh =      scsdata(29,:); %Relative Humidity	%	Starboard VaisalaWXT520
ta =      scsdata(17,:); %Air Temp	C	Starboard VaisalaWXT520
solar =  scsdata(34,:); %Short Wave Radiation	W/m2	IMET (Eppley Laboratories, Inc. pyronometer)
imrain =  scsdata(21,:); %Rainfall Intensity	mm/h	Starboard VaisalaWXT520
lrg =     scsdata(13,:);  %heading
odec =    stx(15,:)*NaN;%odec seems to be in m/s for VOCALS2008
imu =     scsdata(30,:); %True Wind Speed	m/s	Port VaisalaWXT520, corrected for ship's gyro, SOG, and COG
imd =     scsdata(32,:); %True Wind Direction	deg	port VaisalaWXT520, corrected for ship's gyro, SOG, and COG
ir  =     scsdata(35,:); %Long Wave Radiation	W/m2	IMET (Eppley Laboratories, Inc. pyronometer)
impres = scsdata(18,:); %Pressure	hPa	Port VaisalaWXT520, uncorrected for height
% imlbod = stx(11,:);
imur   =  scsdata(26,:); %Relative Wind Speed	m/s	Port VaisalaWXT520
imdr   = scsdata(24,:); %Relative Wind Direction	deg	Port VaisalaWXT520

lat=latx;
lon=lonx; 

% gpshour=hrgps;%fix(timegps./10000);
% gpshour=fix(stx(2,:)./10000);
% gpsmin=fix(timegps./1e5);
% gpssec=(timegps-gpsmin.*1e5)/1e3;
% gpstim=gpshour+gpsmin./60+gpssec./60./60;      %Decimal time in hours

gpstim=scsdata(end,:)+scsdata(1,:)./60+(scsdata(2,:)+scsdata(3,:)/1000)./60./60;      %Decimal time in hours



rdcon=pi/180;

for iii=2:length(gpstim),
    if gpstim(iii)<=gpstim(1),
        gpstim(iii)=gpstim(iii-1)+gpstim(2)-gpstim(1);
    end;
end;

% imlbod=imlbod-273.16;
% why can't rh, rain, or course be zero?
%ii=find(imrain==0);imrain(ii)=imrain(ii-1);
ii=find(rh==0);rh(ii)=rh(ii-1);
ii=find(tsg==0);tsg(ii)=tsg(ii-1);
ii=find(tss==0);tss(ii)=tss(ii-1);
% ii=find(ta==0);ta(ii)=ta(ii-1);
ii=find(rh==0);rh(ii)=rh(ii-1);
ii=find(imd==0);%imd(ii)=imd(ii-1);ii=find(imu==0);imu(ii)=imu(ii-1);
ii=find(imdr==0);%imdr(ii)=imdr(ii-1);ii=find(imur==0);imur(ii)=imur(ii-1);
ii=find(imu==-99);imu(ii)=NaN;ii=find(imur==-99);imur(ii)=NaN;
ii=find(imd==-99);imd(ii)=NaN;ii=find(imdr==-99);imdr(ii)=NaN;
ii=find(lrg==0);lrg(ii)=lrg(ii-1);
%ii=find(lat==0);lat(ii)=lat(ii-1);
ii=find(lon==0);lon(ii)=lon(ii-1);
ii=find(ir==0);ir(ii)=ir(ii-1);

st2=zeros(16,length(gpstim));

st2(1,:)=gpstim;
st2(2,:)=lat;
st2(3,:)=lon;
st2(4,:)=cog;
st2(5,:)=sog;
st2(6,:)=tsg;
st2(7,:)=tss;
st2(8,:)=rh;
st2(9,:)=ta;
st2(10,:)=solar;
st2(11,:)=imrain;
st2(12,:)=lrg;
st2(13,:)=odec;
st2(14,:)=imu;
st2(15,:)=imd;
st2(16,:)=ir;

st2=st2';

st2(:,4)=sog'.*cos(cog'*rdcon);    %convert to N
st2(:,5)=sog'.*sin(cog'*rdcon);    %convert to E
st2(:,14)=imu'.*cos(imd'*rdcon);   %convert to N
st2(:,15)=imu'.*sin(imd'*rdcon);   %convert to E
lrgc=cos(lrg'*rdcon);        %convert to N
lrgs=sin(lrg'*rdcon);        %convert to E

jd_ref=jd_pc;
jd_scs=gpstim./24+ddd;
st3=zeros(length(jd_ref),16);
lrgsm=zeros(1,length(jd_ref));
lrgcm=zeros(1,length(jd_ref));
if length(jd_scs(isnan(jd_scs)))==360
    jd_scs=jd_ref;
end    
for ll=1:16
%     st3(:,ll)=intavg(jd_scs,st2(:,ll),jd_ref); 
      st3(:,ll)=interp1(jd_scs(~isnan(jd_scs)),st2(~isnan(jd_scs),ll),jd_ref); 
end
% lrgcm=intavg(jd_scs,lrgc,jd_ref); 
% lrgsm=intavg(jd_scs,lrgs,jd_ref); 
lrgcm=interp1(jd_scs(~isnan(jd_scs)),lrgc(~isnan(jd_scs)),jd_ref); 
lrgsm=interp1(jd_scs(~isnan(jd_scs)),lrgs(~isnan(jd_scs)),jd_ref); 

if length(jd_ref)<60
    st3(length(jd_ref):60,:)=NaN;
    lrgcm(length(jd_ref):60)=NaN;
    lrgsm(length(jd_ref):60)=NaN;
end




gpstime=st3(:,1);     % you DON'T use the median of time.
latm=st3(:,2);%./100;
lonm=st3(:,3);%./100;
sogx=st3(:,4);
sogy=st3(:,5);
tsgm=st3(:,6);           %Tsea
tssm=st3(:,7);          %salinity
rhm=st3(:,8);           %relative humidity
tam=st3(:,9);           %Tair
solarm=st3(:,10);        %solar
rainm=st3(:,11);         %Rain
odecm=st3(:,13);         %ODEC
imux=st3(:,14);
imuy=st3(:,15);
irm=st3(:,16);

sogm=sqrt(sogx.*sogx+sogy.*sogy);                         %1-min sog
cogm=mod(atan2(sogy,sogx)/rdcon+360,360);
imum=sqrt(imux.*imux+imuy.*imuy);                         %1-min imet speed, m/s
imdm=mod(atan2(imuy,imux)/rdcon+360,360);
lrgm=mod(atan2(lrgsm,lrgcm)/rdcon+360,360);         %laser ring gyro


%%%% 1 min data %%%%%%
% jd_scs=jd_scs(llpos);
% sogm=ones(1,60)*NaN;				%1-min sog, m/s
% cogm=ones(1,60)*NaN; 
% imum=ones(1,60)*NaN; 			%1-min imet port speed, m/s
% imdm=ones(1,60)*NaN; 
% lrgm=ones(1,60)*NaN; 		    %POSMV heading (deg)
lat=latm;   %(deg)
lon=lonm;   %(deg)
% lat(1)=lat(2);
% lon(1)=lon(2);

tsg=tsgm;    %thermosalinograph T, C
tss=tssm;  %salinity, psu
imrh=rhm;     %rel hum,%  from IMET
imta=tam;      %air t, C   from IMET

imsol=solarm;    %solar flux, w/m^2
imrain=rainm;   %Rainfall Intensity	mm/h	Port VaisalaWXT520
odec=ones(1,60)*NaN;             %Surface Sound Velocity	m/s	Calculated from SSTMP and SSCND
imir=irm;         %imet ir, w/m^2




