function rasterPlotF32data(spkdata, stimIdx, stimLabel, options)
% function rasterPlotF32data(spkdata, stimIdx, options)
% makes a raster plot of data imported with spikedatf
% Parameter stimIdx indicates which stimulus parameter to use to label the
%    rasters
% Parameter stimLabel is a string describing the stim parameter stimIdx
% Parameter options sets a number of optional parameters:
%    options.tLimits sets limits for time axis
%    options.fontSz sets fontSize. Set this to ZERO to switch off labels
%    options.markerSize sets the dot size for the raster dots
%    options.patchColor sets the background patch color
%    options.labelsModulo sets one in every how many stimuli gets labelled.
%       setting that to zero switches labels off
%
%   Example:
%   d=spikedatf(fname);
%   rasterPlotF32data(d,1,'level (dB)')
%
%   rasterPlotF32data(d,3,'ITD /muS',[0,100]); % zoom in on forst 100 ms only

if ~exist('options')
    options=[];
end

if ~isfield(options,'tLimits')
    tLimits=[0 spkdata.sweeplength];
else
    tLimits=options.tLimits;
end

if ~isfield(options,'labelsModulo')
    options.labelsModulo=1;
end

if ~isfield(options,'markerSize')
    options.markerSize=3;
end

if ~isfield(options,'patchColor')
    % options.patchColor=[hex2dec('71'),hex2dec('ee'),hex2dec('b8')]/256;
    options.patchColor=[hex2dec('81'),hex2dec('fe'),hex2dec('c8')]/256;
    % default to Alexa's favourite snot green
end

if ~isfield(options,'fontSize')
    fontSz=11;
else
    fontSz=options.fontSize;
end

%clf;
hold on;
yRow=1;
% get sort order so that rasters are ordered in ascending order
stimVals=[spkdata.stim];
stimVals=stimVals(stimIdx,:);
[stimSorted,stimOrder]=sort(stimVals);

for setNumber=stimOrder
    sweeps=spkdata(setNumber).sweep;
    numSweeps=length(sweeps);
    % plot even sets on a yellow background
    if mod(setNumber,2)==1
        %p=patch([tLimits(1), tLimits(1), tLimits(2), tLimits(2)],...
        %    [yRow-0.5, yRow+numSweeps+0.5,yRow+numSweeps+0.5,yRow-0.5],'y','LineStyle','none');
        p=patch([tLimits(1), tLimits(1), tLimits(2), tLimits(2)],...
            [yRow, yRow+numSweeps,yRow+numSweeps,yRow],options.patchColor);
        set(p, 'LineStyle','none','EdgeColor','none')
    else
        p=patch([tLimits(1), tLimits(1), tLimits(2), tLimits(2)],...
            [yRow, yRow+numSweeps,yRow+numSweeps,yRow],'w');
        set(p, 'LineStyle','none','EdgeColor','none')
    end
    if (fontSz>0) && (mod(setNumber,options.labelsModulo)==0)        
        % place stimulus parameter label to the left
        text(tLimits(1)-(tLimits(2)-tLimits(1))*0.01,yRow+numSweeps/2,sprintf('%g',spkdata(setNumber).stim(stimIdx)),...
            'horizontalAlignment', 'right', 'FontSize',fontSz-4, 'fontname','arial')
    end
    % now plot dot raster
    for ii=1:length(sweeps)
        spk=sweeps(ii).spikes;
        adot=plot(spk,ones(size(spk))*yRow,'b.','MarkerSize', options.markerSize);
        yRow=yRow+1;           
    end
end
xlim(tLimits)
ylim([0,yRow+1]);

hold off;
if fontSz > 0
    xlabel('time (ms)','FontSize',fontSz, 'fontname','arial');
    set(gca, 'fontname','arial','FontSize',fontSz)
%     t=text(tLimits(2)+(tLimits(2)-tLimits(1))*0.15,yRow/2,stimLabel,'Rotation',90,'Horiz','Center','FontSize',fontSz-4, 'fontname','arial')
    t=text(tLimits(1)-(tLimits(2)-tLimits(1))*0.28,yRow/2,stimLabel,'Rotation',90,'Horiz','Center','FontSize',fontSz-2, 'fontname','arial');
else
    set(gca,'xtick',[])
end

set(gca,'ytick',[]);
set(gca,'Ycolor','w')
%p=get(gca,'pos'); p(1)=p(1)-.1; set(gca,'pos',p);
%ylabel('sweep #');