%disp('openship.m')
%disp('Open ship info file')
% Inputs data from ASCII data files
%  Note:  hhh=hour

fclose('all'); 
if hhh<10,
   dfx2=['0',num2str(hhh)];
else,
   dfx2=num2str(hhh);
end;

if ddd<10,
   dfx1=['00',num2str(ddd)];
elseif ddd<100,
   dfx1=['0',num2str(ddd)];
else,
   dfx1=num2str(ddd);
end;

dfx=[dfx1 dfx2];
%dfl=['/home/user1/bin/data/P6_',dfx];%disp(dfl)
%dfl=['/scratch2/day',dfx1,'/P6_',dfx];%disp(dfl)
%dfl=['/home/user1/bin/data_backup/day',dfx1,'/P6_',dfx];%disp(dfl)
%dfl=['/home/jeh/fastex/P6_',dfx,';1'];%disp(dfl)
dfl=['d:\hp_data\P6_',dfx];
norman=1;
jax=1;
st1=zeros(17,1800);
st2=zeros(17,1800);
flist=fopen(dfl,'r');
headr=fgetl(flist);%disp(headr);
ship_gyro=[];ship_Tsea=[];ship_cog=[];ship_sog=[];ship_Tair=[];ship_hum=[];gpstm=[];tscs=[];
if flist>0,   %if the file exists, 
      %dmp=fgets(flist);%read header lines and skip blanks
      %dmp2=fgets(flist);%read header lines and skip blanks
		
      while feof(flist)==0,%go until you run out of 10-min sections
         st1=fscanf(flist,'%g,%g,%g,%g%*c,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,',[17,inf]);
                  %         1  2  3  4     5     6  7  8  9  0  1  2  3  4  5  6  7     
         [nr1,nl1]=size(st1);      
			if nr1==17
      		st2(:,jax:jax+nl1-1)=st1; 
      		jax=jax+nl1;
			else
   			x=fgets(flist);
			end;%end if             
       end;%end while
    end;%end flist
    
    %**********  decode gps time
    
hh=floor(st2(3,:)/10000);
mm=floor((st2(3,:)-hh*10000)/100);
ss=st2(3,:)-hh*10000-mm*100;
gpsx=mm*60+ss;
gpst=gpsx-gpsx(1);%time in seconds from start of record
tscs=st2(1,:)+st2(2,:)/10000-st2(1,1)-st2(2,1)/10000;%hp time in seconds

%***********  find spikes in time series
[a,np]=size(st2);
%uz=zeros(np,2);
%on=1:1:np;
%tt=0:2:2*(np-1);
%uz(1:np,1)=gpst'-tt';
%gpstm=despik_js(uz);
ii=find(abs(gpst-tscs)<5 & abs(st2(7,:))<20);

ship_cog=st2(6,ii);
ship_sog=st2(7,ii);
ship_gyro=st2(14,ii);
ship_hum=st2(10,ii);
ship_Tair=st2(11,ii);
ship_Tsea=st2(8,ii);
ship_tm=tscs(ii);
ship_odec=st2(15,ii);


%figure;plot(ship_Tair);title(['JD',dfx1,' Hr',dfx2,' UTC  Tair Ship']);ylabel('deg');xlabel('time (15 sec interval)');
%figure;plot(ship_Tsea);title(['JD',dfx1,' Hr',dfx2,' UTC  Tsea Ship']);ylabel('deg');xlabel('time (15 sec interval)');
%figure;plot(ship_hum);title(['JD',dfx1,' Hr',dfx2,' UTC  RH% Ship']);ylabel('Percent Relative Humidity');xlabel('time (15 sec interval)');
%figure;plot(ship_sog);title(['JD',dfx1,' Hr',dfx2,' UTC  SOG Ship']);ylabel('speed in knots');xlabel('time (15 sec interval)');
%figure;plot(ship_cog);title(['JD',dfx1,' Hr',dfx2,' UTC  COG Ship']);ylabel('course over ground');xlabel('time (15 sec interval)');
%figure;plot(ship_gyro);title(['JD',dfx1,' Hr',dfx2,' UTC  Gyro Ship']);ylabel('heading');xlabel('time (15 sec interval)');
Msog=mean(ship_sog);
Scog=sin(ship_cog/180*pi);Ccog=cos(ship_cog/180*pi);Mcog=atan2(mean(Ccog),mean(Scog));clear Ccog Scog;
if Mcog < 0, Mcog=Mcog+2*pi;end;
clear ans s_s s_g s_h s_a s_T s_c qq norman headr flist dfx1 dfx2 dfl dfx