;+
; Project     : HESI
;                   
; Name        : PLOT_GD
;               
; Purpose     : This procedure plots the grid transmissions as a function of energy.
;               
; Category    : HESI
;               
; Explanation : 
;               
; Use         : 
;	plot_gd,XX,grid_sep,pitch,e_diff_10,e_diff_50,e_diff_fl,e_peak,thickness,gn,density,$
;	  rmod_amp,e_diff_manual,man_rmod_amp,res,subarr,ngrids,$
;	   harmonic,ymin,ymax,xmin,xmax,pl_range,ok_opl
;    
; Inputs      : XX - cross_section arrays vs energy
;	      Gn - collimator number, starts at 1
;	      Man_rmod_amp - If set then input relative modulation amplitude, 
;		using Rmod_amp as a percentage
;	      Rmod_amp - Find the energies corresponding this percentage
;		of the diffraction amplitude curve and 1st-3rd harmonics.
;	      E_peak - Energy (keV) of peak of diffraction amp. curve
;	      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
;	      Res - angular resolution of each grid pair, unused.
;	      Pl_range - plot over this range of collimator numbers,
;		where the numbering starts at 1.
;	      Xmin, Ymin, Xmax, Ymax - Plotting ranges in data coordinates
;		energy in keV for X, and dimensionless amplitude for Y
;	      Ok_opl- If set overplot symbols and E_peak.
; Opt. Inputs : None
;               
; Outputs     : None
;	      
;
; Opt. Outputs: None
;               
; Keywords    : GDFILE- Data file used to characterize grids.
;
; Calls	      :
;
; Common      : None
;               
; Restrictions: 
;               
; Side effects: None.
;               
; Prev. Hist  : Written by Eric Carzon, Hughes/STX, May 1993
;
; Modified    : 
;		Modified by Brian Dennis, November 8, 1993, to include labels in the plot
;		 area.
;		Comments added, and calculations put in subprogram plot_gd_calc
; 			EC, Jan. 1995
;		Rearrange to be part of HESI_menu.pro, not a standalone program anymore
; 		EC, April 1995
; 		5/25/95, changes fov for det. 10-12, changes labels to reflect new placement
; 		10/7/95, multiply grid 1`, 1-3 by .25,.25,.5,.75 respectively for Step
; 		II proposal, change color scheme and labels to reflact dropping of LIGA
; 		process, change Stacking to Van Beek Stacking
;		Version 6, RAS, 14-apr-1997, remove grid factors!!! of 10/7/95
;		Version 7, RAS, 28-apr-1997, added gdfile to pass on to gd_title.
; Version 7, richard.schwartz@gsfc.nasa.gov, 31-dec-1997, suppress gold mask output if not
;	needed, suppress xsubtitle and title if NOSUBTITLE environmental is set to 'T'.
;	
;-            


Pro plot_gd,XX,grid_sep,pitch,e_diff_10,e_diff_50,e_diff_fl,e_peak,thickness,gn,density,$
   rmod_amp,e_diff_manual,man_rmod_amp,res,subarr,ngrids,$
   harmonic,ymin,ymax,xmin,xmax,pl_range,ok_opl, gdfile=gdfile

; Plots Relative Modulation Amplitude versus Energy for HEISPEC bi-grid 
; collimators.  Called from GD.PRO.
; 
Set_defs:
hd_cp = 'n'                                ; hard copy code
checkvar,ok_opl, 0		;OK to overplot symbols at 100%, 90%, and 50%
pl_range = [1,ngrids]	;Collimator numbers to plot
pl_all = 1		;Plot all the collimators (sets pl_range to 1,12)
nxsize = 22              ; x scale size for printer
nysize = 16              ; y scale size
do_color = 1	      ; 0 is black/white and 1 is color 
def_scr_device = 'X'     ; default screen device
collim = pl_range(0)
blue = 11
red  = 2
white= 255
black= 0
;
; Initialize labels for each grid mod-amplitude curve
;
gd_title,pl_range, pitch, thickness, grid_sep, harmonic, xtitle,$
	ytitle, maint, xsubt, density, subarr,ngrids, label = label, gdfile=gdfile

if getenv('NOSUBTITLE') eq 'T' then begin
	xsubt=''
	maint = ''
endif
 
erase
set_x

linecolors
; Relative Modulation Amplitute versus Energy
fork:

Make_plot:
pl_iter = pl_range(0)

All_plot:
;if (strmid(pl_all_enable,0,1) eq 'E') then collim = pl_iter
collim = pl_iter

; For Each iteration of collimator, call plot_gd_calc to return
; relative modulation amplitude for plotting
; resulting arrays are in keV
plot_gd_calc,XX,man_rmod_amp,e_peak,collim,harmonic,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 

;


Titles:

;if (strmid(pl_all_enable,0,1) eq 'E') then begin
if (pl_iter gt pl_range(0)) then goto,for_pl_all

plot_it:


;   plot_oi,/nodata,[xmin,xmax],[ymin,ymax]
;   polyfill,/normal,[0,1,1,0,0],[0,0,1,1,0],color=white
   thick=([2,10])(!d.name eq 'PS')
   font = (!d.name eq 'PS') -1
;
;  Build a plot command here to create the plot box.  Need it as
;  an executable string to reproduce it later for overplotting exactly!
;
  erase
  plotcom= "plot_oi,[xmin,xmax],[ymin,ymax],/nodata,backg=white,thick=10,"+$
    "xtitle=xtitle,ytitle=ytitle,title=maint,subtitle=xsubt,"+$
     "xticklen=1.0,yticklen=1.0,color= black,xchars=1.5, ychars=1.5,"+$
     "ymarg = [8,4],font = font"
  ex_rslt= execute(plotcom)

; plot transmission limited relative modulation amplitude
  if (ok_opl eq 1) then oplot,ener_pl,omod_amp,psym=5,color= black

for_pl_all:
       if(pl_iter ge pl_range(0)) then begin
; ADD Yfact and Change Colors for Step II proposal grid changes 10/7/95
        col = ([blue,red])( strupcase(subarr(pl_iter-1)) eq 'GOLD')
        yfact = 1. + fltarr(ngrids)	;[1.,.25,.5,.75,1.,1.,1.,1.,1.,1.,1.,1.,1.]
        if (ok_opl eq 1) then oplot,ener_pl,omod_amp,psym=5,color= black
        oplot,en1,tot_y*yfact(pl_iter-1),color=col,thick=thick

       endif
skip_first:
	pl_iter = pl_iter+1
	if(pl_iter gt pl_range(1)) then goto,end_loop
        goto,all_plot

end_loop:

if (ngrids gt 7) then begin

  ex_rslt= execute(plotcom+',/noerase')
  
  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=1.5
  endfor
;
;  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=1.5,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=1.5,color= black
  wgold = where( strupcase(strcompress(/remove,subarr)) eq 'GOLD', ngold)

  if ngold ge 1 then begin
 	polyfill,/data,[5.8,5.8,21,21,5.8],[.11,.19,.19,.11,.11],color=white
	oplot,[6,8],[.15,.15],thick=8,color= red
  	xyouts,/data,9,.15,'Gold Masks',color= red,font=font,chars=1.5
	endif
;;;Don't show stacking label
  if getenv('STACKING') eq 'T' then begin
  	polyfill,/data,[5.8,5.8,25,25,5.8],[.05,.1,.1,.05,.05],color=white
  	oplot,[6,8],[.07,.07],thick=8,color= blue
  	xyouts,/data,9,.06,'Stacking',color= blue,font=font,chars=1.5
	endif
                 
endif else begin        ; Min. Science mission labels
  xyouts,400.,.3,/data,font=0,'Grids!c3-7',color= black
    lxc=[2.4,4.5,7.0] & lyc= [.93,.75,.61]
    lab=['#3','#2','#1']
    xyouts,/data,lxc,lyc,lab,font=0,color= black
  xyouts,/data,50.,.55,font=0,'Gold!cmask',color= black
endelse 

hard_copy:

if (!d.name eq 'PS') then begin
    device,/close
    if hd_cp  then if not (eps > 0) then psplot,color=do_color, file= file,$
    	qual=(['','-h'])(os_family() eq 'unix') 
    hd_cp = 0
    goto,get_out
endif else begin
ask_print:
    hard_cp=' '
    rd_land=' '
    eps = ''
    read,'Create (E)PS file (e/p/n)?', eps
    eps = strmid(strupcase(strtrim(eps,2)),0,1)
    
    case 1 of
    	eps eq 'E': eps= 1
    	eps eq 'P': eps=-1
    	else: eps=0
    	endcase
    
    if eps then begin
    ext = (['ps','eps'])(eps > 0)
    print,'Printing Options: b = bw/grayscale; c = color; n = no print'
    print,'The Print Queues may be controlled by setting the '
    print,'Logical/environmental variables PSLASER and PSCOLOR for
    print,'B/W and Color printing, respectively.'
    rd_land = 'Y'
    read,'landscape (y/n)? ',rd_land
        do1_land = strupcase(strmid(strcompress(string(rd_land),/remove),0,1))
        do_land  = (do1_land eq 'Y') 
    hard_cp = 'N'
    read,'hard copy (y/n)?  ',hard_cp
    hd_cp = (strupcase(strmid(strcompress(string(hard_cp),/remove_all),0,1)) eq 'Y')
    do_color = 'N'
    read,'Color Postscript (y/n)?  ',do_color
    do_color = (strupcase(strmid(strcompress(string(do_color),/remove_all),0,1)) eq 'Y')
    ;print, 'Setting PS and replotting to idl.ps'
    sps, encaps = eps>0, color=do_color
    set_plot,'ps',/copy
    file='mod_amp'+  (['.ps','.eps'])(eps > 0) 
    device,file=file, bits=8, landscape=do_land, xoff=3,yoff=25,$
        	xsize=nxsize,ysize=nysize, /bold,/times
        	
	  ; help, /device
	  goto,make_plot
   endif 
   goto, get_out   
   	
endelse
error_exit:
print,'Error in Plot_gd!'

get_out:
set_x
write_gif,'mod_amp.gif',tvrd()
return
end
