;+
; NAME:
;	SMEI_SEQUENCE
;
;
; PURPOSE:
;	Generate a SMEI sequence object
;
;
; CATEGORY:
;	CLI
;
;
; CALLING SEQUENCE:
;	smei_sequence, seqref, files[, name=name]
;	or
;	smei_sequence, seqref, start, stop[, name=name]
;	or
;	smei_sequence, seqref, /menu[, name=name]
;	
;
; INPUTS:
;	files	string	List of files or wildcards specifying the
;			files to build into the sequence (optional if
;			menu is requested).
;	start	dbl/int	Either a scalar JD of the start or an array with
;			[y,d,h,m,s]. N.B. even if only the year is to
;			be given it must be an array (e.g. [2003]
;			rather than 2003)
;	stop	dbl/int	Either a scalar JD of the end point or an array with
;			[y,d,h,m,s]. N.B. even if only the year is to
;			be given it must be an array (e.g. [2003]
;			rather than 2003)
;
; KEYWORD PARAMETERS:
;	name	string	A name to give to the sequence
;	/show		If set, then go ahead and show the sequence.
;	/compact	If set, then compact the images.
;	/menu		If set, then use a GUI to set options
;	/aitoff		If set, then use aitoff projections only (only
;			applicable if a time range is given).
;	/fisheye	If set, then use fisheye projections only (only
;			applicable if a time range is given).
;	/rectangular	If set, then use rectangular projections only
;			(only applicable if a time range is given).
;	/nostop		If set, then don't stop if no images are
;			found (useful for batch operation). It will
;			still stop on bad inputs.
;	/silent		If set, then supress messages from readfits.
;
;
; OUTPUTS:
;	seqref	objref	The object reference of the sequence
;
;
; RESTRICTIONS:
;	Will later aquire a GUI and ways of setting time bounds
;
;
; MODIFICATION HISTORY:
;	Original: 11/12/02; SJT
;	Add compact & menu: 13/12/02; SJT
;	Check the we're not overwiting an existing object: 19/12/02;
;	SJT
;	Add support for aitoff & fisheye projections: 18/2/04; SJT
;	Include images starting up to 1 orbit before the given start
;	time: 12/10/06; SJT
;	Add rectangular images: 25/11/09; SJT
;	Add silent keyword: 8/7/10; SJT
;-

pro smei_sequence, seq, lim0,  lim1, name = name, show = show, compact = $
                   compact, menu = menu, aitoff = aitoff, fisheye = $
                   fisheye, nostop = nostop, rectangular = $
                   rectangular, silent = silent, help = help

if keyword_set(help) then begin
    self_help
    return
endif

if n_params() eq 0 then begin
    smei_msg, /alert, 'SMEI_SEQUENCE requires a sequence argument'
    return
endif

if obj_valid(seq) then begin
    smei_msg, /alert, ['The sequence specified is already a valid', $
                       'object reference, please destroy it before', $
                       'trying to re-initialize it']
    return
endif

orbit = 0.074028                ; One processing orbit of 1:46:36 in
                                ; days.

if n_params() eq 2 then files = lim0 $
else if n_params() eq 3 then begin
    s0 = size(lim0)
    case s0[0] of
        1: jd0 = doy2jd(lim0)
        0: jd0 = lim0
        else: begin
            smei_msg, /alert, 'A time limit must be a scalar or a 1_D ' + $
                      'array'
            return
        end
    endcase
    s1 = size(lim1)
    case s1[0] of
        1: jd1 = doy2jd(lim1)
        0: jd1 = lim1
        else: begin
            smei_msg, /alert, 'A time limit must be a scalar or a 1_D ' + $
                      'array'
            return
        end
    endcase

    sipath = getenv('SMEI_IMAGES')
    if sipath eq '' then sipath = getenv('PWD')

    fdescs = ff_summary('*.fit*', path = sipath, filelist = filel, $
                        startlist = starts, projections = projections, $
                        count = nfound)

    if nfound eq 0 then begin
        smei_msg, /warn, "No files found in path"
        return
    endif

    nps = keyword_set(fisheye) + keyword_set(aitoff) + $
      keyword_set(rectangular)

    if nps gt 1 then begin
        smei_msg, /warn, ["May only specify one of the fisheye,", $
                          "rectangular and aitoff keys"]
        return
    endif
    if keyword_set(fisheye) then begin
        locs = where(strupcase(projections) eq "FISHEYE", npr)
    endif else if keyword_set(rectangular) then begin
        locs = where(strupcase(projections) eq "RECTANG", npr)
    endif else begin
        if not keyword_set(aitoff) then $
          smei_msg, /warn, ["No projection keyword given", $
                            "assuming Aitoff"]
        locs = where(strtrim(strupcase(projections)) eq "AITOFF", npr)
    endelse
    if npr eq 0 then begin
        smei_msg, /warn, "No images matching selected projection"
        return
    endif
    filel = filel[*, locs]
    starts = starts[locs]

    idx = sort(starts)
    filel = filel[*, idx]
    starts = starts[idx]
    locs = where(starts ge jd0-orbit and starts lt jd1, nok)
    if nok eq 0 then begin
        iwarn = keyword_set(nostop)
        smei_msg, alert = ~iwarn, warn = iwarn, 'No files found in ' + $
                  'time range'
        return
    endif
    files = filel[0, locs]+filel[1, locs]
endif

seq = obj_new('SMEI_SEQUENCE', files, name = name, compact = compact, $
              menu = menu, silent = keyword_set(silent))

if keyword_set(show) and obj_valid(seq) and not keyword_set(menu) $
  then seq -> show

end
