clear all;
close all;
dat.meta.cellNum = 2;
dirID = ['C:\Documents and Settings\Annihilatrix II\Desktop\mingfaifong\HEKA data\2012-06-18_zeno1_25DIV_ChR2-stim-OU\matfiles\original data extracted from HEKA\cell0' ...
    num2str(dat.meta.cellNum) '\'];

y = dir(dirID); z = [y.isdir];
fidlist = {y(~z).name};
cultureID = 'zeno1';

for j = 1:length(fidlist)
    clear Trace* x*
    
    fid = fidlist{j}
    load([dirID fid]);
    x = who('Trace_*');
    
    protocolList = {'UpFreqChirp'; ...
        'DownFreqChirp'; ...
        'PowSteps'; ...
        'OU-Rand'; ...
        'OU-Rep2'; ...
        'OU-Rep3'; ...
        'OU-Rep4'; ...
        'OU-Rep'; ...
        'CurrPulse'; ...
        'HypCurrPulse'; ...
        'LongCurr'};
    
    for k = 1:length(protocolList)
        if findstr(fid,protocolList{k})
            dat.meta.protocolType = protocolList{k};
            if k>8
                tmp_tracesPerSweep = 2; %current clamp protocols only have voltage and current
            else
                tmp_tracesPerSweep = 4; %VC and CC protocols have voltage, current, V_LED, and V_photodiode
            end
            break;
        elseif k == length(protocolList)
            dat.meta.protocolType = 'unknown';
            disp('protocol not found');
        else
            continue;
        end
    end
    
    if k ~=1
        continue
    end
    
    dat.meta.sweepTotal = length(x)/tmp_tracesPerSweep;
    
    if findstr(fid,'_CC')
        dat.meta.controlType = 'Current Clamp';
        ctrl_tmp = 'CC';
    else
        dat.meta.controlType = 'Voltage Clamp';
        ctrl_tmp = 'VC';
    end
    
    uloc = findstr(fid,'_');
    dat.meta.date = fid(1:uloc(1)-1);
    dat.meta.culture = fid(uloc(1)+1:uloc(2)-1);
    dat.meta.HEKA.fileName = fid(1:uloc(3)-1);
    dat.meta.HEKA.groupName = fid(uloc(3)+1:uloc(4)-1);
    dat.meta.HEKA.seriesName = fid(uloc(4)+1:end-4);
    dloc = findstr(dat.meta.HEKA.seriesName,'-');
    if length(dat.meta.HEKA.seriesName)-dloc(end)<=1
        seriesNum_tmp = ['0' dat.meta.HEKA.seriesName(dloc(end)+1)];
    else
        seriesNum_tmp = dat.meta.HEKA.seriesName(dloc(end)+1:dloc(end)+2);
    if seriesNum_tmp(2) == '_' || seriesNum_tmp(2) == '.'
        seriesNum_tmp = ['0' seriesNum_tmp(1)];
    end
    end
    dat.meta.HEKA.seriesNum = str2double(seriesNum_tmp);
        
    for i = 1:dat.meta.sweepTotal
        prefix_tmp = [cultureID '_cell0' num2str(dat.meta.cellNum) '_' dat.meta.protocolType '_' ctrl_tmp '_' seriesNum_tmp];
        if length(num2str(i))==1
            dat.meta.name = [prefix_tmp '_00' num2str(i)];
        elseif length(num2str(i))==2
            dat.meta.name = [prefix_tmp '_0' num2str(i)];
        elseif length(num2str(i))==3
            dat.meta.name = [prefix_tmp '_' num2str(i)];
        end

        x_Imem = eval(char(x(1+(tmp_tracesPerSweep*(i-1)))));
        x_Vmem = eval(char(x(2+(tmp_tracesPerSweep*(i-1)))));
        dat.raw.Imem = x_Imem(:,2);
        dat.raw.Vmem = x_Vmem(:,2);
        dat.meta.units.Imem = 'A';
        dat.meta.units.Vmem = 'V';

        if tmp_tracesPerSweep==4
                x_Iled = eval(char(x(3+(tmp_tracesPerSweep*(i-1)))));
                x_Vpd = eval(char(x(4+(tmp_tracesPerSweep*(i-1)))));
                dat.raw.Iled = x_Iled(:,2);
                dat.raw.Vpd = x_Vpd(:,2);
                dat.meta.units.Vled = 'V';
                dat.meta.units.Iled = 'A';
                dat.meta.units.Ipd = 'A';
        end
        
        dat.meta.fs_hz = 1/mean(diff(x_Imem(:,1)));
        %dat.meta.sweepNum = i;
        tmp_tracename = char(x(1+(tmp_tracesPerSweep*(i-1))));
        uloc_tracename = findstr(tmp_tracename,'_'); 
        dat.meta.sweepNum = str2double(tmp_tracename(uloc_tracename(3)+1:uloc_tracename(4)-1));
        dat.meta.sweepLengthSec = max(x_Imem(:,1))-min(x_Imem(:,1));
        dat.meta.wasSweep = 1;
        
%         save([dirID dat.meta.name '.mat'],'dat');
        save([dat.meta.name '.mat'],'dat');
        %save(['C:\Documents and Settings\Annihilatrix II\Desktop\mingfaifong\HEKA data\2012-04-25_Quincy359\matfiles\' dat.meta.name '.mat'],'dat');

        %         dat.raw.Imem = x_Imem;
%         dat.raw.Vmem = x_Vmem;
%         dat.raw.Vctl = [];
%         dat.raw.Iled = x_Iled;
%         dat.raw.Vpd = x_Vpd;
%        dat.meta.name = [dat.meta.name 't'];


    end
    
end