function [SelectedFlag]=simcat_colrange(Sim,ColSelectRange,UnifyFun)
%--------------------------------------------------------------------------
% simcat_colrange function                                          ImPhot
% Description: Given a SIM array with catalog, and a list of column names
%              and ranges, select from each catalog only entries in which
%              the values in the specified columns is in some range.
% Input  : - SIM array of images including populated catalogs.
%          - Three columns cell array indicating the column name
%            and its lower and upper range.
%            For example: {'APASS_htm_r',14,16;'APASS_htm_g',17,18}
%            will select entries with APASS r-band mag in the range 14 to
%            16 and APASS g-band mag in the range 17 to 18.
%          - A function by which to unify the selecttion criteria.
%            Empty matrix will do nothing and return a column of boolean
%            flags per criteria. @all will operate like the and operator,
%            and @any will operate like the or operator.
%            Default is @all.
% Output : - Structure array (element per SIM image) in which the Flag
%            field contains boolean flags indicating if the entries in
%            the catalog were selected (i.e., in the ranges).
% 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:
% [SelectedFlag]=simcat_colrange(Sim,{'APASS_htm_r',14,16;'APASS_htm_g',17,18});
% Reliable: 2
%--------------------------------------------------------------------------
error('Obsolote function: use col_range.m instead or remove error message');

if (nargin==2),
    UnifyFun = @all;
end

CatField            = 'Cat';
CatColField         = 'Col';

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

Nsim = numel(Sim);

Ncrit = size(ColSelectRange,1);

SelectedFlag = struct('Flag',cell(Nsim,1));
for Isim=1:1:Nsim,
    % for each SIM image catalog
    Nobj = size(Sim(Isim).(CatField),1);
    SelectedFlag(Isim).Flag = false(Nobj,Ncrit);
    for Icrit=1:1:Ncrit,
        % for each criteria
        Val = Sim(Isim).(CatField)(:,Sim(Isim).(CatColField).(ColSelectRange{Icrit,1}));
        SelectedFlag(Isim).Flag(:,Icrit) = Val>=ColSelectRange{Icrit,2} & Val<=ColSelectRange{Icrit,3};
    end
    if (~isempty(UnifyFun)),
        % unify columns
        SelectedFlag(Isim).Flag = UnifyFun(SelectedFlag(Isim).Flag,2);
    end
     
    
end