;+
; NAME:
;	SMEI_ADD_IMAGE
;
;
; PURPOSE:
;	Add one or more images to a SMEI sequence
;
;
; CATEGORY:
;	CLI
;
;
; CALLING SEQUENCE:
;	smei_add_image, seqref, files
;
;
; INPUTS:
;	seqref	objref	Object reference of the SMEI sequence
;	files	string	scalar string or string array containing the
;			FITS file(s) to be added, such wildcards as
;			findfile recognizes for your OS are
;			allowed. Currently it must be an explicit path
;			(or the files in the current dir).
;
;
; OPTIONAL INPUTS:
;	
;
;
; KEYWORD PARAMETERS:
;	/first	If set, then add the image at the beginning of the
;		sequence.
;	/last	If set, add the image at the end of the sequence (this
;		is the default behaviour).
;	after	Either a location number or an image reference after
;		which the new images is to be inserted.
;	before	Either a location number of an image reference before
;		which the new image is to be inserted.
;	/compact If set, then compact the image on adding it.	
;
;
; MODIFICATION HISTORY:
;	Original: 19/12/02; SJT
;	Fix some very visible bugs: 30/7/03; SJT
;-

pro smei_add_image, seqref, files, last = last, first = first, $
                    after = after, before = before, compact = compact, $
                    help = help

if keyword_set(help) then begin
    self_help
    return
end

if not obj_valid(seqref) then begin
    smei_msg, /alert,  'SMEI_ADD_IMAGE requires an object reference'
    return
endif

if not obj_isa(seqref, 'SMEI_SEQUENCE') then begin
    smei_msg, /alert, $
              'The object you are trying to modify is not a ' + $
              'SMEI_SEQUENCE'
    return
endif

flist = ''

for j = 0, n_elements(files)-1 do flist = [flist, file_search(files[j])]

locs = where(flist ne '', nfiles)
if nfiles eq 0 then begin
    smei_msg, /warn, 'None of the specified files was found'
    return
endif else flist = flist[locs]

for j = 0, nfiles-1 do $
  if not obj_valid(seqref -> add_image(flist[j],  last = last, first = $
                                       first, after = after, before = $
                                       before, compact = compact)) $
  then smei_msg, /warn, "Failed to add: "+flist[j]

end
