;+
; PROJECT
;           SOLARB EIS
;
; NAME
;           eis_read_dr_events
;
; PURPOSE
;           Function to return op_period events
;;
; CATEGORY
;           EIS timeline planning
;
; WRITTEN
;           John A Rainne RAL
;
; VERSION
;          v0.1 JAR 20-Nov-2007
;               Format of dr files has changed.
;          v0.2 JAR 28-Nov-2007
;               For each file read, added start time of first contact and
;               fileName to returned structure
;          v0.3 JAR 5-Jun-2008
;               Added keyword num_days
;
;
;-
;______________________________________________________________________________
FUNCTION eis_read_dr_events , start_time , duration , num_days= num_days

IF (N_ELEMENTS(num_days) EQ 0) THEN num_days = 3

; Get array of datestrings
ds = eis_create_datestring_array(start_time , duration,num_days = num_days)
; Get array of event fileNames - eg;
;/home/sstdljar/jar24/cmdpln/latest/dr_20061008*.*
;/home/sstdljar/jar24/cmdpln/latest/dr_20061008*.*
;/home/sstdljar/jar24/cmdpln/latest/dr_20061011*.*
fileNames = eis_create_jside_filenames(ds , /DR)

; If none found, then bail out!
IF (STRLEN(fileNames[0]) EQ 0) THEN RETURN , 0
;print,fileNames

all_events = ''

; OK, read each file
FOR i = 0 , N_ELEMENTS(fileNames) - 1 DO BEGIN

    events = eis_read_file_dr_events(fileNames[i])

;stop

    CASE N_TAGS(all_events) OF
        0    :  all_events = events
        ELSE :  all_events = [all_events , events]
    ENDCASE

ENDFOR

; Want array of structures for each date, containing date, eis, sot, and
; xrt volumes

; Also, want a single array of contacts. Lose the POINTERS!
total_contacts = TOTAL(all_events[*].n_contacts)
contact_str    = {res_name   : ''      ,      $
                  start_time : 0.D     ,      $
                  stop_time  : 0.D     ,      $
                  sot_size   : 0L      ,      $
                  eis_size   : 0L      ,      $
                  xrt_size   : 0L             $
                 }
contact   = REPLICATE(contact_str , total_contacts)


vol_struct = {date:0.D,sot:0L,eis:0L,xrt:0L,first_time:0.D,filename:''}
vol_array  = REPLICATE(vol_struct , N_ELEMENTS(all_events))
nc_start   = 0
FOR i = 0 , N_ELEMENTS(all_events) - 1 DO BEGIN

    vol_array[i].date       = anytim2tai(all_events[i].date_string)
    vol_array[i].first_time = all_events[i].first_time
    ;print,anytim2utc(all_events[i].first_time,/stime)
    vol_array[i].filename   = all_events[i].filename
    vol_array[i].sot        = all_events[i].sot_vol
    vol_array[i].eis        = all_events[i].eis_vol
    vol_array[i].xrt        = all_events[i].xrt_vol

    nc       = all_events[i].n_contacts
    ;print , nc_start , nc_start + nc - 1

    contact[nc_start:nc_start + nc - 1] = (*all_events[i].contact)

    nc_start = nc_start + nc

    ; Free up those nasty pointers like a good boy!
    PTR_FREE , all_events[i].contact

ENDFOR

st        = contact[*].start_time
st_unique = UNIQ(st , SORT(st))

unique_contacts = contact[st_unique]
unique_events   = {vol_array : vol_array   ,      $
                   contact   : unique_contacts}

RETURN , unique_events
;RETURN , all_events

END
