;+
; NAME:
;       EIS_FITS_SCRIPT
;
; PURPOSE:
;	Makes FITS files out of telemetry packets for the 
;	EIS instrument on Solar-B from a list of CCSDS files.
;   Either you give a file name (including directory tree)
;   or you giv a directory name, in which case a list of 
;   ccsds files will be given, and FITS files will be produced for each 
;   CCSDS file.
;
; CATEGORY:
;       EIS Data Processing Software
;
; CALLING SEQUENCE:
;       eis_fits_script,  in_dir, out_dir, logfile = logfile
; INPUTS:
;       in_dir  : Telemetry file directory
;       out_dir : Location of fits files 
;
; KEYWORD PARAMETERS:
;       logfile : Construct logfile containing list of processed and produced files 
;
; OUTPUTS:
;
; CALLS:
;
;       eis_mkfits       
;
; COMMON BLOCKS:
;
; PROCEDURE:
;
; RESTRICTIONS:
;
; MODIFICATION HISTORY:
;   Oivind Wikstol   12-Sep-2005 - Original version
;   Matthew Whillock 18-Nov-2006 - Rewrite
;   Viggo Hansteen   23-Nov-2006 - Added logging of file names
;   Viggo Hansteen   10-Dec-2006 - Name change from fits_script to eis_fits_script -
;                                  supersedes old eis_fits_script
;                                  and added a 'path_sep()' to the in_file
;
;-
;
pro eis_fits_script, in_dir, out_dir, logfile = logfile

  if n_elements(logfile) ne 0 then openw,lu,logfile,/append,/get_lun
  
  in_files = findfile(in_dir + path_sep() + 'eis_md*', count=count)
  if count eq 0 then begin
      print, 'No files'
      close,lu
      free_lun,lu
      return
  endif

  for i = 0, count - 1 do begin  
      data=obj_new('eis_data', in_files[i], datasource='ccsds',hdr=hdr)
      exp_info = (hdr[0])->getexp_info()
      seq_id = exp_info.seq_id
      rast_id = exp_info.rast_id
      
      eis_mkfits, data, hdr, /doplan, /dospcd, fitsdir=out_dir,outfile=outfile
      
      if n_elements(logfile) ne 0 then printf,lu,in_files[i]+' '+outfile
  endfor
  if n_elements(logfile) then begin
    close,lu
    free_lun,lu
  endif
  return
      
end
