%% please only change the folder name in line 4.
clear all;
eeglab;
myPath = '/data/projects/yute/Glaucoma/nGoggle3/tmp_arvo17/';%file folder
csv_list = dir([myPath '/*.csv']);
mapSbj = containers.Map();%hashmap for subjects

for s = 1:length(csv_list)
    csv_name = csv_list(s).name;
    S = regexp(csv_name, '-', 'split');
    raw = csvread([myPath '/' csv_list(s).name]);
    raw = raw(:,[2:9 12]); % CHcsv = {'EOG2','O3','EOG1','Oz','PO3','Pz','PO4','O4','PSV'};
    raw = raw(:,[1 3 5:7 2 4 8 9])';
    %     if(size(raw,2) >= 500*189)
    %         raw = raw(:,1:188.5*500);
    %     end
    %hashmap
    if(isKey(mapSbj,S{1}))
        tempSbj = values(mapSbj,cellstr(S{1}));
        sbj = tempSbj{1};
        if(strcmp(S{3},'L_A'))
            sbj.L_A = [sbj.L_A raw];
        elseif(strcmp(S{3},'L_B'))
            sbj.L_B = [sbj.L_B raw];
        elseif(strcmp(S{3},'R_A'))
            sbj.R_A = [sbj.R_A raw];
        elseif(strcmp(S{3},'R_B'))
            sbj.R_B = [sbj.R_B raw];
        elseif(strcmp(S{3},'open'))
            sbj.open = raw;
        elseif(strcmp(S{3},'closed'))
            sbj.closed = raw;
        elseif(strcmp(S{3},'L_cali'))
            sbj.L_cali = raw;
        elseif(strcmp(S{3},'R_cali'))
            sbj.R_cali = raw;
        end
    else%new subject, create a structure and append it
        fprintf('--New subject: %s--\n',S{1});
        sbj.closed = [];
        sbj.open = [];
        sbj.L_cali = [];
        sbj.R_cali = [];
        sbj.L_A = [];
        sbj.R_A = [];
        sbj.L_B = [];
        sbj.R_B = [];
        if(strcmp(S{3},'L_A'))
            sbj.L_A = raw;
        elseif(strcmp(S{3},'L_B'))
            sbj.L_B = raw;
        elseif(strcmp(S{3},'R_A'))
            sbj.R_A = raw;
        elseif(strcmp(S{3},'R_B'))
            sbj.R_B = raw;
        elseif(strcmp(S{3},'open'))
            sbj.open = raw;
        elseif(strcmp(S{3},'closed'))
            sbj.closed = raw;
        elseif(strcmp(S{3},'L_cali'))
            sbj.L_cali = raw;
        elseif(strcmp(S{3},'R_cali'))
            sbj.R_cali = raw;
        end
    end
    newMap = containers.Map(S{1},sbj);
    mapSbj = [mapSbj;newMap];
end
%convert/save the raw data to EEGLAB format
cd(myPath)
sbjs = keys(mapSbj);
%privatePath = '../nGoggle3_private/bp550h4/';
for i=1:length(keys(mapSbj))
    tempSbj = values(mapSbj,sbjs(i));
    sbj = tempSbj{1};
    names = fieldnames(sbj);
    for j=1:length(names)
        if(size(sbj.(names{j}),1)~=0 && size(sbj.(names{j}),2)~=0)
            numOfChannel = size(sbj.(names{j}),1);
            dataLength = size(sbj.(names{j}),2);
            %20161120, we save two version of data: 1) raw 2) applying ASR
            EEGAsr = pop_importdata('dataformat','array','nbchan',numOfChannel,'data',sbj.(names{j}),'srate',500,'pnts',dataLength,'xmin',0);
            photoDiodeData = EEGAsr.data(9,:);%save photoDiodeData so ASR will not remove it
            EEGAsr = clean_rawdata(EEGAsr, -1, [0.25 0.75], 0.8, -1, 10, -1);            %available in EEGLAB version: 13.6.5b
            EEGAsr.data(9,:) = photoDiodeData;%put photoDiodeData back
            EEGAsr = pop_saveset( EEGAsr, 'filename',[sbjs{i} '_' (names{j}) '_raw_asred']);%,'filepath',privatePath);
            
            EEGRaw = pop_importdata('dataformat','array','nbchan',numOfChannel,'data',sbj.(names{j}),'srate',500,'pnts',dataLength,'xmin',0);
            EEGRaw = eeg_checkset( EEGRaw );
            EEGRaw = pop_saveset( EEGRaw, 'filename',[sbjs{i} '_' (names{j}) '_raw']);%,'filepath',privatePath);
        else
             fprintf('\r\n No data in %s\r\n',csv_name);%onset = onset(1:90);
        end
        
    end
end
