;+
; Project     : SOHO - CDS     
;                   
; Name        : CDS_WAVE_FILES()
;               
; Purpose     : Find data files which contain named data window(s).
;               
; Explanation : Returns the names of datafiles whose line list
;               contains ALL the input line IDs.
;               
; Use         : IDL> f = cds_wave_files(line_id)
;                    eg
;                    f= cds_wave_files(['Al_10','Fe','Ca_10_557'])
;
; Inputs      : line id - The line(s) being searched for. Can be array
;               
; Opt. Inputs : None
;               
; Outputs     : Function returns the filenames in which the windows 
;               can be found.
;               
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Catalogues
;               
; Prev. Hist. : None
;
; Written     : A N Pike, RAL, 27/07/98
;               
; Modified    : Corrected bug 10/08/98
;
; Version     : Version 2, 10/08/98
;-

function cds_wave_files, line_id

;
;  check at least one input parameter
;
if n_elements(line_id) eq 0 then begin
   print,'Must specify at least one extaction window name'
   return,''
endif


;
;  check file exists
;
if not file_exist(concat_dir('$CDS_HEAD_CAT','CDS_wave_files')) then begin
   print,'File: $CDS_HEAD_CAT/CDS_wave_files does not exist'
   return,''
endif

;
;  define variables and restore saved data
;
wave= 0
files= 0
restore,concat_dir('$CDS_HEAD_CAT','CDS_wave_files')


newwave= wave
newfiles= files

;
;  Remove blank spaces from input.
;
g= n_elements(line_id)
line= strtrim(line_id, 2)


for i= 0, g-1 do begin

;
;  Set line_id to correct form - second character in lower case,
;  all other characters in capitals.
;

   if strupcase(strmid(line(i),0,1)) eq 'W' then begin

      line(i)=  strupcase(line(i))

   endif else begin

      first= strmid(line(i),0,1)
      first= strupcase(first)

      second= strmid(line(i),1,1)
      second= strlowcase(second)

      rest= strmid(line(i),2,10)
      rest= strupcase(rest)

      line(i)= first + second + rest
   endelse


;
;  Search for where this line appears in 'wave' and count the number of
;  times it appears.
;
 

   n= where(strpos(newwave,line(i)) ge 0, count)

   if count eq 0 then begin
      print,'No files found which contain all of the following lines:'  
      print_str,line_id
      return,''

   endif else begin
      newwave= newwave(n)
      newfiles= newfiles(n)
   endelse


endfor

print, trim(count) + ' files found.'

return, newfiles

end

