pro eis_modfits, data,  hdr,  fitsfile =  fitsfile, noaux=noaux
 
;+
; NAME:
;       EIS_MODFITS
;
; PURPOSE:
;	Write FITS data object and FITS header to EIS type FITS file. 
;       Either create new FITS file or modify existing FITS file 
;       with new header or data)
;
;
; CATEGORY:
;       Data Processing Software
;
; CALLING SEQUENCE:
;       eis_modfits, filename, hdr,data
;
; INPUTS:
;       hdr: FITS header object of type 'fits_hdr'
;       data: data object of type 'eis_data'
;
; KEYWORD PARAMETERS:
;       fitsfile: name of FITS file
;       
;
; OUTPUTS:
;       A FITS file
;
; CALLS:
;
;
; COMMON BLOCKS:
;
;
; PROCEDURE:
;
; RESTRICTIONS:
;
;
; MODIFICATION HISTORY:
;       16-Feb-2004: Oivind Wikstol, version 1.0
;       06-May-2004: Added check on file exist. fitsfile changed to keyword
;       10-Oct-2007: A. Gardini - Added Doppler velocities
;       12-Nov-2008: A. Gardini - Get the values of exp_dur, fmirr etc. from
;                                 data->getaux_data instead that from methods
;       08-Apr-2010: T. Fredvik - Added slit tilt and wavelength correction
;                    (in pixels) based on Kamio-san's HK temperature model
;       27-Nov-2015: T. Fredvik - Fixed fundamental bug that prevented the
;                    slit tilt wavelength correction array to be saved correctly.  
;  
;
; $Id: eis_modfits.pro 5557 2015-11-27 09:00:25Z tfredvik $
;
;-

; check if fisfile name input, else create name:
  if n_elements(fitsfile) eq 0 then begin
    get_utc, date, /ccsds ; date in correct format WHEN FILE IS WRITTEN
    fitsfile='eis_'+strtrim(date,2)+'.fits' 
  endif else begin
    fitsfile = fitsfile
  endelse
  
  if n_elements(noaux) eq 0 then noaux=0
  
; Write the primary header to file
  fxist =  file_test(fitsfile)
  if(fxist) then begin
    message =  'File already exists. Do you want to overwrite?'
    q =  dialog_message(message,  /question,/center)
    if q eq 'No' then return
  endif
  main_hdr = hdr-> getmain_hdr()
  fxwrite, fitsfile, main_hdr

; open bte at the end of fits file, write bte hdr
  bte_hdr = hdr-> getbte_hdr()
  
  hkpixcorrset =  (ptr_valid((data->getaux_data()).hkpixcorrtime)) ? 1 : 0
  IF hkpixcorrset THEN BEGIN 
     hkpixcorrtilt = *(data->getaux_data()).hkpixcorrtilt
     hkpixcorrtime = *(data->getaux_data()).hkpixcorrtime
     tfields = fxpar(bte_hdr,'TFIELDS')

     fxbaddcol,tfields+1,bte_hdr,hkpixcorrtilt,'HKPIXCORRTILT', $
               'Wavelength correction, slit tilt', tunit='Pixels',$
               tdmin=min(hkpixcorrtilt),tdmax=max(hkpixcorrtilt)
      
     fxbaddcol,tfields+2,bte_hdr,hkpixcorrtime[0],'HKPIXCORRTIME', $
               'Wavelength corrections due to orbital variations', $
               tunit='Pixels',tdmin=min(hkpixcorrtime),tdmax=max(hkpixcorrtime)
  ENDIF
  
  fxbcreate, lu, fitsfile, bte_hdr

; write pixel data to BTE:
  nwin=data->getnwin()        ; number of line windows
  nexp = data->getnexp()   ; number of raster position  
  for icol = 0, nwin-1 do begin
    data ->getwin, icol, wd, pos
    for irow = 1, nexp do begin
      fxbwrite, lu, wd[*,*,irow-1], icol+1, irow
    endfor
  endfor
  
  if noaux then goto,finished

  t1 = *(data->getaux_data()).ti_1
  t2 = *(data->getaux_data()).ti_2
  mhc_dur = *(data->getaux_data()).mhc_dur
; exp_dur = hdr-> getexp_dur()
; fmirr = hdr-> getfmirr()
; hsl = hdr->gethsl()
; xrt_flfl = hdr->getxrt_flfl()
; xrt_fl_x = hdr->getxrt_fl_x()
; xrt_fl_y = hdr->getxrt_fl_y()
; aec_hepc = hdr->getaec_hepc()
; aec_lepc = hdr->getaec_lepc()
; mhcsg = hdr->getmhcsg()
  exp_dur = *(data->getaux_data()).exp_dur
  fmirr = *(data->getaux_data()).fmirr
  hsl = *(data->getaux_data()).hslstat
  xrt_flfl = *(data->getaux_data()).xrt_flfl
  xrt_fl_x = *(data->getaux_data()).xrt_fl_x
  xrt_fl_y = *(data->getaux_data()).xrt_fl_y
  aec_hepc = *(data->getaux_data()).aec_hepc
  aec_lepc = *(data->getaux_data()).aec_lepc
  mhcsg = *(data->getaux_data()).mhcfmsg

  xcen = *(data->getaux_data()).xcen
  ycen = *(data->getaux_data()).ycen
  if ptr_valid((data->getaux_data()).ccda_temp) then begin
    ccda_temp = float(*(data->getaux_data()).ccda_temp)
    ccdb_temp = float(*(data->getaux_data()).ccdb_temp)
    ccd_temp=1
  endif else begin
    ccd_temp=0
  endelse
  if ptr_valid((data->getaux_data()).mhc_hz_t10) then begin
    mhc_hz_t10 = float(*(data->getaux_data()).mhc_hz_t10)
    mhc_hz_t15 = float(*(data->getaux_data()).mhc_hz_t15)
    mhc_temp=1
  endif else begin
    mhc_temp=0
  endelse
  if ptr_valid((data->getaux_data()).v_sat) then begin
    v_sat = float(*(data->getaux_data()).v_sat)
    v_sun = float(*(data->getaux_data()).v_sun)
    v_eth = float(*(data->getaux_data()).v_eth)
    doppler_v=1
  endif else begin
    doppler_v=0
  endelse
  icol=nwin
  iadd=0
  for irow = 1, nexp do begin
      fxbwrite, lu, double(t1[irow-1]), icol+1, irow
      fxbwrite, lu, double(t2[irow-1]), icol+2, irow
      fxbwrite, lu, double(mhc_dur[irow-1]), icol+3, irow
      fxbwrite, lu, double(exp_dur[irow-1]), icol+4, irow
      fxbwrite, lu, fmirr[irow-1], icol+5, irow
      fxbwrite, lu, hsl[irow-1], icol+6, irow
      fxbwrite, lu, xrt_flfl[irow-1], icol+7, irow
      fxbwrite, lu, xrt_fl_x[irow-1], icol+8, irow
      fxbwrite, lu, xrt_fl_y[irow-1], icol+9, irow
      fxbwrite, lu, aec_hepc[irow-1], icol+10, irow
      fxbwrite, lu, aec_lepc[irow-1], icol+11, irow
      fxbwrite, lu, mhcsg[irow-1], icol+12, irow
      fxbwrite, lu, xcen[irow-1], icol+13, irow
      fxbwrite, lu, ycen[irow-1], icol+14, irow
      if ccd_temp then begin
         fxbwrite, lu, ccda_temp[irow-1], icol+15, irow
         fxbwrite, lu, ccdb_temp[irow-1], icol+16, irow
         iadd=2
      endif
      if mhc_temp then begin
         fxbwrite, lu, float(mhc_hz_t10[irow-1]), icol+15+iadd, irow
         fxbwrite, lu, float(mhc_hz_t15[irow-1]), icol+16+iadd, irow
         iadd=iadd+2
      endif
      if doppler_v then begin
         fxbwrite, lu, float(v_sat[irow-1]), icol+15+iadd, irow
         fxbwrite, lu, float(v_sun[irow-1]), icol+16+iadd, irow
         fxbwrite, lu, float(v_eth[irow-1]), icol+17+iadd, irow
         iadd = iadd+3
      ENDIF
      IF hkpixcorrset THEN fxbwrite, lu, double(hkpixcorrtime[irow-1]), icol+16+iadd, irow
     
   endfor
  
  ;; Write 2D (NAXIS2*2) array hxpixcorrtilt to the first row of the bte, the
  ;; other rows of this column will be 0. In order to retrieve the array from
  ;; the FITS file in the correct format, fxbread must be used. 
  IF hkpixcorrset THEN fxbwrite, lu, hkpixcorrtilt, icol+15+iadd, 1
  
; Close the binary table extension
  finished:
  
  fxbfinish, lu
end
