%% Modified from Apub_NSx_CohPSD_vb11_OpenEphys_Accelerator_MultiChannels
%  keep the loading part and Acc part only

function [res,para]=F_load_Ephys_data(para)
    %% Main function %%%%%%%%%%
    file=para.Ephys.EphysFile;
    %%%%%%%%%% Check error %%%%%%%%%%%%%%%%%%%%
    if para.Ephys.Highcut >250;
        error('Highcut is above 250 Hz');
    end
    %%%%%%%%  Load NSx data (BlackRock of OpenEphys) %%%%%%%%%%%%%%%%%%%%%%
    if para.Ephys.DataSource==1
    %[GetFileName GetFilePath]=uigetfile('*.ns*'); % get filename and path
    NSx=openNSx(file);
    end
    if para.Ephys.DataSource==2
    [NSx,EphysToMat]=Afun_OpenephysToNSx_v2(0,0,0);
    GetFilePath=[pwd '\']; % pwd is matlab intrinsic code for current path
    end

    if para.Ephys.DataSource==3
    NSx=openOpE(file);
    GetFilePath=NSx.MetaTags.OpenEphysFilePath;% get the source file path of openEphys settings.xml
    NSx.MetaTags.Filename=['o',NSx.MetaTags.OpenEphysFilePath(end-19:end-1)];
    GetFileName=NSx.MetaTags.Filename;
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%AUX904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if para.Ephys.Accelerator_RMS==1
        %find channel
        Accelerator_ID=[]
        for i=1:length(NSx.ElectrodesInfo);
            for j=1:length(para.Ephys.Accelerator_Channel); %usually 1:3
                if NSx.ElectrodesInfo(i).ElectrodeID== para.Ephys.Accelerator_Channel(j)
                    Accelerator_ID=[Accelerator_ID i];
                end
            end
        end

        k=i+1; %number of new electrodes info

        NSx.ElectrodesInfo(k)=NSx.ElectrodesInfo(Accelerator_ID(1));
        NSx.ElectrodesInfo(k).ElectrodeID=904;
        NSx.ElectrodesInfo(k).Label='Accelerator_RMS';
        NSx.ElectrodesInfo(k).ConnectorPin=4;
        disp('ya');
        NSx.Data(k,:)=rms([NSx.Data(Accelerator_ID(1),:);NSx.Data(Accelerator_ID(2),:);NSx.Data(Accelerator_ID(3),:)],1);
        NSx.MetaTags.ChannelID(k,1)=904;
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    SourceFile=[NSx.MetaTags.Filename NSx.MetaTags.FileExt];
    data=double(NSx.Data');
    NSx=rmfield(NSx,'Data'); 
    fs=double(NSx.MetaTags.SamplingFreq);
    para.Ephys.fs_Original=fs;
    para.Ephys.NSx.MetaTags=NSx.MetaTags;
    para.Ephys.NSx.ElectrodesInfo=NSx.ElectrodesInfo;
    for i =1:length(para.Ephys.ChSig)
        ChannelSignal=find(NSx.MetaTags.ChannelID==para.Ephys.ChSig(i));
        if para.Ephys.ChRef(i)==0; ChannelRef=0; 
        else ChannelRef=find(NSx.MetaTags.ChannelID==para.Ephys.ChRef(i));
        end
        %%% Trim data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        if para.Ephys.Timecut~=0;
        data=data(para.Ephys.TrimStartTime*fs+1:para.Ephys.TrimStopTime*fs,:);
        end;
        %%% Assign channel %%%
        if ChannelRef==0;
            Signal=data(:,ChannelSignal);
        else
            Signal=data(:,ChannelSignal)-data(:,ChannelRef);
        end
        %% Down Sampling Data (use decimate)
        if para.Ephys.DownSampling==2;
            Signal=decimate(Signal,fs/para.Ephys.DownSamplingFreq,63,'fir');
            para.Ephys.fs=para.Ephys.DownSamplingFreq;
        end
            %%% Filtering Data (optional) %%%%%%%%%%%
        if para.Ephys.Filter==1;
           if max(ismember([para.Ephys.ChSig,para.Ephys.ChRef],para.Ephys.ChannelNotFilter))==0; % filter if ChSig1 and ChRef1 both not in the "nofilter channel" 
                if para.Ephys.HIGHPASS==1
                  Signal=highpass(Signal,para.Ephys.Lowcut,para.Ephys.fs);
                end
                Signal=A_applyfilter3(Signal,fs,para.Ephys.Lowcut,para.Ephys.Highcut,para.Ephys.FilterOrder);
           end
        end
        res.Ephys.Signal{i,3}=Signal;
        res.Ephys.Signal{i,1}=para.Ephys.ChSig(i);
        res.Ephys.Signal{i,2}=para.Ephys.ChRef(i);
    end
end