function NormFact=catframe(x,FileName,Norm,Intermediate,Decimation)
% CATFRAME  Concatenate an image to a HIPS sequence
%	catframe(x,FileName) concatenate the image x to a HIPS sequence
%	c=catframe(x,FileName,'UnitVar') normalize to unit variance first
%	catframe(x,FileName,c) divide x by the scalar c first
%	catframe(x,FileName,-,'Interm') intermediate, store the files as
%	  FileName_001, FileName_002 etc., to be concluded by:
%	catframe(x,FileName,-,'Final'). This yields a faster operation.
%	catframe(x,FileName,-,-,2) decimates by two in each dimension first

if exist('Norm')~=1,         Norm=[];         end
if exist('Intermediate')~=1, Intermediate=[]; end
if exist('Decimation')~=1,   Decimation=1;    end
CurrFrameFN=tempnam('catframe');
TmpSeqFN=tempnam('catframe');

[M_x,N_x]=size(x);
x=x(1:Decimation:M_x,1:Decimation:N_x);
[M_x,N_x]=size(x);

% Normalization
NormFact=1;
if strcmp(lower(Norm),lower('UnitVar'))
  NormFact=std(x(:));
elseif ~isstr(Norm) & (length(Norm)==1)
  NormFact=Norm;
end
x=x/NormFact;

% Write the current frame
if isempty(x)
  CurrFrameFN='';
else
  hipswrite(x,CurrFrameFN);
end

% Sequence management
if isempty(Intermediate)
  % Add the image to the image sequence
  if exist(FileName)==2
    eval(sprintf('!catframes %s %s > %s',FileName,CurrFrameFN,TmpSeqFN));
    eval(sprintf('!mv %s %s',TmpSeqFN,FileName));
  else
    eval(sprintf('!mv %s %s',CurrFrameFN,FileName));
  end
else
  if strcmp(lower(Intermediate),'interm')
    % Store current frame as a separate file
    SeqNum=0;
    while exist(sprintf('%s_%03d',FileName,SeqNum))==2
      SeqNum=SeqNum+1;
    end
    eval(sprintf('!mv %s %s_%03d',CurrFrameFN,FileName,SeqNum));
  elseif strcmp(lower(Intermediate),'final')
    % Concatenate separate files to one sequence
    [dummy,Comps]=unix(sprintf('ls -1 %s_*',FileName));
    Comps=deblank(strrep(Comps,setstr(10),' ')); % Replace newlines by spaces
    SepPos=[0 findstr(Comps,' ')];
    if length(Comps)>100
      % The list of all files will be too long:
      % Break the concatenation problem up into subproblems
      for sp=1:6:length(SepPos)
	FirstPos=SepPos(sp)+1;
	if sp+6>length(SepPos)
	  LastPos=length(Comps);
	else 
	  LastPos=SepPos(sp+6)-1; 
	end
	TheseComps=Comps(FirstPos:LastPos);
	if sp==1
	  eval(sprintf('!catframes %s > %s',TheseComps,TmpSeqFN));
	else
	  eval(sprintf('!catframes %s %s > %s',FileName,TheseComps,TmpSeqFN));
	end
	eval(sprintf('!mv %s %s',TmpSeqFN,FileName));
	eval(sprintf('!rm -f %s',TheseComps));
      end
    elseif length(SepPos)>1 | ~strcmp(CurrFrameFN,'')
      SepPos
      Comps
      CurrFrameFN
      FileName
      eval(sprintf('!catframes %s %s > %s',Comps,CurrFrameFN,FileName));
    else
      eval(sprintf('!mv %s %s',Comps,FileName));
    end
    eval(sprintf('!rm -f %s',Comps));
    eval(sprintf('!rm -f %s_*',FileName));
    fprintf(1,'(%d) ',length(SepPos)+1);
  else
    eval(sprintf('!rm -f %s %s',CurrFrameFN,TmpSeqFN));
    error(sprintf('Unknown parameter "%s" (legal: "interm" and "final")',...
      Intermediate));
  end
end

% Clean up
eval(sprintf('!rm -f %s %s',CurrFrameFN,TmpSeqFN));
