pro make_xrt_filter, XRT_filter, filter, filter_s, qabort, define=define, $
		     caldir=caldir, debug=debug
; =========================================================================
;+
; PROJECT:
;       Solar-B / XRT
;
; NAME:
;       MAKE_XRT_FILTER
;
; CATEGORY:
;       Calibration
;
; PURPOSE:
;       Generate the XRT FILTER structure. Calculates the transmissions for
;       elements in thicknesses using <multifilt.pro>.
;
; CALLING SEQUENCE:
;       MAKE_XRT_FILTER, XRT_filter, filter, filter_s [,qabort] [,/define]
;                                            [,caldir=caldir]
;
; INPUTS:
;       XRT_FILTER: [Mandatory] Variable to return XRT_FILTER structure
;           FILTER: [Mandatory*] filter input structure array from config file
;                   Also uses n_Al2O3.txt, n_BeO.txt, n_C.txt, 
;                   n_polyimide.txt, n_Ti.txt n_Al.txt, n_Be.txt, n_Open.txt,
;                   n_TiO2.txt from $SSW/hinode/xrt/calibration/refr. 
;                   These are called by the routine MULTIFILT used by 
;                   MAKE_XRT_FILTER. "Mandatory*" = This input is 
;                   mandatory when the /DEFINE keyword is NOT used.
;         FILTER_S: [Mandatory*] scalar string of one filter name, as they are
;                   defined in the FILTER input array.  "Mandatory*" = This 
;                   input is mandatory when the /DEFINE keyword is NOT used.
; KEYWORDS:
;       CALDIR: [Mandatory] location of ...../hinode/xrt/calibration
;      /DEFINE: [Optional] when set returns only XRT_filter structure
;                          definition. If this keyword is used, then
;                          FILTER and FILTER_S inputs are not required.
;
; OUTPUTS:
;      XRT_FILTER: (structure) Returns fully populated filter structure, or
;                              filter skeleton if called with /DEFINE.
;       QABORT: [Optional] program return status
;
; EXAMPLES:
;      make_xrt_filter, XRT_filter, /define
;      make_xrt_filter, XRT_filter, filter, filter_s, qabort
;
; COMMON BLOCKS:
;       None.
;
; NOTES:
;      Uses routine multifilt, now in file multifilt.pro
;      Code and logic reuse from calc_xrt_chn_trans.pro / get_xrt_channel.pro.
;      Calls make_xrt_ccd.pro, make_xrt_filter.pro, make_xrt_geometry.pro and
;      make_xrt_mirror.pro, to calculate channel structure components. Call
;      make_xrt_chn_trans.pro to generate transmission for each channel.
;
; MODIFICATION HISTORY:
;  progver = 'v2008.Sep.15' ; --- (Alisdair Davey/Mark Weber)
;                                 First debugged version. Derived from 
;                                 <mk_xrt_filt_genx.pro>, v2007-May-13.
   progver = 'v2008.Oct.01' ; --- (M.Weber) Reviewed version. Fixed passing
;                                 of mesh transmissions to <multifilt.pro>.
;
;-
; =========================================================================

   qabort = 0

   XRT_filter = {   XRT_filter_v0001, 				$
                  	    type: '',				$
		 flt_str_version: '',				$
		   flt_str_descr: '',				$
	                    name: '',				$
                       long_name: '',				$
                        material: strarr(4),			$
                           thick: fltarr(4), 			$
		     thick_units: '',				$
	         	    dens: fltarr(4),			$
		      dens_units: 'g cm^-3',				$
			    wave: fltarr(5000),			$
		      wave_units: 'Angstroms',			$
			   trans: fltarr(5000),			$
			  length: 0L,				$
	              mesh_trans: float(0),                     $
                       substrate: '',				$
		      data_files: strarr(5),			$
			 history: strarr(3),			$
                        comments: strarr(5)			$
	         }

   if keyword_set(define) then return

   XRT_filter.history[0] = 'MAKE_XRT_FILTER '+progver+timestamp()
   XRT_filter.flt_str_version = tag_names(XRT_filter,/structure)

; Populate structure

   n_filt = where(filter.name eq filter_s)
   wrk_filter = filter[n_filt]

   struct_assign, wrk_filter, XRT_filter, /nozero

   densities = 1
   n_files   = 1

;  Where are the calibration files? (Needed by multifilt)

   resolve_caldir, caldir=caldir, qabort
   if qabort then return

   multifilt, wave, trans, wrk_filter.material, wrk_filter.thick,    $
	      wrk_filter.mesh_trans, densities=densities, $
              n_files=n_files, caldir=caldir+'/refr/'

   XRT_filter.wave = wave
   XRT_filter.trans = trans
   XRT_filter.length = n_elements(wave)
   XRT_filter.dens = densities
   XRT_filter.data_files = n_files
   
end
