;+
; PROJECT
;           SolarB EIS
;
; NAME
;           eis_get_raster_pointing
;
; PURPOSE
;           Function to read
;
; CATEGORY
;           EIS Timeline Planning
;
; INPUTS
;           raster start time (TAI)
;
; WRITTEN
;           John A Rainnie RAL Sept-2006
; HISTORY:
;          v0.1 JAR RAL 14-Jan-2007
;               Added popup menu to select re-pointing file if more than
;               one was found
;          v0.2 JAR RAL 19-Jun-2007
;               Big change to the way re-point information is acquired.
;               The called routine "eis_search_pointing_files_new" will
;               now return only 1 re-point structure (p_list).
;          v0.3 12-Jul-2007
;               Now return tracking curve value. This is now saved for
;               each studyRaster as the tag .cmp
;          v0.4 JAR 18-Sep-2007
;               Take account of X/Y Offsets for tracking curves
;          v0.5 JAR 7-Mar-2008
;               Removed EIS/SOT offsets from calculations
;          v0.6 JAR 5-Jun-2008
;               Added keyword num_days
;          v0.7 JAR 7-Sep-2011
;               For non-zero tracking curves check the validity of
;               p.rp_new. Found to be sometimes zero.
;          v0.8 Commented out call to "eis_pointing_offset". It's not being
;               used here.
;
;-
;______________________________________________________________________________
FUNCTION eis_get_raster_pointing , start_time , stop_time , $
                                   num_days= num_days

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

; JAR 31-Mar-2014
; Comment out this call. We don't use the offsets in this routine.
;; Get EIS pointing offset
;offsets = eis_pointing_offset()

; Get appropriate re-pointing structure
p_list = eis_search_pointing_files_new(start_time,num_days=num_days)

; Catch case where a re-point file was NOT found!
IF (N_TAGS(p_list[0]) EQ 0) THEN BEGIN

    print,'***************************************'
    print,''
    print,' Re-pointing file was NOT found.'
    print,' Setting pointing to 0,0'
    print,''
    print,'***************************************'
    RETURN , [0. , 0.]

ENDIF

; OK, how many structures did we find?
num_p = N_ELEMENTS(p_list)
; Select last entry in list of filenames
p = p_list[num_p - 1]

;IF (num_fileName GT 1) THEN BEGIN
;
;    ; Select last entry in list of filenames
;    fileName = fileName_list[num_fileName - 1]
;
;;    popup_title = 'Select a pointing file'
;;    popup_xsize = MAX(STRLEN(fileName_list))
;;
;;    index = eis_select_pointing_file_popup(title  =    popup_title       ,  $
;;                                           value  =    fileName_list)
;;
;;    fileName = fileName_list[index]
;
;ENDIF ELSE BEGIN
;
;    fileName = fileName_list[0]
;
;ENDELSE

; Get list of pointing start_times
p_start_time = p.rp_ore_array[*].date_time
where_list   = WHERE(start_time GE p_start_time , count)

; Bail out gracefully if nothing is found
IF (count EQ 0) THEN BEGIN
    print,'***************************************'
    print,''
    print,' Re-pointing file was NOT found.'
    print,' Setting pointing to 0,0'
    print,''
    print,'***************************************'
    RETURN , [0. , 0.]
ENDIF

; We want the last ORe-pointing time
latest_point = where_list[count-1]

; Get the whole line, includes tracking mode, offsets and comments.
; ORe-point Start  2007/06/20 06:35:30  3  00.0000 00.0000 /* Blah blah */
rp_line      = p.rp_ore_array[latest_point]


IF (rp_line.track EQ 0) THEN BEGIN

    solar_xy = [rp_line.y_offset*(-3600.),rp_line.x_offset*(-3600.)]

    ; Convert from arcminutes to arcseconds and add offsets
    x_point  = solar_xy[0] ;- offsets.x_offset
    y_point  = solar_xy[1] ;- offsets.y_offset

    pointing = {x_point:x_point , y_point:y_point , tracking:rp_line.track}

ENDIF ELSE BEGIN
    ; JAR 7-Sep-2011
    ; Encountered a problem where p.rp_new is zero!
    IF (N_TAGS(p.rp_new) EQ 0) THEN RETURN,  [0. , 0.]

    rp_track     = p.rp_new.tracks[rp_line.track - 1]

    ; Get duration from re-point start time to required time
    duration = start_time - rp_track.date_time ; seconds
    ; Given the rotation rate, calculate the new angle
    new_angle = rp_track.rate * duration
    ; Add to longitiue
    new_lon = new_angle + rp_track.lon

    ; Calculate
    solar_xy = hel2arcmin(rp_track.lat , new_lon , date = start_time)

    ; Convert from arcminutes to arcseconds and add offsets
    x_point = (solar_xy[0] * 60.) ;- offsets.x_offset
    y_point = (solar_xy[1] * 60.) ;- offsets.y_offset

    ; JAR 18-Sep-2007
    ; Add in X/Y offsets - convert to arcseconds
    x_point = x_point + (rp_line.y_offset * (-3600.))
    y_point = y_point + (rp_line.x_offset * (-3600.))

    pointing = {x_point:x_point , y_point:y_point , tracking:rp_line.track}

ENDELSE


RETURN , pointing

END
