pro pmtras_dbase_verification, tstart, duration
;
; Regenerates the database solution using short intervals and compares it
;	to the pre-existing solution.
;
; 15-Oct-03 gh	Initial version
; 16-oct-03 gh	Output written to pmtras_dbase_verification.txt.
;
OPENW, logunit, 'pmtras_dbase_verification.txt', /GET_LUN
nline = 0
;
; Regenerate the database solution with short intervals as dbsnew.
;
PMTRAS_DBASE_GENERATOR, tstart, duration, DBSOLUTION=dbsnew, /SHORT, /NOLOG, PMTRAS_DIAGNOSTIC=0
;
; Retrieve the pre-existing solution as dbsold
;
obs_time_interval 	= anytim(tstart) +[0.D, duration*60.D]
o 					= hsi_roll_db()
dbsold 				= o->getdata(obs_time_interval=obs_time_interval)
;
; Compare old and new phases for cases where new data quality is good
;
oknew				= WHERE (dbsnew.roll_quality GE 192, noknew)
t = {seconds:0L, bmicro:0L}
t = REPLICATE(t, noknew)
t.seconds = dbsnew[oknew].sctime
timelabel = hsi_sctime2any(t,/ECS)
FOR i = 0, noknew-1 DO BEGIN
	k = oknew[i]
	j = WHERE(dbsold.sctime EQ dbsnew[k].sctime, nj)		; Find time match in old database (--> index, j)
	IF nj EQ 0 THEN BEGIN
		PRINTF, logunit, dbsnew[k].sctime, ' --'
		nline = nline+1
	ENDIF
	IF nj EQ 1 THEN BEGIN
		IF dbsold[j].roll_quality LT 192 THEN BEGIN
			PRINTF, logunit, timelabel[i], dbsnew[k].sctime, dbsold[j].roll_quality, dbsnew[k].roll_quality, ' qual', $
					FORMAT='(A19, 2X, I12, 2I5, A6)'
			nline = nline+1
		ENDIF ELSE BEGIN
			dphase = ABS(dbsnew[k].roll_phase - dbsold[j].roll_phase)*1.E-6
			IF dphase GT !PI THEN dphase = 2*!PI - dphase
			dphase = LONG(dphase*1.E6)
			PRINTF, logunit, timelabel[i], dbsnew[k].sctime, dbsold[j].roll_quality, dbsnew[k].roll_quality, dphase, $
					FORMAT='(A19, 2X, I12, 2I5, 2I9)'
			nline = nline+1
		ENDELSE
	ENDIF
ENDFOR
PRINT, nline, ' lines written to pmtras_dbase_verification.txt'
CLOSE, logunit
STOP
end