;+
; Project     : SOHO - CDS     
;                   
; Name        : GT_CHIANTI()
;               
; Purpose     : Reads a CHIANTI synthetic spectrum.
;               
; Explanation : GT_CHIANTI reads a file saved in CHIANTI_SS so that the 
;               spectrum may be analysed in an IDL session.
;               
; Use         : IDL> sp = gt_chianti(filename)
;    
; Inputs      : filename - name of file created in CHIANTI_SS when the
;                          calculation was SAVEd. 
;               
; Opt. Inputs : None
;               
; Outputs     : Function returns a structure containing all the spectral
;               information.  The structure has the tags:
;
;               
; Opt. Outputs: None
;               
; Keywords    : BROAD - set broadening width (in Angstroms).  If used as just 
;                       a flag keyword (ie /broad) then the default 
;                       broadening will be applied.
;
;               PRIVATE - specifies that file is in current directory rather
;                         then $CDS_ATOMIC
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Spectral
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 27-Sep-96
;               
; Modified    : 
;
; Version     : Version 1, 4-Oct-96
;-            
function gt_chianti, file, broad=broad, private=private

;
;  check input
;
if n_params() ne 1 then begin
   if keyword_set(private) then begin
      file = cdspickfile(/read,filter='*.CH')
   endif else begin
      file = cdspickfile(/read,filter='*.CH',path=getenv('CDS_ATOMIC'))
   endelse   
endif

;
;  check anything selected
;
if file eq '' then return,0

;
;  check if broad specified
;
if not keyword_set(broad) then broad = -1.0

;
;   Retrieve saved info
;
ch = strpos(file,'.CH')
chl = strlen(file)
if (chl-ch) ne 3 then ffile = file+'.CH' else ffile = file
if not file_exist(ffile) then begin
   bell
   print,'File: '+ffile+' does not exist.'
   return,0
endif

;
;  read file
;
text = rd_ascii(ffile)
text = strtrim(text,2)
setup_struct = {isg:'', ichannel:-1, $
                abund_name:'', dem_name:'', $
                ioneq_name:'', pressure:0.0,$
                wvlmin:0, wvlmax:0}

dem_file = strtrim(str_pick(text(3),'DEM:'),2) 
abund_file = strtrim(str_pick(text(2),'Abundances:'),2) 
ioneq_file = strtrim(str_pick(text(4),'Ion Eq:'),2) 

setup_struct.isg  = strtrim(str_pick(text(0),'Detector:'),2)
setup_struct.ichannel = fix(strtrim(str_pick(text(1),'Channel:'),2)) 
setup_struct.abund_name = abund_file
setup_struct.dem_name = dem_file
setup_struct.ioneq_name = ioneq_file
setup_struct.pressure = float(strtrim(str_pick(text(5),'Pressure:'),2))
setup_struct.wvlmin = fix(strtrim(str_pick(text(6),'min:'),2))
setup_struct.wvlmax = fix(strtrim(str_pick(text(7),'max:'),2))

nt = n_elements(text) - 8
nl = long((setup_struct.wvlmax-setup_struct.wvlmin)*100.)
return_struct = {lambda:fltarr(nl),$
        spectrum:fltarr(nl),$
        list_wvl:fltarr(nt),$
        list_ident:strarr(nt),$
        nlines:nt}

return_struct.lambda = findgen(nl)*0.01 + setup_struct.wvlmin

return_struct.list_wvl = float(strmid(text(8:*),0,7))
return_struct.list_ident = strmid(text(8:*),9,10) + '  '+$
                          strpad(strtrim(strmid(text(8:*),36,100),2),50,/after) + '  '+$
                          'Int= '+strmid(text(8:*),21,9)+'  '+$
                          'Tmax= '+strmid(text(8:*),31,3)
return_struct.nlines = nt
for kk= 0,nt-1 do begin
  w = return_struct.list_wvl(kk)
  int = float(strmid(text(kk+8),21,9))
  nn = round( (w - setup_struct.wvlmin)*100)
  if nn lt nl then begin
     return_struct.spectrum(nn) = return_struct.spectrum(nn) + int
  endif
endfor 
return_struct.spectrum = return_struct.spectrum/max(return_struct.spectrum)

out_struct = join_struct(setup_struct,return_struct)

;
;  was broadening required   
                                                       
;
if broad ne -1.0 then begin

;
; default broadening requested
;
   if broad eq 1.0 then begin

;  GIS
;
      if setup_struct.isg eq 'GIS' then begin
         case setup_struct.ichannel of
           1: b = gauss_put(161,0,1.0/(2.51*24.),80.0,24.0)
           2: b = gauss_put(161,0,1.0/(2.51*18.),80.0,18.0)
           3: b = gauss_put(161,0,1.0/(2.51*24.),80.0,24.0)
           4: b = gauss_put(161,0,1.0/(2.51*28.),80.0,28.0)
           else:
         endcase
      endif
;
;  NIS
;
      if setup_struct.isg eq 'NIS' then begin
         if setup_struct.ichannel eq 1 then begin
            b = gauss_put(161,0,1.0/(2.51*12.),80.0,12.0)
         endif else begin
            b = gauss_put(161,0,1.0/(2.51*25.),80.0,25.0)
         endelse
      endif
      
;
;  SUMER
;
      if setup_struct.isg eq 'SUMER' then begin
         b= gauss_put(161,0,1.0/(2.51*9.),80.0,9.0)
      endif

   endif else begin

;
;  user specified broadening
;

      b= gauss_put(161,0,1.0/(2.51*broad*100.),80.0,broad*100.)

   endelse   
   

;
;  broaden spectrum, allowing for edge effects

;   
   
   nel = n_elements(out_struct.spectrum)
   temp = [fltarr(200),out_struct.spectrum,fltarr(200)]
   temp = convol(temp,b)
   out_struct.spectrum = temp(201:201+nel-1)
endif
   
;
;  return 
;
return,out_struct
   
end
   
   
