function [H_Dir_Perc] = wind_reduction(var,P_Hs,Hs,Dir,dirbin,number_of_Dir_bins,number_of_Hs_bins,dDir,dH); 

% Script to visualise sediment transports as a rose
% DELTARES - TU DELFT
% April 2010 - DJR Walstra & Thiel de Vries & Luijendijk
% VERSION 1.0 18-05-2010
%
% construct sel.j
% sel.j describes clusters of waveheights and directions with comparable
% sed. transp. capacity
ncounter = 0;
for idir = 1 : number_of_Dir_bins
    for ihs = 1 : number_of_Hs_bins
        ncounter = ncounter + 1;
        sel(ncounter).j=dirbin{idir};
    end
end
var(isnan(var))=0;
tmp1 = var;
Sum_Hs = zeros(size(var,1),size(sel,2));
% first integrate over Hs per Dir-bin
for i = 1:size(sel,2)
    Sum_Hs(:,i) = sum(var(:,sel(i).j),2);
end
% second integrate over entire Hs (contribution per bin)
Sum_Hs_all = sum(Sum_Hs);
Cumsum_Hs = cumsum(Sum_Hs);
tmp1 = Cumsum_Hs./repmat(Sum_Hs_all,[size(Cumsum_Hs,1) 1]);
% cumulative part per condition
% wght = [.5,1,1/3,2/3,3/3,1/3,2/3,3/3,.5,1];
wght = repmat(1/number_of_Hs_bins:1/number_of_Hs_bins:1,[1 number_of_Dir_bins]) ;
for i= 1:size(sel,2)
    if wght(i)~=1
        if mod(i,number_of_Hs_bins)==1
            sel(i).i=1:find(tmp1(:,i)>=wght(i),1,'first');
        else        
            sel(i).i=max(sel(i-1).i)+1:find(tmp1(:,i)>=wght(i),1,'first');
            if isempty(sel(i).i)==1
                sel(i).i = max(sel(i-1).i)+1;
            end
        end        
    else
        if number_of_Hs_bins == 1
            sel(i).i = 1:size(P_Hs,1);
        else
            sel(i).i=max(sel(i-1).i)+1:size(P_Hs,1);
            % add new criteria
            sel(i).i = max(max(sel(i-1).i)+1,sel(i).i);
            if sel(i).i > size(tmp1,1)
                sel(i).i = NaN;
            end
        end
    end
    
    % check if there is enough space left to fill al Hs bins
    while size(P_Hs,1) - max(sel(i).i) < number_of_Hs_bins - mod(i-1,number_of_Hs_bins) - 1
        sel(i).i = sel(i).i(1):sel(i).i(end)-1;
    end
    %fprintf([num2str(i) ': [' num2str(sel(i).i) ']\n']);
end
% make plot for ppt with grouped classes shown
makePcolorplot2(Hs,Dir,var,'Relative Transports - Defined directional sectors',[0 max(max(abs(var)))])
hold on
% Find xmin,xmax,ymin,ymax for each grouped class (for plotting purposes)
for i= 1:size(sel,2)
    plot([min(sel(i).j)-1,max(sel(i).j)],[min(sel(i).i)-1,min(sel(i).i)-1],'-k','linewidth',4)
    plot([min(sel(i).j)-1,max(sel(i).j)],[min(length(Hs),max(sel(i).i)),min(length(Hs),max(sel(i).i))],'-k','linewidth',4)
    plot([min(sel(i).j)-1,min(sel(i).j)-1],[min(sel(i).i)-1,max(sel(i).i)],'-k','linewidth',4)
    plot([max(sel(i).j),max(sel(i).j)],[min(sel(i).i)-1,max(sel(i).i)],'-k','linewidth',4)
end
set(gcf,'Position',[315 512 1049 413]);

% Now determine conditions (sel_cnd_i/_j) and (upscaled) weights (upscl)
% for grouped classes (sel)
[upscl,sel_cnd_i,sel_cnd_j,tmp1,P_Hs_scaled,tmp2]=reduce_wave_climate(Hs,Dir,P_Hs,var,sel);
% Test to see if total transports are the same
t1 = sum(sum(var));
t2 = nansum(nansum(tmp2.*var));

if t1 - t2 > 1e-6
    fprintf('\n Inconsistency in input reduction \n');
end

% Write the selected conditions to file for LT use
% write_sco_txt(Hs, Tp_Hs ,Dir,P_Hs_scaled,P_Hs,ub_path,outputfilename)
% make plot
makePcolorplot2(Hs,Dir,tmp1,['Relative Transports - Equal Grouping ' num2str(number_of_Dir_bins*number_of_Hs_bins) ' Conditions'],[0 max(max(abs(tmp1)))])


% also plot the selected conditions as Dots in pcolor plot
hold on
plot(sel_cnd_j-0.5,sel_cnd_i-0.5,'ko', 'MarkerFaceColor','k')
% Find xmin,xmax,ymin,ymax for each grouped class (for plotting purposes)
for i= 1:size(sel,2)
    plot([min(sel(i).j)-1,max(sel(i).j)],[min(sel(i).i)-1,min(sel(i).i)-1],'-k','linewidth',4)
    plot([min(sel(i).j)-1,max(sel(i).j)],[min(length(Hs),max(sel(i).i)),min(length(Hs),max(sel(i).i))],'-k','linewidth',4)
    plot([min(sel(i).j)-1,min(sel(i).j)-1],[min(sel(i).i)-1,max(sel(i).i)],'-k','linewidth',4)
    plot([max(sel(i).j),max(sel(i).j)],[min(sel(i).i)-1,max(sel(i).i)],'-k','linewidth',4)
end
set(gcf,'Position',[315 54 1050 385]);
print(gcf,'-dpng',['Relative_Transports_',num2str(number_of_Dir_bins*number_of_Hs_bins),'conditions.png']);

% define reduced conditions
[x1,x2] = meshgrid(Dir,Hs);
for i = 1:length(sel_cnd_i)
    H_Dir_Perc(i,:) = [x2(sel_cnd_i(i),sel_cnd_j(i)) x1(sel_cnd_i(i),sel_cnd_j(i)) P_Hs_scaled(sel_cnd_i(i),sel_cnd_j(i))];
end
% remove cond with NaN prob;
H_Dir_Perc=H_Dir_Perc(~isnan(H_Dir_Perc(:,3)),:);
