; docformat = 'rst'

;+
; Return the EIS/AIA offset values for any given date.
; :params:
;    date:
;      Date in any of the usual formats
; :keywords:
;    infile:
;      Optional location of interpolation data file. It should be
;      found automatically.
; :returns:
;    Array contiaining x-offset and y-offset values
; :uses:
;   anytim2idljd - generate JD as a double instead of a struct
; :author:
;   John Mariska, GMU jtmariska@gmail.com
; :history:
;   (2014-Jan-09) JTM: initial version, single date values only,
;   minimal error checking.
;   (2014-Jan-27) HPW: modified file search and date handling.
;   (2014-Oct-20) JTM: fix value in X from 2014-8-13 to 9-30.
;   (2015-Oct-29) JTM: quick and dirty fix for issues around end
;   of Sep heater changes and Hinode fine Sun sensor changes that
;   took place for a period in 2012.
;   (2018-Oct-10) JTM: Adjusted for additional x-offset that
;   developed after the EIS 2018 winter vacation.
;   (2018-Oct-19) HPW: Bug fix: xoff1 -> x_off1.
;   (2019-Nov-20) JTM: Adjusted for small additional change in
;   x-offset.
;   (2020-Jul-08) JTM: Additional adjustment for change in x-offset.
;-
function eis_aia_offsets, input_date, infile=infile
    compile_opt idl2

    date = anytim(input_date,/ccsds)

    ; Locate interpolation file
    if (n_elements(infile) eq 0) then begin
        path = filepath('',root=getenv('SSW'), $
                        sub=['hinode','eis','data','eis_aia_offset'])
        infile = file_search(path,'eis_aia_yearly_average*.txt',count=n_files)
        infile = infile[n_files-1]
        if (infile eq '') then begin
            message, 'Interpolation file not found.'
            return, [!null, !null]
        endif
    endif

    ; Read interpolation table
    table = rd_tfile(infile, /auto, /nocom, /convert)

    ; First two entries are secular trend info
    xsec = table[*, 0]
    ysec = table[*, 1]

    ; Remainder is the interpolation table
    yfrac = reform(table[0, 2:*])
    xoff = reform(table[1, 2:*])
    yoff = reform(table[2, 2:*])

    ; Figure out fraction of year and interpolate
    year = strtrim((anytim2ex(date))[4], 2)
    ystr0 = year + '-jan-01 00:00:00'
    y0 = anytim2idljd(ystr0)

    ystr1 = strtrim(fix(year)+1, 2) + '-jan-01 00:00:00'
    y1 = anytim2idljd(ystr1)

    y = anytim2idljd(date)
    frac = (y - y0)/(y1 - y0)
    x_off = interpol(xoff, yfrac, frac)
    y_off = interpol(yoff, yfrac, frac)

    ; Add secular trend (Now read from data file.)
    ; xsec = [0.00316568, -1.34315747]
    ; ysec = [0.00606907, -2.76705497]

    xsec0 = anytim2idljd('2012-jan-01 00:00:00')
    x_off1 = x_off + (y - xsec0)*xsec[0] + xsec[1]
    y_off1 = y_off + (y - xsec0)*ysec[0] + ysec[1]

    ; Fudge answer in X during intervals near Sep heater
    ; change.
    dstart = anytim2tai('2014-08-13 00:00:00')
    dend = anytim2tai('2014-09-30 10:46:00')
    in_date_tai = anytim2tai(input_date)
    if (in_date_tai gt dstart) and (in_date_tai le dend) then begin
        x_off1 = 17.1
    endif

    dstart = anytim2tai('2015-08-13 00:00:00')
    dend = anytim2tai('2015-09-30 10:46:00')
    in_date_tai = anytim2tai(input_date)
    if (in_date_tai gt dstart) and (in_date_tai le dend) then begin
        x_off1 = 17.1
    endif

    dstart = anytim2tai('2018-05-01 00:00:00')
    if in_date_tai gt dstart then begin
        x_off1 = x_off1 + 11.1
    endif

    dstart = anytim2tai('2018-09-30 00:00:00')
    if in_date_tai gt dstart then begin
        x_off1 = x_off1 - 2.38
    endif

    dstart = anytim2tai('2020-01-01 00:00:00')
    if in_date_tai gt dstart then begin
        x_off1 = x_off1 - 2.5
    endif

    
   ; print, x_off, y_off 
   ; print, x_off1, y_off1

    return, [x_off1, y_off1]
end
