% matlab to convert argos files to ascii files & plot fixes by drifter id
k=0;

% Open todays file based on the current date in YYMMDD format
pren='a';
ptt='393';
mth='a';
da=date;
if da(2)=='-';
  da=['0',da];
end;
mths=['Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'];
for i=1:12;
 if mths(i,1:3)==da(4:6);
   id=i;
 end;
end;
sid=num2str(id);
if id<10;sid=['0',sid];end;
filename=['c:\gbs99\drift\argos\',pren,ptt,mth,da(1:2),'.dat'];
fid=fopen(filename);
while 1
  line = fgetl(fid);
  if ~isstr(line), break, end;
  if length(line)>54 & line(1)=='0';
    k=k+1;
    id(k)=str2num(line(7:11));
    idy=find(line=='-');
    ss=idy(2);
    yr(k)=str2num(line(ss-7:ss-4));
    mth(k)=str2num(line(ss-2:ss-1));
    day(k)=str2num(line(ss+1:ss+2));
    hrmn(k)=str2num(line(ss+4:ss+5))*100+str2num(line(ss+7:ss+8));
    lat(k)=str2num(line(ss+13:ss+18));
    lon(k)=str2num(line(ss+20:ss+26))-360;
    jd(k)=julian(mth(k),day(k),yr(k)-1900,hrmn(k));
  end;
end;

% plot each drifter a different color
[d,nr,cc]=unique(id');
for i=1:length(d);
  idd=find(id==d(i));
  ii=rem(i,7);
%  keyboard
  pltdrft(lat(idd),lon(idd),ii,0,yr(idd(1))-1900,jd(idd));
  cid=num2str(id(idd(1)),5);
% post the last 3 digits of PTT  
  text(lon(idd(1)),lat(idd(1)),cid(length(cid)-2:length(cid)),...
    'verticalalignment','bottom','fontsize',6);
end;
  str=[day(1), mth(1), yr(1)];
  title([num2str(day(1)),'-',num2str(mth(1)),'-',num2str(yr(1))]);

georbat;
axis([-71 -65 40 43]);
lllabel;

% save ascii results
y=[jd; lon; lat; id]';
fploopy(y,['c:\gbs99\drift\argos\',pren,ptt,mth,da(1:2),'.cnv'],'a');



  