%   Routine for generating an "eventlog" from SEABIRD files
%   A file called "ctdlog.dat" is generated to drop into the eventlog

%   Note the following hardcodes associated with a particular cruise
%   pth=input('Enter directory of data: ','s');
    pth='c:\gbs99\ctd\el9904\';
%   snum=input('Enter station number vector: ');
    snum=[1:36 38];
    prefix='del04';
    suffix='.cnv';
    
    mth=['Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'];
    
    ncasts=length(snum);
    begin=snum(1);
    stop=snum(ncasts);
    
%   for each station go into the file and get position (lat/lon)
    for jj=1:ncasts;
        if snum(jj)<10;numz='00';else;numz='0';end;
      id=fopen([pth,prefix,numz,num2str(snum(jj)),suffix],'r');
      
      % search for system time
      str1='System';
      mark=fgetl(id);
      [mark1,rem]=strtok(mark);
      [mark2,rem]=strtok(rem);
      while strcmp(str1,mark2)~=1;
         mark=fgetl(id);
         [mark1,rem]=strtok(mark);
         [mark2,rem]=strtok(rem);
      end
      
      % grab mth, day, yr, local
      for k=1:4;[mthc,rem]=strtok(rem);end;
      for m=1:12;
         if mthc==mth(m,:);
            mthn=m;
         end;
      end;
      [day,rem]=strtok(rem);
      [yr,rem]=strtok(rem);
      local=str2num(rem(2:3))*100+str2num(rem(5:6));
      day=str2num(day);
      
      % get posn
      mark=fgetl(id);
      [mark1,rem]=strtok(mark);
      [mark2,rem]=strtok(rem);
      [Lat,rem]=strtok(rem);
      [equals,rem]=strtok(rem);
      [degrees,rem]=strtok(rem);
      [dec_min,rem]=strtok(rem);
      latd=str2num(degrees)*100+str2num(dec_min);
      lat=str2num(degrees)+str2num(dec_min)/60;
       mark=fgetl(id);
      [mark1,rem]=strtok(mark);
      [mark2,rem]=strtok(rem);
      [Lon,rem]=strtok(rem);
      [equals,rem]=strtok(rem);
      [degrees,rem]=strtok(rem);
      [dec_min,rem]=strtok(rem);
      lond=str2num(degrees)*100+str2num(dec_min);
      lon=-1*str2num(degrees)+str2num(dec_min)/60;
      
      % get sta #
      for k=1:6;mark=fgetl(id);end;
      [mark1,rem]=strtok(mark);
      [sta,rem]=strtok(rem);
      sta=str2num(rem);
      
      % get cast depth
      str1='span';
      mark=fgetl(id);
      [mark1,rem]=strtok(mark);
      [mark2,rem]=strtok(rem);
      while strcmp(str1,mark2)~=1;
         mark=fgetl(id);
         [mark1,rem]=strtok(mark);
         [mark2,rem]=strtok(rem);
      end
      for k=1:4;[mark2,rem]=strtok(rem);end;
      castdep=str2num(mark2);
      
      % close input and write output
      fclose(id)
      evlog(jj,:)=[ snum(jj) sta 0 mthn day local 0 latd lond  0 castdep lon lat];
    end;
    eval(['save ',pth,'ctdlog.dat evlog -ascii']);


