function varargout=dopProcessRawDataFile(fileNameLoc,varargin)
% dopOSCCI Mark II: dopProcessRawDataFile ~ 11-Jan-2012 (last edit)
%
% import and process the raw data file ready for calculations
%
% [data labels foundMarkers rawInfo rawData]=dopProcessRawDataFile(fileNameLoc,varargin)
%
% where, varargin can include:
% inputs.defaults=struct('dopChs',[1 2],... % doppler left and right channel numbers
%     'evChs',3,... % doppler event channel number/s
%     'rawSample',100,... % raw multidop sampling rate in hertz
%     'outSample',25,... % desired down sampling rate in hertz
%     'epoch',[],... % lower and upper epoch values for data triming and trial normalisation
%     'rawTrim',0,... % request for raw data trimming - yes or no
%     'epochNorm',0); % request for trial normalisation - yes or no
%     'markHeight',1000); % height of Multi-dop trigger/marker pulses - look for pulses greater than this (see dopFindRawMarkers)
%     'evType',2,...        'evType',2,...% event type: 0 = base, 1 = stim only, 2 = both
%     'evMatch',0); %'evMatch',0,...% if event type = 0 or 1, it needs a match for with 1 or 0, only 2 is independent
%
% and varargout:
% varargout{1}=[]; % data: left, right, diference, and markers (1:x)
%   varargout{2}=[]; % data labels (as above)
%   varargout{3}=0; % found markers, yes or no
%   varargout{4}=[]; % information found in TX file
%   varargout{5}=[]; % raw downsampled data
%   varargout{6}=[]; % NaN replacement (number of not-a-number samples
%   replaced with zero)
%
% Includes fucntions:
%     readTWfromTX
%     dopEXPread
%     dopFindRawMarkers
%     dopRawHBmarks
%     dopRawChEqual
%     dopRawHBcor

% notes:
% - not perfectly sure what I should do with multiple markers -
% specifically in the case of trial by trial normalisation.  Could create
% mutliple sets of data - heavy on memory but cleaner data as sometimes
% there might be overlap in the normalisation epochs for markers from
% different channels.  Do all this separately would potentially mean
% slightly different data for each marker but cleaner.  I think that's the
% way to go.
%
% thinking that matching markers base & stim need to be normalised together
% and only indendent event types can be normalised independently.  So,
% matched epoch normalisation will have to be based upon lower epoch with
% respect to base marker and upper epoch with respect to stim marker and I
% think stim marker should be the period of interest rather than stim.
%
% Also need to accommodate situations when more than one poi marker is
% matched to the same baseline - essentially, find the furthest upper
% poi marker and use this.
%
% differences to dopOSCCIm1
% - searches for event markers before downsampling - could alter numbers
%

% -------------------------------------------------------------------------
%     'dopOSCCI' summarises functional transcranial Doppler ultrasonography
%     (fTCD) data.  The primary function of the software is to assess the
%     hemispheric lateralization of cognitive function.
%
%     Copyright (C) 2011 the Chancellor, Masters and Scholars of the
%     University of Oxford.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/> 
%     or write to the Free Software Foundation, Inc., 51 Franklin Street,
%     Fifth Floor, Boston, MA 02110-1301, USA.
%
%     Authors: Nic Badcock, Georgina Holt, Anneka Holden, & Dorothy Bishop.
%       contact: nicholas.badcock@gmail.com
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%
global rp inputs
try
    %% set blank outputs
    varargout{1}=[]; % data: left, right, diference, and markers (1:x)
    varargout{2}=[]; % data labels (as above)
    varargout{3}=0; % found markers, yes or no
    varargout{4}=[]; % information found in TX file
    varargout{5}=[]; % raw downsampled data
    varargout{6}=[]; % multiple event maker normalisations
    %% setGet inputs
    inputs.varargin=varargin;
    inputs.defaults=struct('dopChs',[1 2],... % doppler left and right channel numbers
        'evChs',3,... % doppler event channel number/s
        'evNames',[],... % event marker channel names
        'rawSample',100,... % raw multidop sampling rate in hertz
        'outSample',25,... % desired down sampling rate in hertz
        'epoch',[],... % lower and upper epoch values for data triming and trial normalisation
        'rawTrim',0,... % request for raw data trimming - yes or no
        'epochNorm',0,... % request for trial normalisation - yes or no
        'markHeight',1000,...); % height of Multi-dop trigger/marker pulses - look for pulses greater than this (see dopFindRawMarkers)
        'evType',2,...        'evType',2,...% event type: 0 = base, 1 = stim only, 2 = both
        'evMatch',0,...); %'evMatch',0,...% if event type = 0 or 1, it needs a match with another 1 or 0 type, only 2 is independent
        'fileName',[],...
        'shortWarn',1);
    %         ); % % event marker channel names
    rp=setGetInputsStruct(inputs); % rp = raw process
    rp.structAbbreviation='rp'; % use in dopOSCCIcrash... try to...
    rp.fileNameLoc=fileNameLoc;
    
    % check epoch for normalisation
    rp.epochLimits=[];
    if ~isempty(rp.epoch)
        rp.epoch=rp.epoch/(1/rp.outSample); % convent to samples
        %     if length(rp.evChs)>1
        rp.epochLimits=[min(rp.epoch(:,1)) max(rp.epoch(:,2))];
        %     end
    end

    %% import raw data
    if isTX(rp.fileNameLoc)
        [rp.tmp.raw rp.data.rawInfo]=readTWfromTX(rp.fileNameLoc);
    elseif isEXP(rp.fileNameLoc)
        [rp.tmp.raw rp.data.rawInfo]=dopEXPread(rp.fileNameLoc);% expRead(rp.fileNameLoc);
        %         pause
        if isfield(rp.data.rawInfo,'columnLabels') && isfield(rp.data.rawInfo,'dataLabels')
            rp.colDif=diff([numel(rp.data.rawInfo.dataLabels) numel(rp.data.rawInfo.columnLabels) ]);
            if rp.colDif~=0
                % shift dopChs & evChs by difference
                rp.dopChs=rp.dopChs-rp.colDif;
                rp.evChs=rp.evChs-rp.colDif;
            end
        end
    end
    
    %% find markers first
    % if event markers are very short <downSample value, they can be lost
    % during downsampling.
    [rp.data.mkChs rp.data.mkFound rp.tmp.trimScrn]=...
        dopFindRawMarkers(rp.tmp.raw(:,rp.evChs),...
        [rp.data.rawInfo.sampleRate rp.outSample],...
        rp.epochLimits,rp.rawTrim,rp.markHeight);
    
    %% down sample
    if exist('downsample','file') == 2
        rp.tmp.down=downsample(rp.tmp.raw,floor(rp.data.rawInfo.sampleRate/rp.outSample));
    else
        rp.tmp.down=dopDownsample(rp.tmp.raw,floor(rp.data.rawInfo.sampleRate/rp.outSample));
    end
    % need to put the accurate downsampled event marker data into raw
    rp.tmp.down(:,rp.evChs)=rp.data.mkChs;
    %     %% find event markers & trim data by epoch
    %     % trimming removes early and late sections of data if specified (see
    %     % dopTaskDef);
    %     %
    %     [rp.data.mkChs rp.data.mkFound rp.tmp.trimScrn]=...
    %         dopFindRawMarkers(rp.tmp.down(:,rp.evChs),rp.epoch,rp.rawTrim,rp.markHeight);
    %% > Do we have markers?
    % no point continuing if we have no information to work with
    if rp.data.mkFound
        rp.tmp.dopChs=rp.tmp.down(rp.tmp.trimScrn,rp.dopChs);
        rp.data.mkChs=rp.tmp.down(rp.tmp.trimScrn,rp.evChs);
        
        %% Find heart beat markers
        rp.tmp.hbMark=dopRawHBmarks(rp.tmp.dopChs(:,1),rp.outSample);%,[1000 1500]);
        
        %% normalise data
        rp.tmp.dopChsNorm=dopRawChEqual(rp.tmp.dopChs,rp.data.mkChs,...
            rp.epoch,rp.epochNorm,rp.evType,rp.evMatch,rp.fileName,rp.shortWarn); % was rp.epoch
        
        %% heart cycle correction
        [rp.data.dopChs rp.data.isnan_replace] = dopRawHBcor(rp.tmp.dopChsNorm,rp.tmp.hbMark);
        
        %% create difference channel
        % but do we actually need/use it?
        %         rp.data.dopChs(:,3)=rp.data.dopChs(:,1)-rp.data.dopChs(:,2); % this is faster than using diff
        
        %% multiple marker channels
        % create a 3D matrix with the (:,:,1) layer has all the markers in
        % it and then the (:,1,ith) layer has the set of markers to go with
        % the data of the same layer
        rp.tmp.nc=numel(rp.evChs); % number channels
        if rp.tmp.nc>1
            
            %             rp.data.evChs(:,1:rp.tmp.nc,2:rp.tmp.nc)=zeros(size(rp.data.evChs,1),1:rp.tmp.nc,2:rp.tmp.nc);
            %             for i=2:rp.tmp.nc % make it a 3D matrix as well
            %                 rp.data.evChs(:,1,i)=rp.data.evChs(:,i,1);
            %             end
            rp.tmp.mkSize=size(rp.data.mkChs);
            rp.data.mkChs=reshape(rp.data.mkChs,rp.tmp.mkSize(1),1,rp.tmp.mkSize(2));
        end
        %% set outputs
        rp.tmp.mkLabels=[];
        for i=1:numel(rp.evChs)
            %             if ~isempty(rp.evNames)
            %                 rp.tmp.mkLabels{end+1}=rp.evNames{i};
            %             else
            rp.tmp.mkLabels{end+1}=['mark',num2str(i)];
            %             end
        end
        
        varargout{1}=[rp.data.dopChs rp.data.mkChs]; % data
        varargout{2}={'left','right',rp.tmp.mkLabels{1:end}}; % labels
        varargout{3}=rp.data.mkFound;
        varargout{4}=rp.data.rawInfo;
        varargout{5}=rp.tmp.raw;%rp.tmp.down;
        varargout{6}=rp.data.isnan_replace; % number of nan (not a number) samples replaced with zero
    end
    clear inputs rp
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end
