;+
; PROJECT:
;	HESSI
; NAME: SPECIAL_MOD_AMP_CODE
;
;
; PURPOSE: This procedure contains the code for SPECIAL_MOD_AMP.PRO
;	It is used to compute the transmission vs energy curves for the HESSI grids.
;
;
; CATEGORY: HESSI, SIMS
;
;
; CALLING SEQUENCE:
;
;
; CALLS:
;	
;
; INPUTS:
;       none explicit, this main program code.
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	none
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;	Version 1, extracted from SPECIAL_MOD_AMP.PRO, 21-jan-1997, richard.schwartz@gsfc.nasa.gov.
;	Version 2, richard schwartz, 11-aug-1998
;
;-
file = loc_file(path=hessi_data_paths(), 'special_mod_amp.sav',count=special)

if special eq 0 then begin

;This figure uses a special grid file with 11 thicknesses defined.
;The last two pitches and thicknesses are the thin grids for detectors 1 and 2.
gdfile = get_gdfile( 11 )

gd, XX, grid_sep, pitch, e_diff_10, e_diff_50, e_diff_fl, e_peak,$
    thickness, gridnumber, density,$
    e_diff_manual, man_rmod_amp, res, subs, ngrids, fres, cres, subarr,$
    gdfile=gdfile




energies = fltarr(11,500)+2e4
amps = fltarr(11,500)


; For Each iteration of collimator, call plot_gd_calc to return
; relative modulation amplitude for plotting
; resulting arrays are in keV

for collim=1,ngrids do begin
plot_gd_calc,XX,man_rmod_amp,e_peak,collim,1,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 

energies(collim-1,0) = transpose(en1)
amps(collim-1,0) = transpose(tot_y)
endfor


save,amps, energies, subarr, file='special_mod_amp.sav'
endif else restore, file

e_xmargin = getenv('XMARGIN')
e_charsz  = getenv('CHARSZ')
xmargin = [10,10]
charsz  = 1.2
if keyword_set(e_xmargin) then reads, e_xmargin, xmargin
if keyword_set(e_charsz) then reads, e_charsz, charsz

gdfile = get_gdfile( 11 )
label = ''
read_gdfile_gd, gdfile, label=label
linecolors
blue = 11
red  = 2
white= 255
black= 0
ytitle = 'Relative Modulation Amplitude'
xtitle = 'Energy (keV)'
thick=([2,10])(!d.name eq 'PS')
font = (!d.name eq 'PS') -1
pl_range = [1,n_elements(energies(*,0))]	;Collimator numbers to plot

plot,/xlog,xran=[1.,1e4],energies(0,*), amps(0,*),  xtitle=xtitle, ytitle=ytitle, background=white,xmargin=xmargin,$
	thick=10,xticklen=1.0,yticklen=1.0,color= black, chars=charsz,ymarg = [8,4],font = font,/nodata
for i=0,8 do oplot, energies(i,*), amps(i,*), col=blue,thick=thick
for i=9,10 do oplot, energies(i,*), amps(i,*), col=blue, linestyle=2,thick=thick
;
;  The polyfill command is used to create a clear field on which to
;  write the text labels.
;
  polyfill,/data,[1.6,1.6,23,23,1.6],[.52,.57,.57,.52,.52],color=white
  xyouts,/data,1.8,.53,'Diffraction Limited',font=font,chars=charsz*ch_scale(1.,/xyouts),color= black
  polyfill,/data,[198,198,3600,3600,198],[.52,.57,.57,.52,.52],color=white
  xyouts,/data,200.,.53,'Transmission Limited',font=font,chars=charsz*ch_scale(1.,/xyouts),color= black
  wgold = where( strupcase(strcompress(/remove,subarr)) eq 'GOLD', ngold)
  polyfill,/data,[198,198,900,900,198],[.16,.22,.22,.16,.16],color=white

  wr = indgen(pl_range(1)-pl_range(0)+1)+pl_range(0)-1
  
  for icol=0,1 do begin
  isub = wc_where(subarr(wr),(['*gold*','*tungs*'])(icol),/case_ignore,csub)
  if csub ge 1 then $
    xyouts,/data,label.xc(wr(isub)), label.yc(wr(isub)),$
    label.text(wr(isub)),font=font,$
  	color=([red,blue])(icol),chars=charsz*ch_scale(1.,/xyouts)
  endfor







