% routine to read "batchlis" format & write ".mtl"
%
fid=fopen('bl2ml.ctl');
fn=fgetl(fid);
exp=fn(length(fn)-5:length(fn)-3);
k=0;
while fn~=-1;
  k=k+1;

% read start & stop
  fid2=fopen([fn,'.bls']);
  li=fgetl(fid2);
  yy=str2num(li(35:36));
  mth=rom2reg(li(38:41));
  day=str2num(li(43:44));
  hr=str2num(li(47:48));
  tmin=str2num(li(50:51));
  yd1=julian(mth,day,yy,hr*100+tmin);
  li=fgetl(fid2);
  yy=str2num(li(35:36));
  mth=rom2reg(li(38:41));
  day=str2num(li(43:44));
  hr=str2num(li(47:48));
  tmin=str2num(li(50:51));
  yd2=julian(mth,day,yy,hr*100+tmin);

% read lat&lon
  fgetl(fid2);
  li=fgetl(fid2);
  la=str2num(li(10:11))*100+str2num(li(13:17));  
  li=fgetl(fid2);
  lo=str2num(li(10:11))*100+str2num(li(13:17));  
  li=fgetl(fid2);
  depthw=str2num(li(17:20));

% skip 17 lines
  for i=1:17;fgetl(fid2);end;

% read data
  d=fscanf(fid2,'%f',inf);
  d=reshape(d,8,length(d)/8);
  d=d';

% form yrday0_gmt vector
  yy=floor(d(:,1)/10000);
  mth=floor((d(:,1)-yy*10000)/100);
  day=d(:,1)-yy*10000-mth*100;
  hr=floor(d(:,2)/10000);
  mn=floor((d(:,2)-hr*10000)/100);
  sec=d(:,2)-hr*10000-mn*100;
  t=julian(mth,day,yy,hr*100+mn)+sec/60/60/24;

% output results to a mat file
  y=d(:,3:8);
  eval(['save ',fn,' t y']);

% save to master file with min max range
  id=find(t>=223&t<=265.5);
  if k==1;
    yd=t(id);ym=y(id,:);
  else;
    ym=[ym y(id,:)];
  end; 
  

% get another filename from the .ctl
  fn=fgetl(fid);
end;

% save master mat file
y=ym;
t=yd;
eval(['save ',exp,' t y']);
