%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)
norman=1;
flist=fopen(dfl,'r');
headr=fgetl(flist);%disp(headr);
ship_gyro=[];ship_Tsea=[];ship_cog=[];ship_sog=[];ship_Tair=[];ship_hum=[];

while norman==1,
   headr=fgetl(flist);
   if headr==-1,norman=0;break;end;
   for qq=1:length(headr)-12,
      if headr(qq:qq+3)=='009|',
         s_g=str2num(headr(qq+5:qq+9));
         ship_gyro=[ship_gyro s_g];
      end;
      if headr(qq:qq+3)=='025|',
         if length(ship_Tsea) > 1 & str2num(headr(qq+5:qq+12)) == [];
            s_T=ship_Tsea(length(ship_Tsea));
         else,
            s_T=str2num(headr(qq+5:qq+12));
         end;
         ship_Tsea=[ship_Tsea s_T];
         if ship_Tsea ~= [] & length(ship_Tsea) > 1 
            if ship_Tsea(length(ship_Tsea)) > 20,
               ship_Tsea(length(ship_Tsea)) = ship_Tsea(length(ship_Tsea)-1);
            end;            
         end;
      end;
      if headr(qq:qq+3)=='036|',
         s_c=str2num(headr(qq+5:qq+9));
         ship_cog=[ship_cog s_c];       
      end;
      if headr(qq:qq+3)=='039|',
         s_s=str2num(headr(qq+5:qq+8));
         ship_sog=[ship_sog s_s];         
      end;
      if headr(qq:qq+3)=='042|',
         if str2num(headr(qq+5))==1,
            s_a=str2num(headr(qq+5:qq+10));
         else,
            s_a=str2num(headr(qq+5:qq+9));
         end;   
         ship_Tair=[ship_Tair s_a];         
      end;
      if headr(qq:qq+3)=='045|',
         s_h=str2num(headr(qq+5:qq+10));
         ship_hum=[ship_hum s_h];         
      end;
   end;
end;
if length(ship_hum) < length(ship_gyro),
   ship_hum=[ship_hum ship_hum(length(ship_hum))];
end;
if length(ship_Tair) < length(ship_gyro),
   ship_Tair=[ship_Tair ship_Tair(length(ship_Tair))];
end;
if length(ship_sog) < length(ship_gyro),
   ship_sog=[ship_sog ship_sog(length(ship_sog))];
end;
if length(ship_cog) < length(ship_gyro),
   ship_cog=[ship_cog ship_cog(length(ship_cog))];
end;
if length(ship_Tsea) < length(ship_gyro),
   ship_Tsea=[ship_Tsea ship_Tsea(length(ship_Tsea))];
end;
if length(ship_hum) < length(ship_gyro),
   ship_hum=[ship_hum ship_hum(length(ship_hum))];
end;
ship_sog=ship_sog./1.94;            % convert to m/s
%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