function C2_X = PROC_GetC2Representation_FeatureLocal(savename,S2Root,S2CentroidFN,objname,PoolSize);
%fuction C2_X = PROC_GetC2Representation_FeatureLocal(savename,S2Root,S2CentroidFN,objname,PoolSize);
%
%S2Root = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/c2/NaturalS2Centroids_70x5';
%corresponding global version is in /cbcl/cbcl01/bileschi/MATLAB/UsefullStuff

HelpTextInfo = GetHelpTextInfo;
stillMore = 1;
partNum = 1;
load(S2CentroidFN,'SourceLocations');%--> SourceLocations
% source = ReArrangeSource(SourceLocations);
while(stillMore)
  [BBoxs,SSIdxs,stillMore] = GetBBoxAndSSIdxs(objname,partNum);
  if(not(stillMore))
     break;
  end
  fprintf('BEGINNING PART NUMBER %d\n',partNum);
  if(length(PoolSize) > 1)
    C2_X = 'Pool size vector, not scalar\n';
    for i= 1:length(PoolSize)
      varn = sprintf('C2_X_%d',i);
      cmd = sprintf('%s = C2_LocalMaxInBox(SSIdxs, BBoxs, S2Root, SourceLocations, PoolSize(i));',varn);
      eval(cmd)
    end
  else
    [C2_X] = C2_LocalMaxInBox(SSIdxs, BBoxs, S2Root, SourceLocations, PoolSize);
  end
  fullsavename = sprintf('%s_%s_f%.3d.mat',savename, objname,partNum);
  if(length(PoolSize > 1))
    save(fullsavename,'C2_X_*','HelpTextInfo','S2Root');
  else
    save(fullsavename,'C2_X','HelpTextInfo','S2Root');
  end
  partNum = partNum + 1;
end

function [BBox, Idx,ThisExists] = GetBBoxAndSSIdxs(objname,partNum)
Root = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector';
l = dir(fullfile(Root, sprintf('AppearanceData_128x128_%s_f%.3d.mat',objname,partNum)));
BBox = [];
Idx =[];
if(length(l) == 0)
  ThisExists = 0;
  return;
end
ThisExists = 1;
load(fullfile(Root,l(1).name));%--> myIdx, myBBox, y
BBox = myBBox;
Idx = myIdx;

function Nsource = ReArrangeSource(source)
Nsource = [];
for i = 1:length(source)
  Nsource = [Nsource,source{i}];
end
