;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_OBSTBL_INDEX
;
; Purpose     : Gets index(es) of table relative to start of OBSTBL, length and number of tables.
;
; Explanation : OBSTBL contains many different tables.
;               This routine returns index(es) of tables, length and number of tables of specified type
;               in OBSTBL data array.
;
; Use         : < res = eis_cpt_obstbl_address(table_index, index,length,number,SEQUENCE=sequence, LINELIST=linelist, $
;                         AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, $
;                         XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, $
;                         EIS_EVENT=eis_event, ALL=all ) >
;
; Inputs      : table_index    : INT index of table.
;                                May be undefined if ALL indexes required.
;                                May be undefined if only length and number of tables needed.
;
; Opt. Inputs : None.
;
; Outputs     : index          : LONG or []LONG index relative to start of OBSTBL data.
;               length         : INT length of table
;               number         : INT number of tables
;
; 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
;               ALL               : FLAG indicating should return all indexes for linelists or sequences
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 29/04/05
;
; Modified    : Version 0.0, 29/04/05, MKC
;               Version 0.1, 11/05/05, MKC
;                 Added number argument.
;                 Allowed undefined table_index.
;               Version 0.2, 05/10/05, MKC
;                 Allowed table_index = -1.
;               Version 0.3, 16/06/06, MKC
;                 Disallow table_index = -1.
;
; Version     : 0.3, 16/06/06
;-
;**********************************************************

FUNCTION eis_cpt_obstbl_index, table_index, index, length, number, SEQUENCE=sequence, LINELIST=linelist, $
                         AEC_EXPOSURE=aec_exposure, AEC_TABLE=aec_table, $
                         XRT_FLARE=xrt_flare, EIS_FLARE=eis_flare, $
                         EIS_EVENT=eis_event, ALL=all

  ; check which table required

  ; set up table base address, length and number of tables

  IF KEYWORD_SET(sequence) THEN BEGIN

    ; set up values for sequence tables

    length  = !EIS_CPT_CONSTANTS.sql

    number  = !EIS_CPT_CONSTANTS.nsq

    address = !EIS_CPT_CONSTANTS.sequence_address

  ENDIF ELSE IF KEYWORD_SET(linelist) THEN BEGIN

    ; set up values for linelist tables

    length  = !EIS_CPT_CONSTANTS.lll

    number  = !EIS_CPT_CONSTANTS.nll

    address = !EIS_CPT_CONSTANTS.linelist_address

  ENDIF ELSE IF KEYWORD_SET(aec_exposure) THEN BEGIN

    ; set up values for AEC exposure table

    length  = !EIS_CPT_CONSTANTS.AEC_exposure_size

    number  = 1

    address = !EIS_CPT_CONSTANTS.AEC_exposure_address

  ENDIF ELSE IF KEYWORD_SET(aec_table) THEN BEGIN

    ; set up values for AEC control parameters

    length  = !EIS_CPT_CONSTANTS.AEC_control_size

    number  = 1

    address = !EIS_CPT_CONSTANTS.AEC_control_address

  ENDIF ELSE IF KEYWORD_SET(xrt_flare) THEN BEGIN

    ; set up values for XRT flare trigger control parameters

    length  = !EIS_CPT_CONSTANTS.XRT_flare_size

    number  = 1

    address = !EIS_CPT_CONSTANTS.XRT_flare_address

  ENDIF ELSE IF KEYWORD_SET(eis_flare) THEN BEGIN

    ; set up values for EIS flare trigger control parameters

    length  = !EIS_CPT_CONSTANTS.EIS_flare_size

    number  = 1

    address = !EIS_CPT_CONSTANTS.EIS_flare_address

  ENDIF ELSE IF KEYWORD_SET(eis_event) THEN BEGIN

    ; set up values for EIS event trigger control parameters

    length  = !EIS_CPT_CONSTANTS.EIS_event_size

    number  = 1

    address = !EIS_CPT_CONSTANTS.EIS_event_address

  ENDIF ELSE BEGIN

    MESSAGE, 'IMPLEMENTATION ERROR'

  ENDELSE

  ; check table index

  IF KEYWORD_SET(all) THEN BEGIN

    ; give indexes all tables relative to start of OBSTBL

    indexes = LINDGEN(number)

  ENDIF ELSE IF N_ELEMENTS(table_index) EQ 0 THEN BEGIN

    ; if undefined give index of first table relative to start of OBSTBL

    indexes = 0

  ENDIF ELSE IF table_index LT 0 OR table_index GE number THEN BEGIN

    eis_cpt_message, 'ERROR, INVALID TABLE INDEX :'+eis_cpt_strtrim(FIX(table_index))

    GOTO, error

  ENDIF ELSE BEGIN

    indexes = table_index

  ENDELSE

  ; set up table address

  index =  address - !EIS_CPT_CONSTANTS.obstbl_address + length*indexes

  ; return

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error

  RETURN, 0

END





