;+
; PROJECT
;           SolarB EIS
;
; NAME
;           eis_search_pointing_files_new
;
; PURPOSE
;           Function to read
;
; NAME
;           eis_search_pointing_files_new
;
; CATEGORY
;           EIS Timeline Planning
;
; INPUTS
;           raster start time (TAI)
;
; WRITTEN
;           John A Rainnie RAL Sept-2006
;
; HISTORY:
;           v0.1 JAR RAL 19-Jun-2007
;               Completely re-designed this routine. Now it reads in all the
;               info from each re-point file and searches for instances
;               where the raster start_time falls within the time of the
;               first ORe-point Start and the "To:" time. In the case of
;               overlapping re-point files (where more than 1 file satisfies
;               this condition) the one with the latest ORe-point Start
;               date-time is selected.
;          v0.2 JAR 19-Sep-2007
;               If a re-point file contains NO "ORe-point Start" lines
;               then bail out
;          v0.3 JAR 5-Jun-2008
;               Added keyword NUM_DAYS
;          v0.4 DHB 24-Apr-2009
;               Modified to skip files with no "ORe-point" lines rather
;               than bail out completely so we can pick up later files
;               if they exist. JAR slightly modified the message to
;               including fileName of offending file.
;
;
;-
;______________________________________________________________________________
FUNCTION eis_search_pointing_files_new , start_time , num_days= num_days

; If keyword NUM_DAYS not set, then default is 3 days.
IF (N_ELEMENTS(num_days) EQ 0) THEN num_days = 3

disp_start = start_time

ds     = eis_create_datestring_array(disp_start , 24. ,num_days = num_days)
p_list = 0

FOR i = 0, N_ELEMENTS(ds) -1 DO BEGIN

    pathName = STRJOIN([GETENV('CMDPLN'),ds[i]] , PATH_SEP() , /SINGLE)
    baseName = STRJOIN(['re-point_',ds[i],'*','.','*'] , /SINGLE)
    filter   = CONCAT_DIR(pathName , baseName)
    search   = FILE_SEARCH(filter , count = count , /FOLD_CASE)

    IF (count EQ 1) THEN BEGIN

        fileName = search[0]


        ; Read entire contents of re-point file into a structure
        p = eis_read_file_repoint_events(fileName)

      ; Get Start_Time
      IF (N_TAGS(p.rp_ore_array[0]) EQ 0) THEN BEGIN

         ;RETURN , 0
         mText = 'Skipping this file because there are no re-points...'
         MESSAGE , mText    , /INFO , /NONAME
         MESSAGE , '  ...' + fileName , /INFO , /NONAME

      ENDIF ELSE BEGIN

           ; Get first "ORe-point Start" time
           time_from = p.rp_ore_array[0].date_time


        ; Get date-time from "Fm:" field (2nd line in file)
        time_to   = p.rp_prev.stop_time

        ; Catch if raster start_time is within this time range.
        IF (start_time GT time_from) AND                                    $
           (start_time LT time_to)   THEN BEGIN

            CASE N_TAGS(p_list[0]) OF
                ; If it's the first instance, then save it
                0    : p_list = p

                ; If it's NOT the first instance, then check if it is
                ; later  - using the "Fm:" date-time.
                ; If so, then save it.
                ELSE : IF (p.rp_prev.start_time       GT                    $
                           p_list.rp_prev.start_time) THEN p_list = p
            ENDCASE
        ENDIF

      ENDELSE

    ENDIF

ENDFOR

RETURN , p_list


END
