    ; NAME:
    ;  darcurrent_analysis.pro (v1.7)
    ;
    ; PURPOSE:
    ;  This procedure reads an input data file produced by
    ;  darkcurrent_reduce.pro and uses the data therein to produce a series
    ;  of dark current slope plots associated with images generated in a
    ;   dark current experiment.
    ; CALLING SEQUENCE:
    ;   darkcurrent_analysis,testdir, [outdir=outdir, [infile=infile
    ;   [,jumbofile [,rd_time]]]]
    ;
    ; INPUTS:
    ;  testdir   the full path name of the directory containing an input data
    ;               file
    ;
    ; KEYWORD PARAMETERS:
    ;  outdir     the full path name of a directory in which the output plots
    ;               will be written.  If unspecified, plots will be placed in
    ;               the directory indicated by the testdir input variable
    ;
    ;  infile     the name of the input data file.  If not specified,
    ;               the default filename generated by fe55_reduce.pro
    ;               (fe55imgs.txt) will be used.
    ;
    ;  jumbofile    the name of the output data file containing values used by
    ;               the dark jumbo plotting routine. The default value is
    ;               'darkjumbo.txt'
    ;
    ; EXAMPLE
    ;
    ;  testdir = '\\rabbit\Raytheon\SB304-008-5.0mu\cold1\dktest.15Mar03\'
    ;  fe55_analysis, testdir
    ;
    ; REFERENCE:
    ;
    ;
    ; MODIFICATION HISTORY:
    ;  Created by:

pro fe55_analysis, dir, outdir=outdir, infile=infile

    ;set directory path delimiter
    delim=path_sep()

    if (not keyword_set(outdir)) then outdir = dir
    if (not keyword_set(infile)) then infile = 'fe55imgs.txt'

    ; add delimiter at end of directory names if necessary
    if (strlen(dir)-1 ne rstrpos(dir,delim)) then dir=dir+delim
    if (strlen(outdir)-1 ne rstrpos(outdir,delim)) then outdir=outdir+delim

    ; Set up for nice quality PostScript plots
    !p.charsize = 1.4
    !x.charsize = 1.4
    !y.charsize = 1.4
    !p.charthick = 4.0
    !x.thick = 8.0
    !y.thick = 8.0
    !p.thick = 8.0
    !x.minor = 5
    !y.minor = 5
    !y.margin=[6,2]
    !x.margin=[14,4]

	; set plot colors
	black = 0
	white = 255

	readcol,dir+infile,filenames,format='a'

	bigs=[0]
	for i=0,n_elements(filenames)-1 do begin
		s=0
		readcol,dir+filenames(i),n,x,y,s

		theHist = histogram(s, binsize=1 , min=0, max=2e4, /nan)
		theVals = findgen(n_elements(theHist))+0.5
		gaussparms = gaussfit(theVals, theHist, A,nterms=3)

		filename = strmid(filenames(i), 0,strpos(filenames(i),'.txt'))
		psfile = outdir+filename+'.ps'
		jpgfile = outdir+filename+'.jpg'
		; make the actual plots twice, saving one in PS format and one in
		; JPG format
		; note that the loop count has been changed to only write JPG files. change it back to 0,1 to
		; get both ps and JPG
		for outcount=0,1 do begin
			if (outcount eq 0) then begin

				; Open the postscript device
				set_plot, 'ps'
				device, filename = psfile, /LANDSCAPE
				!p.font = -1    ; use vector fonts for PS
				!P.MULTI = 0    ; Set up for one plot per page
				fsub = psfile   ; set text to print at bottom of plot

				; set some size values
				asize = 1.3    ; charsize of annotations
				athick = 4.0    ; charthick of annotations
				lthick = 8.0    ; line thickness

			endif else begin

				; Open the z-buffer device
				set_plot, 'z'

				; set resolution and font
				device, set_resolution=[8000,6000]
				device, set_font='Courier'

				!P.MULTI = 0    ; Set up for one plot per page
				fsub = jpgfile  ; set text to print at bottom of plot

				; set some size values
				asize = 12.0     ; charsize of annotations
				athick = 15.0   ; charthick of annotations
				lthick = 20.0   ; line thickness
			endelse

			; Plot the result
			plot, theHist,TITLE=''+filename, xtitle='Counts (ADU)', xrange=[0,A[1]*5.],$	;xrange=[0,A[2]*5.+A[1]],$
				YTITLE='# pixels', psym=10, BACKGROUND=white, charsize=asize*1.25, charthick=athick, xthick=lthick, ythick=lthick, COLOR=black

			xyouts,[(!x.crange(1)-!x.crange(0))*0.7+!x.crange(0)],[(!y.crange(1)-!y.crange(0))*0.9+!y.crange(0)],'gain='+strtrim(string(1620./a(1),format='(G10.3)'),2)+' e-/ADU',color=black,charsize=asize,charthick=athick
			xyouts,[(!x.crange(1)-!x.crange(0))*0.7+!x.crange(0)],[(!y.crange(1)-!y.crange(0))*0.85+!y.crange(0)],'N!Ievents!N='+strtrim(string(n_elements(s)),2),color=black,charsize=asize,charthick=athick
			xyouts,[(!x.crange(1)-!x.crange(0))*0.7+!x.crange(0)],[(!y.crange(1)-!y.crange(0))*0.8+!y.crange(0)],'X!Icenter!N='+$
				strtrim(string(A[1],format='(f10.1)'),2),alignment=0.0,color=black,charsize=asize,charthick=athick
			xyouts,[(!x.crange(1)-!x.crange(0))*0.7+!x.crange(0)],[(!y.crange(1)-!y.crange(0))*0.75+!y.crange(0)],'sigma='+$
				strtrim(string(A[2],format='(f10.1)'),2),alignment=0.0,color=black,charsize=asize,charthick=athick

			; draw vertical line through center
			xpeak = [A[1],A[1]]
			ypeak = [0,!Y.CRANGE(1)]
			oplot,xpeak,ypeak,color=black,thick=!P.THICK

			; Add labels & fitted line
			; write the file name outside the margin of the plot
			xyouts, 1.0, 40, fsub, charsize = asize*0.75, charthick = athick, color = black, /device

			; For the JPEG plot, the plot image must be read in from memory and
			; written out to a file
			if (outcount eq 1) then begin
			jpgimg = tvrd()
			write_jpeg, jpgfile, $
			congrid(jpgimg, 1600, 1200, /center, /interp), quality=100
			endif

			; finish up by closing device
			device, /close

		endfor

		bigs=[bigs,s]
	endfor

	theHist = histogram(bigs, binsize=1 , min=0, max=2e4, /nan)
	theVals = findgen(n_elements(theHist))+0.5
	gaussparms = gaussfit(theVals, theHist, A,nterms=3)

	filename ='fe55_all'
	psfile = outdir+filename+'.ps'
	jpgfile = outdir+filename+'.jpg'
	; make the actual plots twice, saving one in PS format and one in
	; JPG format
	; note that the loop count has been changed to only write JPG files. change it back to 0,1 to
	; get both ps and JPG
	for outcount=0,1 do begin
		if (outcount eq 0) then begin

			; Open the postscript device
			set_plot, 'ps'
			device, filename = psfile, /LANDSCAPE
			!p.font = -1    ; use vector fonts for PS
			!P.MULTI = 0    ; Set up for one plot per page
			fsub = psfile   ; set text to print at bottom of plot

			; set some size values
			asize = 1.12    ; charsize of annotations
			athick = 4.0    ; charthick of annotations
			lthick = 8.0    ; line thickness

		endif else begin

			; Open the z-buffer device
			set_plot, 'z'

			; set resolution and font
			device, set_resolution=[8000,6000]
			device, set_font='Courier'

			!P.MULTI = 0    ; Set up for one plot per page
			fsub = jpgfile  ; set text to print at bottom of plot

			; set some size values
			asize = 8.0     ; charsize of annotations
			athick = 15.0   ; charthick of annotations
			lthick = 20.0   ; line thickness
		endelse

		; Plot the result
		plot, theHist,TITLE=''+filename, xtitle='Counts (ADU)', xrange=[0,A[1]*5.], $	;xrange=[0,A[2]*5.+A[1]],$
			YTITLE='# pixels', psym=10, BACKGROUND=white, charsize=asize*1.25, charthick=athick, xthick=lthick, $
			ythick=lthick, COLOR=black

		xyouts,[(!x.crange(1)-!x.crange(0))*0.7+!x.crange(0)],[(!y.crange(1)-!y.crange(0))*0.9+!y.crange(0)],'gain='+strtrim(string(1620./a(1),format='(G10.3)'),2)+' e-/ADU',color=black,charsize=asize,charthick=athick
		xyouts,[(!x.crange(1)-!x.crange(0))*0.7+!x.crange(0)],[(!y.crange(1)-!y.crange(0))*0.85+!y.crange(0)],'N!Ievents!N='+strtrim(string(n_elements(bigs)),2),color=black,charsize=asize,charthick=athick
		xyouts,[(!x.crange(1)-!x.crange(0))*0.7+!x.crange(0)],[(!y.crange(1)-!y.crange(0))*0.8+!y.crange(0)],'X!Icenter!N='+$
			strtrim(string(A[1],format='(f10.1)'),2),alignment=0.0,color=black,charsize=asize,charthick=athick
		xyouts,[(!x.crange(1)-!x.crange(0))*0.7+!x.crange(0)],[(!y.crange(1)-!y.crange(0))*0.75+!y.crange(0)],'sigma='+$
			strtrim(string(A[2],format='(f10.1)'),2),alignment=0.0,color=black,charsize=asize,charthick=athick

		; draw vertical line through center
		xpeak = [A[1],A[1]]
		ypeak = [0,!Y.CRANGE(1)]
		oplot,xpeak,ypeak,color=black,thick=!P.THICK

		; Add labels & fitted line
		; write the file name outside the margin of the plot
		xyouts, 1.0, 40, fsub, charsize = asize*0.75, charthick = athick, $
		color = black, /device

		; For the JPEG plot, the plot image must be read in from memory and
		; written out to a file
		if (outcount eq 1) then begin
		jpgimg = tvrd()
		write_jpeg, jpgfile, $
		congrid(jpgimg, 1600, 1200, /center, /interp), quality=100
		endif

		; finish up by closing device
		device, /close

	endfor

end
