PRO PMTRAS_DBASE_GAPS, dbsolution, gapmin, FLARE_FLAG=flare_flag
;
; Summaries gaps > gapmin in an ascii file, PMTRAS_DBASE_GAPS.txt.
; Criteria for good data is roll_quality GE 192.
;
; 11-Aug-03 gh	Initial version
; 12-Aug-03 gh	Clean up handling of initial and final data points.
;				Change output filename to ROLL_DBASE_GAPS.txt.
; 21-Aug-03 gh	Change criteria for roll quality to GE 192 to accept good interpolated points.
;				Implement flare checking (slow!)
; 14-Sep-03 gh	Add FLARE_FLAG keyword to make flare checking optional.
;
; Open output file and create time display strings.
npt 					= N_ELEMENTS(dbsolution)
sc_event_time 			= {seconds: 0L, bmicro: 0L}						; define a proper s/c time structure
sc_event_time 			= REPLICATE(sc_event_time, npt)
sc_event_time.seconds 	= dbsolution.sctime
showtime				= hsi_sctime2any(sc_event_time, /STIME)			; = array of time-display strings.
;
igood	= WHERE(dbsolution.roll_quality GE 192, ngood)
IF (ngood LT 2) THEN BEGIN
	PRINT,  'PMTRAS_DBASE_GAPS: No output since only ', ngood, 'data points.'
	RETURN
ENDIF
gapmin			= gapmin > 66
scgoodtime		= dbsolution[igood].sctime
showgoodtime 	= showtime[igood]
anygoodtime		= hsi_sctime2any(sc_event_time[igood])
dt				= SHIFT(scgoodtime, -1) - scgoodtime							; should be > 0
i				= WHERE ((dt[0:ngood-2] LT 63) OR (dt[0:ngood-2] GE gapmin), ngap)
ngaptot			= ngap
;
; Set up for crosscheck with flare list.
IF KEYWORD_SET(flare_flag) THEN flist_obj = obj_new('hsi_flare_list')
;
; Process start of file
OPENW, dbmgapfile, 'ROLL_DBASE_GAPS.txt', /GET_LUN
PRINTF, dbmgapfile, gapmin, FORMAT="('LISTING OF ROLL DATABASE GAPS >', I5, ' SECONDS')"
now 			= SYSTIME()
current_time 	= STRMID(now,8,2)+'-'+STRMID(now,4,3)+'-'+STRMID(now,20,4)+STRMID(now,10,6) ; 'yyyy-mmm-dd hh:mm'
PRINTF, dbmgapfile, current_time, FORMAT="('  generated ', A17)"
PRINTF, dbmgapfile
PRINTF, dbmgapfile, showtime[0], FORMAT="(A20, 32X, 'START')"
nline = 4
dt0		= scgoodtime[0] - dbsolution[0].sctime
IF dt0 GE gapmin THEN BEGIN
	PRINTF, dbmgapfile, showtime[0], showgoodtime[0], dt0, 		$
			FORMAT="(A20, ' to ', A20, I7, ' sec gap')"
	nline	= nline + 1
	ngaptot	= ngaptot + 1
ENDIF
;
; Mid file processing
IF (ngap GT 0) THEN BEGIN
	FOR j = 0L, ngap-1 DO BEGIN
		flare_label = ' '
		IF KEYWORD_SET(flare_flag) THEN BEGIN
			flares = flist_obj->getdata(obs_time_interval=[anygoodtime[i[j]], anygoodtime[i[j]+1]])
			IF SIZE(flares, /TYPE) EQ 8	THEN flare_label = 'FLARE'
		ENDIF
		PRINTF, dbmgapfile, showgoodtime[i[j]], showgoodtime[i[j]+1], dt[i[j]], flare_label, $
				FORMAT="(A20, ' to ', A20, I7, ' sec gap', 2X, A5)"
	ENDFOR
	nline = nline + ngap
ENDIF
;
; Finish up.
dtlast = dbsolution[npt-1].sctime - scgoodtime[ngood-1]
IF dtlast GE gapmin THEN BEGIN
	PRINTF, dbmgapfile, showtime[npt-1], showgoodtime[ngood-1], dtlast,	$
	FORMAT="(A20, ' to ', A20, I6, ' sec gap')"
	nline = nline + 1
	ngaptot = ngaptot + 1
ENDIF
nreq = (dbsolution[npt-1].sctime - dbsolution[0].sctime)/64 + 1
percent_gaps = 100. - FLOAT(ngood)/nreq*100.
PRINTF, dbmgapfile, showtime[npt-1], 	FORMAT="(A20, 32X, 'END')"
PRINTF, dbmgapfile
PRINTF, dbmgapfile, ngaptot, 			FORMAT="(I8, ' gaps.')"
PRINTF, dbmgapfile, ngood, 				FORMAT="(I8, ' good data points.')"
PRINTF, dbmgapfile, npt, 				FORMAT="(I8,' total data points.')"
PRINTF, dbmgapfile, nreq,				FORMAT="(I8, ' required data points.')"
PRINTF, dbmgapfile, percent_gaps, 		FORMAT="(F8.1, ' percent gaps.')"
CLOSE, dbmgapfile
nline = nline + 7
PRINT, nline, ' lines written to ROLL_DBASE_GAPS.txt.'
RETURN
END