!   TITLE:  arry_search.mac (MANAGEMENT)
!
!
!       Added search capability within row, column and plane range values.
!
!   OBJECTIVE:
!
!   This sub-macro searches an array for a user specified element value and
!   returns the indices of that element.
!
!
!   COMMAND SYNTAX:
!
!                         (1)       (2)     (3) (4) (5) (6) (7) (8)
!        ARRY_SEARCH, 'arry_nam', elem_val, ri, rf, ci, cf, pi, pf
!
!
!   ARGUMENTS:
!
!       (1) arry_nam = the name of the array. (in single quotes)
!
!       (2) elem_val = the element value within the array to search for.
!
!       (3,4) ri/rf = initial/final row index:
!                     -If both are 0 (or blank) or out-of-range, all rows are
!                       queried.
!                     -If either one is 0 (or blank), or out-of-range, and the
!                       other is in-range, then only that row (in-range value)
!                       is queried.
!                     -If both are in-range and not equal, then the range is
!                       from lowest to highest (independent of order).
!
!       (5,6) ci/cf = initial/final column index:
!                     -If both are 0 (or blank) or out-of-range, all columns
!                       are queried.
!                     -If either one is 0 (or blank), or out-of-range, and the
!                       other is in-range, then only that column (in-range value)
!                       is queried.
!                     -If both are in-range and not equal, then the range is
!                       from lowest to highest (independent of order).
!
!       (7,8) pi/pf = initial/final plane index:
!                     -If both are 0 (or blank) or out-of-range, all planes
!                       are queried.
!                     -If either one is 0 (or blank), or out-of-range, and the
!                       other is in-range, then only that plane (in-range value)
!                       is queried.
!                     -If both are in-range and not equal, then the range is
!                       from lowest to highest (independent of order).
!
!
!   DESCRIPTION:
!
!   This macro searches a user specified array for a specified element value,
!   and returns the index positions of that value in an array called 'hit'.
!   This array is an N x 3 array, where N is the number of occurences ("hits")
!   of the element value, and 3 corresponds to each of the 1st, 2nd and 3rd
!   columns containing the row (r), column (c) and plane (p) indices for that
!   element, respectively.
!
!   If more than 1 element is found (and, therefore, the 'hit' array has more
!   than 1 row), then each row is a particular element. Therefore, 'hit' has
!   the following form:
!
!                                        COLUMNS
!                       ROW     |   1   |   2   |   3   |
!                   -------------------------------------
!       1st hit -->     1       |   r1  |   c1  |   p1  |
!       2nd hit -->     2       |   r2  |   c2  |   p2  |
!       3rd hit -->     3       |   r3  |   c3  |   p3  |
!           .           .       |   .   |   .   |   .   |
!           .           .       |   .   |   .   |   .   |
!       Nth hit -->     N       |   rN  |   cN  |   pN  |
!
!   where, the rN, cN, and pN are the row, column and plane index positions
!   of the Nth element found in the array.
!
!   Additionally, the parameter 'hits' is the number of occurences (hits) of
!   the element value in the array which is output by this macro:
!
!                           hits = N
!
!   The specified element value may be searched within a subset of all elements
!   in the array. The row, column and plane range-values within which to search
!   is specified in arguments 3 through 8.
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
arrynam_=arg1
arryval_=arg2
rinit_=arg3
rfin_=arg4
cinit_=arg5
cfin_=arg6
pinit_=arg7
pfin_=arg8
!
*get,hittyp_,parm,hit,type
!
*if,hittyp_,ne,-1,then
    *set,hit(1),
*endif
!
*get,rsiz_,parm,%arrynam_%,dim,1
*get,csiz_,parm,%arrynam_%,dim,2
*get,psiz_,parm,%arrynam_%,dim,3
!
*do,indx_,1,3,1
    *if,indx_,eq,1,then
        init_='rinit_'
        fin_='rfin_'
        siz_='rsiz_'
    *elseif,indx_,eq,2,then
        init_='cinit_'
        fin_='cfin_'
        siz_='csiz_'
    *elseif,indx_,eq,3,then
        init_='pinit_'
        fin_='pfin_'
        siz_='psiz_'
    *endif
    !
    *if,%init_%,le,0,or,%init_%,gt,%siz_%,then
        *if,%init_%,eq,0,xor,%fin_%,eq,0,then
            *if,%init_%,eq,0,then
                *if,%fin_%,gt,0,and,%fin_%,le,%siz_%,then
                    %init_%=%fin_%
                    bypass_=1
                *elseif,%fin_%,lt,0,and,%fin_%,gt,%siz_%,then
                    %init_%=1
                    %fin_%=%siz_%
                    bypass_=1
                *endif
            *else
                *if,%init_%,gt,0,and,%init_%,le,%siz_%,then
                    %fin_%=%init_%
                    bypass_=1
                *elseif,%init_%,lt,0,or,%init_%,gt,%siz_%,then
                    %init_%=1
                    %fin_%=%siz_%
                    bypass_=1
                *endif
            *endif
        *elseif,%init_%,eq,0,and,%fin_%,eq,0,then
            %init_%=1
            %fin_%=%siz_%
            bypass_=1
        *endif
        !
        *if,bypass_,ne,1,then
            *if,%fin_%,lt,0,or,%fin_%,gt,%siz_%,then
                %init_%=1
                %fin_%=%siz_%
            *elseif,%fin_%,gt,0,and,%fin_%,le,%siz_%,then
                %init_%=%fin_%
            *endif
        *else
            *set,bypass_,
        *endif
    *elseif,%init_%,gt,0,and,%init_%,le,%siz_%,then
        *if,%fin_%,gt,0,and,%fin_%,le,%siz_%,then
            *if,%init_%,gt,%fin_%,then
                rhld_=%init_%
                %init_%=%fin_%
                %fin_%=rhld_
            *endif
        *else
            %fin_%=%init_%
        *endif
    *endif
*enddo
!
rowhit_=0
!
*do,piter_,pinit_,pfin_,1
    *do,citer_,cinit_,cfin_,1
        *do,riter_,rinit_,rfin_,1
            *if,%arrynam_%(riter_,citer_,piter_),eq,arryval_,then
                *if,rowhit_,eq,0,then
                    *dim,hit,array,1,3
                    !
                    hit(1,1)=riter_
                    hit(1,2)=citer_
                    hit(1,3)=piter_
                    !
                    *get,rowhit_,parm,hit,dim,x
                *else
                    *dim,buffer_,array,rowhit_+1,3
                    !
                    *do,abc_,1,rowhit_,1
                        buffer_(abc_,1)=hit(abc_,1)
                        buffer_(abc_,2)=hit(abc_,2)
                        buffer_(abc_,3)=hit(abc_,3)
                    *enddo
                    !
                    *set,hit(1),
                    !
                    *dim,hit,array,rowhit_+1,3
                    !
                    *do,abc_,1,rowhit_,1
                        hit(abc_,1)=buffer_(abc_,1)
                        hit(abc_,2)=buffer_(abc_,2)
                        hit(abc_,3)=buffer_(abc_,3)
                    *enddo
                    !
                    hit(rowhit_+1,1)=riter_
                    hit(rowhit_+1,2)=citer_
                    hit(rowhit_+1,3)=piter_
                    !
                    *get,rowhit_,parm,hit,dim,x
                    !
                    *set,buffer_(1),
                *endif
            *endif
        *enddo
    *enddo
*enddo
!
hits=rowhit_
!
*set,abc_,
*set,rowhit_,
*set,hittyp_,
*set,arrynam_,
*set,arryval_,
*set,rinit_,
*set,cinit_,
*set,pinit_,
*set,rfin_,
*set,cfin_,
*set,pfin_,
*set,riter_,
*set,citer_,
*set,piter_,
*set,rsiz_,
*set,csiz_,
*set,psiz_,
*set,bypass_,
!
*if,prkey_,eq,1,then
    /go
*endif