;+
; Project     : SOHO - CDS     
;                   
; Name        : RD_LINE_LIST
;               
; Purpose     : Read a CDS line list into the line editor.
;               
; Explanation : Reads an ASCII  line list file.
;               
; Use         : IDL> rd_line_list, in_file, title, desc, array
;    
; Inputs      : in-file    -   the name of the file with the line list.
;               array      -   array of line details, is blank on entry
;                              but used for its dimensions
;
; Opt. Inputs : None
;               
; Outputs     : title - the line list title
;               desc  - the line list description
;               array - an array of parameters ready for putting into the 
;                       line table editor
;               
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : None
;               
; Restrictions: Really only useful within TPLAN
;               
; Side effects: None
;               
; Category    : Technical planning.
;               
; Prev. Hist. :
;
; Written     : C D Pike, 14-May-1993
;               
; Modified    : Rewrite of the original.  CDP, 23-Sep-94
;               Use input array to set max number of lines allowed.  
;                                                              CDP, 9-Nov-95
;
; Version     : Version 3, 9-Nov-95
;-            

pro rd_line_list, in_file, title, desc, array

;
;  get instrument from extension of file name
;
break_file, in_file, disk, dir, file, ext
ext = strlowcase(ext)
if strpos(ext,'nis') ge 0 then det = 'NIS' else det = 'GIS'


;
;  title and descriptors
;
title = ' '
desc  = ' '

;
;  maximum lines allowed
;
mline = (n_elements(array)/5)-1

;
;  open the file
;
openr,lun,in_file,/get_lun

;
;  read through the file.  Format is expected to be:
;
;   comment lines beginning with ! - will be ignored
;   2 lines for title and description
;   n lines with spectral line details
;       name, wavelength, grating order 
;         .       .             .
;         .       .             .

text = ' '
readf,lun,text
while(strmid(text,0,1) eq '!') do readf,lun,text
title = text   
;
;  now read description - too bad if haven't stuck to format requirements
;
readf,lun,desc

num = 0
while (not eof(lun)) and (num le mline) do begin
   readf,lun,text
   in = str2arr(text,' ',/nomult)
   if n_elements(in) eq 3 then begin
      array(0,num) = repchar(in(0),'_',' ')
      array(1,num) = string(in(1),format='(f9.3)')
      array(2,num) = string(in(2),format='(i9)')
      if det eq 'NIS' then begin
         array(4,num) = which_nis_band(float(in(1))*float(in(2)))
      endif else begin
         array(4,num) = which_gis_band(float(in(1))*float(in(2)))
      endelse
      array(3,num) = wave2pix(det+string(array(4,num),form='(i1)'),$
                              float(in(1))*float(in(2)))
      num = num + 1
   endif else begin
      if text ne '' then begin
         bell
         print,'Invalid line read  - ',text
      endif
   endelse   
endwhile

free_lun,lun


end
