function Res=simcat_colarith(Sim,Expression)
%--------------------------------------------------------------------------
% simcat_colarith function                                          ImPhot
% Description: Given a SIM array of images with a catalog and arithmatic
%              expression on the catalog columns, evaluate the expression.
% Input  : - SIM array of images with catalogs.
%          - Expression on the columns, or a cell array of multiple
%            expressions.
% Output : - A structure array (elemnt per SIM) with a field 'Cat'
%            containing the evaluated column.
% License: GNU general public license version 3
% Tested : Matlab R2015b
%     By : Eran O. Ofek                    Feb 2016
%    URL : http://weizmann.ac.il/home/eofek/matlab/
% Example: Res=simcat_colarith(Sim,'XWIN_IMAGE + sin(YWIN_IMAGE)')
%          % e.g., calculate the pixel phase in X and Y
%          Res = simcat_colarith(Sim,{'mod(XWIN_IMAGE,1)','mod(YWIN_IMAGE,1)'});
% Reliable: 2
%--------------------------------------------------------------------------
error('Obsolote function: use col_arith.m instead or remove error message');

DefVal = 0;

CatField            = 'Cat';
CatColField         = 'Col';
CatColCellField     = 'ColCell';

if (~iscell(Expression)),
    Expression = {Expression};
end
Nexp = numel(Expression);

if (~issim(Sim)),
    error('Input for simcat_colarith.m must be a SIM array of images');
end

Nsim = numel(Sim);

Res  = struct('Cat',cell(Nsim,1));
for Isim=1:1:Nsim,
    Ncol = numel(Sim(Isim).(CatColCellField));
    Nobj = size(Sim(Isim).(CatField),1);
    for Icol=1:1:Ncol,
        % assign the catalog column into a variable name with the column
        % name:
        %sprintf('%s = %s;',Sim(Isim).(CatColCellField){Icol},'Sim(Isim).(CatField)(:,Sim(Isim).(CatColField).(Sim(Isim).(CatColCellField){Icol}))')
        eval(sprintf('%s = %s;',Sim(Isim).(CatColCellField){Icol},'Sim(Isim).(CatField)(:,Sim(Isim).(CatColField).(Sim(Isim).(CatColCellField){Icol}))'));
    end
    for Iexp=1:1:Nexp,
        if (isempty(Expression{Iexp})),
            Res(Isim).Cat(:,Iexp) = DefVal.*ones(Nobj,1);
        else
            Res(Isim).Cat(:,Iexp) = eval(Expression{Iexp});
        end
    end
end
