pro eis_moment::savefits  fitsfile = fitsfile
;+
; NAME:
;       EIS_MOMENT__SAVEFITS
;
; PURPOSE:
;	Makes FITS files out of telemetry packets for the 
;	EIS instrument on Solar-B.
;
;
; CATEGORY:
;       Data Processing Software
;
; CALLING SEQUENCE:
;       eis_moment__savefits, fitsfile=fitsfile
;
; INPUTS:
;
; KEYWORD PARAMETERS:
;       fitsfile: Input name of fits file (otherwise created automatically 
;                 from date)
; OUTPUTS:
;       A FITS file
;
; CALLS:
;
;
; COMMON BLOCKS:
;
;
; PROCEDURE:
;
; RESTRICTIONS:
;
;
; MODIFICATION HISTORY:
;       2004-Oct-15: Øivind Wikstøl - First version
;       2009-Apr-30: A. Gardini     - Fixel 1 pixel shift error.
;-
  hdr = self-> gethdr()  ; FITS hdr object (from pixel data)
  main_hdr = hdr-> getmain_hdr()
  date = fxpar(main_hdr, 'DATE_OBS')  ; date and time of raster start
  if n_elements(fitsfile) eq 0 then begin
    fitsfile='eis_moment_'+strtrim(date,2)+'.fits' 
  endif else begin
    fitsfile = fitsfile
  endelse

  
;  data->getwin, 0, wd, pos
; Create the primary FITS header with Binary Table Extensions:
  fxhmake, header, /initialize, /date, /extend
;

; Add keywords to the primary header. One subroutine for each 
; category of header data
  eis_fits_dataid, header, hdr, plan,  datatype
  eis_fits_obstime, header, hdr, plan
  eis_fits_coord, header, hdr, plan
  eis_fits_calib, header, hdr, calstat
;  eis_fits_dqual, header, hdr

; Write the primary header to file
  fxwrite, fitsfile, header
; Define the FITS Binary Table Extension
  nexp = data->getnraster()   ; number of raster position
  fxbhmake, header, nexp, 'DATA', 'Heres a comment'
;
; Create the columns for the line windows
  nwin=data->getnwin() 
  exp_info = hdr[0]-> getexp_info()
  for iwin=0, nwin-1 do begin
    data->getwin, iwin, wd, pos
    ccd_nr = fix(exp_info.ccd_nr[iwin])
    wdmin = min(wd)  ; minimum value in line window       
    wdmax = max(wd)  ; maximum value in line window       

; get wavelength scale 
    if pos[0] gt 2147 then begin 
      addpix = -2148
      lambda = lambda_a
      wband = 'A'
    endif else begin 
      addpix = 0
      lambda = lambda_b
      wband = 'B'
   endelse

    lstart = string(lambda[pos[0]+addpix])
    dispstr = string(disp)
    ; column titles should be replaced with line IDs when available
    if n_elements(plan) eq 0 then begin
      colname = 'Line '+strtrim(iwin,2)
    endif else begin
      colname = (plan-> getline_id())[iwin]
    endelse
    fxbaddcol, col, header, fix(wd[*,*,0]), colname, $
               'Data window', tunit = 'ADC', $
                tdesc = '(Wavelength, solar_x, solar_y)', $
                tcuni = '(Angstrom, arcsec, arcsec)', $
                tdmin = wdmin, tdmax = wdmax, $
                trval = '('+lstart+', 0, 0)', $
                tdelt = '('+dispstr+', 0, 0)', $
                trota = '(0, 0, 0)'

; add keywords that are not standard to fxbaddcol, 
; using fxaddpar.
    detx = pos[0]  ; line window start x position
    dety = pos[2]  ; line window start y position
    cwave = lambda[pos[0]+addpix] ; this should be taken from planning tool, 
                           ; since it should be central wavelength, 
                           ; and not lambda start in line window.
    wmin = lambda[pos[0]+addpix]  ; minimum wavelength
    wmax = lambda[pos[0] + pos[1]-1+addpix]  ; maximum wavelength
    
    col=strtrim(col,2)
    fxaddpar,header,'tdetx'+col,detx, $
		'detector start column'
    fxaddpar,header,'tdety'+col,dety, $
		'Detector start row'
    fxaddpar,header,'tdetn'+col,ccd_nr, $
		'CCD number (A=1, B=2)'
    fxaddpar,header,'twave'+col,cwave, $
		'Principal wavelength in Angstrom'
    fxaddpar,header,'twmin'+col,wmin, $
		'Preliminary minimum wavelength in Angstrom'
    fxaddpar,header,'twmax'+col,wmax, $
		'Preliminary maximum wavelength in Angstrom'
    fxaddpar,header,'twbnd'+col,wband, $
		'Wavelength band'
  endfor
;
; Add columns for EIS Hdr data.
;
  nhdr = 5
  t1=lonarr(nexp)
  t2=lonarr(nexp)
  durmhc=lonarr(nexp)
  dursec=intarr(nexp)
  fmirr=intarr(nexp)
  for i = 0, nexp -1 do begin 
    t1[i] = long((hdr[i]-> getexp_info()).ti_1)
    t2[i] = long((hdr[i]-> getexp_info()).ti_2)
    durmhc[i] = (hdr[i]-> getexp_info()).exp_mhc
    dursec[i] = (hdr[i]-> getexp_info()).exp_dur
    fmirr[i] = (hdr[i]-> getexp_info()).fmirr_pos
  endfor

  fxbaddcol, col, header, [t1[0]], 'TI_1', 'Shutter open time', $
                tunit = 'TI', $
                tdmin = min(t1), $
                tdmax = max(t1)
  fxbaddcol, col, header, t2[0], 'TI_2', 'Shutter close time', $
                tunit = 'TI', $
                tdmin = min(t2), $
                tdmax = max(t2)
  fxbaddcol, col, header, durmhc[0], 'mhc_dur', $
                'Exposure duration as measured by the MHC', $
                tunit = '100 microsec', $
                tdmin = min(durmhc), $
                tdmax = max(durmhc)
  fxbaddcol, col, header, dursec[0], 'exp_dur', $
                'Exposure time as specified by the sequence', $
                tunit = '50 ms', $
                tdmin = min(dursec), $
                tdmax = max(dursec)
  fxbaddcol, col, header, fmirr[0], 'fmirr', $
                'Fine mirror position in unknown units', $
                tunit = 'unknown', $
                tdmin = min(fmirr), $
                tdmax = max(fmirr)

  ncol = nwin + nhdr
  errmsg = ''
  fxbcreate, lu, fitsfile, header,errmsg = errmsg

  if errmsg ne '' then begin
    ok = dialog_message(errmsg,/informational)
    return
  endif

  for icol = 0, nwin-1 do begin
    data ->getwin, icol, wd, pos
    for irow = 1, nexp do begin
      fxbwrite, lu, fix(wd[*,*,irow-1]), icol+1, irow
    endfor
  endfor

  icol=nwin
  for irow = 1, nexp do begin
      fxbwrite, lu, t1[irow-1], icol+1, irow
      fxbwrite, lu, t2[irow-1], icol+2, irow
      fxbwrite, lu, durmhc[irow-1], icol+3, irow
      fxbwrite, lu, dursec[irow-1], icol+4, irow
      fxbwrite, lu, fmirr[irow-1], icol+5, irow
  endfor

; write EIS hdr data

; Close the binary table extension
  fxbfinish, lu
  

end

