pro synth, chan, temp, wave, inten, over=over, noplot=noplot
;+
;  Procedure to (o)plot, or just read, a theoretical spectrum calculated 
;  for a given temperature.  If suitable arays are given as arguments, 
;  then the spectral data are returned in them.
;
;  Parameters:  chan    1-4  the channel to simulate. FeXXVI, FeXXV, Ca, S
;               temp    temperature of calculation in Mdegs
;               wave    wavelength of data points
;               inten   spectrum intensity
;               /over   if specified, theoretical spectrum is plotted over
;                       existing plot.
;               /noplot if specified, supresses plotting (only makes sense
;                       if return arrays are specified.
;
;  Use    IDL> synth,2,9         plot FeXXV spectrum for temperature of 9 Mdegs
;         IDL> synth,2,9,/over   oplot FeXXV spectrum for temperature of 9 Mdegs
;         IDL> synth [,/over]    parameters will be prompted for.
;         IDL> synth,2,9,wave_25,fe25_cnts,/noplot   just load arrays, no plot
;
;
;  In oplot mode the scale factor is prompted for.
;
;  Data files: ACS
;  Procedure:  CDP     Feb 92
;
;-
;
;
;  total idiot - no parameters at all
;
if n_params() eq 0 then begin
   print,'Give channel number required: 1 -> FeXXVI'
   print,'                              2 -> FeXXV'
   print,'                              3 -> CaXIX'
   print,'                              4 -> SXV'
   chan = 0
   while chan lt 1 or chan gt 4 do read,' --> ',chan

   case chan of 
     1: print,'Temperatures available for FeXXVI are  20 - 60 Mdegs'
     2: print,'Temperatures available for FeXXV  are   9 - 27 Mdegs'
     3: print,'Temperatures available for CaXIX  are   9 - 22 Mdegs'
     4: print,'Temperatures available for SXV    are  xx - xx Mdegs'
   endcase
   read,'Give temperature required (in Mdegs): ',temp
endif

;
;  half wit - only channel given
;
if n_params() eq 1 then begin
   case chan of 
     1: print,'Temperatures available for FeXXVI are  20 - 60 Mdegs'
     2: print,'Temperatures available for FeXXV  are   9 - 27 Mdegs'
     3: print,'Temperatures available for CaXIX  are   9 - 22 Mdegs'
     4: print,'Temperatures available for SXV    are  xx - xx Mdegs'
   endcase
   read,'Give temperature required (in Mdegs): ',temp
endif

;
;  form temperature part of data file name
;
if temp lt 10 then begin
   tname = '0' + string(temp,format='(i1)')
endif else begin
   tname = string(temp,format='(i2)')
endelse

;
; form main part of file name
;
case chan of
1: begin
   cname = 'fe26_'
   th_l_i = fltarr(2,410)
   end
2: begin
   cname = 'fe25_'
   th_l_i = fltarr(2,720)
   end
3: begin
   cname = 'ca19_'
   th_l_i = fltarr(2,428)
   end
4: begin
   cname = 's15_'
   print,'No theoretical spectra for SXV available yet. See Alphonse!'
   return
   end
endcase

;
;  full file name
;
filename = concat_dir('$DIR_BCS_SYNSPEC',cname + tname + '.dat')

;
;  try to access the data file
;
get_lun,lun1
on_ioerror, ioerr
openr,lun1,filename,ERROR=err
if err ne 0 then goto, openerr

;
;  skip and report file header line
;
text = ' '
readf,lun1,text
print,text

;
;  read data
;
readf,lun1,th_l_i

;
; tidy file
;
close,lun1
free_lun,lun1

;
;  scale to maximum of 1.0
;
th_l_i(1,*) = th_l_i(1,*) * 1.0/max(th_l_i(1,*))

;
;  want to plot at all?
;
if n_elements(noplot) eq 0 then begin
;
;  if overplot requested, allow user scaling
;
   if n_elements(over) gt 0 then begin
      read,'Give value to scale maximum of theoretical spectrum to: ',maxt
      th_l_i(1,*) = th_l_i(1,*) * maxt/max(th_l_i(1,*))
      oplot, th_l_i(0,*), th_l_i(1,*), psym=10
   endif else begin
      plot, th_l_i(0,*), th_l_i(1,*), psym=10,xtitle='Wavelength (A)',$
            ytitle='Relative Intensity',title=cname+tname
   endelse
endif

;
;  load arrays
;
if n_params() eq 4 then begin
   wave  = reform(th_l_i(0,*))
   inten = reform(th_l_i(1,*))
endif
return

;
;  io error 
;
ioerr:
print,'Error reading required spectral file - ',filename
close,lun1
free_lun,lun1
return

;
;  opening file error
;
openerr:
print,'Error opening required spectral file - ',filename
print,'does it exist? - Check the temperature requested.'
free_lun,lun1
return

end

