% routine to convert BI drifter gonio "winwedge.log" file to matlab file

direc='/data5/jmanning/drift/al9805/';
fn='wwlog8lt.log';
fid=fopen([direc,fn]);

% initialize counter
i=0;
d=ones(500,4);

% read the file in a while-loop until fgetl error
a='xx';
  i=1;
while a~=-1;
  a=fgetl(fid);
   a=[' ',a];
  % check for good data
  % ship buoy specified

  ship='24';
  if length(a)>37;
    if a(5)~='/'&floor(hex2dec(a(14:15))/64)==1&hex2dec(a(16:17))<64&length(a)>37;
      i=i+1;  
%     identify buoy
      disp(' ');
      if a(12)=='1';disp('200 Buoy');did=200;colval='g';
      elseif a(12)=='E';disp('6 Buoy');did=36;colval='m';
      elseif a(11:12)=='E2';disp('20 Buoy  ');did=20;colval='g';
      elseif a(12)=='A';disp('234 Buoy');did=234;colval='m';
      elseif a(11:12)=='24';disp('Deck Buoy 34');did=34;colval='y';
      elseif a(12)=='7';disp('PillBox Buoy  ');did=57;colval='c';
      elseif a(11:12)=='DF';  disp('Buoy 1');did=21;colval='b';
      else;did=99;colval='r';
      end;

%     parse time, lat, lon, and temperature
      time=hex2dec(a(20:27));
      stevetime=((time-86400*floor(time/86400))/3600);
        minit=round((stevetime-fix(stevetime))*60);
        hour=fix(stevetime);
      latm1=(hex2dec(a(28:29))-192)*65535+hex2dec(a(30:31))*256+...
        hex2dec(a(32:33))-540001;
      latd=floor(latm1/6000);
      latm=(latm1-latd*6000)/100;
      lat=latd+latm/60;
      lonm=(hex2dec(a(34:35))-192)*65535+hex2dec(a(36:37))*256+...
          hex2dec(a(38:39))-1080001;
      lond=ceil(lonm/6000);
      lonm=(lonm-lond*6000)/100;
      lon=lond+lonm/60;
      if length(a)>=43;
       tmpc=(hex2dec(a(40:41))*256+hex2dec(a(42:43))-49152)/100;
      end;

%       plot
        plot(lon,lat,'.','color',colval,'markersize',20)

        hold on;
%     with each good data line add to "d" matrix
      d(i,1)=did;
      d(i,2)=(hour*100+minit)/2400;
      d(i,3)=lon;
      d(i,4)=lat;
      d(i,5)=tmpc;
    end;

 
  end;

end;

% calulcate yrday
%d(:,8)=julian(d(:,2),d(:,3),d(:,4),d(:,5)*100);

% convert lat/lon to dec deg
%[d(:,6),d(:,7)]=ddmm2dec(d(:,6),d(:,7));

% get file for particulat drifter
idd=input('What id do you want?');
id=find(d(:,1)==idd);
dn=d(id,:);

% get acceptable positions
minyd=input('Minimum lon?');
maxyd=input('Maximum lon?');
minla=input('Minimum lat?');
maxla=input('Maximum lat?');
id=find(dn(:,3)>=minyd&dn(:,3)<=maxyd&dn(:,4)>=minla&dn(:,4)<=maxla);
dn=dn(id,:);

% rough plot
plot(dn(:,3),dn(:,4),'r.','markersize',10);


% generate a yearday given start
start=124.0;
yd(1)=dn(1,2)+start;
for k=2:length(dn);
  if (dn(k,2)-dn(k-1,2))<(-.8);start=start+1;end;
  yd(k)=start+dn(k,2);
end;
disp('sorting time');
[syd,isd]=sort(yd);
y=[syd' dn(isd,3) dn(isd,4) dn(isd,5)];
eval(['save d',num2str(idd),'a_wwlt.dat  y -ascii']);




