;+
; PROJECT
;           Solar-B EIS
;
; NAME
;           EIS_READ_FILE_REPOINT_EVENTS
;
; PURPOSE
;           Function to read a re-point file and return its entire contents
;           as a structure
;
; CATEGORY
;           Planning, Science
;
; EXPLANATION
;
; SYNTAX
;           eis_read_file_repoint_events , fileName
;
; INPUTS
;          fileName - of the re-point file
;          (eg: /full/path/re-point_200611110001.txt)
;
; WRITTEN
;          John A Rainnie RAL Aug-2005
;
; HISTORY
;           V0.1 JAR 26-Jul-2006
;           v0.2 JAR 19-Sep-2007
;              Dave W reported a new case of where re-point files may
;              exist only to re-define the tracking curves. Hence, these
;              re-point files won't contain any "ORe-point Start lines".
;              (1) Check there are any "ORe-point Start lines".
;              (2) Check there is a "Fm:" valus in the "New AOCS Tracking
;                  Parameters after the memory upload".
;
;-
;************************************************************************
FUNCTION eis_read_file_repoint_events , fileName

;print,fileName
;fileName = '/home/sstdljar/jar24/cmdpln/latest/re-point_200611110001.txt'

IF (FILE_TEST(fileName) NE 1) THEN RETURN , ''


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

; Remove blank lines
lines = remove_blank_lines(lines)

;
; Get previous pointing block
;
prev_start_index = 0
prev_stop        = STREGEX(lines,'End of re-pointing and' , /BOOLEAN)
prev_stop_index  = WHERE(prev_stop EQ 1 , count)
IF (count NE 1) THEN stop
prev_block       = lines[prev_start_index:prev_stop_index]
; Create structure
rp_prev = {eis_rp_tracks_previous}
; Get start_time time
split       = STRSPLIT(prev_block[1] , /EXTRACT)
time_string = STRJOIN([split[1],split[2]],'.',/SINGLE)
rp_prev.start_time = eis_stev2tai(time_string)
; Get stop_time time
split       = STRSPLIT(prev_block[2] , /EXTRACT)
time_string = STRJOIN([split[1],split[2]],'.',/SINGLE)
rp_prev.stop_time = eis_stev2tai(time_string)
;
; Get "Latest re-pointing"
;
split       = STRSPLIT(prev_block[5] , /EXTRACT)
rp_prev.latest_rp.track = split[4]
time_string = STRJOIN([split[2],split[3]],'.',/SINGLE)
rp_prev.latest_rp.date_time = eis_stev2tai(time_string)
rp_prev.latest_rp.x_offset = split[5]
rp_prev.latest_rp.y_offset = split[6]
;
; n_uploads - number of "AOCS Mem-Upload-**" lines
;
find_n_uploads  = STREGEX(lines,'AOCS Mem-Upload' , /BOOLEAN)
n_uploads_index = WHERE(find_n_uploads EQ 1 , n_uploads)
upload_times = 0.D
FOR i = 0, n_uploads - 1 DO BEGIN

    split       = STRSPLIT(lines[n_uploads_index] , /EXTRACT)
    time_string = STRJOIN([split[2],split[3]],'.',/SINGLE)
    CASE upload_times[0] OF
        0.   :  upload_times = eis_stev2tai(time_string)
        ELSE :  upload_times = [upload_times , eis_stev2tai(time_string)]
    ENDCASE

ENDFOR
;
;ORe/RRe-point Start
;
find_ore  = STREGEX(lines,'^[R,O]Re-point Start' , /BOOLEAN)
ore_index = WHERE(find_ore EQ 1 , count)
IF (count EQ 0) THEN BEGIN

    rp_ore_array = 0

ENDIF ELSE BEGIN

    rp_ore_array = REPLICATE({eis_rp_repoint},count)

    FOR i = 0 , count -1 DO BEGIN

        split  = STRSPLIT(lines[ore_index[i]] , /EXTRACT)
        ; Get type (either "ORe" or "RRe")
        rp_ore_array[i].type = STRMID(split[0] , 0 , STRPOS(split[0] , '-'))
        ; Get date_time
        time_string = STRJOIN([split[2],split[3]],'.',/SINGLE)
        rp_ore_array[i].date_time = eis_stev2tai(time_string)

        rp_ore_array[i].track    = split[4]
        rp_ore_array[i].x_offset = split[5]
        rp_ore_array[i].y_offset = split[6]
        ; Get x,y coordinates
        xy = eis_rp_get_coords(split[7:*])
        rp_ore_array[i].x_point  = xy.x_point
        rp_ore_array[i].y_point  = xy.y_point

   ENDFOR

ENDELSE

;
; Get start pointing block
;
current_line  = STREGEX(lines,'before the memory' , /BOOLEAN)
current_index = WHERE(current_line EQ 1 , count)
IF (count NE 1) THEN stop
curr_start_index = current_index
curr_stop_index  = curr_start_index + 7
curr_block       = lines[curr_start_index[0]+1:curr_stop_index[0]+1]
; Update tr_mem_id
rp_start = {eis_rp_tracks_start}
rp_start.tr_mem_id = STRMID(curr_block[0],10,STRLEN(curr_block[0]))
; Get tracks
FOR j = 0, 3 DO BEGIN

    split = STRSPLIT(curr_block[3+j] , /EXTRACT)
    ; Get track number
    rp_start.tracks[j].track = split[1]
    ; Get reference time
    time_string = STRJOIN([split[2],split[3]],'.',/SINGLE)
    rp_start.tracks[j].date_time = eis_stev2tai(time_string)

    ; Get long, latitude and rotation rate
    rp_start.tracks[j].lon  = split[4]
    rp_start.tracks[j].lat  = split[5]
    rp_start.tracks[j].rate = split[6]

ENDFOR


;
; Get New pointing blocks (if any)
;
search_string = 'Tracking Parameters after the memory upload'
new_line  = STREGEX(lines, search_string , /BOOLEAN)
new_index = WHERE(new_line EQ 1 , count)
IF (count GT 0) THEN BEGIN
    rp_new = REPLICATE({eis_rp_tracks_new},count)
ENDIF ELSE rp_new = 0
FOR i = 0, count -1 DO BEGIN

    new_block = lines[new_index[i]+1:new_index[i]+8]
    ; Get TR_MEM_D
    rp_new[i].tr_mem_id = STRMID(new_block[0],10,STRLEN(new_block[0]))

    ; Get start (FM:) date - if it's there!
    split     = STRSPLIT(new_block[1] , /EXTRACT , count = count_split)
    IF (count_split GT 1) THEN BEGIN
        time_string = STRJOIN([split[1],split[2]],'.',/SINGLE)
        rp_new[i].start_time = eis_stev2tai(time_string)
    ENDIF

    ; Get tracks
    FOR j = 0, 3 DO BEGIN

        split = STRSPLIT(new_block[4+j] , /EXTRACT)
        ; Get track number
        rp_new.tracks[j].track = split[1]
        ; Get reference time
        time_string = STRJOIN([split[2],split[3]],'.',/SINGLE)
        rp_new.tracks[j].date_time = eis_stev2tai(time_string)

        ; Get long, latitude and rotation rate
        rp_new.tracks[j].lon  = split[4]
        rp_new.tracks[j].lat  = split[5]
        rp_new.tracks[j].rate = split[6]

    ENDFOR

ENDFOR

rp = {rp_prev      : rp_prev             ,                         $
      n_uploads    : n_uploads           ,                         $
      upload_times : upload_times        ,                         $
      rp_ore_array : rp_ore_array        ,                         $
      rp_start     : rp_start            ,                         $
      rp_new       : rp_new}


RETURN , rp


END
