function [t,awvl,cwvl,adata,cdata]=read_ac_data(filename)
% Reads ANY AC data regardless of instrument type
% input filepath
% output
% t = time vector
% awvl = a wave vector
% cwvl = c wave vector
% adata = a data array
% cdata = c data array
% tempc = temperature (requires temperature to be encoded in filename as
% ##p# (this feature may not work 100% of the time)
% Written by ENA 07122013

acdata=importdata(filename);
actype=acdata.textdata{2}(3);
acserial=str2num(acdata.textdata{3}(1:8));
% kloc=find(ismemeber(filename,'p'));
% if not(isempty(kloc))
%     kwhole=str2num(filename(kloc-2:kloc-1));
%     kdec=str2num(filename(kloc+1));
%     if not(or(isempty(kwhole),isempty(kdec)))
%         tempc=kwhole+(kdec./10);
%     else
%         warning('id:notempnearp','no temperature found in filename near p. this may not be your fault. skipping.')
%         tempc=[];
%     end
% else
%     warning('id:notemp','no temperature found in filename. skipping.')
%     tempc=[];
% end
if strcmp(actype,'9')
    disp('found ac9 data. extracting.')
    awlines=[17 18 19 23 24 25 11 12 13];
    cwlines=[26 27 28 14 15 16 20 21 22];
    awcols=awlines-9;
    cwcols=cwlines-9;
    adata=zeros(size(acdata.data,1),9);
    cdata=adata;
    awvl=zeros(1,9);
    cwvl=awvl;
    for i=1:9
        awvl(i)=str2double(acdata.textdata{awlines(i)}(2:4));
        cwvl(i)=str2double(acdata.textdata{cwlines(i)}(2:4));
        adata(:,i)=(acdata.data(:,awcols(i)));
        cdata(:,i)=(acdata.data(:,cwcols(i)));
    end
    t=acdata.data(:,1);
elseif    strcmp(actype,'S')
    disp('found acs data. extracting.')
    allwaves=textscan(acdata.textdata{end},'%s');
    allwave=allwaves{1};
    bands=length(allwave)/2;
    adata=acdata.data(:,bands+2:(2*bands)+1);
    cdata=acdata.data(:,2:bands+1);
    awvl=zeros(1,bands);
    cwvl=awvl;
    for i=1:bands
        awvl(i)=str2double(allwave{i+bands}(2:end));
        cwvl(i)=str2double(allwave{i}(2:end));
    end
    t=acdata.data(:,1);
else
    error('id:unidentified','unexpected datafile format.  cannot identify ac meter type.')
end