disp('openship_csp.m')
%********************************************************

lv=7;                   %GPS data file P7_?????.dat
disp('Loading GPS file')
dfl=['d:\P',num2str(lv),'_',dfx '.dat'];  % no '.DAT' for day 268
disp(dfl)

flist=fopen(dfl,'r');
headr=fgetl(flist);
lat=(1:3600).*0;
long=(1:3600).*0;
disp(headr)
jj=0;
while 1
   z=fgetl(flist);
   if z==-1;
      break;
   end;
   if length(z)>20,
      if z(14)==',';
         if z(15:20)=='$GPGLL' & length(z) > 38,
            jj=jj+1;
            lat(jj)=str2num(z(22:28))/100;
            long(jj)=str2num(z(32:39))/100;
            if z(30)=='S',
               lat(jj)=-lat(jj);
            end;
            if z(41)=='E',
               long(jj)=-long(jj);
            end;
         end;
      else,
         if z(16:21)=='$GPGLL' & length(z) > 41,
            jj=jj+1;
            lat(jj)=str2num(z(23:29))/100;
            long(jj)=str2num(z(33:40))/100;
            if z(31)=='S',
               lat(jj)=-lat(jj);
            end;
            if z(42)=='E',
               long(jj)=-long(jj);
            end;
         end;
      end;   
   end;
end;
lat=lat(1:jj);lat=lat';
long=long(1:jj);long=long';


lv=6;                   %Ship data file P6_?????.dat
disp('Loading ship data file')
dfl=['d:\P',num2str(lv),'_',dfx '.dat'];  % no '.DAT' for day 268
disp(dfl)

flist=fopen(dfl,'r');
headr=fgetl(flist);
disp(headr)
HEAD=[];TT=[];COG=[];SOG=[];TSEA=[];YTAIR=[];YRH=[];
head=[];ttt=[];cog=[];sog=[];tsea=[];ytair=[];yrh=[];

while 1
   z=fgetl(flist);
   if z==-1;
      break;
   end;
   if length(z)>42,
      if z(13)==',';
         if z(17:19)=='MAR' | z(17:19)=='APR',
            tt1=str2num(z(1:9));
            tt2=str2num(z(11:12))/10000;
            ttt=tt1+tt2;
            if length(z)>96,
               head=str2num(z(38:41))/10;
               cog=str2num(z(43:47));
               sog=str2num(z(49:53));
               tsea=str2num(z(78:83));
               ytair=str2num(z(88:91));
               yrh=str2num(z(93:97));
            end;
            if head==[] | ttt==[] | cog==[] | sog==[] | tsea==[] | ytair==[] | yrh==[],
            else;
               TT=[TT ttt];
               HEAD=[HEAD head];
               COG=[COG cog];
               SOG=[SOG sog];
               TSEA=[TSEA tsea];
               YTAIR=[YTAIR ytair];
               YRH=[YRH yrh];
            end;
         end;
      elseif z(14)==',';
         if z(18:20)=='MAR' | z(18:20)=='APR',
            tt1=str2num(z(1:9));
            tt2=str2num(z(11:13))/10000;
            ttt=tt1+tt2;
            if length(z)>97,
               head=str2num(z(39:42))/10;
               cog=str2num(z(44:48));
               sog=str2num(z(50:54));
               tsea=str2num(z(79:84));
               ytair=str2num(z(89:92));
               yrh=str2num(z(94:98));
            end;
            if head==[] | ttt==[] | cog==[] | sog==[] | tsea==[] | ytair==[] | yrh==[],
            else;
               TT=[TT ttt];
               HEAD=[HEAD head];
               COG=[COG cog];
               SOG=[SOG sog];
               TSEA=[TSEA tsea];
               YTAIR=[YTAIR ytair];
               YRH=[YRH yrh];
            end;
         end;
      else;
         if z(19:21)=='MAR' | z(19:21)=='APR',
            tt1=str2num(z(1:9));
            tt2=str2num(z(11:14))/10000;
            ttt=tt1+tt2;
            if length(z)>98,
               head=str2num(z(40:43))/10;
               cog=str2num(z(45:49));
               sog=str2num(z(51:55));
               tsea=str2num(z(80:85));
               ytair=str2num(z(90:93));
               yrh=str2num(z(95:99));
            end;
            if head==[] | ttt==[] | cog==[] | sog==[] | tsea==[] | ytair==[] | yrh==[],
            else;
               TT=[TT ttt];
               HEAD=[HEAD head];
               COG=[COG cog];
               SOG=[SOG sog];
               TSEA=[TSEA tsea];
               YTAIR=[YTAIR ytair];
               YRH=[YRH yrh];
            end;
         end;
      end;   
   end;
end;
if TT~=[],
   TT=TT-TT(1);
   TT=TT';
end;
head=HEAD';clear HEAD;
cog=COG';clear COG;
sog=SOG';clear SOG;
Tsea=TSEA';clear TSEA tsea;
yTair=YTAIR';clear YTAIR ytair;
yRH=YRH';clear YRH yrh;

clear z;
