function varargout=dopGraph2Norm(normData,varargin)
% dopOSCCI Mark II: dopGraph2Norm~ 21-9-2010 (last edit)
%
% make normalised data graph
%
% undecided as to how best to create these graphs:
% - options include:
%   > separate graph for each event marker
%   > single graph but very cluttered for additional event markers or at
%   least potentially cluttered.
%   > single graph with addition warning graph for overlapping event epochs
%
% >> separate subplots for each event marker
%
fprintf('\n%s\n\t%s',['Running ',mfilename],...
    'Making Graph:');
try
    inputs.varargin=varargin;
    inputs.turnOn={'test'}; % makes the figure visible automatically
    inputs.defaults=struct(...);
        'sampleRate',25,...); % down sampleRate in hertz to figure out timing
        'indName','Bruce',... % individual data file identification
        'graphAbb','Norm',... % graph abbreviation - used for saving
        'graphName','Norm Data',...); % graph name
        'outPos',[0 .5 1 .5],...); % outer position of figure in normalized units
        'actRej',[70 130],...); % activation rejection range
        'mk',[],...); % dop.mk structure - event names + raw and matched markers
        'epoch',[],... % lower and upper epoch values for events
        'base',[],...); % lower and upper epoch values for events
        'evType',2); % event type: 0 = base, 1 = stim only, 2 = both
    gn=setGetInputsStruct(inputs);
    
    gn.translation='graph norm';
    gn.structAbbreviations='gr';
    gn.data=normData; clear normData
    gn.sampleSec=gn.sampleRate^-1;
    
    %% setup figure
    gn.h=figure('NumberTitle','off','Name',[gn.graphName,': ',gn.indName],...
        'Units','normalized','OuterPosition',gn.outPos,'Visible','Off');
    gn.hNames{1}=[gn.graphAbb,'_',gn.indName];
    
    %% subplots
    % create subplots if multiple event markers
    gn.subplots=1;
    if ~isempty(gn.mk)
        gn.ev.names=gn.mk.evNames;%fieldnames(gn.mk);
        gn.subplots=length(gn.ev.names);
    end
    for j=1:gn.subplots
        % check for event multiples
        if size(gn.actRej,1)==j
            gn.acrj=gn.actRej(j,:);
        else
            gn.acrj=gn.actRej(1,:); % multiple values for dif event don't exist
        end
        % set subplot
        if gn.subplots>1
            subplot(gn.subplots,1,j);
            % adjust size of figure
            gn.outPos([2 4])=[gn.outPos(2)-.5/3 gn.outPos(2)+.5/3];
            set(gn.h,'OuterPosition',gn.outPos);
        end
        %% left right data
        gn.pl.labels={'left','right'}; % TX data
        gn.pl.colours={'b','r'};
        for i=1:size(gn.data,2)
            plot((1:size(gn.data,1))*gn.sampleSec,gn.data(:,i,j),'color',gn.pl.colours{i},'DisplayName',gn.pl.labels{i})
            if i==1; hold; end;
        end
        %% event markers
        if ~isempty(gn.mk)
                gn.mkData=zeros(1,size(gn.data,1));
                gn.mkData(gn.mk.(gn.ev.names{j}).marks)=gn.acrj(2)*.98; % gn.actRej(j,2)*.98;
                plot((1:size(gn.data,1))*gn.sampleSec,gn.mkData,'color','g',...
                    'LineStyle','-','LineWidth',2,...
                    'DisplayName', [gn.ev.names{j},' (',num2str(length(gn.mk.(gn.ev.names{j}).marks)),')'])               
                gn.mkData=zeros(1,size(gn.data,1));
                gn.mkData(gn.mk.(gn.ev.names{j}).rawMks)=gn.acrj(1)*1.1; %gn.actRej(j,1)*.9;
                plot((1:size(gn.data,1))*gn.sampleSec,gn.mkData,...
                    'color','m','LineStyle','-','LineWidth',2,...
                    'DisplayName',...
                    ['raw events',' (',num2str(length(gn.mk.(gn.ev.names{j}).rawMks)),')']);   
                
        end
        %% epoch
        if ~isempty(gn.mk) && ~isempty(gn.epoch)
            gn.mkData=zeros(1,size(gn.data,1));
            gn.ep.mks=gn.mk.(gn.ev.names{j}).marks;
            gn.ep.data=bsxfun(@plus,[gn.ep.mks' gn.ep.mks'],gn.epoch(j,:)/gn.sampleSec); % two columns of epochs
            for i=1:length(gn.ep.mks)
                % adjust if values are outside available range
                if sum(gn.ep.data(i,:)<1)
                    gn.ep.data(i,gn.ep.data(i,:)<1)=1;
                end
                if sum(gn.ep.data(i,:)>length(gn.mkData))
                    gn.ep.data(i,gn.ep.data(i,:)>length(gn.mkData))=length(gn.mkData);
                end
                gn.mkData(gn.ep.data(i,1):gn.ep.data(i,2))=gn.acrj(2)*.95;
            end
            plot((1:size(gn.data,1))*gn.sampleSec,gn.mkData,'color','y',...[1 .5 .3],... 'y'
                'DisplayName', 'epoch');%'LineStyle','--'
        end
        %% base
        if ~isempty(gn.mk) && ~isempty(gn.base)
            gn.mkData=zeros(1,size(gn.data,1));
            gn.ep.mks=gn.mk.(gn.ev.names{j}).marks;
            gn.ep.data=bsxfun(@plus,[gn.ep.mks' gn.ep.mks'],gn.base(j,:)/gn.sampleSec); % two columns of epochs
            for i=1:length(gn.ep.mks)
                 % adjust if values are outside available range
                if sum(gn.ep.data(i,:)<1)
                    gn.ep.data(i,gn.ep.data(i,:)<1)=1;
                end
                if sum(gn.ep.data(i,:)>length(gn.mkData))
                    gn.ep.data(i,gn.ep.data(i,:)>length(gn.mkData))=length(gn.mkData);
                end
                gn.mkData(gn.ep.data(i,1):round(gn.ep.data(i,2)))=gn.acrj(2)*.9;
            end
            plot((1:size(gn.data,1))*gn.sampleSec,gn.mkData,'color','k',...
                'DisplayName', 'base'); % 'LineStyle',':',
        end
        %% activation rejection
%         for i=1:size(gn.actRej(j,:),2)
            gn.ar.data=ones(1,floor(size(gn.data,1)*gn.sampleSec))*gn.acrj(1);
            gn.ar.data(2,:)=ones(1,floor(size(gn.data,1)*gn.sampleSec))*gn.acrj(2);
            plot(1:size(gn.data,1)*gn.sampleSec, gn.ar.data,'c',...); % line handle
                'DisplayName',{'act. rej.','act. rej.'}); % ':c'
%         end
        %% axes & labels
        gn.axes=get(gn.h,'CurrentAxes');
        set(get(gn.axes,'Title'),'string',[gn.graphName,': ',gn.indName]);
        set(get(gn.axes,'XLabel'),'String',['Time in seconds (approx. ',...
            num2str(round(size(gn.data,1)*gn.sampleSec/60)),' minutes)']);
        set(gn.axes,'XLim',[0 size(gn.data,1)*gn.sampleSec]);
        set(gn.axes,'YLim',[gn.acrj(1)-gn.acrj(1)*.4 gn.acrj(2)+gn.acrj(1)*.4]);
        set(gn.axes,'XTick',(1:size(gn.data,1)*(gn.sampleSec/60))*60)
        set(get(gn.axes,'YLabel'),'String',...
            sprintf('%s\n%s','Normalised Doppler Velocity','(Mean = 100 %)'));
        
        %% legend
        gn.lgd=legend('location','northEast','orientation','horizontal');
        gn.lgdString=get(gn.lgd,'String');
        set(gn.lgd,'String',gn.lgdString(1:end-1));
        set(gn.lgd,'Box','off');
        
        %% epoch numbers
        for i=1:length(gn.mk.(gn.ev.names{j}).marks)
            figure(gn.h); if ~gn.test; set(gn.h,'Visible','Off'); end
                % having some trouble with this figure not maintaining
                % status are current figure - this pops in on
            text(gn.mk.(gn.ev.names{j}).marks(i)*(gn.sampleSec),...
                min(get(gn.axes,'Ylim'))+.1*diff(get(gn.axes,'Ylim')),num2str(i));
        end
    end
    %% test
    if gn.test; set(gn.h,'Visible','on'); end
%         fprintf('%s\n','Test procedure on - throwing error for debugging');
%         plang; % should generate an error
%     end
    
    %% set ouputs
    %    gn.out.note='li = laterality index output';
    varargout{1}=gn.h;
    varargout{2}=gn.hNames;
    fprintf('\t%s\n','Done');
catch err
    %% catch dopOSCCI error
    % 
save(dopOSCCIdebug(mfilename));throw(err); 
%     dopOSCCIcrash(err,gn,mfilename);
end

clear gn inputs