;+
; PROJECT
;           SOLARB EIS
;
; NAME
;           EIS_READ_FILE_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
;              Also return start_time of first contact and the fileName
;              itself
;          v0.3 JAR 10-Apr-2008
;               Save station name as "USC", "SVA" instead of single letters
;
;-
;______________________________________________________________________________
FUNCTION eis_read_file_dr_events , fileName

; eg: fileName = $HOME/cmdpln/20070614/auxiliary/dr_200706140100.txt
; And just to make sure....
IF (FILE_TEST(fileName) NE 1) THEN RETURN , 0
; Read file
orig_lines      = rd_ascii(fileName , lun = lun)
FREE_LUN , lun

;*********************************************************
;/* DR VOLUME ALLOCATION */                                  Remove
;/*    SOT            EIS          XRT        */             Remove
;/*  (Mbit)         (Mbit)       (Mbit)       */             Remove
;/*----------------------------------------------*/          Remove
;      2800.00      600.000      600.000                     Line 0
;/*----------------------------------------------*/          Remove
;                                                            Remove
;                                                            Remove
;/* DATA SIZE DOWNLINKED AT CONTACT PASSES */                Remove
;/*STA   DATE     TIME    DUR    SOT    EIS     XRT  */      Remove
;/*               (UT)   (sec) (Mbit) (Mbit)  (Mbit) */      Remove
;/*--------------------------------------------------*/      Remove
;SVA   20061030  111220   199    480    102    102           Line 1
;SVA   20061030  124930   199    480    102    102           Line 2
;  ................................................          ...
;  ................................................          ...
;  ................................................          ...
;SVA   20061101  073430   205    494    105    105           Line n - 2
;SVA   20061101  091220   201    483    103    103           Line n - 1
;(1-day total)                  9849   2110   2110           Line n
;/*--------------------------------------------------*/
;*********************************************************

;
; 1. Remove blank lines
no_blanks_lines = remove_blank_lines(orig_lines)
; 2. Remove commented lines (comment = "/*"). So left with...
line_array      = eis_remove_lines(no_blanks_lines , '/*')
; So we're left with...
;
;*********************************************************
;      2800.00      600.000      600.000                     Line 0
;SVA   20061030  111220   199    480    102    102           Line 1
;SVA   20061030  124930   199    480    102    102           Line 2
;  ................................................          ...
;  ................................................          ...
;  ................................................          ...
;SVA   20061101  073430   205    494    105    105           Line n - 2
;SVA   20061101  091220   201    483    103    103           Line n - 1
;(1-day total)                  9849   2110   2110           Line n
;*********************************************************

;
; Get volume allocations - on the first line
;
split   = STRSPLIT(line_array[0] , /EXTRACT)
sot_vol = LONG(split[0])
eis_vol = LONG(split[1])
xrt_vol = LONG(split[2])

;
; Contact lines
;
contact_lines = line_array[1:*]
nContacts     = N_ELEMENTS(contact_lines)

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 , nContacts)
FOR i = 0 , nContacts -1  DO BEGIN

    split = STRSPLIT(contact_lines[i] , /EXTRACT)
    ; Get resource name - SVA, USC, etc
    contact[i].res_name   = split[0]
    ; Get start_time
    datetime = eis_fix_time(split[1],split[2])
    contact[i].start_time = utc2tai(datetime)
    ; Get stop_time
    datetime_utc = anytim2utc(datetime)
    datetime_utc.time = datetime_utc.time + (split[3] * 1000.)
    contact[i].stop_time = utc2tai(datetime_utc)

    ; Get  SOT, EIS and XRT
    contact[i].sot_size = split[4]
    contact[i].eis_size = split[5]
    contact[i].xrt_size = split[6]

ENDFOR

; Eg: date_string = "20070613"
date_string = STRMID(file_baseName(fileName),3,8)

; OK, tie the whole lot up into 1 structure
dr_struct = {sot_vol:sot_vol,eis_vol:eis_vol,xrt_vol:xrt_vol , $
             contact:PTR_NEW(contact),n_contacts:nContacts ,   $
             date_string:date_string , fileName:fileName   ,   $
             first_time:contact[0].start_time}

RETURN , dr_struct

END
