;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_PROCESS_EIS_FLARE_TABLE
;
; Purpose     : Processes EIS flare table.
;
; Explanation : Control tables are only processed if required by the current time-line..
;
; Use         : < result = eis_cpt_process_eis_flare_table ( control, study_details, $
;                            sequences, engineering_parameters) >
;
; Inputs      : control                : STRUCTURE of type EIS_flare_control.
;               study_details          : STRUCTURE ARRAY of type EIS_study_detail.
;               sequences              : STRUCTURE ARRAY of type EIS_CPT_sequence.
;               engineering_parameters : STRUCTURE of type EIS_engineering_parameters.
;
; 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 24/06/05
;
; Modified    : Version 0.0, 24/06/05, MKC
;               Version 0.1, 18/09/05, MKC
;                 Changed PLACE keyword to SET in eis cpt obstbl..
;                 Changed to use sequences argument.
;               Version 0.2, 04/10/05, MKC
;                 Allowed re-use of control table.
;               Version 0.3, 16/06/06, MKC
;                 Changed so that table index is undefined if no place found for table.
;                 Changed tag study identifier.
;                 Hardcoded study identifiers for response study.
;
; Version     : 0.3, 16/06/06
;-
;**********************************************************

FUNCTION eis_cpt_process_eis_flare_table, control, study_details, sequences, engineering_parameters

  ; get list of studies using control table
  ; NB have already checked that study_details is defined

  study_list = WHERE(study_details.EIS_flare_control_flag NE 0, study_count)

  ; check if control table used

  IF study_count GT 0 THEN BEGIN

    ; get response study index
    ; Nb already checked that it exists

    list = WHERE(sequences.study_identifier EQ 3)

    response_index = sequences[list[0]].index

    ; process control table

    control_table = eis_cpt_set_eis_flare_table(control, engineering_parameters, response_index)

    ; get latest study using control table

    latest_study = study_list[(SORT(study_details[study_list].start_time))[study_count-1]]

    ; get time after which it is safe to overwrite control table

    time_for_reuse = eis_cpt_study_time(study_details[latest_study], engineering_parameters.event_safety_margin_time)

    ; check if table matches table in OBSTBL

    IF NOT eis_cpt_obstbl(control_table, table_index, time_for_reuse, 'EIS FLARE CONTROL TABLE', /PLACE, /SET, /USED, /EIS_FLARE) THEN GOTO, error

    ; check table index set

    IF N_ELEMENTS(table_index) EQ 0 THEN MESSAGE, 'IMPLEMENTATION ERROR'

  ENDIF

  ; return finished OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





