;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_SEQUENCE_PLACE
;
; Purpose     : Places sequence tables into OBSTBL.
;
; Explanation : This routine puts the sequence tables forming each study into the OBSTBL.
;               The optimum location for each table  is determined.
;               OBSTBL and OBSTBL_MAP are updated.
;               The mandatory control sequence is completed outside of this routine.
;
; Use         : < result = eis_cpt_sequence_place( sequences  ) >
;
; Inputs      : sequences               : STRUCTURE ARRAY of type EIS_CPT_sequence.
;
; Opt. Inputs : None.
;
; Outputs     : result      : INT flag :
;                               1 : processed science plan OK.
;                               0 : processing failed.
;
; Opt. Outputs: None.
;
; Keywords    : None.
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: May output ASCII files.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 261/10/04
;
; Modified    : Version 0.1, 26/01/05, MKC
;                 Changed how sequences constructed
;               Version 0.2, 01/02/05, MKC
;                 Changed study_index argument to seq_start and seq_end.
;                 Added extra control for re-use of sequence tables.
;                 Moved checksum and following study calculations.
;                 Removed obstbl argument.
;                 Added eis cpt obstbl.
;               Version 0.3, 02/03/05, MKC
;                 Corrected bug in logic.
;                 Added sequence position to eis cpt hex raster.
;                 Corrected seq_start unset.
;               Version 0.4, 18/03/05, MKC
;                 Moved eis cpt check study.
;               Version 0.5, 03/04/05, MKC
;                 Added sequences argument.
;               Version 0.6, 10/05/05, MKC
;                 Renamed from eis cpt process sequences for study.
;                 Changed INDEX keyword to LINK keyword.
;                 Fixed bug with loop back position.
;               Version 0.7, 05/07/05, MKC
;                 Used eis cpt set raster id.
;                 Implement only set slit/slot once per study and when changes.
;               Version 0.8, 21/07/05, MKC
;                 Added sequence ID to obstbl map description.
;               Version 0.9, 12/09/05, MKC
;                 Changed raster number/number of rasters to raster identifier.
;                 Changed sense of LINK keyword so that refers to previous table rather than following table.
;                 Changed so that uses an intermediate structure array of sequences.
;                 Removed sequence starts, sequence ends list of sequenes arguments.
;               Version 1.0, 14/07/06, MKC
;                 Writes sequence table even if exact match..
;
; Version     : 1.0, 14/07/06
;-
;**********************************************************

FUNCTION eis_cpt_sequence_place, sequences

  ; loop through sequences

  FOR s = 0, N_ELEMENTS(sequences)-1 DO BEGIN

    ; check if exact match was found

    IF sequences[s].exact THEN BEGIN

      ; update time and description of sequence table in OBSTBL
      ; NB rewrites table except for control sequence argument and checksum (though is exact match)

      IF NOT eis_cpt_obstbl(sequences[s].bytes, sequences[s].index, sequences[s].time, sequences[s].description, $
                              /SET, /SEQUENCE) THEN GOTO, error

    ENDIF ELSE BEGIN

      ; study not yet loaded

      ; undefine table_index

      eis_cpt_undefine, table_index

      ; get best place to put sequence (ignoring last two bytes) and add sequence to table
      ; NB cannot use already USED tables (check does not include linked table or checksum)

      IF NOT eis_cpt_obstbl(sequences[s].bytes, table_index, sequences[s].time, sequences[s].description, $
                            /PLACE, /SET, /SEQUENCE) THEN GOTO, error

      IF N_ELEMENTS(table_index) EQ 0 THEN MESSAGE, 'IMPLEMENTATION ERROR'

      ; store table index

      sequences[s].index = table_index

    ENDELSE

  ENDFOR

  ; return finished OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





