;+
; NAME:
;	SMEI_SEQUENCE::ADD_IMAGE
;
;
; PURPOSE:
;	Add a new image to a SMEI sequence
;
;
; CATEGORY:
;	SMEI_SEQUENCE
;
;
; CALLING SEQUENCE:
;	imref = seqref -> add_image(file)
;
;
; INPUTS:
;	file	string	The FITS file containing the image.
;
;
; 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.
;	/silent	Passed via image constructor to readfits, if set, then
;		messages about the images read are supressed
;	image_data	Float	The image values (usually a 1-plane
;				derived image)
;	ancil_data	float	The ancillary data for the image (DI)
;	flag_data	uint	The contaminant flagging data for the
;				image (DI)
;	start		double	The start time of the image (DI only)
;	stop		double	The end time of the image (DI only)
;	unit		string	The unit of the image (DI)
;	centre		float	The centre point of the image
;				coordinates (DI)
;       scale		float	The image scale (DI)
;       fitshead	string	A "fake" fitsheader for the image (DI)
;       ancilhead	string	A "fake" ancil data header (DI)
;       flaghead	string	A "fake" flag data header (DI)
;       flags		struct	The processing flags for the image (DI)
;       filename	string	The filename of the "dominant" image (DI)
;       projection	int	The projection of the "dominant" image (DI)
;
;
; OUTPUTS:
;	imref	objref	The objrect reference of the image added (or
;			invalid on failure)
;
; MODIFICATION HISTORY:
;	Original: 10/12/02; SJT
;	Add compact option: 13/12/02; SJT
;	Corrected documentation: 19/12/02; SJT
;	Added silent key: 10/1/03; SJT
;	Added keys to handle new methods of deriving images: 22/10/03; SJT
;	Add filename key for derived images: 1/12/03; SJT
;	Add projection key for derived images: 13/2/04; SJT
;	Add ancil and flag data keys: 24/10/07; SJT
;-

function smei_sequence::add_image, file, last = last, first = first, $
                      after = after, before = before, $ 
                      compact = compact, silent = silent, $
                      image_data = image_data, start = $
                      start, stop = stop, unit = unit, $
                      centre =  centre, scale = scale, $
                      cvalue = cvalue, fitshead = fitshead, $
                      flags = flags, filename = filename, projection = $ 
                      projection, ancil_data = ancil_data, flag_data = $
                      flag_data, ancilhead = ancilhead, flaghead = $
                      flaghead

if keyword_set(first) then begin
    image = obj_new('SMEI_IMAGE', self, file = file, compact = $
                    compact, silent = silent, image_data = image_data, $
                    start = start, stop = stop, unit = unit, centre = $
                    centre, scale = scale, fitshead = fitshead, flags $
                    = flags, fakename = filename, projection = $
                    projection, ancil_data = ancil_data, flag_data = $
                    flag_data, ancilhead = ancilhead, flaghead = $
                    flaghead, cvalue = cvalue)
endif else if keyword_set(after) then begin
    s = size(after, /type)
    switch s of
        1:
        2:
        3:
        12:
        13:
        14:
        15:begin                ; The various integer types
            im = self -> get_image(after)
            break
        end
        11: begin               ; An object reference
            im = after
            break
        end
        else: smei_msg, /alert, "Invalid type for AFTER "+size(after, $
                                                               /tname)
    endswitch
    image = obj_new('SMEI_IMAGE', self, im, file = file, compact = $
                    compact, silent = silent, $
                    image_data = image_data, start = $
                    start, stop = stop, unit = unit, $
                    centre =  centre, scale = scale, $
                    fitshead = fitshead, $
                    flags = flags, fakename = filename, projection = $
                    projection, ancil_data = ancil_data, flag_data = $
                    flag_data, ancilhead = ancilhead, flaghead = $
                    flaghead, cvalue = cvalue)
endif else if keyword_set(before) then begin
    s = size(before, /type)
    switch s of
        1:
        2:
        3:
        12:
        13:
        14:
        15: begin               ; The various integer types
            if before eq 0 then im = obj_new() $
            else im = self -> get_image(before-1)
            break
        end
        11: begin               ; An object reference
            im = before -> get_prev()
            break
        end
        else: smei_msg, /alert, "Invalid type for BEFORE " + $
          size(before, /tname)
    endswitch
    image = obj_new('SMEI_IMAGE', self, im, file = file, compact = $
                    compact, silent = silen, $
                    image_data = image_data, start = $
                    start, stop = stop, unit = unit, $
                    centre =  centre, scale = scale, $
                    fitshead = fitshead, $
                    flags = flags, fakename = filename, projection = $
                    projection, ancil_data = ancil_data, flag_data = $
                    flag_data, ancilhead = ancilhead, flaghead = $
                    flaghead, cvalue = cvalue) 
endif else begin
    image = obj_new('SMEI_IMAGE', self, self -> get_last(), file = $
                    file, compact = compact, silent = silent, $
                    image_data = image_data, start = start, stop = $
                    stop, unit = unit, centre =  centre, scale = $
                    scale, fitshead = fitshead, flags = flags, $
                    fakename = filename, projection = projection, $
                    ancil_data = ancil_data, flag_data = flag_data, $
                    ancilhead = ancilhead, flaghead = $ 
                    flaghead, cvalue = cvalue) 
endelse
self -> mk_pixmap, /destroy

return, image

end
