function [pos, dat, date]=rd_apex(fnam)

% program to read edf (profile) data files
% called from gen_apex_data 

% INPUTS
  % FNAM = file name to read in

% OUTPUTS
  % pos: position data
  % dat: data(p, T, S, O, F_ref, F_sig, LSS_ref, LSS_Sig, theta, sigma, O2sat, O2, chl, B_LSS, date)

% written by E. Boss, UMaine, 2004
   
fid=fopen(fnam,'r')

% skip nondata lines
junk=fscanf(fid,'%c',[6]);
while(strcmp(junk,'$  Fix')==0)
    line=fgetl(fid);
    junk=fscanf(fid,'%c',[6]);
end
%
i=1;
while(junk=='$  Fix')
    junk=fscanf(fid,'%c',[10]);
    junk=fscanf(fid,'%e',[3]);
    pos(i,1:3)=junk(1:3)';
    date1=fscanf(fid,'%s',[1]);
    mo=str2num(date1(1,1:2));
    da=str2num(date1(1,4:5));
    yr=str2num(date1(1,7:10));
    time1a=fscanf(fid,'%f',[1]); % break into hours and minutes
    time1b=time1a*3600; %convert hrs/min to seconds
    hr=fix(time1b/3600); %get number of hours
    time1b=time1b-(3600*hr); %remove the hours
    min=fix(time1b/60); %get number of minutes
    time1b=time1b-(60*min); %remove the minutes
    sec=time1b; %get seconds
    date(i,:)=datenum(yr,mo,da,hr,min,sec);
    i=i+1;
    line=fgetl(fid);
    junk=fscanf(fid,'%c',[6]);
end

% skip the format and data header lines
line=fgetl(fid);
line=fgetl(fid);

% get the data (14 fields) and assign -999 to * fields
i=1;
clear junk
while(feof(fid)==0);
    for j=1:14
        junk2=fscanf(fid,'%s',[1]);
        if junk2=='*'
             junk(j)=-999;
        else
             junk(j)=str2num(junk2);
        end
    end
    dat(i,:)=junk;
    i=i+1;
    line=fgetl(fid);
end
dat(:,15)=date(1,1);

fclose(fid);
