;+
; NAME:
;	smei_base_testcase	
; PURPOSE:
;	Runs test case for smei_base program and validates results
; CATEGORY:
;	camera/idl/toolbox
; CALLING SEQUENCE:
	PRO smei_base_testcase, make=make, db=db, keep=keep,	$
		source=source, digsource=digsource
; OPTIONAL INPUT PARAMETERS:
;	/make		if set, the testcase results are regenerated
;	source=source	passed to smei_base
;	/digsource	passed to smei_base
; INCLUDE:
	@compile_opt.pro		; On error, return to caller
; CALLS:
;	GetFileSpec, smei_getfile, who_am_i, flt_string, CheckDir
;	FindAllFiles, TimeOp, TimeUnit, TimeSet, InitVar
;	txt_read, smei_base
; PROCEDURE:
;	The testcase results are stored in an ASCII file in the
;	same directory as this procedure (smei_base_testcase.txt).
;	The file contains frame names and values for pedestal and
;	dark current.
;
;	The executable smei_base is run for all frames in the testcase.
;	Resulting Fits frames are written to $TUB/smei_base_testcase.
;	These files are cleaned up afterward.
;
;	Differences between pedestal and dark current should be at
;	the single precision level.
; MODIFICATION HISTORY:
;	DEC-2004, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;-

InitVar, make     , /key
InitVar, digsource, /key
InitVar, db 	  , /key
InitVar, keep	  , /key

testcase = filepath(root=who_am_i(/dir),'smei_base_testcase.txt')

format = 'YYYY_DOY_hhmmss'
tstart = '2003_296_051731'
tstop  = '2003_296_062643'
camera = '2'

IF NOT make THEN BEGIN

	IF NOT txt_read(testcase,data) THEN message, 'testcase results not found'

	ocount = n_elements(data)

	space = strpos(data[0],' ')
	oname = strmid(data,0,space)
	data  = strmid(data,space) 

	obase = flt_string(data[0],fmt=format)
	obase = fltarr(2,ocount)
	reads, data, format=format, obase


	IF tstart NE strmid(oname[0       ],strpos(oname[0       ],'_')+1) OR $
	   tstop  NE strmid(oname[ocount-1],strpos(oname[ocount-1],'_')+1) OR $
	   camera NE strmid(oname[0],1,1) THEN	$
		message, 'testcase screwed up'

	message, /info, 'testcase has'+strcompress(ocount)+' frames'

ENDIF


tstart = TimeOp(/add,TimeSet(tstart),TimeSet(/diff,-1,TimeUnit(/sec)))
tstart = TimeGet(format=format,tstart)

tstop  = TimeOp(/add,TimeSet(tstop),TimeSet(/diff,1,TimeUnit(/sec)))
tstop  = TimeGet(format=format,tstop)

destination = filepath(root=getenv('TUB'),'smei_base_testcase')

IF NOT CheckDir(destination) THEN BEGIN
	spawn, 'mkdir '+destination
	IF NOT CheckDir(destination) THEN message, 'failed creating '+destination
ENDIF

cmd = 'smei'+(['','db'])[db]+'_base -force -start='+tstart+' -stop='+	$
	tstop+' -camera='+camera+' -destination='+destination 

IF IsType(source,/defined) THEN BEGIN
	IF NOT CheckDir(source) THEN message, 'source does not exist, '+source
	cmd = cmd+' -source='+source
	IF digsource THEN cmd = cmd+' -digsource'
ENDIF

message, /info, cmd
spawn, cmd

fts = FindAllFiles('*.fts.gz',path=destination,count=ncount)

message, /info, strcompress(ncount,/rem)+' fits files created'

hdr = smei_getfile(fts,/get_hdr,/exist)
IF NOT keep THEN spawn, 'rm -vrf '+destination

CASE make OF

0: BEGIN
	FOR i=0L,ncount-1 DO BEGIN

		name = GetFileSpec(GetFileSpec(fts[i],part='name'),part='name')

		CASE oname[i] EQ name OF

		0: message, 'unmatched frames: '+oname[i]+' and '+name

		1: BEGIN
			ped  = smei_property(hdr[i],/pedestal)
			dark = smei_property(hdr[i],/dark_current)

			d_ped  = ped -obase[0,i]
			d_dark = dark-obase[1,i]

			CASE IsType(d_ped_max,/defined) OF
			0: BEGIN
				d_ped_max  = abs(d_ped )
				d_dark_max = abs(d_dark)
			END
			1: BEGIN
				d_ped_max = abs(d_ped ) > d_ped_max
				d_ped_min = abs(d_dark) > d_dark_max
			END
			ENDCASE 

			print, format='(A,2F12.4,2E10.2)',smei_property(hdr[i],/name), ped, dark, d_ped, d_dark

		END

		ENDCASE

	ENDFOR

	message, /info, 'max. pedestal difference    '+strcompress(d_ped_max )
	message, /info, 'max. dark current difference'+strcompress(d_dark_max)

END

1: BEGIN
	openw, /get_lun, iu, testcase
	FOR i=0L,ncount-1 DO printf, iu, format='(A,2F14.7)',	$
		GetFileSpec(GetFileSpec(fts[i],part='name'),part='name'),$
		smei_property(hdr[i],/pedestal), smei_property(hdr[i],/dark_current)
	free_lun, iu	
END

ENDCASE

RETURN  &  END
