;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_OBSTBL_SET
;
; Purpose     : Inserts table into OBSTBL.
;
; Explanation : Inserts table into OBSTBL.
;
; Use         : < res = eis_cpt_obstbl_set(obstbl, table_index, table, time_for_reuse, description ) >
;
; Inputs      : obstbl         : STRUCTURE of type EIS_CPT_obstbl.
;               table_index    : INT index of table.
;               table          : BYTARR[] table data
;               time_for_reuse : DOUBLE TAI time after which table may be reused.
;               description    : STRING description of table
;
; Opt. Inputs : None.
;
; Outputs     : result         : INT flag :
;                                1 : processed OK.
;                                0 : processing failed.
;
; Opt. Outputs: None.
;
; Keywords    : SEQUENCE          : FLAG indicating that processing a sequence.
;               LINELIST          : FLAG indicating that processing a linelist
;               AEC_EXPOSURE      : FLAG indicating that processing an AEC exposure table
;               AEC_TABLE         : FLAG indicating that processing an AEC control table
;               XRT_FLARE         : FLAG indicating that processing an XRT flare trigger  control table
;               EIS_FLARE         : FLAG indicating that processing an EIS flare trigger  control table
;               EIS_EVENT         : FLAG indicating that processing an EIS event trigger  control table
;               RESPONSE          : FLAG indicating that table is used in response study.
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 04/03/05
;
; Modified    : Version 0.0, 04/03/05, MKC
;               Version 0.1, 29/04/05, MKC
;                 Changed use of !EIS_CPT_CONSTANTS.
;               Version 0.2, 16/05/05, MKC
;                 Changed processing of linelist map details.
;               Version 0.3, 25/06/05, MKC
;                 Added map entries for control tables.
;               Version 0.4, 28/06/05, MKC
;                 Added map entries for aec_exposure table.
;               Version 0.5, 16/09/05, MKC
;                 Split into two routines : PLACE and SET.
;                 Changed delimiter between comments to '#'
;                 Added keyword response.
;               Version 0.6, 04/10/05, MKC
;                 Allowed re-use of control tables..
;               Version 0.7, 17/06/06, MKC
;                 Changed so that always writes table even if exact match.
;               Version 0.8, 14/07/06, MKC
;                 Changed so that always writes sequence table even if exact match.
;
; Version     : 0.8, 14/07/06
;-
;**********************************************************

FUNCTION eis_cpt_obstbl_set, obstbl, table_index, table, ext_time_for_reuse, ext_description, $
                         SEQUENCE=sequence, LINELIST=linelist, $
                         AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, $
                         XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, $
                         EIS_EVENT=eis_event, RESPONSE=response

  ; get internal copies of some arguments

  time_for_reuse = ext_time_for_reuse

  description = ext_description

  ; get table length

  length = table[0]

  ; set up value for used
  ; if table is unused                then used = 0
  ; if table is used and reusable     then used = 1
  ; if table is used and not reusable then used = 2

  ; check if response study

  IF KEYWORD_SET(response) THEN used = 2 ELSE used = 1

  ; check type of table

  IF KEYWORD_SET(sequence) THEN BEGIN

    ; get OBSTBL index

    IF NOT eis_cpt_obstbl_index( table_index, obstbl_index, max_length, number, /SEQUENCE) THEN GOTO, error

    ; check if table already in use
    ; if so take care with description and time for reuse

    IF obstbl.map.sequences[table_index].used NE 0 THEN BEGIN

      ; update time and description

      eis_cpt_obstbl_map_description, obstbl.map.sequences[table_index], description, time_for_reuse

    ENDIF

    ; add sequence to table
    ; do not write control sequence argument or checksum
    ; these are done later

    obstbl.data[obstbl_index:obstbl_index+length-3] = table[0:length-3]

    obstbl.map.sequences[table_index].used = used

    obstbl.map.sequences[table_index].time = time_for_reuse

    obstbl.map.sequences[table_index].description = description

  ENDIF ELSE IF KEYWORD_SET(linelist) THEN BEGIN

    ; get OBSTBL index

    IF NOT eis_cpt_obstbl_index( table_index, obstbl_index, max_length, number, /LINELIST) THEN GOTO, error

    ; check if table already in use
    ; if so take care with description and time for reuse

    IF obstbl.map.linelists[table_index].used NE 0 THEN BEGIN

      ; update time and description

      eis_cpt_obstbl_map_description, obstbl.map.linelists[table_index], description, time_for_reuse

    ENDIF

    ; add linelist to table

    obstbl.data[obstbl_index:obstbl_index+length-1] = table[0:length-1]

    obstbl.map.linelists[table_index].used = used

    obstbl.map.linelists[table_index].time = time_for_reuse

    obstbl.map.linelists[table_index].description = description

  ENDIF ELSE IF KEYWORD_SET(aec_exposure) THEN BEGIN

    ; get OBSTBL index

    IF NOT eis_cpt_obstbl_index( 0, obstbl_index, length, number, /AEC_EXPOSURE) THEN GOTO, error

    ; check if table already in use
    ; if so take care with description and time for reuse

    IF obstbl.map.aec_exposure[0].used NE 0 THEN BEGIN

      ; update time and description

      eis_cpt_obstbl_map_description, obstbl.map.aec_exposure[0], description, time_for_reuse

    ENDIF

    ; add AEC exposure table

    obstbl.data[obstbl_index:obstbl_index+length-1] = table[0:length-1]

    obstbl.map.aec_exposure[0].used = 1

    obstbl.map.aec_exposure[0].time = time_for_reuse

    obstbl.map.aec_exposure[0].description = description

  ENDIF ELSE IF KEYWORD_SET(aec_table) THEN BEGIN

    ; set up AEC control parameters

    IF NOT eis_cpt_obstbl_index( 0, obstbl_index, length, number, /AEC_TABLE) THEN GOTO, error

    ; check if table already in use
    ; if so take care with description and time for reuse

    IF obstbl.map.aec_table[0].used NE 0 THEN BEGIN

      ; update time and description

      eis_cpt_obstbl_map_description, obstbl.map.aec_table[0], description, time_for_reuse

    ENDIF

    ; add AEC exposure table

    obstbl.data[obstbl_index:obstbl_index+length-1] = table[0:length-1]

    obstbl.map.aec_table[0].used = 1

    obstbl.map.aec_table[0].time = time_for_reuse

    obstbl.map.aec_table[0].description = description

  ENDIF ELSE IF KEYWORD_SET(xrt_flare) THEN BEGIN

    ; set up XRT flare trigger control parameters

    IF NOT eis_cpt_obstbl_index( 0, obstbl_index, length, number, /XRT_FLARE) THEN GOTO, error

    ; check if table already in use
    ; if so take care with description and time for reuse

    IF obstbl.map.xrt_flare[0].used NE 0 THEN BEGIN

      ; update time and description

      eis_cpt_obstbl_map_description, obstbl.map.xrt_flare[0], description, time_for_reuse

    ENDIF

    ; add AEC exposure table

    obstbl.data[obstbl_index:obstbl_index+length-1] = table[0:length-1]

    obstbl.map.xrt_flare[0].used = 1

    obstbl.map.xrt_flare[0].time = time_for_reuse

    obstbl.map.xrt_flare[0].description = description

  ENDIF ELSE IF KEYWORD_SET(eis_flare) THEN BEGIN

    ; set up EIS flare trigger control parameters

    IF NOT eis_cpt_obstbl_index( 0, obstbl_index, length, number, /EIS_FLARE) THEN GOTO, error

    ; check if table already in use
    ; if so take care with description and time for reuse

    IF obstbl.map.eis_flare[0].used NE 0 THEN BEGIN

      ; update time and description

      eis_cpt_obstbl_map_description, obstbl.map.eis_flare[0], description, time_for_reuse

    ENDIF

    ; add AEC exposure table

    obstbl.data[obstbl_index:obstbl_index+length-1] = table[0:length-1]

    obstbl.map.eis_flare[0].used = 1

    obstbl.map.eis_flare[0].time = time_for_reuse

    obstbl.map.eis_flare[0].description = description

  ENDIF ELSE IF KEYWORD_SET(eis_event) THEN BEGIN

    ; set up EIS event trigger control parameters

    IF NOT eis_cpt_obstbl_index( 0, obstbl_index, length, number, /EIS_EVENT) THEN GOTO, error

    ; check if table already in use
    ; if so take care with description and time for reuse

    IF obstbl.map.eis_event[0].used NE 0 THEN BEGIN

      ; update time and description

      eis_cpt_obstbl_map_description, obstbl.map.eis_event[0], description, time_for_reuse

    ENDIF

    ; add AEC exposure table

    obstbl.data[obstbl_index:obstbl_index+length-1] = table[0:length-1]

    obstbl.map.eis_event[0].used = 1

    obstbl.map.eis_event[0].time = time_for_reuse

    obstbl.map.eis_event[0].description = description

  ENDIF ELSE BEGIN

    ; invalid option

    MESSAGE, 'IMPLEMENTATION ERROR'

  ENDELSE

  ; return OK

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error

  RETURN, 0

END





