% routine to convert minlog  files to .mat files and , in "phase 2" plot  them
ph=menu('what phase?','convert data to mat form','plot results w/other records');
%ph=2;
if ph==1;
dirf=input('Enter directory where file is stored excluding last slash: ','s');
%dirf='/data5/jmanning/minilog'
fn=input('Enter filename: ','s');
%fn='mpalumbo.96b'


eval(['fid=fopen(''',dirf,'/',fn,''');']);

disp('Note: This takes a few minutes.');

% intialize number of points
m=0;

% skip a standard header but pick up notable information
fgetl(fid);
lin=fgetl(fid);
sernum=str2num(lin(17:20));
fgetl(fid);
lin=fgetl(fid);
byr=str2num(lin(14:15));
bmth=str2num(lin(17:18));
bday=str2num(lin(20:21));
bhr=str2num(lin(23:24));
bmn=str2num(lin(26:27));
lin=fgetl(fid);
eyr=str2num(lin(15:16));
emth=str2num(lin(18:19));
eday=str2num(lin(21:22));
ehr=str2num(lin(24:25));
emn=str2num(lin(27:28));

% calculate start & stop yeardays
byd=julian(bmth,bday,byr,bhr*100+bmn);
eyd=julian(emth,eday,eyr,ehr*100+emn);

% read and calulate sample period (assumed in minutes)
lin=fgetl(fid);
sp=str2num(lin(20:21));
if sp==0;sp=str2num(lin(17:18))*60;end;

% calculate the number of samples to expect
spyd=(sp/60)/24;
if eyd<byd;
  ndfy=julian(12,31,byr,2359);
  nsam=(ndfy-byd+eyd)/spyd;
else;
  nsam=(eyd-byd)/spyd;
end;

if sernum>268&sernum<570;

% fix this value in the case of correction
% due to the 1:28 addition every day as in sernum 393
  disp(' ');
  disp('         Warning: made a correction on number of samples')
  minus=floor(eyd-byd)*16/(sp*60);
  nsam=nsam-minus;
  disp(['        lost: ',num2str(minus),' samples to do minilog problem']);
end;

fgetl(fid);

for k=1:nsam;

  lin=fgetl(fid);
  yr=str2num(lin(1:2));
  mth=str2num(lin(4:5));
  day=str2num(lin(7:8));
  hr=str2num(lin(10:11));
  mn=str2num(lin(13:14));
  yd(k)=julian(mth,day,yr,hr*100+mn);

% grab the temperature after testing for <10 degrees or not
  test=lin(20);
  if test=='.'
    temp(k)=str2num(lin(19:21));
  else;
    temp(k)=str2num(lin(19:22));
  end;

end;
  
% fix yd vector if it strattled the new year
if eyr>byr;
  idd=find(diff(yd)<0);
  yd(idd+1:length(yd))=yd(idd+1:length(yd))+yd(idd);
end;

%check with plot
plot(yd,temp,'r','linewidth',2);

% fix axis
ylabel('Temperature (degC)')
title(fn)
mint=input('Minumum yearday?');
maxt=input('Maxumum yearday?');
axis([mint maxt min(temp) max(temp)]);
%yd2calab(byr);

%make depth
depth=input('What depth of deployment?');

% save matrix as a mat file
eval(['save /data5/jmanning/minilog/',fn(1:length(fn)-4),' yd temp sernum depth']);

add=menu('Biology?','yes','no');
if add==1;
  load /home3/ocn/jmanning/lob/mpalumbo.dat
  m=mpalumbo;
  myd=julian(m(:,1),m(:,2),97*ones(length(m),1),1200*ones(length(m),1));
  figure(2)
  [ax,pl,ylab]=plotyy(yd,temp,...
   myd(2:length(m)),m(2:length(m),4)./diff(myd),...
   'Lobsters(lbs/day)',...
   [0 0 0 99 258 5 25 0 35]);
  ylabel('Temperature (degC)')
  hold on;
  get(ax(1));
  plot(myd,(m(:,3)-32)*(5/9),'g--');
end;

end; % end of phase 1 conversion





% plot results
if ph==2;
  leg=[''];

  % add drifter temps first
  hmd=input('How many drifter temps to plot?');
  for i=1:hmd;
    % see "readdjgf.m" for source of code
    fn=input('filename (w/out .jgf extension)?','s');
    fid=fopen(['/data5/jmanning/drift/al9805/',fn,'.jgf']);
    fgetl(fid);
    dd=fscanf(fid,'%f');
    d=reshape(dd,4,length(dd)/4);
    d=d';
    idg=find(d(:,4)<30);
    plot(d(idg,1),d(idg,4),['c',num2str(i+1)],'linewidth',i);
    hold on;
    leg=[leg;' 1m'];
  end;

  hmt=input('How many tpods to plot?');
  for i=1:hmt;
    fn=input('filename (w/out .mat extension)?','s');
    eval(['load /data5/jmanning/minilog/',fn]);
    if i==1;
      disp('Note: if you want deck comparison ask for deck time.')
      mint=input('Minumum yearday?');
      maxt=input('Maxumum yearday?');
    end;
    id=find(yd>mint&yd<maxt);
    wa=1;    
    disp(['Hardcoded to NOT window average every ',num2str(wa),' points...']);
%    [tempf]=runave(yd(id)',temp(id)',wa,[0],[0],[0]); %smooths resolution problem

    plot(yd(id),temp(id),['c',num2str(hmd+i+1)],'linewidth',i+hmd);
    hold on;
    leg=[leg; [num2str(depth),'m']];
  end;
  disp('WARNING:  axis([124.775 126.75 6.7 8.5])')
  axis([124.775 126.85 6.7 8.5])
  yd2calab(str2num(fn(2:3)));
  legend(leg);
  ylabel('Temperature (degC)');
end;
    
