PRO ridl_makelog, INDIR=indir, OUTPUT=output
;
; NAME:
;       ridl_sumlog
; PURPOSE:
;       Produce a time-sorted summary log of RIDL data.
; CALLING SEQUENCE:
;       ridl_sumlog[, indir=input_directory, output=filename]
; OPTIONAL INPUTS:
;       indir --  directory of files to log
; OPTIONAL OUTPUT:
;       output -- output file name [string]
; NOTES:
;       Only files with the extension '.fits' will be logged.
;       By default, temp.log will be created, unless OUTPUT is set.
; PROCEDURES CALLED:
;       ns_sxpar, radecs
; MODIFICATION HISTORY:
; 1999/06/03 created NAL/JHU
; 1999/08/30 added intelligent default output filename DFF/STSCI
; 		also added more specific file specification rules for infiles so that
;		not just any old fits file will be used in the log.
;		also added a line to cd to indir if indir is set.
;		also added a trap in the case that no files are found.
; 2005/04/18 DFF/STSCI
; 		added character space in filename
; 2007/04/25 DFF/RiT
;		modified for RIDL

	;set directory according to whether using a PC or workstation
	slashchar=path_sep()

	IF keyword_set(indir) THEN cd,indir
	cd, '.', current=cur_dir
	infiles=file_search('*.fits',count=count)
	outfile=strmid(cur_dir,1+strpos(cur_dir,slashchar),strlen(cur_dir)-strpos(cur_dir,slashchar)+1)+'.log'
	IF keyword_set(output) THEN outfile=output ELSE outfile=outfile

	IF count eq 0 THEN BEGIN
		print,'No Files Found'
		STOP
	ENDIF

	num=n_elements(infiles)
	ifile=strarr(num)

	openw, unit, outfile, /get_lun
	printf, unit, '    FILE                                                                          UT           LST        OBJECT  RA     DEC    RDS GRPS DRPS   FILT    COMMENT'

	;sort by time
	mjdlst = dblarr(num)
	FOR i=0, num-1 DO BEGIN
	    hd=headfits(infiles[i])
	    mjd=sxpar(hd, 'UT')
	    mjdlst(i) = mjd
	ENDFOR
	sortmjd = sort(mjdlst)
	fsort = infiles(sortmjd)

	;get header keyword values
	FOR i=0, num-1 DO BEGIN
	    hd=headfits(fsort[i])
	    fdecomp, fsort(i), disk, dir, fname
	    fnamestr=strmid(fname,strpos(fname,path_sep())+1,strlen(fname))
	    obj=sxpar(hd, 'OBJECT')
	    com=sxpar(hd, 'COMMENT2')
	    nreads=sxpar(hd, 'NREADS')
	    ngroups=sxpar(hd, 'NGROUPS')
	    ndrops=sxpar(hd, 'NDROPS')
	    fil=sxpar(hd, 'FILTER')
	    ut=sxpar(hd, 'UT')
	    lst=sxpar(hd, 'LST')
	    ra=sxpar(hd, 'RA')
	    dec=sxpar(hd, 'DEC')

	    printf, unit, fnamestr,' ', ut, lst, obj, ra, dec, nreads, ngroups, ndrops, '    ',fil, com, $
	     format='(A100, A1, A15, 2A13, f6.2, F8.2, I4, 2I3, A1, A11, A15, 2F8.3, 2I2, A1, A15)'

	ENDFOR

	free_lun, unit
	print, 'Summary log ', strcompress(outfile), ' created'

END


