;+
; PROJECT
;           SolarB EIS
;
; NAME
;           EIS_CHECK_RASTER_REPEATS
;
; PURPOSE
;           Function to check 5% rule. The block of entries preceding
;           the cursor time is found - and more importantly its
;           stop_time. Output is the start_time of the new entry.
;
; CATEGORY
;           EIS Timeline Planning
;
; INPUTS
;           parentClass - eis_mk_plan object reference
;           ra_index    - index of working raster
;           n_repeats   - number of times to repeat working raster
; KEYWORDS
;           None
;
; WRITTEN
;          JAR 22-May-2008
;
; HISTORY
;          v0.1 JAR 19-Jun-2008
;               Simplified function and added diagnostic information
;
;-
;______________________________________________________________________________
FUNCTION eis_check_raster_repeats , parentClass , ra_index           ,     $
                                    n_repeats

valid = {valid:0B , mText:'' , curr_tl_list:-1}

print,'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++' +     $
      '++++++++++++++++++++++'
print,'         Checking raster repeats'
print,''
; Get current cursor time
parentClass->getProperty , cursor_time_gui = cursor_time_gui
cursor_time_gui->getProperty , datetime = cp_tai
print,'Cursor Time is ' + anytim2utc(cp_tai,/stime)
; Get all_blocks
parentClass->getProperty , eis_timeline_container = tlc
tlc->getProperty , all_blocks       = all_blocks                     ,     $
                   working_position = wp

;
; Check we have at least 1 block. Shouldn't be here otherwise
;
IF (PTR_VALID(all_blocks) NE 1) THEN BEGIN
    valid.mText = "Encountered a problem (1). Aborting!"
    RETURN , valid
ENDIF

temp  = tlc->get( position = wp)
tl    = temp->get()
; Get current block info
tl_start_time = tl.time_component.start_time
tl_stop_time  = tl.time_component.end_time
num_blocks    = N_ELEMENTS(*all_blocks)
curr_block    = -1
FOR i = 0, num_blocks - 1 DO BEGIN
   IF (tl_start_time GE (*all_blocks)[i].start_time)    AND                $
      (tl_stop_time  LE (*all_blocks)[i].stop_time)     THEN curr_block=i
ENDFOR
IF (curr_block EQ -1) THEN  BEGIN
    valid.mText = "Encountered a problem (2). Aborting!"
    RETURN , valid
ENDIF

;
; Get current block info
;
curr_tl_list = tlc->get_curr_block_info()
print,'Current block index list: ',STRCOMPRESS(curr_tl_list,/REMOVE_ALL)
; Just check that our wp is included in this list
valid_tl = WHERE(curr_tl_list EQ wp , count)
IF (count EQ 0) THEN BEGIN
    valid.mText = "Encountered a problem (4). Aborting!"
    RETURN , valid
ENDIF
; So we have tl entries 12 and 13
; Now get this block's duration by summing the studies/rasters
total_time = 0.D
print,''
print,STRING(format='(A-6,A-2,A-5,A-25,A-25,A-10,A-9)'      ,     $
           'index'                                                    ,     $
           ''                                                        ,     $
           'Reps'                                        ,     $
           'Start_time'                                         ,     $
           'Stop_time'                                         ,     $
           'Duration'                                                ,     $
           'Do wot?')


FOR i = 0, N_ELEMENTS(curr_tl_list) - 1 DO BEGIN

   wp_string = 'Leave it!'
   temp_1 = tlc->get( position = curr_tl_list[i])
   tl_1   = temp_1->get()
   IF (tl_1.time_component.read_all NE 1) THEN BEGIN
       temp_1->get_all_info
       tl_1   = temp_1->get()
   ENDIF
   IF (i EQ 0) THEN very_start_time = tl_1.time_component.start_time
   st_start_time = tl_1.time_component.start_time
   IF (curr_tl_list[i] EQ wp) THEN BEGIN
       tl_1.science_component.studyRasters[ra_index].ra_repeats = FIX(n_repeats)
       wp_string = 'WP'
   ENDIF ELSE BEGIN
       IF (curr_tl_list[i] GT wp) THEN wp_string = 'Move It!'
   ENDELSE

   st_time = eis_tl_duration(tl_1)
   total_time = total_time + st_time
   print,STRING(format='(A-3,I-3,A-2,I-5,A-25,A-25,A-10,A-9)'   ,     $
           'TL'                                                      ,     $
           curr_tl_list[i]                                           ,     $
           ': '                                                      ,     $
           tl_1.science_component.studyRasters[ra_index].ra_repeats  ,     $
           anytim2utc(st_start_time,/stime)                          ,     $
           anytim2utc(st_start_time + (st_time/1000.),/stime)        ,     $
           sec2dhms(total_time/1000.)                                ,     $
           wp_string)
ENDFOR


new_start_time_string = very_start_time
new_stop_time_string  = very_start_time + (total_time/1000.)

;
; If this is the last block, then return structure - 'cos
; we don't have to worry about overlapping the next block/study
;
IF (num_blocks-1 EQ curr_block) THEN RETURN , {valid:1B , $
                                               mText:'' , $
                                               curr_tl_list:curr_tl_list}

;
; Get start_time of first study in next block
;
next_block            = (*all_blocks)[curr_block + 1]
next_block_start_time = next_block.start_time
; Check start_time of next block is greater than new_stop_time_string
IF (new_stop_time_string GE next_block_start_time) THEN BEGIN
   valid.mText = "Not sufficent time. Aborting!"
   RETURN , valid
ENDIF

print,''
print,'     Raster Repeats Operation : Successful'
print,'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++' +     $
      '++++++++++++++++++++++'

; Cleared for action. Go!
RETURN , {valid:1B , mText:'' , curr_tl_list:curr_tl_list}

END
