;+
; Project     : HESI
;                   
; Name        : PLOT_GD_CALC
;               
; Purpose     : Calculate energies to use for the Diffraction limited portion of 
;	      Relative modulation amplitude (from about 1 to 100 keV)
;	      Use either manual reduction in modulation amplitude or the standard
;	      10,50, and 100% reductions & generate energies based on division of 10
;	      in between the peak and trough
;
;               
; Category    : HESI
;               
; Explanation : The HESI grid collimator physical dimensions are used with
;	      together with the Xray cross-sections to calculate the
;	      transmission functions describing the performance of the HESI
;	      collimator system.
;               
; Use         : plot_gd_calc,XX,man_rmod_amp,e_peak,collim,harm,e_diff_manual,e_diff_fl,$
; 	e_diff_10,e_diff_50,grid_sep,pitch,density,thickness,ener_pl,en_xax,st_en,rel_mod_50,$
;	mod_amp,en1,tot_y,new_mod,new_rel, rmod_amp=rmod_amp
;    
; Inputs      : XX - cross_section arrays vs energy
;	      Collim - collimator number, starts at 1
;	      Man_rmod_amp - Input relative modulation amplitude, as a percent
;	      E_peak - Energy (keV) of peak ?
;	      Harm - Harmonic number for modulation calculation
;	      E_diff_manual- energies corresponding to 1st 3 harmonic numbers and Man_rmod_amp.
;	      E_diff_fl - energies corresponding to 1st 3 harmonic numbers and 0 modulation.
;	      E_diff_10 - energies corresponding to 1st 3 harmonic numbers and 90% modulation.
;	      E_diff_50 - energies corresponding to 1st 3 harmonic numbers and 50% modulation.
;	      Grid_sep- grid separation distance in mm
;	      Pitch - Array of grid pitches in mm
;	      Density - Array of Density of grid materials in gm/cm3
;	      Thickness - Array of grid thicknesses in mm
; Opt. Inputs : None
;               
; Outputs     : None
;	      Ener_pl - Plot energies in keV
;	      En_xax  - Exact energies for calculation
;	      St_en   - Indices into sorted energies
;	      Rel_mod_50 -Transmission limited modulation amplitude
;	      Mod_amp - Diffraction limited modulation amplitude
;	      En1     - New sorted energies
;	      Tot_y   - total modulation amplitude defined on En1
;	      New_mod - Transmission limited mod amplituded defined on En1
;	      New_rel - Diffraction limited mod amplitude defined on En1
;	      
;
; Opt. Outputs: None
;               
; Keywords    : RMOD_AMP- User input value of relative mod amplitude for which
;		energies need to be found.
;
; Calls       :
;
; Common      : None
;               
; Restrictions: 
;               
; Side effects: None.
;               
; Prev. Hist  : Written by Eric Carzon, 1993
;	
; Modified    : Version 2, RAS, 16-apr-1997 
;		Version 3, richard.schwartz@gsfc.nasa.gov, relax diffraction solution to 99% from 100%.
;		5-jan-1998.
;-            
Pro plot_gd_calc,XX,man_rmod_amp,e_peak,collim,harm,e_diff_manual,e_diff_fl,$
 e_diff_10,e_diff_50,grid_sep,pitch,density,thickness,ener_pl,en_xax,st_en,rel_mod_50,$
 mod_amp,en1,tot_y,new_mod,new_rel, rmod_amp=rmod_amp 


checkvar, enrg_limits, [0.1, 2e4] ;limits on energy in keV

; Look for the manual keyword, if the user has specified a reduction in
; modulation amplitude, then use that value in energy calucations
; Otherwise calculate red. in mod. amplitude effects at 10,50, &100% red.
;
If keyword_set(rmod_amp) then begin
	man_rmod_amp = 1
        goto, manual_rmodamp
endif
;
; initialize arrays for 10,50, 100%, 0% reduction in mod. amplitude from 
; the peak value of 1 for 12 collimators at 3 harmonics.
;
gdiff = grid_diffraction([0.90,0.50,0.0,0.99],3,pitch,grid_sep, $
	elim=0.1)
E_diff_10= gdiff(*,*,0)
E_diff_50= gdiff(*,*,1)
E_diff_fl= gdiff(*,*,2)
e_peak   = gdiff(*,*,3)


goto, skip_manual   	; passes by the manual reduction (user defined)
			;in modulation amplitude, rmodamp:reduction in mod. amp.
;
; User input value for  reduction in modulation amplitude from diffraction.
;  
manual_rmodamp:

c1 = 1.-(rmod_amp/100.)	; calculate diffraction limit (1-reduction)
gdiff = grid_diffraction([c1, 0.99],3,pitch,grid_sep,elim=0.1)

e_diff_manual = gdiff(*,*,0)
e_peak = gdiff(*,*,1)

skip_manual:

;
;   The collim(ators) are numbered 1 thru Ngrids, generate the array index, igrid.
;
igrid = collim -1 

if (man_rmod_amp eq 1) then begin
  ener_pl = [1.257,e_peak(igrid,harm-1),e_diff_manual(igrid,harm-1),e_diff_fl(igrid,harm-1), e_diff_fl(igrid,harm-1)/3.,$
	e_diff_fl(igrid,harm-1)/5.]
  en_xax = [ener_pl,(findgen(10)*.01)+1,(findgen(891)*.01)+1.09,$
          (findgen(98)*.1)+10,(findgen(90))+10,(findgen(10))+100,$
          (findgen(90)*10)+100,(findgen(10)*10)+1000,$
          (findgen(90)*100)+1000] 
endif else begin
  ener_pl = [1.257,e_peak(igrid,harm-1),e_diff_fl(igrid,harm-1),$
             e_diff_50(igrid,harm-1),e_diff_10(igrid,harm-1), e_diff_fl(igrid,harm-1)/3.,$
	e_diff_fl(igrid,harm-1)/5.]
;
; Odd harmonics are solutions to the diffraction equations.  Need them to have sufficient
; energy density for plot to fall to zero.
;
  En_xax = [ener_pl,(findgen(10)*.01)+1,(findgen(90)*.1)+1,(findgen(10)*.1)+10,$
          (findgen(90))+10,(findgen(10))+100,(findgen(90)*10)+100,(findgen(10)*10)+1000,$
          (findgen(90)*100)+1000]
endelse

en_xax = en_xax > 1.0
;Sort the elements of en_xax
n_elem = n_elements(en_xax)
mod_amp = fltarr(n_elem)
st_en = sort(en_xax)
;
; Valid energies range from 0.1 keV to 20.0 MeV
;
wnz = where( xx(*,0,igrid) ge enrg_limits(0)/1e3 and xx(*,0,igrid) lt enrg_limits(1)/1e3 ) 

; create an the Diffraction limited portion of the relative 
; modulation amplitude versus Energy, covering a range from 
; 1-100 keV
; Mod_amp = ABSOLUTE | Cos(12.3e-7*pi*collimator length*harmonic^2) /
;                       4*((.5*pitch)^2)*Energy |
; This equation can be more generally put forth as
;  Modulation Amplitude = pi*harmonic^2*wavelength(lambda)/
;                            4*aperture width^2 (.5*pitch = w)
;******** 10/13/95, for grids 1,2,3 make diffraction limitation 0 by using duplicating
; relative modulation amplitude at e_peak
;
;	14-apr-1997, ras, don't understand this need explanation
;	commented out next line
;if (collim ge 2 and collim le 4) then en_xax(st_en) = ener_pl(1)

Mod_amp =ABS( COS( (12.39854e-7*!pi*grid_sep*harm^2)/$
         (4*((.5*pitch(igrid))^2)*En_xax(st_en)) ) )
; omod_amp deals with only the rel. mod. amplitude at peak,10,50, and 100% reductions
omod_amp =ABS( COS( (12.39854e-7*!pi*grid_sep*harm^2)/(4*((.5*pitch(igrid))^2)*Ener_pl) ) )

; Relative Mod. Amp. versus Energy for 50% transmission
; Calculate the Transmission limited portion of the relative modulation
; amplitude versus energy, covering the range 100 keV to 10000 keV
; rel_mod_50 = [exponential( (-2*thickness*Attenuation Coeff. w/ Coherent Scat.*
;                             density)/10 ) + 1 ] -
;                             [ 2*exponential( (-1*thickness*Att. Coeff. * 
;                                  density)/10)]

rel_mod_50 = exp( (-2*thickness(igrid)*xx(wnz,6,igrid)*density(igrid))/10 ) +1 -$
             (2*exp( (-1*thickness(igrid)*xx(wnz,6,igrid)*density(igrid))/10 ))

en = ([xx(wnz,0,igrid)*1000.,en_xax] > enrg_limits(0) ) < enrg_limits(1)
en1 = en(uniq(en,sort(en)))	; create a combined x array

lims = limits(en_xax(st_en))
if lims(0) eq lims(1) then new_mod = mod_amp(st_en(0)) else $
new_mod = interpol(mod_amp,en_xax(st_en),en1) 
new_rel = interpol(rel_mod_50,xx(wnz,0,igrid)*1000.,en1)
tot_y = new_mod * new_rel

end
