function [] = wrappedRipplePlotter(varargin)

if nargin > 1
    p = inputParser;
    addParameter(p,'rippleChan')
    addParameter(p,'refChan')
    addParameter(p,'animalFolder','/mnt/packrat/userdirs/david/zpool2/ripples/',@ischar)
    parse(p,varargin{:})
    rippleChan = p.Results.rippleChan;
    refChan = p.Results.refChan;
    animalFolder = p.Results.animalFolder;
else
    try
        csv = readtable('/mnt/packrat/userdirs/david/zpool2/ripples/metadata.csv');
        temp = pwd;
        temp = strsplit(temp,'/');
        animal = temp{length(temp)-1};
        recording = temp{length(temp)};
        temp{length(temp)} = []; temp = removeEmptyCells(temp);
        animalFolder = strjoin(temp,'/');
        for c = 1:length(csv.animal)
           if strcmp(lower(csv.animal{c}),lower(animal))
               refChan = csv.refChan(c);
               rippleChan = csv.rippleChans{c};
               rippleChan = cellfun(@str2num,(strsplit(rippleChan,'-')));
           end
        end
        if ~exist('rippleChan','var') % John/Andres' animals w/ different channel counts...
           for c = 1:length(csv.animal)
               if strfind(lower(recording),lower(csv.animal{c}))
                   refChan = csv.refChan(c);
                   rippleChan = csv.rippleChans{c};
                   rippleChan = cellfun(@str2num,(strsplit(rippleChan,'-')));
               end
           end
        end
    catch
        warning(['couldnt find meta information for this recording: ' pwd])
    end
end


try
    xml = LoadParameters; 
    if exist([xml.FileName '.SleepState.states.mat']) %& ~exist(['/' animalFolder '/' recording '/' xml.FileName '.ripples.event.mat'])
        if length(rippleChan) > 1
            lfp = bz_GetLFP(rippleChan(1):rippleChan(2));
            rippleChan = bz_GetBestRippleChan(lfp);
        end
%         
        rippleChan =54;
        refChan = 13;
        lfp = bz_GetLFP([rippleChan refChan]);

        [b a] = butter(4,[120/(xml.lfpSampleRate./2) 200/(xml.lfpSampleRate./2)],'bandpass');
        for i=1:2
            lfp.filt(:,i) = FiltFiltM(b,a,double(lfp.data(:,i)));
        end

        % need to get intervals of SWS here...

            load([xml.FileName '.SleepState.states.mat'])

            disp(['finding ripples with ch #: ' num2str(rippleChan) ', and noise ch #: ' num2str(refChan)])
            [ripples] = bz_FindRipples(lfp.data(:,1),lfp.timestamps,'noise',...
                lfp.data(:,2),'durations',[20 300] ,'restrict',SleepState.ints.NREMstate,'saveMat',false,'frequency',lfp.samplingRate);
            [ripples.maps,ripples.data,ripples.stats] = bz_RippleStats(lfp.filt(:,1),lfp.timestamps,ripples,'frequency',lfp.samplingRate);

            ripples.rippleChan = rippleChan;
            ripples.refChan = refChan;

            save(['/' animalFolder '/' recording '/' xml.FileName '.ripplesALL.event.mat'],'ripples','-v7.3')

            %% parse by NREM
            ind = [];
            for s = 1:size(SleepState.ints.NREMstate,1)
                temp = FindInInterval(ripples.peaks,double(SleepState.ints.NREMstate(s,:)));
                if ~isempty(temp)
                ind = [ind,temp(1):temp(2)];
                end
            end
            ripplesNREM.times = ripples.times(ind,:);
            ripplesNREM.peaks = ripples.peaks(ind);
            ripplesNREM.stdev = ripples.stdev;
            ripplesNREM.noise = ripples.noise;
            ripplesNREM.peakNormedPower = ripples.peakNormedPower(ind);
            ripplesNREM.detectorParams = ripples.detectorParams;
            ripplesNREM.detectorName = ripples.detectorName;
            if ~isempty(ripplesNREM.times)
                [ripplesNREM.maps,ripplesNREM.data,ripplesNREM.stats] = bz_RippleStats(lfp.filt(:,1),lfp.timestamps,ripplesNREM,'frequency',lfp.samplingRate);
                save(['/' animalFolder '/' recording '/' xml.FileName '.ripplesNREM.event.mat'],'ripplesNREM','-v7.3')
            end
            %% parse by WAKE
            ind = [];
            for s = 1:size(SleepState.ints.WAKEstate,1)
                temp = FindInInterval(ripples.peaks,double(SleepState.ints.WAKEstate(s,:)));
                if ~isempty(temp)
                ind = [ind,temp(1):temp(2)];
                end
            end
            ripplesWAKE.times = ripples.times(ind,:);
            ripplesWAKE.peaks = ripples.peaks(ind);
            ripplesWAKE.stdev = ripples.stdev;
            ripplesWAKE.noise = ripples.noise;
            ripplesWAKE.peakNormedPower = ripples.peakNormedPower(ind);
            ripplesWAKE.detectorParams = ripples.detectorParams;
            ripplesWAKE.detectorName = ripples.detectorName;
            if ~isempty(ripplesWAKE.times)
                [ripplesWAKE.maps,ripplesWAKE.data,ripplesWAKE.stats] = bz_RippleStats(lfp.filt(:,1),lfp.timestamps,ripplesWAKE,'frequency',lfp.samplingRate);
                save(['/' animalFolder '/' recording '/' xml.FileName '.ripplesWAKE.event.mat'],'ripplesWAKE','-v7.3')
            end
    else
        disp('couldnt find sleep scoring, doing nothing')
    end

catch
    disp(['something went wrong detecting ripples for ' animalFolder '/' recording])
end