;+
; Project     :	SOLAR-B - EIS
;
; Name        :	EIS_CPT_OBSTBL_EXTRACT
;
; Purpose     :	Gets table from OBSTBL.
;
; Explanation :	OBSTBL contains many different tables.
;               This routine extracts a table of specified type.
;
; Use         :	< res = eis_cpt_obstbl_extract(obstbl, table_index, table, time_for_reuse, description ) >
;
; Inputs      :	obstbl         : STRUCTURE of type EIS_CPT_obstbl.
;               table_index    : INT index of table.
;
; Opt. Inputs : None.
;
; Outputs     : result         : INT flag :
;                                1 : processed OK.
;                                0 : processing failed.
;               table          : BYTARR[] table data
;               time_for_reuse : DOUBLE TAI time after which table may be reused.
;               description    : STRING description of table
;
; 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
;
; 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, 25/04/05, MKC
;                 Removed check that table is used.
;                 Removed fatal check on table length.
;                 Added table check routine.
;               Version 0.2, 29/04/05, MKC
;                 Changed use of !EIS_CPT_CONSTANTS.
;               Version 0.3, 11/05/05, MKC
;                 Updated.
;               Version 0.4, 28/06/05, MKC
;                 Added time_for_reuse and description from control tables.
;
; Version     :	0.4, 28/06/05
;-
;**********************************************************

FUNCTION eis_cpt_obstbl_extract, obstbl, table_index, table, time_for_reuse, 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

  ; check which table required

  IF KEYWORD_SET(sequence) THEN BEGIN

    ; set up sequence table index

    IF NOT eis_cpt_obstbl_index( table_index, index, max_length, number, /SEQUENCE) THEN GOTO, error

    ; get table length

    length = obstbl.data[index]

    ; get sequence table

    IF length GT 0 AND length LE max_length THEN BEGIN

      table = obstbl.data[index:index+length-1]

      time_for_reuse = obstbl.map.sequences[table_index].time

      description = obstbl.map.sequences[table_index].description

    ENDIF ELSE BEGIN

      table = 0

      time_for_reuse = 0.0D0

      description = 'INVALID'

    ENDELSE

  ENDIF ELSE IF KEYWORD_SET(linelist) THEN BEGIN

    ; set up linelist table index

    IF NOT eis_cpt_obstbl_index( table_index, index, max_length, number, /LINELIST) THEN GOTO, error

    ; get table length

    length = obstbl.data[index]

    ; get linelist table

    IF length GT 0 AND length LE max_length THEN BEGIN

      table = obstbl.data[index:index+length-1]

      time_for_reuse = obstbl.map.linelists[table_index].time

      description = obstbl.map.linelists[table_index].description

    ENDIF ELSE BEGIN

      table = 0

      time_for_reuse = 0.0D0

      description = 'INVALID'

    ENDELSE

  ENDIF ELSE IF KEYWORD_SET(aec_exposure) THEN BEGIN

    ; get AEC exposure table

    IF NOT eis_cpt_obstbl_index( 0, index, length, /AEC_EXPOSURE) THEN GOTO, error

    table = obstbl.data[index:index+length-1]

    time_for_reuse = obstbl.map.aec_exposure[0].time

    description = obstbl.map.aec_exposure[0].description

  ENDIF ELSE IF KEYWORD_SET(aec_table) THEN BEGIN

    ; get AEC control parameters

    IF NOT eis_cpt_obstbl_index( 0, index, length, number, /AEC_TABLE) THEN GOTO, error

    table = obstbl.data[index:index+length-1]

    time_for_reuse = obstbl.map.aec_table[0].time

    description = obstbl.map.aec_table[0].description

  ENDIF ELSE IF KEYWORD_SET(xrt_flare) THEN BEGIN

    ; get XRT flare trigger control parameters

    IF NOT eis_cpt_obstbl_index( 0, index, length, number, /XRT_FLARE) THEN GOTO, error

    table = obstbl.data[index:index+length-1]

    time_for_reuse = obstbl.map.xrt_flare[0].time

    description = obstbl.map.xrt_flare[0].description

  ENDIF ELSE IF KEYWORD_SET(eis_flare) THEN BEGIN

    ; get EIS flare trigger control parameters

    IF NOT eis_cpt_obstbl_index( 0, index, length, number, /EIS_FLARE) THEN GOTO, error

    table = obstbl.data[index:index+length-1]

    time_for_reuse = obstbl.map.eis_flare[0].time

    description = obstbl.map.eis_flare[0].description

  ENDIF ELSE IF KEYWORD_SET(eis_event) THEN BEGIN

    ; get EIS event trigger control parameters

    IF NOT eis_cpt_obstbl_index( 0, index, length, number, /EIS_EVENT) THEN GOTO, error

    table = obstbl.data[index:index+length-1]

    time_for_reuse = obstbl.map.eis_event[0].time

    description = obstbl.map.eis_event[0].description

  ENDIF ELSE BEGIN

    MESSAGE, 'IMPLEMENTATION ERROR'

  ENDELSE

  ; return OK

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error

  RETURN, 0

END





