% routine to convert 104arg43 output to matlab
ik=0;
for ik=1:1;
   %by=['200';'201';'234';'089';'393'];
   by=['093'];
  drogd=[35;15;15;35;13];
  %for ik=1:1;
  %   by=['200'];
  buoy=by(ik,:);
  filn=['c:\gbs99\drift\argos\a',buoy,'a20b.dat'];
  fid=fopen(filn);
  d='this';
  i=0;
  while d~=-1;
    d=fgetl(fid);
    if d~=-1;
      % read until you get good data
      if d(4)=='/'&d(24:26)=='VAL';
         i=i+1;
          mth(i)=str2num(d(2:3));
          day(i)=str2num(d(5:6));
          yr(i)=str2num(d(8:9));
          hr(i)=str2num(d(13:14));
          mn(i)=str2num(d(16:17));
          sec(i)=str2num(d(19:20));
          latd(i)=str2num(d(29:30));
          latm(i)=str2num(d(32:36));
          lond(i)=str2num(d(42:44));
          lonm(i)=str2num(d(46:50));
          temp(i)=str2num(d(46:50));
       end;
    end;   
  end;  
       
       %  now generate julian day
  for i=1:length(mth);
         yd(i)=julian(mth(i),day(i),yr(i),hr(i)*100+mn(i));
  end;


  %   generate decimal degree positions
  [y,x]=ddmm2dec(latd*100+latm,lond*100+lonm);
 
  minyd=input('Enter minimum yearday to save: ');
  %maxyd=input('Enter maximum yearday to save: ');
  % minyd=107.5;
  maxyd=120.5;
  ids=find(yd>=minyd&yd<=maxyd);
  mth=mth(ids);
  day=day(ids);
  hr=hr(ids);
  mn=mn(ids);
  sec=sec(ids);
  x=x(ids);
  y=y(ids);
  yd=yd(ids);
 
  % sort by time
  [syd,i]=unique(yd);
  x=x(i);
  y=y(i);
  yd=yd(i);
  temp=temp(i);
  mth=mth(i);
  day=day(i);
  hr=hr(i);
  mn=mn(i);
  sec=sec(i);

  figure(1);
  clf;
  %  pltdrft(y,x,1,14400,yr(1),yd,-1);
  %try plotting segments without large dropouts?
  idb=find(diff(yd)>2/24);
  if isempty(idb);idb=length(x);end;
  pltdrft(y(1:idb(1)),x(1:idb(1)),ik,14400,yr(1),yd(1:idb(1)),-1);
  for mm=2:length(idb)-1;
     pltdrft(y(idb(mm)+1:idb(mm+1)),x(idb(mm)+1:idb(mm+1)),...
        ik+1,0,yr(1),yd(idb(mm)+1:idb(mm+1)),-1);
  end;
 
  %pltdrft(y,x,3,-14400,yr(1),yd,-1);
  
  a=axis;
  axis(a);
  georbat;
  %lllabel(1,2,a(3),a(4),a(1),a(2))
  title(['Buoy# ',buoy,' as of ',...
        num2str(mth(length(mth))),'/',num2str(day(length(mth))),...
        ' @',num2str(hr(length(hr))),num2str(mn(length(mn))),'GMT']);
  disp('Press return to continue')
  pause;
  
  % velocity
  figure(2);
  [u,v,spd,jdn]=ll2uv(yd,y,x);
  plot(jdn,u,'r.',jdn,v,'g.','markersize',20);
  legend('u','v');
  title('Velocities');
  %  set(gca,'xtick',[minyd:1:109]);
  %  set(gca,'xticklabels',['107';'108';'109']); 
  %  yd2calab(yr(1));
  idd=find(isnan(u)~=1&abs(u)~=Inf);

  % save to a file
  fploopy([jdn(idd);x(idd(1:length(idd)));y(idd(1:length(idd)));...
       u(idd);v(idd)]',['d',buoy,'_',num2str(day(idd(length(idd)))),'.dft'],'o');
  disp('Run dft2m3d');
  
  figure(3)
  if ik==1;atsee;end;
  %try plotting segments without large dropouts?
  idb=find(diff(yd)>2/24);
  if isempty(idb);idb=length(x);end;
  pltdrft(y(1:idb(1)),x(1:idb(1)),ik,0,yr(1),yd(1:idb(1)),-1);
  for mm=2:length(idb)-1;
     pltdrft(y(idb(mm)+1:idb(mm+1)),x(idb(mm)+1:idb(mm+1)),...
        ik+6,0,yr(1),yd(idb(mm)+1:idb(mm+1)),-1);
  end;
  % pltdrft(y,x,ik,0,yr(1),yd,-1);
  hold on;
  fclose(fid)
end;

%fixup figure 3
  a=axis;
  axis(a);
  %georbat;
  lllabel(4,6,a(3),a(4),a(1),a(2))
  title(['All Drifters as of ',...
        num2str(mth(length(mth))),'/',num2str(day(length(mth))),...
        ' @',num2str(hr(length(mn))),num2str(hr(length(mn))),'GMT']);
  %legend(['60m';by],4)
