PRO mk_xrt_ccd_genx, outfile=outfile, outvar=outvar, qabort=qabort

; =========================================================================
;+
; PROJECT:
;       Solar-B / XRT 
;
; NAME:
;       
;       MK_XRT_CCD_GENX
;
; CATEGORY:
;       
;       Calibration
;
; PURPOSE:
;       
;       Generate the XRT CCD description structure.
;
; CALLING SEQUENCE:
;
;       MK_XRT_CCD_GENX [, outfile=outfile] [, outvar=outvar]
;                       [, qabort=qabort]
;       
; INPUTS:
;       
;       Just reads two files: "atten_len_Si.txt", "atten_len_SiO2.txt".
;
; KEYWORDS:
;
;       OUTFILE - [Optional] (string) May specify the genx filename.
;                 Default = "./xrt_ccd.geny".
;
; OUTPUTS:
;
;       OUTVAR  - [Optional] (structure) The CCD structure as written
;                 to the genx file.
;       QABORT  - [Optional] (Boolean) Indicates that the program
;                 exited gracefully without completing. (Might be
;                 useful for calling programs.)
;                 0: Program ran to completion.
;                 1: Program aborted before completion.
;
; EXAMPLES:
;      
;       Full auto:
;       IDL> mk_xrt_ccd_genx
;
;       Want to see result in IDL:
;       IDL> outvar = 1
;       IDL> mk_xrt_ccd_genx, outvar=outvar
;       IDL> help, outvar 
;
;       Make file and put it in $SSW_XRT_CALIB:
;       IDL> outfile = get_logenv('$SSW_XRT_CALIB')+'/genx/xrt_ccd.geny'
;       IDL> mk_xrt_ccd_genx, outfile=outfile
;
; COMMON BLOCKS:
;
; 	none 
;
; NOTES:
;
;       1) The QE formula and parameter values are taken from the
;          "Gray Book": "Solar-B XRT Flight CCD Camera System Data Package"
;          (2003.Aug.26)
;
;       2) The CCD Gain is also taken from the "Gray Book".
;
; CONTACT:
;
;       Comments, feedback, and bug reports regarding this routine may be 
;       directed to this email address: 
;                xrt_manager ~at~ head.cfa.harvard.edu 
;       
; MODIFICATION HISTORY:
;
progver = 'v2007-May-13' ;--- (M.Weber (SAO)) Written. 
;                      
;-
; =========================================================================


;=== Initial setup =======================================

  prognam           = 'MK_XRT_CCD_GENX'
  def_outfile       = 'xrt_ccd'  ;; '.geny' is added automatically
  XRT_CALIB         = get_logenv('$SSW_XRT_CALIB')
  qabort            = 0B

  ccd_name          = 'Hinode_XRT_FM_CCD'
  ccd_descrip       = 'Marconi; back-illuminated; non-AIMO; ' + $
                      'non-AR coated; non Anti-bloom'
  wave_units        = 'Angstroms'
  full_well         = 222000.   ;; electrons 
  pixel_size        = 13.5
  pixel_size_units  = 'microns'

  infile_atten_Si   = XRT_CALIB+'/atten/atten_len_Si.txt'
  infile_atten_SiO2 = XRT_CALIB+'/atten/atten_len_SiO2.txt'

  d_Si = 15e4
  d_SiO2 = 61.
  eta = 0.40
  gamma = 1./(0.21e4)

  ;; One electron-hole pair per 3.65 eV.
  ev_per_el = 3.65

  ;; Camera Gain for the Left and Right readout ports.
  gain_l = 58.8  ;; electrons per 12-bit ADU
  gain_r = 59.1  ;; electrons per 12-bit ADU

  h_planck = 4.136e-15 ;; eV s
  c_light = 2.998e18 ;; Angstroms / s


;=== Check inputs ========================================

  if keyword_set(outfile) then begin
    if (datatype(outfile) ne 'STR') then begin
      qabort = 1B
      return
    endif
    if (n_elements(outfile) ne 1) then begin
      qabort = 1B
      return
    endif
  endif else begin
    outfile = def_outfile
  endelse


;=== Calculate components ================================

  ;=== The Quantum Efficiency (QE)
  vtemp = rd_tfile(infile_atten_Si, 2, /hskip)
  wave_Si = reform(vtemp[0,*]) * 10. ;; Angstroms
  len_Si  = reform(vtemp[1,*]) * 1e4

  vtemp = rd_tfile(infile_atten_SiO2, 2, /hskip)
  wave_SiO2 = reform(vtemp[0,*]) * 10. ;; Angstroms
  len_SiO2  = reform(vtemp[1,*]) * 1e4

  ph_energy = h_planck * c_light / wave_Si ;; eV

  aa = exp(-d_SiO2/len_SiO2)
  bb = (1-eta)/len_Si/(gamma+(1./len_Si))
  cc = exp(-d_Si/len_Si)
  dd = bb * exp(-gamma*d_Si)
  qe = aa * (1 - bb - cc*(1-dd))

  ;=== Electrons per photon, versus wavelength.
  ph_energy = h_planck * c_light / wave_Si ;; eV
  el_per_ph = ph_energy / ev_per_el


;=== Make CCD structure ==================================

  outvar = {type:'ccd', name:ccd_name, descrip:ccd_descrip, wave:wave_Si, $
         wave_units:wave_units, length:n_elements(wave_Si), qe:qe,      $
         el_per_ph:el_per_ph, gain_l:gain_l, gain_r:gain_r,             $
         full_well:full_well, pixel_size:pixel_size,                    $
         pixel_size_units:pixel_size_units,                             $
         data_files:[infile_atten_Si,infile_atten_SiO2]                   }


;=== Write genx file =====================================

  savegenx, outvar, file=outfile



END ;======================================================================
