% routine to convert Brightwater Instrument log file to 
% and ascii column formatted file

% April 1999 modifications to sort by time

%fn=input('input filename (w/out direc or extension): ','s');
fn='ba24_34';
%fn='beacona22';
%direc='/data5/jmanning/drift/al9805/';
direc='c:\gbs99\drift\';
disp(' ');

%wform=menu('which form of bi file','tom wilson log','jim churchil mat');
wform=1;
if wform==1;
  disp(['Hardcoded to look in ',direc,fn,'.edt'])
  inext='.edt';
  outext='.dat';
  fid=fopen([direc,fn,inext]);
% read lines one-by-one and decode
  i=0;

  for k=1:5000;
    d=fgetl(fid);

%   check to see if this is a decode-able line
    if d~=-1&length(d)>=3;
      if d(1)=='0'&d(3)~='/'&length(d)>=65;

%      check to see if this is a valid fix
        if d(30:32)=='VAL';
          i=i+1;
          mth(i)=str2num(d(8:9));
          day(i)=str2num(d(11:12));
          yr(i)=str2num(d(14:15));
          hr(i)=str2num(d(19:20));
          mn(i)=str2num(d(22:23));
          sec(i)=str2num(d(25:26));
          latd(i)=str2num(d(35:36));
          latm(i)=str2num(d(38:42));
          lond(i)=str2num(d(49:50));
          lonm(i)=str2num(d(52:56));
          if d(63)=='.';
            temp(i)=str2num(d(62:65));
          else;
            temp(i)=str2num(d(62:66));
          end;
        end;
      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.625;
  maxyd=120;
  ids=find(yd>=minyd&yd<=maxyd);
  mth=mth(ids);
  day=day(ids);
  hr=hr(ids);
  mn=mn(ids);
  sec=sec(ids);
  temp=temp(ids);
  x=x(ids);
  y=y(ids);
  yr=yr(ids);
  yd=yd(ids);
  
% sort by time
 %y=y(i);
 % yd=yd(i);
 % temp=temp(i);
 % mth=mth(i);
 % day=day(i);
 % hr=hr(i);
 % mn=mn(i);
 % sec=sec(i);


% fix time problem

% mth
  idm=find(diff(mth)~=0);
  for k=1:length(idm);
    mth(idm(k))=mth(idm(k)-1);
  end;
  idm=find(diff(mth)~=0);
  if idm~=[];disp('Warning: still problems with mth');end;

% day
  idm=find(diff(day)~=0);
  for k=1:length(idm);
    if day(idm(k))~=day(idm(k)-1)+1|day(idm(k)+1)~=day(idm(k));
      day(idm(k))=day(idm(k)-1);
    end;
  end;
  idm=find(diff(day)~=0);
  if idm~=[];
    disp(['Day changes: ',num2str(day(idm))]);
  end;

% hour
  idm=find(diff(hr)~=0);
  for k=1:length(idm);
    if idm(k)~=1;
      if hr(idm(k)-1)~=23;
        hr(idm(k))=hr(idm(k)-1);
      end;
    end;
  end;
  idm=find(diff(hr)~=0&diff(hr)~=1);
  idm2=find(hr(idm)~=23);
  if idm~=[]&idm2~=[];
    disp(['Hour changes (other than midnight): ',num2str(hr(idm(idm2)))]);
    disp(['Occurred on day: ',num2str(day(idm(idm2)))]);
  end;


%  now regenerate julian day
  clear yd;
  for i=1:length(mth);
    yd(i)=julian(mth(i),day(i),yr(i),hr(i)*100+mn(i));
  end;

yy=[yd; x; y; temp; mth; day; yr; hr*100+mn]';
 % atsee;
  pltdrft(y,x,1,0,yr(1),yd,-1);
 % axis([-69 -66 40 42]);

elseif wform==2;
  eval(['load ',direc,fn,'.mat -ascii']);
  eval(['d=',fn,';']);
% made a yearday from column 2

  for i=1:length(d);
    c=sprintf('%10f',d(i,2));
    yr=str2num(c(1:2));
    mth=str2num(c(3:4));
    day=str2num(c(5:6));
    hhmm=str2num(c(7:10));
    yd(i)=julian(mth,day,yr,hhmm);
  end;

  yy=[yd' d(:,4) d(:,3) d(:,5)]; 
end;

  figure(2);
  [u,v,spd,jdn]=ll2uv(yd,y,x);
  
  %remove unlikely points
  id=find(abs(u)<150&abs(v)<150);
  u=u(id);v=v(id);jdn=jdn(id);x=x(id);y=y(id);
  
%  plot(jdn,u,'r.',jdn,v,'g.','markersize',20);
 % plot(jdn,u,'r',jdn,v,'g','linewidth',1);
  hold on;
  plot(jdn,u,'r.',jdn,v,'g.','markersize',8);
  hold on;
  
  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);
 
  %running average velocities
  [uf,tf]=runave(jdn',u',6,0,0,0);
  [vf,tf]=runave(jdn',v',6,0,0,0);
  plot(jdn,uf,'r',jdn,vf,'g','linewidth',1);

  % save to a file
 fploopy([jdn(idd);x(idd(1:length(idd)));y(idd(1:length(idd)));...
         u(idd);v(idd)]',['c:\gbs99\drift\d34_',num2str(day(idd(length(idd)))),'.dft'],'o');

% throwout any time problems
%idgood=find(diff(yy(:,1))>0);
%yyy=yy(idgood,:);


% decided not to save a ".dat" file
%eval(['save ',direc,fn,outext,' yyy -ascii']);


% save file for JGOFS
%yyy=yy(idgood,1:4);
%eval(['save ',direc,fn,'.jgf yyy -ascii']);

%disp('Add a header line as head.dat');
%eval(['!joe ',direc,fn,'.jgf']);

% Now, make final plot with good data
