function [filename, t, T, Tcal, wla, wlc, a_data, c_data]=read_acs_data
%read and plot time series of the ac data
%Manually select a acs-meter file you want to read
%Outputs:
%   t: time vector, sec
%   Tcal: factory calibration temperature, oC
%   T: temperature of sample, oC
%   wla, wlc: wavelength vectors for absorption and attenuation, respectively
%   a_data, c_data: absorption and attenuation data
%
%Written by Linhai Li, July 9, 2013

[filename, path]=uigetfile({'*.dat'}, 'Select a ACS file', 'MultiSelect', 'off');

%Get the factory calibration file
fac_cal_file=dir(fullfile(path, '*.dev'));
fac_fid=fopen(fullfile(path, fac_cal_file.name));
for i=1:3
    junk=fgetl(fac_fid);
end
fgets(fac_fid, 6);
Tcal=fscanf(fac_fid, '%f', 1);
fclose(fac_fid);

%Get data from actual data files of sample
temp_ind=strfind(filename, 'p');
for j=1:numel(temp_ind);
    if (0<=str2num(filename(temp_ind(j)-1))<=9) && (0<=str2num(filename(temp_ind(j)+1))<=9)
        T=str2num(filename(temp_ind(j)-2:temp_ind(j)-1))+str2num(filename(temp_ind(j)+1))/10;
    else
        continue;
    end
end

fid=fopen([path filename],'r');

str_token='1			; acquisition binsize';
str_token(str_token==' ')='';

%skip non-data lines
line=fgetl(fid);
line(line==' ')='';
while strcmp(line, str_token)==0
    line=fgetl(fid);
    line(line==' ')='';
end

%get the wavelength for C and A
line=fgetl(fid);
wl=regexp(line, '	', 'split');
wl=wl(2:end)';
wl=char(wl);
wl=str2num(wl(:,2:end));

%number of wavelength
nwl=size(wl,1);

wlc=wl(1:nwl/2)'; %first half is wavelength for C
wla=wl(nwl/2+1:end)'; %second half is wavelength for A

i=1;
while(feof(fid)==0)
    line=fgetl(fid);
    data=str2num(line);
    if size(data)<nwl+1
        break
    end
    t(i,1)=data(1)/1000; %first column of junk is time
    c_data(i,:)=data(2:nwl/2+1)'; %second column to nwl/2+1 column is C data
    a_data(i,:)=data(nwl/2+2:nwl+1)'; %nwl/2+2 column to nwl+1 column is A data
    i=i+1;
end
fclose(fid);

%plot time series
ind_c=find(wlc>439, 1);
ind_a=find(wla>439, 1);

figure('Name', filename, 'NumberTitle', 'off');
subplot(2,1,1)
plot(t, a_data(:,ind_a));
ylabel(['a_uncorr(' num2str(wla(ind_a)), ') (1/m)'], 'interpreter', 'none');

subplot(2,1,2)
plot(t, c_data(:,ind_c));
ylabel(['c_uncorr(' num2str(wlc(ind_c)), ') (1/m)'], 'interpreter', 'none');
xlabel('Time (sec)');


