;+
; PROJECT
;           SOLARB EIS
;
; NAME
;           EIS_READ_FILE_ORBIT_EVENTS
;
; PURPOSE
;           This function reads the contents of an J-side supplied
;           obev file
;
; OUTPUTS
;           An array of pass structures - 1 for each entry in the list
;
; CATEGORY
;           EIS timeline planning
;
; WRITTEN
;           John A Rainne RAL 7-Nov-2006
;
; VERSION
;           v0.1 JAR 18-Apr-2007
;                Added DAY and NGT capability due to imminent demand
;           v0.2 JAR 24-Apr-2007
;                Modified DAY/Night to NGT_ENTRY & NGT_EXIT as defined
;                in an example file
;           v0.3 JAR 24-Apr-2007
;                Modified Day/Night entries. These have been replaced with
;                NGT_ENTRY and NGT_EXIT tags - in line with the SAA and HLA
;                entries.
;           v0.4 JAR 5-May-2009
;                Added XTW_ENTRY and XTW_EXIT tags.
;
;
;______________________________________________________________________________
FUNCTION eis_read_file_orbit_events , fileName

IF (FILE_TEST(fileName) NE 1) THEN stop

lines    = RD_ASCII(fileName , lun = lun)
FREE_LUN , lun


hla_entry_search = STREGEX(lines , 'HLA_ENTRY' , /BOOLEAN , /FOLD_CASE)
hla_entry_list   = WHERE(hla_entry_search EQ 1 , hla_entry_count)

hla_exit_search  = STREGEX(lines , 'HLA_EXIT'  , /BOOLEAN , /FOLD_CASE)
hla_exit_list    = WHERE(hla_exit_search  EQ 1 , hla_exit_count)

saa_entry_search = STREGEX(lines , 'SAA_ENTRY' , /BOOLEAN , /FOLD_CASE)
saa_entry_list   = WHERE(saa_entry_search EQ 1 , saa_entry_count)

saa_exit_search  = STREGEX(lines , 'SAA_EXIT'  , /BOOLEAN , /FOLD_CASE)
saa_exit_list    = WHERE(saa_exit_search  EQ 1 , saa_exit_count)

ngt_entry_search = STREGEX(lines , 'NGT_ENTRY' , /BOOLEAN , /FOLD_CASE)
ngt_entry_list   = WHERE(ngt_entry_search EQ 1 , ngt_entry_count)

ngt_exit_search  = STREGEX(lines , 'NGT_EXIT'  , /BOOLEAN , /FOLD_CASE)
ngt_exit_list    = WHERE(ngt_exit_search  EQ 1 , ngt_exit_count)

xtw_entry_search = STREGEX(lines , 'XTW_ENTRY' , /BOOLEAN , /FOLD_CASE)
xtw_entry_list   = WHERE(xtw_entry_search EQ 1 , xtw_entry_count)

xtw_exit_search  = STREGEX(lines , 'XTW_EXIT'  , /BOOLEAN , /FOLD_CASE)
xtw_exit_list    = WHERE(xtw_exit_search  EQ 1 , xtw_exit_count)


n_hla = MAX([hla_entry_count,hla_exit_count])
n_saa = MAX([saa_entry_count,saa_exit_count])
n_ngt = MAX([ngt_entry_count,ngt_exit_count])
n_xtw = MAX([xtw_entry_count,xtw_exit_count])


saa         = {saa , name:'saa' , start_time:0.D , stop_time:0.D}
hla         = {hla , name:'hla' , start_time:0.D , stop_time:0.D}
ngt         = {ngt , name:'ngt' , start_time:0.D , stop_time:0.D}
xtw         = {xtw , name:'xtw' , start_time:0.D , stop_time:0.D}

IF (n_saa GT 0) THEN saa_entries = REPLICATE({saa},n_saa)
IF (n_hla GT 0) THEN hla_entries = REPLICATE({hla},n_hla)
IF (n_ngt GT 0) THEN ngt_entries = REPLICATE({ngt},n_ngt)
IF (n_xtw GT 0) THEN xtw_entries = REPLICATE({xtw},n_xtw)

IF (hla_entry_count NE hla_exit_count) OR                                   $
   (saa_entry_count NE saa_exit_count) THEN BEGIN

;    ;
;    ; Get op_period end_time
;    ;
;    pathName     = FILE_DIRNAME(fileName)
;    baseName     = FILE_BASENAME(fileName)
;    date_string  = STRMID(baseName , STRPOS(baseName , '_') + 1 , 8)
;    op_baseName  = STRJOIN(['op_period_' , date_string , '*.*'] , /SINGLE)
;    op_fileName  = CONCAT_DIR(pathName  , op_baseName)
;    search_file  = FILE_SEARCH(op_fileName , count = count)
;    CASE count OF
;        0    : stop
;        1    : op_times = eis_read_file_op_events(search_file[0])
;        ELSE : op_times = eis_read_file_op_events(search_file[0])
;    ENDCASE

    IF (n_saa GT 0) THEN BEGIN
        saa_entries[*].start_time = 0. ;op_times[0].start_time
        saa_entries[*].stop_time  = 0. ;op_times[0].stop_time
    ENDIF

    IF (n_hla GT 0) THEN BEGIN
        hla_entries[*].start_time = 0. ;op_times[0].start_time
        hla_entries[*].stop_time  = 0. ;op_times[0].stop_time
    END

    IF (n_ngt GT 0) THEN BEGIN
        ngt_entries[*].start_time = 0. ;op_times[0].start_time
        ngt_entries[*].stop_time  = 0. ;op_times[0].stop_time
    END

ENDIF

FOR i = 0, saa_entry_count -1 DO BEGIN

    l_entry = STRCOMPRESS(lines[saa_entry_list[i]] , /REMOVE_ALL)
    d_entry = STRMID(l_entry , STRPOS(l_entry , ':') + 1 , 19)

    saa_entries[i].start_time = eis_stev2tai(d_entry)
    saa_entries[i].name       = 'saa'

ENDFOR

FOR i = 0, saa_exit_count -1 DO BEGIN

    l_exit  = STRCOMPRESS(lines[saa_exit_list[i]]  , /REMOVE_ALL)
    d_exit  = STRMID(l_exit , STRPOS(l_exit  , ':') + 1 , 19)

    saa_entries[i].stop_time = eis_stev2tai(d_exit)
    saa_entries[i].name      = 'saa'

ENDFOR

FOR i = 0, hla_entry_count -1 DO BEGIN

    l_entry = STRCOMPRESS(lines[hla_entry_list[i]] , /REMOVE_ALL)
    d_entry = STRMID(l_entry , STRPOS(l_entry , ':') + 1 , 19)

    hla_entries[i].start_time = eis_stev2tai(d_entry)
    hla_entries[i].name       = 'hla'

ENDFOR

FOR i = 0, hla_exit_count -1 DO BEGIN

    l_exit  = STRCOMPRESS(lines[hla_exit_list[i]]  , /REMOVE_ALL)
    d_exit  = STRMID(l_exit , STRPOS(l_exit  , ':') + 1 , 19)

    hla_entries[i].stop_time = eis_stev2tai(d_exit)
    hla_entries[i].name      = 'hla'

ENDFOR

FOR i = 0, ngt_entry_count -1 DO BEGIN

    l_entry = STRCOMPRESS(lines[ngt_entry_list[i]] , /REMOVE_ALL)
    d_entry = STRMID(l_entry , STRPOS(l_entry , ':') + 1 , 19)

    ngt_entries[i].start_time = eis_stev2tai(d_entry)
    ngt_entries[i].name       = 'ngt'

ENDFOR

FOR i = 0, ngt_exit_count -1 DO BEGIN

    l_exit  = STRCOMPRESS(lines[ngt_exit_list[i]]  , /REMOVE_ALL)
    d_exit  = STRMID(l_exit , STRPOS(l_exit  , ':') + 1 , 19)

    ngt_entries[i].stop_time = eis_stev2tai(d_exit)
    ngt_entries[i].name      = 'ngt'

ENDFOR

FOR i = 0, xtw_entry_count -1 DO BEGIN

    l_entry = STRCOMPRESS(lines[xtw_entry_list[i]] , /REMOVE_ALL)
    d_entry = STRMID(l_entry , STRPOS(l_entry , ':') + 1 , 19)

    xtw_entries[i].start_time = eis_stev2tai(d_entry)
    xtw_entries[i].name       = 'xtw'

ENDFOR

FOR i = 0, xtw_exit_count -1 DO BEGIN

    l_exit  = STRCOMPRESS(lines[xtw_exit_list[i]]  , /REMOVE_ALL)
    d_exit  = STRMID(l_exit , STRPOS(l_exit  , ':') + 1 , 19)

    xtw_entries[i].stop_time = eis_stev2tai(d_exit)
    xtw_entries[i].name      = 'xtw'

ENDFOR

IF (n_saa EQ 0) THEN saa_entries = saa
IF (n_hla EQ 0) THEN hla_entries = hla
IF (n_ngt EQ 0) THEN ngt_entries = ngt
IF (n_xtw EQ 0) THEN xtw_entries = xtw

RETURN , [saa_entries , hla_entries , ngt_entries  , xtw_entries]


END
