;+
; Project     : SOLAR-B - EIS
;
; Name        : EIS_CPT_RESPONSE_TIMELINE
;
; Purpose     : Gets timeline list of studies in event response.
;
; Explanation : Gets timeline list of studies in event response.
;
; Use         : < result = eis_cpt_response_timeline ( study_details, time_period, hunter_index,
;                                                      response_study_identifier, type, timeline) >
;
; Inputs      : study_details    : STRUCTURE ARRAY of type EIS_study_detail.
;               time_period      : DBLARR[2] TAI time to start and finish timeline.
;               hunter_index     : LONG index into timeline of event hunter study
;                                  If no hunter study then -1.
;               response_study_identifier: LONG ID of response study
;               timeline         : STRUCTURE ARRAY of type EIS_CPT_timeline. Updated.
;
; Opt. Inputs : None.
;
; Outputs     : result     : INT flag :
;                              1 : processed science plan OK.
;                              0 : processing failed.
;               timeline   : STRUCTURE ARRAY of type EIS_CPT_timeline updated
;
; Opt. Outputs: None.
;
; Keywords    : None.
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; Category    : EIS_CPT.
;
; Prev. Hist. : None.
;
; Written     : Martin Carter RAL 23/09/05
;
; Modified    : Version 0.0, 23/09/05, MKC
;                 Changed behaviour with respect to errors and warnings and daisy-chain loops.
;               Version 0.2, 22/06/06, MKC
;                 Removed type argument.
;
; Version     : 0.2, 22/06/06
;-
;**********************************************************

FUNCTION eis_cpt_response_timeline, study_details, time_period, hunter_index, response_study_identifier, timeline

  ; check there is a hunter study

  IF hunter_index GE 0 THEN BEGIN

    ; use latest time possible as start of study

    study_index = timeline[hunter_index].index

    start_time  = timeline[hunter_index].time + $
                  study_details[study_index].duration*study_details[study_index].study_repeat_number

    ; get index in study_details of event response study
    ; NB have checked there is only one

    link = WHERE(study_details.study_identifier EQ response_study_identifier, count)

    IF count NE 1 THEN MESSAGE, 'IMPLEMENTATION ERROR'

    link = link[0]

    ; calculate timeline for event response study
    ; issue warning if response timeline overlaps end of plan

    IF NOT eis_cpt_daisy_chain(study_details, time_period[1], link, start_time, response_timeline, /COMPLETE) THEN GOTO, error

    ; set type

    response_timeline.type = eis_cpt_strtrim(response_study_identifier)

    ; add to timeline

    eis_cpt_add, timeline, response_timeline

  ENDIF

  ; return finished OK flag

  RETURN, 1

error :

  ; issue routine name

  MESSAGE, 'ERROR', /INFORMATIONAL

  ; return error flag

  RETURN, 0

END






