;+
; NAME:
;	MAKE_SMEI_OP
;
;
; PURPOSE:
;	Simple interface to generating SMEI operations
;
;
; CATEGORY:
;	OPS
;
;
; CALLING SEQUENCE:
;	make_smei_op, ref, operator, arg0[, ... argn]
;
;
; INPUTS:
;	operator	string	The operator or function to apply
;	arg0		obj/str	Either: a SMEI_ARGUMENT object
;				reference, or a string defining an
;				argument , or a numeric (possibly
;				array) value.
;
;
; OPTIONAL INPUTS:
;	arg1 .. arg9	obj/str	Up to 9 further arguments to the
;				operator in the same form as arg0
;
;
; KEYWORD PARAMETERS:
;	/return_operator	If set, then return the SMEI_OPERATOR
;				object reference, by default the
;				generated sequence is returned.
;	get_level	int	The calling level from which to get
;				the object being argumentified
;				(usually only used by the
;				interpreter).
;	For "function" operators, any keys to the function can be passed.
;
;
; OUTPUTS:
;	ref	objref	The sequence or operator reference generated.
;
;
; NOTES:
;	When the argument is expressed as a string, then the object
;	referred must exist at the top $MAIN$ level in SSWIDL. The
;	format is "name{plane}[start:stop:step]", if no plane is given
;	then the "processed" plane is assumed, if no range specifier
;	is given for a sequence then the whole sequence is assumed.
;
; RESTRICTIONS:
;	A Maximum of 10 arguments to the operator may be specified
;
; MODIFICATION HISTORY:
;	Original: 28/10/03; SJT
;-
 
pro make_smei_op, ref, operator, arg0, arg1, arg2, arg3, arg4, arg5, $
                  arg6, arg7, arg8, arg9, return_operator = $
                  return_operator, get_level = get_level, _extra = $
                  _extra, help = help

if keyword_set(help) then begin
    self_help
    return
endif

top = size(operator, /type)
if top ne 7 then begin
    smei_msg, /alert, 'The operation must be a string'
    return
endif
if not arg_present(ref) then begin
    smei_msg, /alert, 'The reference must be a named variable'
    return
endif

if n_params() lt 3 then begin
    smei_msg, /alert, 'MAKE_SMEI_OP must have at least 3 arguments'
    return
endif

arglist = make_smei_arg(arg0, get_level = get_level, /transient)

for j = 4, n_params() do begin
    name = 'arg'+string(j-3, format = '(I1)')
    ok = execute('na = make_smei_arg('+name+ $
                 ', get_level = get_level, /transient)')
    if not ok then begin
        smei_msg, /alert,  ["Failed to add argument #"+string(j-3), $
                            !error_state.msg, !error_state.sys_msg]
        return
    endif
    arglist = [arglist, na]
endfor


opref = obj_new('SMEI_OPERATOR', operator, arglist, _extra = _extra)

if keyword_set(return_operator) then ref = opref $
else ref = opref -> get_sequence(/destroy)

end
