% routine to convert BI drifter "beacon.dat" file to matlab file

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

% initialize counter
i=0;

% read the file in a while-loop until fgetl error
m='xx';
while m~=-1;
  m=fgetl(fid);

  if length(m)>55;

    % fix the case of missing serial number (ie adjust string)
    if m(18:23)=='S/N 0 ';m=[m(1:21),m(5:6),m(23:length(m))];end;

    % fix the case of missing call number 
    if m(1:6)=='NOCALL';m(1:6)='WXNH20';end;


    if m(43:45)=='VAL';
      % now parse the string
      i=i+1;
      d(i,1)=str2num(m(5:6));    %buoy id
      d(i,2)=str2num(m(25:26));  %mth
      d(i,3)=str2num(m(28:29));  %day
      d(i,4)=str2num(m(31:32));  %yr
      d(i,5)=(str2num(m(34:35))*100+str2num(m(37:38)))/100;  %hrmn
      d(i,6)=str2num(m(47:48))*100+str2num(m(50:54));  %lat
      d(i,7)=str2num(m(59:60))*100+str2num(m(62:66));  %lon
    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 times desired
minyd=input('Minimum yearday (yrday0_gmt)?');
maxyd=input('Maximum yearday (yrday0_gmt)?');
id=find(dn(:,8)>=minyd&dn(:,8)<=maxyd&dn(:,4)==98);
dn=dn(id,:);

% rough plot
plot(dn(:,7),dn(:,6),'r.','markersize',20);

y=[dn(:,8) dn(:,7) dn(:,6)];
eval(['save d',num2str(idd),'a_beaconpc.dat  y -ascii']);





