;+
; PROJECT
;
;           SolarB EIS
;
; NAME
;
;           EIS_ADD_STUDY
;
; PURPOSE
;
;           Procedure to do something or another!
;
; CATEGORY
;
;           EIS Timeline Planning
;
; INPUTS
;
;           Self        - eis_mk_plan_gui tlb object
;           start_time  - study time start (in TAI)
;
; KEYWORDS
;
;           time_tagged - indicates whether the study
;
; MODIFICATION HISTORY
;
;          v0.0 Written by John Rainnie (JAR), RAL, Dec 2005
;          v0.1 JAR May 2006
;               Updated procedure to take care of engineering studies
;          v0.3 JAR 17 August2006
;               Included XRTFlare hunter study properties
;          v0.4 JAR 26-Mar-2007
;               Added recursive tag to tc structure
;          v0.5 JAR 12-Jul-2007
;               Initialise OBS_DEC as a null string
;          v0.6 JAR 26-11-2007
;               Set OBSTITLE to null string/study title if science or
;               engineering study. PRY requested.
;          v0.7 JAR 7-Dec-2007
;               Included SCI_OBJ. Set to 'ENG' for engineering study
;               or st.target
;          v0.8 JAR 17-Dec-2007
;               Refresh DR GUI after adding a study
;          v0.9 JAR 5-Jun-2008
;               Added keyword num_days
;          v1.0 JAR 9-Jun-2010
;               OK, if this is an enabled trigger, disbale it.
;               This is where studies have been event-triggered.
;          v1.1 JAR 27-Aug-2014
;               Save SOT offsets (as a string) in the "disturbance"
;               field of the "science_component" structure
;
;-
;______________________________________________________________________________
PRO eis_add_study , Self , start_time , time_tagged = time_tagged

; Create a new "eis_structure_container" object
temp = OBJ_NEW('eis_structure_container' , {eis_timeline_entry} )
s    = temp->get()
;help,s,/str
;** Structure EIS_TIMELINE_ENTRY, 4 tags, length=4176, data length=3874:
;   TIME_COMPONENT     STRUCT    -> EIS_TIME_COMPONENT Array[1]
;   SCIENCE_COMPONENT  STRUCT    -> EIS_SCIENCE_COMPONENT Array[1]
;   PAUSE_COMPONENT    STRUCT    -> EIS_PAUSE_COMPONENT Array[1]
;   REALTIME_COMPONENT INT          0

Self->getProperty , eis_timeline_container  = eis_timeline_container  ,     $
                    eis_study               = st                      ,     $
                    o_eis_timeline_plot_gui = o_eis_timeline_plot_gui ,     $
                    o_ct_summary            = o_ct_summary            ,     $
                    num_days                = num_days                ,     $
                    sot_offset              = sot_offset

; How many entries are there?
count = eis_timeline_container->count()

; Get study duration
st_dur = st.duration
; Get stop_time
stop_time  = start_time + (st_dur/1000.)
;
; TIME Component
;
;
; JAR 26-Mar-2007
; Added recursive tag to tc structure - update here!
s.time_component.recursive             = st.recursive
;
s.time_component.time_tagged           = KEYWORD_SET(time_tagged)
s.time_component.five_percent_rule     = KEYWORD_SET(time_tagged)
s.time_component.start_time            = start_time
s.time_component.end_time              = stop_time
s.time_component.time_duration         = st_dur
s.time_component.acronym               = STRTRIM(st.acronym,2)
s.time_component.title                 = STRTRIM(st.title,2)
s.time_component.author                = STRTRIM(st.author,2)
s.time_component.category              = STRTRIM(st.category,2)
s.time_component.st_id                 = st.id
s.time_component.visible               = 1
s.time_component.save                  = 1
s.time_component.read_all              = 1
CASE st.type OF
    0    : s.time_component.entry_type = 'S'
    1    : s.time_component.entry_type = 'E'
    ELSE : stop
ENDCASE
;
; SCIENCE Component
;
s.science_component.file_name          = STRTRIM(st.description_file,2)
CASE st.type OF
    0    : s.science_component.target  = st.target
    1    : s.science_component.target  = 'Engineering'
    ELSE : s.science_component.target  = 'Unknown'
ENDCASE
CASE st.type OF
    0    : s.science_component.obstitle = ''
    1    : s.science_component.obstitle = STRTRIM(STRMID(st.title,0,80),2)
    ELSE : s.science_component.obstitle = ''
ENDCASE
CASE st.type OF
    0    : BEGIN
               st_target = st.target
               CASE st_target OF
                   'Quiet Sun'     : sci_obj = 'QS'
                   'Active Region' : sci_obj = 'AR'
                   'Coronal Hole'  : sci_obj = 'CH'
                   ELSE            : sci_obj = 'QS'
               ENDCASE
               s.science_component.sci_obj = sci_obj
           END
    1    : s.science_component.sci_obj = 'ENG'
    ELSE : s.science_component.sci_obj = ''
ENDCASE

s.science_component.study_id           = st.id
s.science_component.join_sb            = 'E'
s.science_component.obs_dec            = ''
s.science_component.observer           = 'None'
s.science_component.planner            = 'None'
s.science_component.tohbans[0]         = 'None'
s.science_component.tohbans[1]         = 'None'
s.science_component.duration           = st_dur
s.science_component.volume             = st.volume
s.science_component.nRasters           = st.nRasters
s.science_component.nRepeats           = 1
s.science_component.slit_slot          = st.slit_slot
s.science_component.xrt_flare_response = st.xrt_flare_response
s.science_component.eis_flare_response = st.eis_flare_response
s.science_component.eis_event_response = st.eis_event_response
s.science_component.aec_response       = st.aec_response
s.science_component.studyRasters       = st.studyRasters

; JAR 27-Aug. Save SOT offset
sot_offset_text = STRING(format='(D6.1,A-1,D6.1)',          $
                   sot_offset[0],',',sot_offset[1])
sot_offset_text = STRCOMPRESS(sot_offset_text,/REMOVE_ALL)
s.science_component.disturbance        = sot_offset_text

;
; 9-Jun-2010
; OK, if this is an enabled trigger, disbale it
; 14-Jun-2010
; Learned that CPT checks that no lines are selected if the event
; flag is NOT set. So also have to zero all event_flags.
;
s.science_component.eis_event_response.respond_to_eis_event = 0
s.science_component.studyRasters[0].eis_event_props.event_flag = BYTARR(25)

s.science_component.eis_flare_response.respond_to_eis_flare = 0
s.science_component.xrt_flare_response.respond_to_xrt_flare = 0


;
; Save changes in TC object
;
temp->set , s
temp->update_study_properties
;temp->update_raster_properties
; Compute raster pointings
temp->initialise_raster_pointing , num_days = num_days
;
; Add entry to the tlc
;
eis_timeline_container->add_entry, temp
;
; Update working_position value
;
working_position = eis_timeline_container->count() - 1
eis_timeline_container->setproperty, working_position = working_position
;
; Update cursor_position to end of study
;
Self->getProperty , cursor_time_gui = cursor_time_gui
cursor_time_gui->setProperty , datetime = stop_time
;
; Redraw timeline graphics
;
o_eis_timeline_plot_gui->initialize_plot
;
; Update Plan Summary Tool
;
Self->refreshSummary
Self->refresh_dr_gui , /UPDATE
;
;
; Update widgets with values
;
Self->updateStudyWidgets

END
