function PROC_InriaDBToNewFeature(FunctionName, NewLabel, options);
%function PROC_StreetScenesDBToNewFeature
%
% takes the first 4 groups of data from the street scenes database, and the name of a function
% which takes a grayscale image (matrix), the image size, and returns a 
% struct array of feature vectors (matricies).
%  Built in Parallelsim
%
% cmd = sprintf('saX2 = %s(X1,options)',FuncitonName)
% saX2 is a struct array of all matricies to be recorded (if multiple different features are recorded)
%
 
 d.HelpTextInfo = GetHelpTextInfo;
 d.ResetParallel = 0;
 d.bOverwrite = 1;
 d.bMandateSourceIndex = 0;
 d.OldLabel = 'GS';
 d.SourceRoot = '/cbcl/scratch03/bileschi/Images/INRIAPerson/MatrixFormat/';
 d.Sourcevariablename = 'X';
 d.nMaxNegativeSets = 10;
 if(nargin < 3), options = [];, end;
 options = ResolveMissingOptions(options,d);
 
 here = pwd;
 Root = options.SourceRoot;
 cd(Root);
 variablename = options.Sourcevariablename;
 Parallelizor = sprintf('/cbcl/scratch01/bileschi/Parallel_PROC_IDBTNF_%s.mat',FunctionName);
 if(not(options.bMandateSourceIndex))
   if((not(exist(Parallelizor))) || (options.ResetParallel))
     PROCEDURE_ResetMyIdx(Parallelizor);
   end
   myIdx = PROCEDURE_IncrementMyIdx(Parallelizor);
   whichtodo = [1,51,1+(1:options.nMaxNegativeSets),51+(1:options.nMaxNegativeSets)]
   while(isempty(intersect(myIdx,whichtodo)))
     if(myIdx > 102), break, end
     myIdx = PROCEDURE_IncrementMyIdx(Parallelizor);
   end
   sourceIdx = myIdx;
 else
   sourceIdx = options.bMandateSourceIndex;
   myIdx = sourceIdx;
 end
 
 if(myIdx > 102)
  fprintf('job completely allocated %d\n',myIdx);
 else
  savename = GetSaveName(myIdx,NewLabel,options.SourceRoot);
  loadname = GetSaveName(myIdx,options.OldLabel,options.SourceRoot);
  fprintf('FunctionName %s ProcessNumber %d starting...\n', FunctionName, myIdx);
  if(not(options.bOverwrite))
    if(exist(savename))
      fprintf('%s already exists.  No overwrite.\n',savename);
      return;
    end
  end
  [y, NewDataX] = GetNewDataX(loadname, variablename);
  cmd = sprintf('saNewMats = %s(NewDataX,options);',FunctionName);
  eval(cmd);
  fns = fieldnames(saNewMats);
  nfns = length(fns);
  savecmd = 'save(savename,''y'',''options''';
  for i = 1:nfns
     cmd = sprintf('%s = saNewMats.%s;',fns{i},fns{i});
     eval(cmd);
     savecmd = [savecmd,sprintf(',''%s''',fns{i})];
  end  
  savecmd = [savecmd,');'];
  eval(savecmd);
  fprintf('saving %s\n',savename);
 end
 cd(here);

function [yfull, NewDataX] = GetNewDataX(loadname, variablename)
 D = load(loadname);%-->grayX,toyotaX,y;
 NewDataX = getfield(D,variablename);
 yfull = D.y; 

function savename = GetSaveName(myIdx,FeatureType,Root);
if(myIdx == 1) || (myIdx == 51)
  p1 = 'Pos';
  p3 = [];
else
  p1 = 'Neg';
  p3 = sprintf('Part%.3d_',mod(myIdx-1,50));
end
if(myIdx < 51)
  p2 = 'Train';
else
  p2 = 'Test';
end
savename = [p1,p2,'Data_',p3,FeatureType,'.mat'];
savename = fullfile(Root, savename);
