;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_PROCESS_TIMELINE_STUDIES
;
; Purpose     : Processes studies on the timeline.
;
; Explanation : This routine processes the studies used for event response.
;               Studies are either science or engineering studies.
;
; Use         : < result = eis_cpt_process_timeline_studies ( engineering_parameters, study_details, $
;                                                             expiry_times, list_of_lines, sequences) >
;
; Inputs      : engineering_parameters     : STRCUTURE of type EIS_engineering_parameters.
;               study_details              : STRUCTURE ARRAY of type EIS_study_detail.
;               expiry_times               : DBLARR[nstudies] TAI time study expires.
;
; Opt. Inputs : None.
;
; Outputs     : result           : INT flag :
;                                   1 : processed science plan OK.
;                                   0 : processing failed.
;               list_of_lines    : INTARR() list of all linelist tables used.
;               sequences        : STRUCTURE ARRAY of type EIS_CPT_sequences
;
; Opt. Outputs: None.
;
; Keywords    : None.
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: May output ASCII files.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : Adapted from EIS CPT process.
;
; Written     : Martin Carter RAL 21/10/04
;
; Modified    : Version 0.0, 01/03/05
;               Version 0.1, 09/03/05, MKC
;                 Added time-tagged studies to OP store.
;               Version 0.2, 18/03/05, MKC
;                 Renamed eis get study details.
;                 Renamed argument.
;               Version 0.3, 24/03/05, MKC
;                 Used eis cpt command store.
;                 Allowed studies to be run from GSE.
;                 Renamed keywords.
;                 Added INTEL keyword.
;               Version 0.4, 03/04/05, MKC
;                 Added ACSII output of lines and sequences.
;               Version 0.5, 21/04/05, MKC
;                 Allowed EIS_CPT to be called without a time period defined.
;               Version 0.6, 27/04/05, MKC
;                 Removed ISACS keyword.
;               Version 0.7, 09/05/05, MKC
;                 Removed Bakeout study processing.
;                 Added in engineering file processing.
;                 Changed to use eis cpt process study.
;               Version 0.8, 24/06/05, MKC
;                 Added sequence_starts to the argument list.
;               Version 0.9, 27/06/05, MKC
;                 Changed argument list.
;                 Added eis cpt process control tables.
;               Version 1.0, 12/07/05, MKC
;                 Added engineering parameters to eis cpt check studies list arguments.
;                 Used eis cpt obstbl to output ASCII files.
;               Version 1.1, 19/09/05, MKC
;                 Added reuse of sequences.
;                 Added timeline processing.
;                 Added expiry times argument.
;               Version 1.2, 19/06/06, MKC
;                 Moved check on whether study details defined out of this routine.
;               Version 1.3, 08/09/06, MKC
;                 Separated from eis cpt process studies.
;
; Version     : 1.3, 08/09/06
;-
;**********************************************************

FUNCTION eis_cpt_process_timeline_studies, engineering_parameters, study_details, expiry_times, list_of_lines, sequences

  ; check studies

  IF NOT eis_cpt_check_studies_list(study_details, engineering_parameters) THEN GOTO, error

  ; do studies

  FOR s = 0, N_ELEMENTS(study_details)-1 DO BEGIN

    ; get list of studies identical with the present one

    list = eis_cpt_find_study(study_details, study_details[s])

    ; only process if this study is the latest of these

    IF study_details[s].start_time EQ MAX(study_details[list].start_time) THEN BEGIN

      ; get study details from timeline database
      ; this is a structure array of type either EIS_RASTER_DETAIL or EIS_ENGINEERING_DETAIL

      study = eis_get_study(study_details[s].study_identifier, ENGINEERING=study_details[s].engineering)

      ; check study valid

      IF NOT KEYWORD_SET(study) THEN GOTO, error

      ; process study
      ; sequences contains the sequence table data
      ; list of lines contain the table indexes used so far

      IF NOT eis_cpt_process_study(engineering_parameters, study_details[s], study, expiry_times[s], $
                                   list_of_lines, sequences) THEN GOTO, error

    ENDIF

  ENDFOR

  ; return finished OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END





