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

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=[drive,'\DAY',dfx1,'\P6_',dfx];%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=[];ship_time=[];ship_wind=[];ship_dir=[];

while norman==1,
   headr=fgetl(flist);
   if headr==-1,norman=0;break;end;
   for qq=1:length(headr)-3,
      if length(headr)>41 & headr(qq:qq+3)=='003|',
         s_t=str2num(headr(qq+5:qq+6))+str2num(headr(qq+8:qq+9))/60+str2num(headr(qq+11:qq+12))/60/60;
         ship_time=[ship_time s_t];
      end;
      if length(headr)>52 & headr(qq:qq+3)=='009|',
         s_g=str2num(headr(qq+5:qq+9));
         ship_gyro=[ship_gyro s_g];
      end;
      if length(headr)>66 & headr(qq:qq+3)=='025|',
         if length(ship_Tsea) > 1 & isempty(str2num(headr(qq+5:qq+12)));
            s_T=ship_Tsea(length(ship_Tsea));
         else,
            s_T=str2num(headr(qq+5:qq+12));
         end;
         if length(s_T)>1, 
            s_T=s_T(1,1);
         end;
         ship_Tsea=[ship_Tsea s_T];
         if ~isempty(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 length(headr)>77 & headr(qq:qq+3)=='036|',
         s_c=str2num(headr(qq+5:qq+9));
         ship_cog=[ship_cog s_c];       
      end;
      if length(headr)>124 & headr(qq:qq+3)=='039|',
         s_s=str2num(headr(qq+5:qq+8));
         ship_sog=[ship_sog s_s];         
      end;
      if length(headr)>136 & headr(qq:qq+3)=='042|',
         if headr(qq+5)=='1' & headr(qq+6)~='.',
            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 length(headr)>160 & headr(qq:qq+3)=='045|',
         if length(headr)==161,
            s_h=str2num(headr(qq+5:qq+9));
         else,
            s_h=str2num(headr(qq+5:qq+10));
         end;   
         ship_hum=[ship_hum s_h];         
      end;
      if length(headr)>193 & headr(qq:qq+3)=='070|',
         if length(headr)==195 | length(headr)==196,
            s_w=str2num(headr(qq+5:qq+8));
         elseif length(headr)==194,
            s_w=str2num(headr(qq+5:qq+8));
         else,   
            s_w=str2num(headr(qq+5:qq+9));
         end;   
         ship_wind=[ship_wind s_w];         
      end;
      if length(headr)>202 & headr(qq:qq+3)=='071|',
         s_d=str2num(headr(qq+5:length(headr)-1));
         ship_dir=[ship_dir s_d];         
      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_time) < length(ship_gyro),
   ship_time=[ship_time ship_time(length(ship_time))];
end;
if length(ship_wind) < length(ship_gyro),
   ship_wind=[ship_wind ship_wind(length(ship_wind))];
end;
if length(ship_dir) < length(ship_gyro),
   ship_dir=[ship_dir ship_dir(length(ship_dir))];
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);

Mcog=atan2(mean(sin(ship_cog/180*pi)),mean(cos(ship_cog/180*pi)));

if Mcog<0,
   Mcog=Mcog+2*pi;
end;

clear ans s_d s_w s_s s_g s_h s_a s_T s_t s_c qq norman headr dfx1 dfx2 dfx
fclose(flist);