
FUNCTION eis_mapper_study_times, s

;+
; NAME:
;     EIS_MAPPER_STUDY_TIMES
;
; PURPOSE:
;     Given a structure from eis_obs_structure, this routine identifies
;     blocks of adjacent rasters and outputs the start and end times of
;     each block.
;
; CATEGORY:
;     Hinode; EIS; EIS Mapper
;
; CALLING SEQUENCE:
;     Result = EIS_MAPPER_STUDY_TIMES( Struc )
;
; INPUTS:
;     Struc:  An IDL structure in the form returned by eis_obs_structure.
;
; OUTPUTS:
;     A structure with one element for each block that has been identified.
;     The tags are:
;      .t_start  Start time of block (CCSDS format).
;      .t_end    End time of block (CCSDS format).
;      .dur      Duration of block in seconds.
;
;     If a problem occurs or no blocks are found, then -1 is returned.
;
; EXAMPLE:
;     IDL> s=eis_obs_structure('11-may-2013','12-may-2013')
;     IDL> block=eis_mapper_study_times(s)
;
; MODIFICATION HISTORY:
;     Ver.1, 09-Jun-2022, Peter Young
;-


IF n_tags(s) EQ 0 THEN return,-1

n=n_elements(s)

str={t_start: '', t_end: '', dur: 0.}
output=replicate(str,n)

IF n EQ 1 THEN BEGIN 
  output[0].t_start=s[0].date_obs
  output[0].t_end=s[0].date_end
  output[0].dur=anytim2tai(s[0].date_end)-anytim2tai(s[0].date_obs)
  return,output
ENDIF 

t_start_tai=anytim2tai(s.date_obs)
t_end_tai=anytim2tai(s.date_end)

;
; The section below groups together adjacent rasters if the
; gap between them is less than 60 seconds.
;
swtch=0
ii=0
output[ii].t_start=s[0].date_obs
FOR i=1,n-1 DO BEGIN
  IF t_start_tai[i]-t_end_tai[i-1] GT 60. THEN BEGIN
    output[ii].t_end=s[i-1].date_end
    ii=ii+1
    IF ii LT n THEN output[ii].t_start=s[i].date_obs
  ENDIF 
  IF i EQ n-1 AND ii LT n THEN output[ii].t_end=s[i].date_end
ENDFOR

k=where(output.t_start NE '',nk)
output=output[k]

output.dur=anytim2tai(output.t_end)-anytim2tai(output.t_start)

return,output

END
