;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_PROCESS_ENGINEERING_SEQUENCES
;
; Purpose     : Implements engineering study as OBSTBL sequence tables
;
; Explanation : An engineering study consists of a single Intel Hex file sequence table.
;               There optimum location is determined.
;               OBSTBL and OBSTBL_MAP are updated.
;               The mandatory control sequence is completed
;               outside of this routine.
;
; Use         : < result = eis_cpt_process_engineering_sequences( engineering_parameters, study_detail, engineering_detail, $
;                                                     time_for_reuse, linelist, seq_start, seq_end, sequences ) >
;
; Inputs      : engineering_parameters  : STRUCTURE of type EIS_engineering_parameters.
;               study_detail            : STRUCTURE of type EIS_study_detail.
;               engineering_detail      : STRUCTURE ARRAY of type EIS_engineering_detail.
;               time_for_reuse          : DOUBLE TAI time after which sequences may be overwritten.
;               linelists               : INTARR[] gives linelist table index for engineering table.
;
; Opt. Inputs : None.
;
; Outputs     : result      : INT flag :
;                               1 : processed science plan OK.
;                               0 : processing failed.
;               sequences   : STRUCTURE ARRAY of type EIS_CPT_sequence.
;
; 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 10/05/05
;
; Modified    : Version 0.1, 28/06/05, MKC
;                 Added ISACS PLN type engineering files.
;               Version 0.2, 14/07/05, MKC
;                 Used file name directly.
;                 Renamed engineering study filename tag.
;                 Removed onboard tag from engineering detail.
;                 Added sequence ID to obstbl map description.
;               Version 0.3, 12/09/05, MKC
;                 Changed so that uses an intermediate structure array of sequences.
;                 Added study repeats.
;               Version 0.4, 19/07/06, MKC
;                 Added first sequence in study flag.
;               Version 0.5, 12/10/06, MKC
;                 Added multiple linelisst for engineering studies.
;               Version 0.6, 10/01/07, MKC
;                 Added disallowed slit/slot movement.
;
; Version     : 0.6, 10/01/07
;-
;**********************************************************

FUNCTION eis_cpt_process_engineering_sequences, engineering_parameters, study_detail, engineering_detail, $
                                                time_for_reuse, linelists, sequences

  ; set up sqeuence data structure

  sequence = {EIS_CPT_sequence}

  ; get sequence ID in bytes

  seqid = eis_cpt_uinttobytes(study_detail.study_identifier)

  ; set up ID

  sequence.study_identifier = study_detail.study_identifier

  ; set up sequence link for study

  sequence.linked_study_identifier = study_detail.linked_study_identifier

  ; set up study repeat number

  sequence.study_repeat_number = study_detail.study_repeat_number

  ; set up slit/slot disallowed flag

  sequence.slit_slot.disallowed = study_detail.slit_slot.disallowed

  ; set up description used in obstbl_map

  sequence.description = eis_cpt_bytestohex(seqid,/OX) + ' : ' + study_detail.description

  ; set up time associated with tables

  sequence.time = time_for_reuse

  ; set first sequence in study flag

  sequence.first = 1

  ; set last sequence in study flag

  sequence.last = 1

  ; get engineering sequence table

  IF NOT eis_cpt_read_intel_hex_file( engineering_detail.study_filename, start_address, header, $
                                      ulba, bytes, /NOENDOFFILE) THEN GOTO, error

  ; get sequence length

  length = bytes[0]

  IF length EQ 0 OR length GT N_ELEMENTS(bytes) THEN BEGIN

    eis_cpt_message, 'ERROR, INVALID ENGINEERING SEQUENCE LENGTH'

    GOTO, error

  ENDIF

  ; truncate padded values

  bytes = bytes[0:length-1]

  ; check sequence valid
  ; NB if sequence is [0] then will be checked and fail check

  IF NOT eis_cpt_obstbl(bytes, /CHECK, /SEQUENCE, RASTERS=rasters) THEN GOTO, error

  ; overwrite sequence ID

  bytes[1:2] = seqid

  ; overwrite sequence repeat number

  bytes[3] = BYTE(study_detail.study_repeat_number)

  ; use dummy mandatory sequence control and dummy checksum

  IF study_detail.linked_study_identifier NE 0 THEN BEGIN

    ; there is a follow on study

    bytes[length-3:length-1] = [eis_cpt_call_seq(0), 0B]

  ENDIF ELSE BEGIN

    ; there is no follow on study

    bytes[length-3:length-1] = [eis_cpt_term_seq(0), 0B]

  ENDELSE

  ; deal with associated linelists

  ; get different linelists used in study

  IF N_ELEMENTS(rasters) GT 0 THEN BEGIN

    ; get linelists for each raster

    linelist_indexes = bytes[rasters+21]

    ; check linelists present
    ; there is a linelists element for every engineering_detail.linelist_filenames ne blank
    ; for each of these engineering_detail.linelist_indexes is unique
    ; want to match every linelist_indexes with a corresponding engineering_detail.linelist_indexes
    ; NB allows items in engineering_detail.linelist_indexes to not actually be used

    list = WHERE(engineering_detail.linelist_filenames NE '', count)

    IF count EQ 0 THEN BEGIN

      eis_cpt_message, 'ERROR, MISSING LINELISTS FOR '+engineering_detail.study_filename

      GOTO, error

    ENDIF

    ; get index representing each element in linelists

    indexes = engineering_detail.linelist_indexes[list]

    ; set linelist for each raster

    FOR k = 0, N_ELEMENTS(rasters)-1 DO BEGIN

      list = WHERE(indexes EQ linelist_indexes[k], count)

      IF count NE 1 THEN BEGIN

        eis_cpt_message, 'ERROR, MISMATCHED LINELISTS FOR '+engineering_detail.study_filename

        GOTO, error

      ENDIF

      ; NB list indexes into indexes and therefore linelists

      bytes[rasters[k]+21] = linelists[list[0]]

    ENDFOR

  ENDIF

  ; add to sequence structure

  sequence.bytes = bytes

  ; add sequence to list

  eis_cpt_add, sequences, sequence

  ; return finished OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





