;+
; PROJECT:
;   HESSI
;
; NAME:
;   Brm2sm_ThinTarget
;
; PURPOSE:
;   This Function computes the thin-target bremsstrahlung x-ray/gamma-ray
;   spectrum from an isotropic electron distribution function provided in
;   Pro Brm2sm_Distrn.  The units of the computed flux is photons per second per
;   keV per square centimeter.
;   
;   The electron flux density distribution function is a smoothly broken power law in electron energy
;   with a low-energy cutoff and a high-energy cutoff.  
;   
;   Note: If you want to plot the derivative of the flux or the spectral index of the photon 
;               spectrum as a function of energy, you should set RERR to 1.d-6, 
;               because it is more sensitive to the RERR than the flux. 
;               Then: 
;       Flux = Brm2sm_ThinTarget(energy, A)  or  Flux=F_Thin2sm(energy, A)
;       index = -Deriv(Alog10(energy),Alog10(Flux))
;
; CATEGORY:
;   HESSI, Spectra, Modeling
;
; CALLING SEQUENCE:
;   RESULT = Brm2sm_ThinTarget(eph, a)
;
; CALLS:
;   Brm2sm_Dmlin
;
; INPUTS:
;   eph - Array of photon energies (in keV) at which to compute the
;         photon flux.
;   a - parameters describing the nonthermal electron broken power-law, where:
;   a(0) - normalization factor in units of 1.0d55 cm-2 sec-1 keV-1,
;             i.e. plasma density * volume of source * nonthermal electron flux density at Eno=100 keV
;	      such that X(Eno) = a(0) *1d55 cm-2 sec-1 keV-1, with X(E) = nVF(E).
;   a(1) - Power-law index of the electron distribution function below eebrk.
;   a(2) - Break energy in the electron distribution function (in keV)
;   a(3) - Power-law index of the electron distribution function above eebrk.
;   a(4) - Low energy cutoff in the electron distribution function (in keV).
;   a(5) - High energy cutoff in the electron distribution function (in keV).
;   a(6) - width of transition between power-laws (in keV).
;
;
; OPTIONAL INPUTS:
;
; OUTPUTS:
;
;   Multiplying the output of Brm2sm_ThinTarget by a[0] * 1.0d+55 gives an
;   array of photon fluxes in photons s^-1 keV^-1 cm^-2
;   corresponding to the photon energies in the input array eph.
;   The detector is assumed to be 1 AU from the source.
;
; OPTIONAL OUTPUTS:
;   none
;
; KEYWORDS:
;
;
; COMMON BLOCKS:
;   none
;
; SIDE EFFECTS:
;
;
; RESTRICTIONS:
;
;
; PROCEDURE:
;
;
; MODIFICATION HISTORY:
;   27-Jul-2010, 19-Feb-2014, Qingrong Chen
;       Documentation follows brm2_thintarget.pro
;-

Function Brm2sm_ThinTarget, eph, a

    ;set parameters
    p = double(a[1])
    eebrk = double(a[2])
    q = double(a[3])
    eelow = double(a[4])
    eehigh = double(a[5])
    widtrans = double(a[6])  ;; QRCHEN
    
    IF eebrk lt eelow then eebrk=eelow
    IF eebrk gt eehigh then eebrk=eehigh
    
    ;use electron flux distribution (the electron density distribution is used if EFD = 0)
    EFD = 1
    
    mc2 = 510.98d+00
    clight = 2.9979d+10
    au = 1.496d+13
    r0 = 2.8179d-13

    ; Maximum number of points for the Gaussian quadrature integration, maxfcn.
    
    maxfcn = 2048

    ; Average atomic number (z).
    
    z = 1.2d0
    
    ; Specify the relative error.
    
    rerr = 1.d-4

    ; Compute the numerical coefficient for the photon flux.
    
    fcoeff = (clight/(4.d0*!dpi*(au^2)))/mc2^2   ; * mc2 (was /mc2^3) because now the distribution function is a function of E, not gamma
        
    ; Create arrays for the photon flux and error flags.
    
    flux = DblArr(N_Elements(eph))
    iergq = IntArr(N_Elements(eph))
        
    l = Where( (eph LT eehigh) and (eph gt 0) )
    
    If l[0] ne -1 then begin
        flux[l] = Brm2sm_Dmlin( $
                       double(eph[l]), $
                       make_array(n_elements(l), /double,value=eehigh), $
                       maxfcn, $
                       rerr, $
                       eph[l],$
                       eelow, $
                       eebrk, $
                       eehigh, $
                       widtrans, $ ;;QRCHEN
                       p, $
                       q, $
                       z, $
                       EFD, $
                       ier)

        iergq[l] = ier
        
        flux = fcoeff * flux

    endif else begin
        Message,'!!! ERROR --- The photon energies are higher than the highest electron energy or not greater than zero.'
    endelse

    RETURN, flux

END
