function BLO = getleft (BL,SL,SR,BR,G)

global JA JB tmax wlimit;

ml = BL.m*SL.m;
mr = BR.m*SR.m;

GM = reshape(G,mr,ml);  % density matrix calculation
DM = full (GM'*GM);

DM = (DM + DM')/2;

JBL = ones(BL.m,1);
JSL = ones(SL.m,1);
[X1,X2] = ndgrid (SL.NTOTA,BL.NTOTA);
NTOTA = X1+X2; NTOTA = NTOTA(:);
[X1,X2] = ndgrid (SL.NTOTB,BL.NTOTB);
NTOTB = X1+X2; NTOTB = NTOTB(:);

V = zeros(ml,ml);
D = zeros(ml,1);
SA = uint16(zeros(ml,1));
SB = uint16(zeros(ml,1));

i = 1;
for IA = unique (NTOTA')
    for IB = unique (NTOTB')
        IND = find ((NTOTA == IA) & (NTOTB == IB));
        n = size(IND,1);
        DMX = DM(IND,IND);
        [V0,D0] = eig(DMX);
        VX = zeros(ml,n);
        VX(IND,:) = V0;
        DX = diag(D0,0);
        SXA = IA*uint16(ones(n,1));
        SXB = IB*uint16(ones(n,1));
        I = i:(i+n-1);
        V(:,I) = VX;
        D(I) = DX;
        SA(I) = SXA;
        SB(I) = SXB;
        i = i+n;
    end;
end;

[D,IX] = sort (D,'descend');
V = V(:,IX);
SA = SA(IX);
SB = SB(IX);

F = find (1 - cumsum(D) < wlimit);
IND = transpose (1:F(1));
n = size(IND,1);

if (n <= tmax)
    BLO.m = n;
    BLO.NTOTA = SA(IND);
    BLO.NTOTB = SB(IND);
    O = V(:,IND);
else
    BLO.m = tmax;
    BLO.NTOTA = SA(1:tmax);
    BLO.NTOTB = SB(1:tmax);
    O = V(:,1:tmax);
    disp (['Left block size = ', num2str(BLO.m)]);
end

O = sparse(O);
IBL = speye(BL.m);
ISL = speye(SL.m);

CA = kron(IBL,SL.CA);
CB = kron(IBL,SL.CB);
BLO.CA = O'*CA*O;
BLO.CB = O'*CB*O;

NA = kron(BL.NA,ISL) + kron(IBL,SL.NA);
NB = kron(BL.NB,ISL) + kron(IBL,SL.NB);
BLO.NA = O'*NA*O;
BLO.NB = O'*NB*O;

H = kron(BL.H,ISL) + kron(IBL,SL.H) - JA*(kron(BL.CA,SL.CA') + kron(BL.CA',SL.CA)) - JB*(kron(BL.CB,SL.CB') + kron(BL.CB',SL.CB));
BLO.H = O'*H*O;
