PRO mk_xrt_mirr_genx, outfile=outfile, outvar=outvar, qabort=qabort

; =========================================================================
;+
; PROJECT:
;       Solar-B / XRT 
;
; NAME:
;       
;       MK_XRT_MIRR_GENX
;
; CATEGORY:
;       
;       Calibration
;
; PURPOSE:
;       
;       Generate the XRT Mirror description structure.
;       Convert textfile tables of multilayer data into an IDL genx file.
;       WARNING: This is for ONE BOUNCE off the mirror.
;
; CALLING SEQUENCE:
;
;       MK_XRT_MIRR_GENX [, outfile=outfile] [, outvar=outvar]
;                        [, qabort=qabort]
;       
; INPUTS:
;       
;       Reads four files: zerodur.00.10-05.00nm.txt,
;                         zerodur.05.01-10.00nm.txt,
;                         zerodur.10.01-15.00nm.txt,
;                         zerodur.15.01-20.00nm.txt 
;
; KEYWORDS:
;
;       OUTFILE - [Optional] (string) May specify the genx filename.
;                 Default = "./xrt_mirror.geny".
;
; OUTPUTS:
;
;       OUTVAR  - [Optional] (structure) The mirror 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_mirr_genx
;
;       Want to see result in IDL:
;       IDL> outvar = 1
;       IDL> mk_xrt_mirr_genx, outvar=outvar
;       IDL> help, outvar 
;
;       Make file and put it in $SSW_XRT_CALIB:
;       IDL> outfile = get_logenv('$SSW_XRT_CALIB')+'/genx/xrt_mirror.geny'
;       IDL> mk_xrt_mirr_genx, outfile=outfile
;
; COMMON BLOCKS:
;
; 	none 
;
; NOTES:
;
;       1) (NB: Needs to be updated.)
;          Each .txt file in directory "dir" is read, and the
;          columns are converted to variables. The variables are
;          saved to a .sav file in "dir" with the same base-name. 
;          These .sav files are used to generate the instrument 
;          response .genx files.
;
;          The textfiles must be of the following form. (Ie, they
;          should be consistent with the files we get from Dave
;          Windt.) The first 18 lines are ignored, so they may be
;          used for header info. Below that... The first column
;          is wavelength [Angstroms]. The second column is 
;          reflectance [fractional transmission].
;
;
; 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 = 'v2003-Feb-10' ;--- (M.Weber) Generated from someone else's 
;                             version of <mk_mir_save.pro> for DECODE 
;                             and AIA.
progver = 'v2005-Mar-09' ;--- (MW) Generated from <mk_aia_mirr_genx.pro>.
progver = 'v2007-May-13' ;--- (MW) Modified for XRT analysis tree.
;                      
;-
; =========================================================================


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

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

  mirr_name         = 'Hinode_XRT_FM_mirror'
  mirr_descrip      = 'Source database at ' +                          $
              'http://www.cxro.lbl.gov/optical_constants/mirror2.html' $
              + '; material = Zerodur; density = -1; angle = 0.91 deg'
  wave_units        = 'Angstroms'

  data_files = [XRT_CALIB+'/refl/zerodur.00.10-05.00nm.txt', $
                XRT_CALIB+'/refl/zerodur.05.01-10.00nm.txt', $
                XRT_CALIB+'/refl/zerodur.10.01-15.00nm.txt', $
                XRT_CALIB+'/refl/zerodur.15.01-20.00nm.txt'   ]


;=== 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 ================================

  wave = 0.
  refl = 0.
  for ii = 0,(n_elements(data_files)-1) do begin
    readcol, data_files(ii), wave0, refl0, skip=2
    wave = [wave, wave0]
    refl = [refl, refl0]
  endfor

  wave = wave[1:*] * 10. ;; Convert nm to Angstroms
  refl = refl[1:*]


;=== Make mirror structure ===============================

  outvar = {type:'mirror', name:mirr_name, descrip:mirr_descrip, $
            wave:wave, wave_units:wave_units, refl:refl,         $
            length:n_elements(wave), data_files:data_files        } 


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

  savegenx, outvar, file=outfile


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