;+
; NAME
;          EIS_AIA_READ_OFFSET_FILE
;
; PURPOSE
;          This function reads the text file;
;          "$SSW/hinode/eis/data/eis_aia_offset/eis_aia_avgoffsets.txt"
;          and returns the EIS-AIA offsets as a structure array.
;
; INPUTS
;          NONE
; USE:
;          r = eis_aia_read_offset_file()
;
; WRITTEN
;          John A Rainnie, RAL, 16-Jul-2013
;
; HISTORY
;          v0.1 JAR 16-Jul-2013
;               Blah
;-
;______________________________________________________________________________
FUNCTION eis_aia_read_offset_file

; OK, let's find the file. It has a fixed name and location.
basename = 'eis_aia_avgoffsets.txt'
pathname = STRJOIN([GETENV("SSW")                      ,           $
                    'hinode','eis','data','eis_aia_offset'], $
                 PATH_SEP(),/SINGLE)
filename = CONCAT_DIR(pathname,basename)
; And just for insurance, check it's there. If not, then return zero.
; This represents a failure - and success will return a structure.
IF ~FILE_TEST(filename) THEN BEGIN
   print," File not found!"
   RETURN , 0
ENDIF
; OK, let's get to work. First read in the file
lines = rd_ascii(filename)
; Remove lines that begin with comment "#". Create a list of indicies
; locating where the first character "#" is located. It it's zero, then
; this line is a comment - and remove it from the array
list = STRPOS(STRCOMPRESS(lines,/REMOVE_ALL),'#')
nonzero_list = WHERE(list NE 0 , nonzero_count)
; OK, if ALL lines are commented out (why would they be, but hey)
IF (nonzero_count EQ 0) THEN BEGIN
   print," File contains no data!"
   RETURN , 0
ENDIF

; OK, we've eliminated the lines which have been commented out
IF (nonzero_count NE 0) THEN lines = lines[nonzero_list]

;
; OK, so loop over each line and populate an array of structures
struct = {date:0.D,avg_x:0.,sigma_x:0.,avg_y:0.,sigma_y:0.,npts:0}
struct_array = REPLICATE(struct,N_ELEMENTS(lines))
FOR i = 0 , N_ELEMENTS(lines) - 1 DO BEGIN

   ; OK, each line is of the form;
   ; date, avg_x, sigma_x, avg_y, sigma_y, npts
   split = STRSPLIT(lines[i] , /EXTRACT)
   struct_array[i].date    = anytim2tai(split[0])
   struct_array[i].avg_x   = FLOAT(split[1])
   struct_array[i].sigma_x = FLOAT(split[2])
   struct_array[i].avg_y   = FLOAT(split[3])
   struct_array[i].sigma_y = FLOAT(split[4])
   struct_array[i].npts    = FIX(split[5])

ENDFOR


RETURN , struct_array

END
