function [hist_H,hist_x] = buoyHist(day)
    switch day
        case 'march2'
            fn = '../../waveData/xy029p101_201903020000-201903032359'; %Day of visit
        case 'march1'
            fn = '../../waveData/xy029p101_201903010000-201903022359'; %Day before
        otherwise
            disp('hey dumby that is not an option. I dont know how to spell but you dont know how to type.');
            return;
    end
    [r,stepError] = readBuoyFile(fn);
    fs = 1.28;
    N30 = 30*60*fs;
    Ns = size(r,1);
    Ng = floor(Ns/N30);
%    Ng = 5;
    zg = reshape(r(1:Ng*N30,3)/100,N30,Ng);
    error = sum(reshape(stepError(1:Ng*N30),N30,Ng));
    hist_H = NaN(14,Ng);
    Hm     = NaN(Ng,4);
    hist_T = NaN(12,Ng);
    Tm     = NaN(Ng,4);
    
    for ng = 1:Ng
        if error(ng)==0
            [hist_H(:,ng),hist_Hx,hist_T(:,ng),hist_Tx,Hm(ng,:),Tm(ng,:)] = comp(zg(:,ng),ng);
        end
    end
    figure(1)
    imagesc([1,Ng],hist_Hx,hist_H)
    hold on
    plot(1:Ng,Hm,'r')
    hold off
    texText('group','x')
    texText('$H$ [m]','y')
    graphToPDF('hist_height_all.pdf',4,4)
    
    figure(10)
    imagesc([1,Ng],hist_Tx,hist_T)
    hold on
    plot(1:Ng,Tm,'r')
    hold off
    texText('group','x')
    texText('$T$ [s]','y')
    graphToPDF('hist_period_all.pdf',4,4)
end


function [hist_H,hist_Hx,hist_T,hist_Tx,Hm,Tm] = comp(z,ng)
    fs = 1.28;
    dt = 1/fs;
    Ns = size(z,1);
    NcUB = round(Ns/5);
    ind = NaN(1,NcUB);
    tc  = NaN(1,NcUB);
    Nc = 0;
    for n=1:Ns-1
        if z(n)>=0 && z(n+1)<0
            Nc = Nc+1;
            ind(Nc) = n;
            tc(Nc)  = (n+z(n)/(z(n)-z(n+1)))*dt;
        end
    end
    ind = ind(1:Nc);
    tc  = tc(1:Nc);
    
    T = NaN(1,Nc-1);
    H = NaN(1,Nc-1);
    
    for n = 1:Nc-1
        rng = (ind(n)+1):ind(n+1);
        T(n) = tc(n+1)-tc(n);
        z_section = z(rng);
        H(n) = max(z_section) - min(z_section);
    end
    
    ind = find(isfinite(H));
    H = H(ind);     %remove the NaN elements of H and T
    T = T(ind);
    [~,ind] = sort(H,'descend');  
    H = H(ind);     % now put in descending order
    T = T(ind);
    
    Nf = length(H);
    N_third = round(Nf/3);
    N_tenth = round(Nf/10);
    
    H_mean = mean(H);
    H_rms  = sqrt(mean(H.^2));   
    H_third = mean(H(1:N_third));
    H_tenth = mean(H(1:N_tenth));
    
    T_mean  = mean(T);
    T_rms   = sqrt(mean(T.^2));
    T_third = mean(T(1:N_third));
    T_tenth = mean(T(1:N_tenth));
    
    
    figure(2)
    subplot(2,1,1)
        dH = 0.3;
        h = histogram(H,dH/2:dH:4.5,'displaystyle','stairs');
        hold on
        A = 1;
        plot(H_mean  *[1,1],[0,A],'k');
        plot(H_rms   *[1,1],[0,A],'k');
        plot(H_third *[1,1],[0,A],'k');
        plot(H_tenth *[1,1],[0,A],'k');
        hold off
        text(H_mean ,A,sprintf( '$\\bar H=%0.2f$m',H_mean ),'rotation',90,'interpreter','latex')
        text(H_rms  ,A,sprintf('$H_{rms }=%0.2f$m',H_rms  ),'rotation',90,'interpreter','latex')
        text(H_third,A,sprintf('$H_{1/3 }=%0.2f$m',H_third),'rotation',90,'interpreter','latex')
        text(H_tenth,A,sprintf('$H_{1/10}=%0.2f$m',H_tenth),'rotation',90,'interpreter','latex')
        xticks(0:1:5)
        texText('$H$ [m]','x')
        hist_H  = h.Values;
        hist_Hx = h.BinEdges;
        Hm = [H_mean,H_rms,H_third,H_tenth];
        
    subplot(2,1,2)
        dT = 2;
        h = histogram(T,dT/2:dT:25,'displaystyle','stairs');
        hold on
        A = 1;
        plot(T_mean  *[1,1],[0,A]);
        plot(T_rms   *[1,1],[0,A]);
        plot(T_third *[1,1],[0,A]);
        plot(T_tenth *[1,1],[0,A]);
        hold off
        text(T_mean ,A,sprintf( '$\\bar T=%0.2f$m',T_mean ),'rotation',90,'interpreter','latex')
        text(T_rms  ,A,sprintf('$T_{rms }=%0.2f$m',T_rms  ),'rotation',90,'interpreter','latex')
        text(T_third,A,sprintf('$T_{1/3 }=%0.2f$m',T_third),'rotation',90,'interpreter','latex')
        text(T_tenth,A,sprintf('$T_{1/10}=%0.2f$m',T_tenth),'rotation',90,'interpreter','latex')
        xticks(0:2:20)
        hist_T  = h.Values;
        hist_Tx = h.BinEdges;
        Tm = [T_mean,T_rms,T_third,T_tenth];
        
    graphToPDF(sprintf('histHT%d.pdf',ng),4.5,4.5)
end

