FUNCTION multiply_curves, input1, input2, xrange=xrange, qstop=qstop

progver = 'v2007-May-15' ;--- (M.Weber) Written. (But based heavily on
;                             previous work.

  qstop = keyword_set(qstop)


; === Set x-range for product =========================


  x_lows   = [min(input1[*,0]), min(input2[*,0])]
  x_highs  = [max(input1[*,0]), max(input2[*,0])]

  if not keyword_set(xrange) then begin

    x_lo_out = min(x_lows)
    x_lo_in  = max(x_lows)
    x_hi_in  = min(x_highs)
    x_hi_out = max(x_highs)

    ss1 = where((input1[*,0] ge x_lo_in) and (input1[*,0] le x_hi_in), $
                count1)
    ss2 = where((input2[*,0] ge x_lo_in) and (input2[*,0] le x_hi_in), $
                count2)

    if (count1 ge count2) then xrange = input1[ss1,0] $
                          else xrange = input2[ss2,0]
  endif else begin

    ss1 = where((input1[*,0] ge min(xrange)) and (input1[*,0] le $
                max(xrange)), count1)
    ss2 = where((input2[*,0] ge min(xrange)) and (input2[*,0] le $
                max(xrange)), count2)
    if ((count1 eq 0) or (count2 eq 0)) then begin
      print, 'ERROR: The inputs do not both overlap the ' $
             + 'given x-range. --- Aborting.'
      return, 0
    endif
  endelse

  if (input1[ss1[0],0] gt x_lows[0])  then begin
    ss1 = [ss1[0]-1,ss1]
    if (input1[ss1[count1],0] lt x_highs[0]) then ss1 = [ss1,ss1[count1]+1]
  endif else begin
    if (input1[ss1[count1-1],0] lt x_highs[0]) then ss1 = [ss1,ss1[count1-1]+1]
  endelse
  if (input2[ss2[0],0] gt x_lows[1])  then begin
    ss2 = [ss2[0]-1,ss2]
    if (input2[ss2[count2],0] lt x_highs[1]) then ss2 = [ss2,ss2[count2]+1]
  endif else begin
    if (input2[ss2[count2-1],0] lt x_highs[1]) then ss2 = [ss2,ss2[count2-1]+1]
  endelse


; === Interpolate both curves onto the x-range ========

  input1b = interpol(input1[ss1,1], input1[ss1,0], xrange)
  ss = where((xrange lt x_lows[0]) or (xrange gt x_highs[0]), count)
  if (count gt 0) then input1b[ss] = 0.

  input2b = interpol(input2[ss2,1], input2[ss2,0], xrange)
  ss = where((xrange lt x_lows[1]) or (xrange gt x_highs[1]), count)
  if (count gt 0) then input2b[ss] = 0.


; === Multiply curves =================================

if qstop then stop

  output = input1b * input2b


; === Finish ==========================================

  return, output


END


; =========================================================================


FUNCTION calc_xrt_chn_trans, chn, outfile=outfile, qfail=qfail


progver = 'v2007-May-15' ;--- (M.Weber) Written. (But based heavily on
;                             previous work.


; === Check input, retrieve channels =============

  if not keyword_set(qfail) then qfail = 0b

  if (n_params() eq 0) then begin
    chn = get_xrt_channel()
  endif else begin
    if (not required_tags(chn, 'type')) then qfail = 1B $
    else begin
      if (chn[0].type ne 'channel') then qfail = 1B
    endelse
  endelse

  if qfail then begin
    print, 'ERROR: Problem recognizing "channel" structure. Aborting.'
    return, 0
  endif

  if keyword_set(outfile) then begin
    if ((datatype(outfile) ne 'STR') or (strlen(outfile) le 2)) then begin
      outfile = 'xrt_channel_transmissions'
    endif
  endif


; === Generate net channel transmission ==========

  trans_temp = {type:'chn_trans', channel_name: '', wave:findgen(5000), $
                wave_units:'Angstroms', trans:fltarr(5000), length:1L,  $
                data_files:''                                            }

  for i_chn = 0,(n_elements(chn)-1) do begin

    ;; Multiply entrance filter with primary mirror.
    len1 = chn[i_chn].en_filter.length
    len2 = chn[i_chn].mirror1.length
    input1 = [[chn[i_chn].en_filter.wave[0:len1-1]], $
              [chn[i_chn].en_filter.trans[0:len1-1]] ]
    input2 = [[chn[i_chn].mirror1.wave[0:len2-1]], $
              [chn[i_chn].mirror1.refl[0:len2-1]]  ]
    xrange = 0
    output = multiply_curves(input1, input2, xrange=xrange)

    ;; Multiply with secondary mirror.
    input1 = [[xrange], [output]]
    xrange = 0
    len2 = chn[i_chn].mirror2.length
    input2 = [[chn[i_chn].mirror2.wave[0:len2-1]], $
              [chn[i_chn].mirror2.refl[0:len2-1]]  ]
    output = multiply_curves(input1, input2, xrange=xrange)

    ;; Multiply with FP filter1.
    input1 = [[xrange], [output]]
    xrange = 0
    len2 = chn[i_chn].fp_filter1.length
    input2 = [[chn[i_chn].fp_filter1.wave[0:len2-1]], $
              [chn[i_chn].fp_filter1.trans[0:len2-1]] ]
    output = multiply_curves(input1, input2, xrange=xrange)

    ;; Multiply with FP filter2.
    input1 = [[xrange], [output]]
    xrange = 0
    len2 = chn[i_chn].fp_filter2.length
    input2 = [[chn[i_chn].fp_filter2.wave[0:len2-1]], $
              [chn[i_chn].fp_filter2.trans[0:len2-1]] ]
    output = multiply_curves(input1, input2, xrange=xrange)

    ;; Multiply with CCD QE.
    input1 = [[xrange], [output]]
    xrange = 0
    len2 = chn[i_chn].ccd.length
    input2 = [[chn[i_chn].ccd.wave[0:len2-1]], $
              [chn[i_chn].ccd.qe[0:len2-1]]    ]
    output = multiply_curves(input1, input2, xrange=xrange)

    trans_temp.channel_name = chn[i_chn].name
    trans_temp.length = n_elements(xrange)
    trans_temp.wave[0:trans_temp.length-1]  = xrange
    trans_temp.trans[0:trans_temp.length-1] = output
    if (i_chn eq 0) then trans = trans_temp else trans = [trans, trans_temp]

  endfor ;; i_chn


; === Finish =====================================

  if keyword_set(outfile) then begin
    trans.data_files = outfile + '.geny'
    savegenx, trans, file=outfile
  endif

  return, trans


END
