pro reformat, infil, progverno, strec=strec, enrec=enrec, year=year, $
			sxt=sxt, bcs=bcs, wbs=wbs, hxt=hxt, cba=cba, att=att, all=all, instru=instru, $
			dir_out=dir_out, reffil=reffil, qstop=qstop, $
			not_sxt_patrol=not_sxt_patrol, $
			fileid=fileid, runtime=runtime, nrec=nrec, autodel=autodel, qdebug=qdebug
;
;+
;NAME:
;	reformat
;PURPOSE:
;	The Solar-A/Yohkoh reformatter for all instruments
;INPUT:
;	infil	- Input file name
;OPTIONAL INPUT:
;	strec	- Starting record (major frame) to begin reformatting
;	enrec	- Ending record (major frame) to stop reformatting
;	year	- Because telemetry does not have year number in it,
;		  it was necessary to pass the year of the data being
;		  reformatted
;		  TEMPORARY FIX IS TO USE "LEAP YEAR NIBBLE" + 1991
;		  to get the year, this works for 1991 and 1992
;		  NEED TO HAVE THAT NIBBLE REDEFINED
;	sxt	- If set (or "all" is set), reformat sxt data
;	wbs	- If set (or "all" is set), reformat wbs data
;	bcs	- If set (or "all" is set), reformat bcs data
;	hxt	- If set (or "all" is set), reformat hxt data
;	cba	- If set (or "all" is set), reformat cba data
;	att	- If set (or "all" is set), reformat att data
;	instru	- Reformat instrument data (sxt,bcs,hxt,wbs)
;	dir_out	- The directory where reformatted data should be
;		  written.
;	reffil	- OBSOLETE: Used when reformatting from direct
;		  BDR dump data that does not have actual observation
;		  time (has ground receipt times instead)
;	qstop	- If set, stop inside the routine at the end of
;		  execution
;	not_sxt_patrol - If set, it assumes that a buffer dump is
;		  not an sxt patrol (internal handling is different)
;	autodel	- If set, and the output file already exists, then
;		  just go ahead and delete
;OUTPUT:
;	progverno- If input file name is a null string, just return the
;		   program version number (to be logged elsewhere)
;	fileid	- The file ID of the output files
;	runtime	- The number of minutes it took to execute the routine
;	nrec	- The number of major frames reformatter
;HISTORY:
;Ver 0.1 - Up to 1-Jun-91	Fortran Version, SXT, BCS only
;Ver 0.2 - 17-Jun-91		IDL first version
;Ver 0.43   5-Oct-91		Reversed the sign of the SXT_FOV_CENTER field
;Ver 0.44  19-Oct-91		By now, roadmap structure version are being saved, as well as data structure versions
;				for ATT and WDA files.  Also, this was the first location that data was written into the
;				"pntg" fields in the ATT and GEN_INDEX structures.
;Ver 0.45  12-Nov-91		Corrected error in SXT "mloop" and "loops" extraction - was getting wrong bytes
;Ver 0.46  18-Nov-91		(A) When BCS mode header has three "FF" pattern, it was not handling it
;				    properly (a double "FF" is the mark for a new spectra).
;				(B) When DP rate changes from Med to High, the frame indicator telemetered down gets corrupted.
;				    This was causing anomalous BCS quasi-static entries.  A patch was made to
;				    ignore changes until at least 4 major frames after a rate change.  
;				(C) Bob Bentley figured out that the S/C DP was corrupting the BCS spectral data in
;				    Quiet/Medium rate.  He also figured out how to recover part of the data.  The
;				    fix was implemented in the reformatter. 
;				(D) It was discovered that the S/C has some processed pointing information available that 
;				    is telemetered down (offset angle from sun center).  The ATT structure was expanded to 
;				    include this information (new tag name "sc_pntg").
;				(E) The BCS DP_Sync structure was expanded to include a new tag "commandStat" and the 
;				    "accInterval" tag was changed from byte to integer*2 type.
;Ver 0.47  19-Nov-91		Fixed "ext1ada" to take into account the sign bit while extracting "sc_pntg"
;Ver 0.48  20-Nov-91		Forced the SXT table read to re-initialize the table read/listing since if the reformatter
;				is run twice in a row, the tables in the directory are not read the second time.
;Ver 0.49  21-Nov-91		Changed SXT table dump extraction to use "table_dump" instead of "table1"
;Ver 0.50  23-Nov-91		Fixed trouble where BCS is putting out dummy data in Quiet/Medium rate - it was not
;				recognizing it was all dummy data since the "QT/Med" 232 word fix was applied
;Ver 0.51   2-Dec-91		Changed to call "ext1bcs" during all modes (not just QT/FL/and BCS-OUT)
;				Also patched the date for the QT/Med BCS extraction fix which occurred on 27-Nov-91
;Ver 0.52  10-Dec-91		Corrected HXT to look at proper bit to determine if PC or PH data
;Ver 0.53  11-Dec-91		Added "telemetry" field to ATT roadmap
;				Added "flare_status" field to SXT roadmap (and observing log)
;				Added /progver option - just return from routine with program version
;          27-Feb-92		Ran "update" with "update_wbs_rmap" and "update_hxt_rmap" to change the units on the
;				summary light curve fields.  The "pointer.roadmap_version" was updated to "xxx2" hex for each
;Ver 0.54  27-Feb-92		Changed HXT index and roadmap summary count variable (sum_l, sum_h1, ...) from total cnts/major 
;				frame to cnts/sensor/sec
;				Changed WBS index and roadmap summary count variables (sxs1, sxs2, hxs, ...) from cnts/integration
;				interval to cnts/sec.
;				In both cases above, the decompression algorithm was used before summing (it was not done
;				before this version)
;Ver 0.55   2-Mar-92		Added "qproblem" parameter to WRT1BDA to mark cases where spectra is extracted incorrectly
;				(special case where #Extracted is greater than #Expected and there is a good modeID block)
;Ver 0.56   3-Mar-92		Changed output filename generation to use "concat_dir"
;Ver 0.57   4-Mar-92		Slight modification with respect to "qproblem" parameter for WRT1BDA - see it for more details.
;				Changed the error log name from being ERRyyddmm.hhmm to ERRyyddmm.hhmm_LOG
;Ver 0.58   6-Mar-92		Changed the writing of the CBA file to exclude the ground derived time information.
;				This portion was 33% of the data file and was not being used.
;Ver 0.59   8-Mar-92		Changed SXT index structure version from 3011 to 3012 (more housekeeping temperature data)
;				Removed the setting of the year within the routine.  TTIM2EX was changed to use the
;				"leap year nibble" to derive the year if the user does not specify it himself.
;Ver 1.00   8-Mar-92		Reformatter used for first "offical" distribution tape
;Ver 1.01  10-Mar-92		Added capability to delete existing reformatted data files if they have the same FileID
;				(if they exist, and were written by another user, they must be explicitly deleted before
;				reformatter can run)
;Ver 1.02  10-Mar-92		Mod to "ext1ada" to put time tags on hxa scans that start at beginning of telemetry dump
;Ver 1.03  13-Mar-92		Adjustment to handle the case where the first PFI or FFI image in the raw telemetry file is
;				missing the "start image dump" mark.  IMPORTANT MODIFICATION
;Ver 1.04  19-Mar-92		Changed logic for "qvalid_sxt" to not check the DP_ORBTIM (it used to check that it was day
;				mode but data can be transmitted in the Morning ?!?)
;Ver 1.05  20-Mar-92		BCS modification to WRT1BDA to mark spectra that have 'F9'x fill data with BLOCKID=5
;Ver 1.06  24-Mar-92		BCS modification to WRT1BDA to mark spectra that have 'AA'x fill data with BLOCKID=5
;Ver 1.07  25-Mar-92		Adjusted the SXT table writing software some
;				Changed the .sxs1 from pc12 to pc21, and changed .sxs2 from pc21 to pc22
;Ver 1.08  16-Apr-92		Change to REF_SUPER
; 				Fixed major bug where last orbit file for a given day
;                         	was being clobbered by the partial first orbit of the
;                         	next day.  Should only effect the ADA and CBA files(?)
;	   26-Apr-92		FileID definition was changed by 5 minutes
;Ver 1.09  29-Apr-92		Change to EXT1FFI to check X range as well as Y range
;				before inserting MF buffer into image array
;Ver 1.10  21-May-92		Trouble with SXT FFI BUFF extraction.  Needed to have the "nx" and "ny" variables be 
;				integer*4 because 256*512 in integer*2 equals 0
;				Also changed logic to make sure SXT control is manual for buffer extraction
;Ver 1.11   4-Jun-92		Modified so that WBS data set is written out when there is a discontinuity in the next
;				frame (separate the two halfs)
;				Also changed MAP_DISCON to allow an extra 0.25 seconds difference in the successive frames
;				because there appears to be that error during rate changes.
;Ver 1.12   9-Jun-92		MAJOR BUG fixed with WBS extraction.  Previously, it was possible to have the second half
;				of the previous major frame pair written out with the "current" major frame pair.  THe data
;				was out of time order because of this.
;				Also changed to write out dataset when the DP rate changes
;Ver 1.13  22-Jul-92		Modification to use BCS_CLOCK_FIX for the time period 16-Jul-92 17:00 to 20-Jul-92 14:50
;				since an SEU caused the BCS clock value to not be telemetered down.  The fix was to use a
;				single tie point and to re-calculate the reference time every major frame using that one tie
;				point.  There is a 0.2-0.3 sec/day drift in the clock that was corrected for.  The calculation
;				is done every major frame just because it is easier to tie into the way the clock was being used
;				elsewhere in the code (it updated each time it rolled over).
;Ver 1.14   4-Aug-92		Added keyword /AUTODEL
;Ver 1.15   5-Aug-92		Modification so that BCS clock tie point routine is not called when in BCS memory dump mode
;				(since DP_SYNC portion is overwritten with memory addresses).  BCS CPU must be enabled too.
;Ver 1.16   6-Aug-92		Removed 8 lines of code (writing DIR_REFORMAT_STAT/refdata files for BCS team - obsolete now)
;				in order to remove compile problem on isassX machines - not seen on sxt2!!
;Ver 1.17   9-Oct-92		Modified the SXT patrol image buffer extraction - forced it to be quarter resolution
;				(actually modified 28-Aug-92 but not put on-line)
;	   30-Nov-92		Modified to change protection of ERR file to -rw-rw-rw
;Ver 1.18   9-Apr-93		Modification to WRT1SDA to look at the next major frame when the exposure duration value
;				is zero.
;Ver 1.19  14-Jun-93		Corrected error where SXT images were not being reformatted when the telemetry was
;				corrupted for the first frame of the image.
;	   16-Jun-93		Added print statement about PFI/FFI serial numbers when they change and force a write
;				Added /QDEBUG statement
;Ver 1.20  11-Jul-93		Added code to flag when BLS is on for SXT FFI images
;				Corrected error in extraction of WBS power status 
;	   17-Jul-93		Corrected error of setting reformatter version to 1.12 instead of 1.20
;	   18-Aug-93		Changed call to GET_TIM_ALL to simply set the /TABLES_DIR switch rather than pass the
;				directory name
;Ver 1.21  30-Sep-93		Modified so that the clock tie point is never used
;				for a BCS clock value of 0 (avoid anomalous problem case of 24-Aug-93 23:00)
;	   15-Nov-93		Modified WRT1SDA to delete the file if the image is all zeros
;				and it is the first image being saved (other images
;				might not appear and that leaves a bad file behind)
;Ver 1.22  12-Apr-95		Changes to EXT1PFI and EXT1FFI to calculate the PercentD value properly
;-
common blk_ext1ada, att_pntg, natt_pntg		;used to store the processed pointing information since SXT and BCS data
						;can come down "out of sync"
;
progVerNo = long(1.220 *1000)
progName = 'REFORMAT.PRO'
;
if (n_params(0) eq 2) then return
;
qwrt_cba = 0
qwrt_sxt = 0
qwrt_bcs = 0
qwrt_wbs = 0
qwrt_hxt = 0
qwrt_att = 0
;
if (keyword_set(cba)) then qwrt_cba = 1
if (keyword_set(att)) then qwrt_att = 1
if (keyword_set(sxt)) then qwrt_sxt = 1
if (keyword_set(bcs)) then qwrt_bcs = 1
if (keyword_set(wbs)) then qwrt_wbs = 1
if (keyword_set(hxt)) then qwrt_hxt = 1
if (keyword_set(all)) then begin
    qwrt_cba = 1
    qwrt_sxt = 1
    qwrt_bcs = 1
    qwrt_wbs = 1
    qwrt_hxt = 1
    qwrt_att = 1
end
if (keyword_set(instru)) then begin
    qwrt_sxt = 1
    qwrt_bcs = 1
    qwrt_wbs = 1
    qwrt_hxt = 1
end
if (not keyword_set(reffil)) then reffil = infil
;
;----- Call subroutines that define the structures
;
hxt_struct
sxt_struct
gen_struct
bcs_struct
wbs_struct
att_struct
cba_struct
ref_struct, ref, dpos1, dpos2, sxtos1, sxtos2, sxtos3, stat1
;
;	----------------------------------------  ATT Stuff
;
natt_pntg = 0		;number of processed att records available
;
;	---------------------------------------- SXT Stuff
;
p_midext = 0	& f_midext = 0		;not in middle of extraction
p_qfimg = 0	& f_qfimg = 0		;not filled img buffer
p_ix = 0	& f_ix = 0
p_iy = 0	& f_iy = 0
p_nx = 0	& f_nx = 0
p_ny = 0	& f_ny = 0
p_next = 0L	& f_next = 0L		;#words extracted
;
b_midext = 0				;middle of BUFFER extraction
					;Use the "f_" variables since cannot have FFI dump and buffer dump at the same time
;
ref_colsum = long([1024, 512, 256])
ref_ffimode = [524288, 262144, 131072, 65536]	;512k, 256k, 128k, 64k
;
;	---------------------------------------- WBS Stuff
;
wbs_struct, wbs_index=wbs_index, wbs_dhk=wbs_dhk, wbs_pc_data=wbs_pc, wbs_ph_data=wbs_ph
wbs_index0	= wbs_index		;zeros data fields
wbs_index1	= wbs_index		;zeros data fields
gen_struct, gen_index=gen_index
gen_index0 = gen_index
gen_index1 = gen_index
;
;	---------------------------------------- HXT Stuff
;
hxa_scan = replicate({hxa_scan_rec}, 10)
ihxa_scan = 0
;
;	---------------------------------------- BCS Stuff
;
nbcs_dp_sync 	= 0			;number of MF written to bcs_dp_sync
bcs_buff	= byte(0)
bcs_fixext	= 5			;start extracting everything
					; 0 = "non-fixed" extraction
					; 1 = unknown reason - fixed extraction
					; 2 = memory dump - fixed extraction
					; 3 = stim/cal data - fixed extraction
					; 5 = Dummy data - not used after 6-Nov-91
;
bcs_sav_norm	= 0			;save partial extraction of normal queue
bcs_sav_fast	= 0			;save partial extraction of fast queue
bcs_last_mode	= 0			;save last mode (for saving partial extraction)
bcs_last_block	= 0
;
;--- Workaround because of trouble in Quiet/Medium bit rate before ??-???-91
;
xx = indgen(32)*8
xx1 = indgen(8)*4
xx(xx1) = -1
xx = xx(where(xx ge 0))

zz = indgen(256)
zz(xx) = -1
zz = zz(where(zz ge 0))
;
bcs_rate_subs = zz
qbcs_rate_fix = 1	;see below where the date is checked to see if the fix should be applied
ndummy = [256, 232]	;Workaround for case where Quiet/Medium rate fix was applied to dummy data
;
if (qwrt_bcs) then bcs_rd_grpplan, bcs_grpplan_file, bcs_grpplan
;
;	---------------------------------------- Output File Stuff
;
lun_in = 1
lun_bcs =  -10
lun_cba =  -11
lun_hxt =  -12
lun_wbs =  -13
lun_sxtp = -14
lun_sxtf = -15
lun_att =  -16
;
bytout_bcs = 0
bytout_cba = 0
bytout_hxt = 0
bytout_wbs = 0
bytout_sxtp = 0
bytout_sxtf = 0
bytout_att = 0
;
if (n_elements(infil) eq 0) then begin
    read, 'Enter the input file name', infil
end
infil = filnam_fix(infil)		;convert to Ultrix if necessary
print, 'Now Processing ', infil
;
;---------------------------------------- Get DP clock reference time (needs to be a real-time data file) - TODO right?
;
get_lun, lun
reffil = filnam_fix(reffil)
openr, lun, reffil
;
irec = 0
rd_telem, lun, irec, rec1, err
free_lun, lun
if (err eq 36) then begin
    print, 'There is no good data in this file', string(7b)
    return
end
dp_clock_ref, rec1, dp_clock_ref, year	;tie time between real time and DP clock
;
;---------------------------------------- Get output file names
;
if (not keyword_set(dir_out)) then dir_out = ''
close, lun_in
openr, lun_in, infil
;
irec = 0
rd_telem, lun_in, irec, rec1
;;get1gen, rec1, dp_clock_ref, gen_index, /reference            ;extract the DP time stuff
;;dp_clock, dp_clock_ref, gen_index, msod, ds79
;;int2ex, msod, ds79, daytim
ttim2ex, rec1, daytim, year
;
;TODO - fileID is actually ground derived
fileID = string(daytim(6), daytim(5), daytim(4), '.', daytim(0), daytim(1), FORMAT = '(3i2.2,a,2i2.2)')
;
;--- Check if BCS rate fix should be applied
tarr_fix = timstr2ex('27-Nov-91 08:15:00')	;fix date inserted on 2-Dec-91
ex2int, tarr_fix, msod_fix, ds79_fix
diff = int2secarr(daytim, [msod_fix, ds79_fix])
qbcs_rate_fix = (diff(0) lt 0)		;if before fix date, do the fix
;
qbcs_clock_fix = ( (int2secarr(daytim, '16-jul-92') gt 0) and (int2secarr(daytim, '21-jul-92') le 0))
;Since DAYTIM is the time for the FIRST major frame in a file, do the fix for all data taken on 16-Jul thru 20-Jul inclusive
;
;--------------------------------------- Check if those files already exist
;
ff = findfile( concat_dir(dir_out, '*'+fileID + '*'))
if (ff(0) ne '') then if (not keyword_set(autodel)) then begin
    print, 'The reformatter has already created some files with this FileID'
    for i=0,n_elements(ff)-1 do print, '     ', ff(i)
    print, 'It is necessary to delete these files before continuing'
    yesnox, 'May I delete these files?', in, 'Yes'
    if (in eq 1) then begin
	for i=0,n_elements(ff)-1 do begin
	    cmd = 'rm -f ' + ff(i)
	    spawn, cmd
	end
    end else begin
	print, 'Stopping within REFORMAT'
	stop
    end
end
;
;--------------------------------------- Open log file
;
close, ref.lun_err
;outfil = concat_dir(dir_out, 'ERR' + fileID)
outfil = concat_dir(dir_out, 'ERR' + fileID + '_log')
outfil = filnam_fix(outfil)
openw, ref.lun_err, outfil
spawn, 'chmod 666 ' + outfil
start_time = systime(1)
err_log, 'STR', 'Program Name:      ' + progName
err_log, 'STR', 'Program Version    ' + strtrim(progVerNo, 2)
err_log, 'STR', 'Program Run:       ' + !stime
err_log, 'STR', 'Input File:        ' + infil
err_log, 'STR', 'FileID             ' + fileID
err_log, 'STR', '  '
;
err_log, 'ONOFF', qwrt_cba, qwrt_att, qwrt_bcs, qwrt_sxt, qwrt_hxt, qwrt_wbs
err_log, 'STR', ' '
;
;---------------------------------------- Get the times to map the discontinuities
;     ALSO, look through for SXT table dumps and write them out if found
;
get_tim_all, infil, day, time, rate, year=year, /tables_dir, /info, strec=strec, enrec=enrec
map_discon, day, time, rate, discon_arr, /print, strec=strec, enrec=enrec, /err_log, /info
dummy = get_seq_tab(150, /qforcerd)	;Added 20-Nov-91 - force an update of the table list held in memory
;
;---------------------------------------- Initialize variables
;
nsince_ratechange = 99		;extract information right away
last_dprate = -99
last_dpmode = -99
last_sxttelem = -99
last_bcs_cpu = -99
p_last_sernum = -1
f_last_sernum = -1
wbs_cnt = 0
wbs_nmf = 0
bcs_clock0 = long(999999)
bcs_clock1 = 0
qeof = 0
qdiscont = 1
if (keyword_set(strec)) then irec = strec else irec = 0
while (not qeof) do begin
    irec0 = irec	;save the ingoing record since it can be changed if it skips a record
    rd_telem, lun_in, irec, rec1, err
    if (err eq 36) then qeof = 1

    ;if (not qeof) then qdiscont = discon_arr(irec)
    if (not qeof) then qdiscont = discon_arr(irec) or discon_arr(irec0) or (irec ne irec0)	;MDM correction 14-Jun-93
    qdiscont_next = 0
    if (irec+1 lt n_elements(discon_arr)-1) then qdiscont_next = discon_arr(irec+1)	;the next frame is a discontinuity

    ;-------------------- Flag Check --------------------

    fi		= fix(rec1(16+3,0))/64			;0,1,2, or 3
    dpmode	= mask(rec1(dpos1,  2), 0, 4)	; 9 = Flare
						;11 = BCS-Out
						;12 = Night
						;13 = Quiet

						; 6 = BCSmem
    dprate	= mask(rec1(stat1, 15), 5, 3)	;
    if ((dprate eq last_dprate) or (last_dprate eq -99)) then nsince_ratechange = nsince_ratechange+1 else nsince_ratechange = 1
    ;don't reset if first record being read

    p_stflg	= btest0(rec1(sxtos2, 0), 7)
    p64_stflg	= btest0(rec1(sxtos2, 0), 6)
    p_dummy	= btest0(rec1(sxtos2, 0), 5)
    f_stflg	= btest0(rec1(sxtos2, 0), 2)
    f_dummy	= btest0(rec1(sxtos2, 0), 1)
    iew		= mask(rec1(sxtos2, 43), 0, 4)
    pstrip_stflg= 0
    if (p64_stflg and (iew eq 0)) then pstrip_stflg = 1

    dp_orbtim	= mask(rec1(sxtos2, 32), 2, 2)		;0 = day
							;1 = Evening
							;2 = Night
							;3 = Morning

    sxttelem	= mask(rec1(sxtos2, 16), 6, 2)
    sxtfmt	= btest0(rec1(sxtos3, 0), 7)	;true = PFI uses 8 col
    if (dpmode eq 9) then sxtfmt = 1	;when in flare mode, PFI data uses 8 col
    sxtcont	= btest0(rec1(sxtos2, 32), 6)	;true = control is auto
    sxt_pow	= btest0(rec1(stat1, 25), 7)
    p_sernum	= rec1(sxtos3, 18)*long(256*256.) + rec1(sxtos3, 34)*long(256) + rec1(sxtos3, 50)
    f_sernum	= rec1(sxtos3, 16)*long(256*256.) + rec1(sxtos3, 32)*long(256) + rec1(sxtos3, 48)

    ;qvalid_sxt = (((dpmode eq 9) or (dpmode eq 13)) and sxtcont and sxt_pow and (dp_orbtim eq 0))
    qvalid_sxt = (((dpmode eq 9) or (dpmode eq 13)) and sxtcont and sxt_pow)	;DP_ORBTIM condition removed 19-Mar-92
    ;^^ how to use SXTCONTROL information during extraction?
    ;^^ does DP_ORBTIM have to be day for valid data??

    qvalid_sxt2 = (((dpmode eq 9) or (dpmode eq 13)) and sxtcont and sxt_pow)	;no check on DP_ORBTIM
    if (qvalid_sxt2 and (sxttelem eq 0) and (not f_midext) and (not f_dummy) and (bytout_sxtf eq 0)) then f_stflg = 1
    if (qvalid_sxt2 and (sxttelem eq 0) and (not p_midext) and (not p_dummy) and (bytout_sxtp eq 0)) then p_stflg = 1
    ;^^ above cases cover the very first image in the raw telemtry file when the start flag is missing.  If any
    ;   data has been started to be written to the output file, then ignore
    ;^^ added 13-Mar-92

    bcs_pow	= btest0(rec1(16+112, 4), 7)	;BCS digital electronics
    bcs_cpu	= btest0(rec1(16+112, 4), 3)	;BCS CPU enabled/disabled
    bcs_cpu	= abs(bcs_cpu-1)		;inverted logic
    stat = mask(rec1(16+66,60),1,2)
    if ((fi eq 2) and (stat eq 2)) then bcs_fixext = 2 ;mem dump
    stat = mask(rec1(16+66,30),3,1)
    if ((fi eq 1) and (stat eq 1)) then bcs_fixext = 0  ;PH data
    ;disabled 25-Oct-91 Midnight   - if ((fi eq 1) and (stat eq 0)) then bcs_fixext = 3  ;non-PH data (stim/cal data)
    ;because of telemetry rate problems giving false data

    ;--- See if BCS clock has rolled over and needs to be "re-associated" with the DP clock
    if (fi eq 0) then bcs_clock1 = rec1(16+66, 29)*long(256) + rec1(16+66, 28)
    del_bcs_clock = bcs_clock1 - bcs_clock0
    bcs_clock0 = bcs_clock1	;update for next calculation
    ;see below where "bcs_clock" is called (after the err_log status messages)

    temp = rec1(16+48, 1)
    hxt_pow = 0
    if ((btest0(temp, 6)) or (btest0(temp, 7))) then hxt_pow = 1
    cal_status = rec1(16+51, 55)
    qhxt_caldata = btest0(cal_status, 5) 	;HXT-CAL-DATA

    wbs_pow	= btest0(rec1(16+48, 2), 5)

    ttim2ex, rec1, daytim
    err_log, 'STD_INFO', irec, dpmode, dprate, sxt_pow, sxtcont, bcs_pow, bcs_cpu, bcs_fixext, hxt_pow, wbs_pow, $
										fmt_tim(daytim)
    if (qdiscont) then err_log, 'STR', 'INF/GEN: Discontinuity / gap in the data'
    if (keyword_set(qdebug)) then err_log, 'DEB_INFO', qvalid_sxt, qvalid_sxt2, p_stflg, f_stflg, $
								p_dummy, f_dummy, p_sernum, f_sernum

    ;re-establish the tie point for BCS clock to DP clock
    if (((del_bcs_clock lt 0) or qdiscont) and qwrt_bcs and bcs_cpu and (dpmode ne 6)) then $
							bcs_clock, lun_in, irec, dp_clock_ref, bcs_clock_ref
    if (bcs_cpu and (last_bcs_cpu eq 0)) then bcs_clock, lun_in, irec, dp_clock_ref, bcs_clock_ref	;Added 5-Aug-92
    ;^^ is for cases where coming out of BCS dump mode - re-sync the clock
    if (qbcs_clock_fix and qwrt_bcs) then bcs_clock_fix, daytim, bcs_clock_ref		;added 22-Jul-92

    ;-------------------- SXT Write Check --------------------

    if (qwrt_sxt) then begin
	;
	;----- Check PFI image for write conditions
	;
	qwrite = 0
	if (qvalid_sxt and pstrip_stflg and p_midext) then qwrite = 1	;Found new start, and in the middle of extraction
	if (qeof and p_midext) then qwrite = 1			;End of file and in middle of extraction
	if ((not qwrite) and p_midext and (p_sernum ne p_last_sernum)) then begin
	    err_log, 'STR', 'WRN/SXT: PFI serial number changed.  Forced write'
	    err_log, 'STR', 'WRN/SXT: PFI serial numbers. Old:' + string(p_last_sernum) + ' New:' + string(p_sernum)
	    qwrite = 1
	end
	if ((not qwrite) and p_midext and p_qfimg) then begin	;Trouble case: filled buffer, but no write flag TODO??- KEEP?
	    err_log, 'STR', 'WRN/SXT: Filled PFI buffer'
	    qwrite = 1
	    p_qfimg = 0
	end
	if (qwrite and qwrt_sxt) then begin
	    if (lun_sxtp lt 0) then open_da_file, lun_sxtp, dir_out, 'SPR', fileid, ref.SXT_Fil_Rec_Siz, bytout_sxtp, $
											fpoint_sxtp, qs_sxtp, /init_qs
	    wrt1sda, lun_in, rec1, dp_clock_ref, p_strec, p_img, 'P', p_nx, p_ny, p_ix, p_iy, p_next, lun_sxtp, bytout_sxtp
	end
	if (qwrite) then p_midext = 0					;Wrote out ==> no longer in middle of extraction
	if ((irec ne 0) and (p_sernum ne p_last_sernum) and qdiscont and sxt_pow and sxtcont) then pstrip_stflg = 1
	;^^ Covers cases where there are gaps and extractions should
	;   START in the middle of an image

	;
	;----- Check FFI image for write conditions
	;
	qwrite = 0
	if (qvalid_sxt and f_stflg and f_midext) then qwrite = 1
	if (qeof and f_midext) then qwrite = 1			;end of file
	if ((not qwrite) and f_midext and (f_sernum ne f_last_sernum) and sxt_pow and sxtcont) then begin
	    err_log, 'STR', 'INF/SXT: FFI serial number changed.  Forced write'
	    err_log, 'STR', 'WRN/SXT: FFI serial numbers. Old:' + string(f_last_sernum) + ' New:' + string(f_sernum)
	    qwrite = 1
	end
	if ((not qwrite) and f_midext and f_qfimg) then begin	;Trouble case: filled buffer, but no write flag TODO??- KEEP?
	    err_log, 'STR', 'ERR/SXT: Filled FFI buffer'
	    qwrite = 1 	;Filled buffer, but no write flag 
	end
        if (f_midext and (not sxtcont)) then begin
            err_log, 'STR', 'WRN/SXT: Change to control manual before FFI dump is finished'
            qwrite = 1
        end
	;Removed 4-Oct-91: check control auto to verify if it is dumping an image
	if (qwrite and qwrt_sxt) then begin
	    if (lun_sxtf lt 0) then open_da_file, lun_sxtf, dir_out, 'SFR', fileid, ref.SXT_Fil_Rec_Siz, bytout_sxtf, $
											fpoint_sxtf, qs_sxtf, /init_qs
	    wrt1sda, lun_in, rec1, dp_clock_ref, f_strec, f_img, 'F', f_nx, f_ny, f_ix, f_iy, f_next, lun_sxtf, bytout_sxtf
	end
	if (qwrite) then f_midext = 0					;Wrote out ==> no longer in middle of extraction
	if ((irec ne 0) and (f_sernum ne f_last_sernum) and qdiscont and sxt_pow and sxtcont) then f_stflg = 1
	;^^ Covers cases where there are gaps and extractions should
	;   START in the middle of an image

	;--- Check for Buffer write
	qwrite = 0
	if (b_midext and f_qfimg) then begin
	    err_log, 'STR', 'WRN/SXT: Finished reading Buffer'
	    qwrite = 1
	end
	if (b_midext and sxtcont) then begin
	    err_log, 'STR', 'WRN/SXT: Change to control auto before BUFF dump is finished'
	    qwrite = 1
	end
        if (b_midext and qeof) then qwrite = 1
	if (qwrite and qwrt_sxt) then begin
	    if (lun_sxtf lt 0) then open_da_file, lun_sxtf, dir_out, 'SFR', fileid, ref.SXT_Fil_Rec_Siz, bytout_sxtf, $
											fpoint_sxtf, qs_sxtf, /init_qs
	    wrt1sda_buff, lun_in, dp_clock_ref, f_strec, f_img, 'F', f_nx, f_ny, f_ix, f_iy, f_next, lun_sxtf, bytout_sxtf
	end
	if (qwrite) then b_midext = 0					;Wrote out ==> no longer in middle of extraction
    end

    if (err ne 36) then begin	;TODO - fix - want to access the write routines to dump the buffers
	get1gen, rec1, dp_clock_ref, gen_index
	ext1ada, rec1, gen_index, att_data, hxa_scan, ihxa_scan		;always extract the telemetry pointing data
									;so that pointing information can be put into the
									;instrument files
	ext1pntg, gen_index, /last					;copy the "pntg" data into the general index struct

        ;-------------------- ATT Extraction --------------------

        if (qwrt_att) then begin
            qwrite = 1				;extract all telemetry modes
            if (qwrite) then begin
                if (lun_att lt 0) then open_da_file, lun_att, dir_out, 'ADA', fileid, ref.ATT_Fil_Rec_Siz, bytout_att, $
                                                                                        fpoint_att, qs_att, /init_qs
                wrt1ada, rec1, gen_index, lun_att, bytout_att, att_data, hxa_scan, ihxa_scan
            end
        end

	;-------------------- Spacecraft Extraction --------------------

	if (qwrt_cba) then begin
	    if (lun_cba lt 0) then open_da_file, lun_cba, dir_out, 'CBA', fileid, ref.CBA_Fil_Rec_Siz, bytout_cba, $
											fpoint_cba, qs_cba, /init_qs
	    wrt1cba, rec1, gen_index, lun_cba, bytout_cba
	end

	;-------------------- SXT Extraction --------------------

	if (qwrt_sxt) then begin
	    if (sxtfmt) then begin		;bit set ==> PFI dominant
		f_istcol = 6
		f_iencol = 7
		p_istcol = 8
		p_iencol = 15
	    end else begin
		p_istcol = 6
		p_iencol = 7
		f_istcol = 8
		f_iencol = 15
	    end


	    if (qvalid_sxt and pstrip_stflg) then begin
		p_nx = (mask(rec1(sxtos2, 57), 0, 4) + 1)*64
		p_ny = long(64)
		;p_ny = (mask(rec1(sxtos2, 57), 4, 4) + 1)*64	;OR size N-S
		p_midext = 1
		p_next = 0L
		recsize = (p_iencol-p_istcol+1)*8*64
		p_nmf = p_nx*p_ny / recsize		;number of major frames need to read
		p_strec = irec
		if (ref.log_sxt_istart) then err_log, 'SXT_IMG_START', 'PFI', p_nx, p_ny, p_nmf
		p_img = bytarr(p_nx, p_ny)
	    end
	    if (qvalid_sxt and p64_stflg) then begin
		p_ix = 0
		p_iy = 0
	    end
	    if (p_iy eq 64) then begin
		;--- EW_SUBPOS is not incrementing with valid data frames
		err_log, 'SXT_P_IY_ERR'		;TODO - FIX?
		p_ix = 0
		p_iy = 0
	    end

	    if (qvalid_sxt and f_stflg) then begin
		f_res   = mask(rec1(sxtos2, 18), 0, 2)
		ffimode = mask(rec1(sxtos3, 0),  2, 2)
		f_nx = ref_colsum(f_res)
		f_ny = ref_ffimode(ffimode) / f_nx
		if (f_ny gt f_nx) then f_ny = f_nx	;PATCH - NOT NEEDED?
		f_midext = 1
		recsize = (f_iencol-f_istcol+1)*8*64
		f_nmf = f_nx*f_ny / recsize		;number of major frames need to read
		f_strec = irec
		f_ix = 0
		f_iy = 0
		f_next = 0L
		if (ref.log_sxt_istart) then err_log, 'SXT_IMG_START', 'FFI', f_nx, f_ny, f_nmf
		f_img = bytarr(f_nx, f_ny)
	    end

	    if ((sxttelem eq 1) and (last_sxttelem ne 1) and (not sxtcont))  then begin		;manual buffer dump
												;SXT control needs to be manual
		if (keyword_set(not_sxt_patrol)) then begin
		    f_res	= mask(rec1(sxtos1,8), 0, 2)
		    f_nx 	= ref_colsum(f_res)
		    rowwd	= rec1(sxtos1, 10)	;take last commanded value
		end else begin
		    f_res = 2		;Added 28-Aug-92
		    f_nx = 256L		;force to be full CCD with (in 4x4 summation)
		    rowwd = 127		;force to be 1/2 of CCD
		end
		f_ny	= (rowwd+1)*4/(2^f_res)

                f_istcol = 8            ;buffer dumps always in the 8 column allocation
                f_iencol = 15

		b_midext = 1
		recsize = (f_iencol-f_istcol+1)*8*64.
		f_nmf = f_nx*f_ny / recsize		;number of major frames need to read
		f_strec = irec
		f_ix = 0
		f_iy = 0
		f_next = 0L
		if (ref.log_sxt_istart) then err_log, 'SXT_IMG_START', 'BUFF', f_nx, f_ny, f_nmf
		f_img = bytarr(f_nx, f_ny)
	    end

;;	    fmt1 = '(4x, a, i4, a, i4)'
;;	    str1 = '  '
;;	    if (p_midext and qwrt_sxt) then str1 = string('PFI MF #', (irec-p_strec+1), ' of', p_nmf, FORMAT=fmt1)
;;	    str2 = '  '
;;	    if (f_midext and qwrt_sxt) then str2 = string('FFI MF #', (irec-f_strec+1), ' of', f_nmf, FORMAT=fmt1)
;;	    if (qprint) then begin
;;		if (qdebug) then print, '   '    ;other stuff aleady printed
;;		print, str1, str2		;TODO wanted '+' format (no CR)
;;	    end

	    qavoid = 0
	    if (qhxt_caldata and (p_istcol eq 6)) then qavoid = 1
	    if (qvalid_sxt and p_midext and not qavoid) then begin
		ext1pfi, ref, rec1, sxtos2, p_img, p_nx, p_ny, p_istcol, p_iencol, p_ix, p_iy, p_qfimg, p_next
	    end

	    qavoid = 0
	    if (qhxt_caldata and (f_istcol eq 6)) then qavoid = 1
	    if (sxtcont and (dpmode eq 13) and f_midext and not qavoid) then begin	;quiet mode only
		ext1ffi, ref, rec1, sxtos2, f_img, f_nx, f_ny, f_istcol, f_iencol, f_ix, f_iy, f_qfimg, f_next
	    end

	    if (b_midext) then begin
		ext1ffi_buff, ref, rec1, sxtos2, f_img, f_nx, f_ny, f_istcol, f_iencol, f_ix, f_iy, f_qfimg, f_next
	    end
	end

	;-------------------- BCS Extraction --------------------

	;TODO - if BCS_CPU is off - do WHAT?
	;
	if (dpmode eq 11) then qbcsout = 1 else qbcsout = 0
	if (dpmode eq 6) then qbcsout = 1		;BCS Memory dump - uses last 8 columns of telemetry

	qbcs_rate_fix0 = (qbcs_rate_fix and (dpmode eq 13) and (dprate eq 2))
	;only fix the telemetry if before the fix date and in Quiet/Medium mode
	ext1bcs, rec1, gen_index, qbcsout, bcs_dp_sync, nbcs_dp_sync, bcs_mf_buff, qbcs_rate_fix0, bcs_rate_subs

	;Changed to call "ext1bcs" during all modes (2-Dec-91)

	qextract = 0
	if (bcs_pow and ((dpmode eq 9) or (dpmode eq 13) or (dpmode eq 11)) and qwrt_bcs) then qextract=1
        if ((dpmode eq 6) and qwrt_bcs) then qextract = 1
	if (qextract) then begin
	    ;if (((bcs_mf_buff(0) eq 255) and (bcs_mf_buff(1) eq 255)) and (bcs_fixext ne 3)) then begin
	    if (((bcs_mf_buff(0) eq 255) and (bcs_mf_buff(1) eq 255) and $
	    		(bcs_mf_buff(256) eq 255) and (bcs_mf_buff(257) eq 255)) and (bcs_fixext ne 3)) then begin
		;Changed to check 0,1 AND 256,257 on 25-Oct-91
		err_log, 'BCS_FDTYPE_CAL'
		bcs_fixext = 3
	    end
	    if (not bcs_cpu and (bcs_fixext ne 2)) then begin	;If BCS CPU is disabled, then it is probably a memory dump
		err_log, 'BCS_FDTYPE_MEM'
		bcs_fixext = 2 ;mem dump
	    end

	    bcs_dummy_data = 0
	    if (bcs_mf_buff(0) eq 'f9'x) then begin		;check for dummy data
		ss = where(bcs_mf_buff eq 'f9'x)
		;if (n_elements(ss) eq n_elements(bcs_mf_buff)) then begin
		if ( (n_elements(ss) eq ndummy(qbcs_rate_fix0)) or (n_elements(ss) eq n_elements(bcs_mf_buff)) ) then begin
		    bcs_dummy_data = 1
		    ;;bcs_fixext = 5	;do not reset extraction type since it will not be reset for up to 4 major frames
					;use a temporary variable	- Changed 6-Nov-91
		    err_log, 'bcs_dum_data'
		end
	    end

	    if (lun_bcs lt 0) then open_da_file, lun_bcs, dir_out, 'BDA', fileid, ref.BCS_Fil_Rec_Siz, bytout_bcs, $
										fpoint_bcs, qs_bcs, /init_qs, /qsbuff

	    if (bcs_cpu and (dpmode ne 6) and (nsince_ratechange gt 5)) then begin
		;do not do QS extraction for cpu disabled or BCS-Mem mode - also, check that at least 5 MF after rate change
		for ihalf=0,1 do bcs_qs, lun_in, irec, rec1, gen_index, ihalf, qs_bcs	;make sure it is after "open_da_file"	
	    end

	    if ((bcs_fixext gt 0) and (bcs_fixext lt 5) and (not bcs_dummy_data)) then begin	;don't save dummy data
		wrt1bda, irec, rec1, lun_bcs, bytout_bcs, bcs_fixext, dp_clock_ref, bcs_clock_ref, $
				bcs_mf_buff, bytarr(13), bcs_last_block, 0, qdiscont, grpplan, modeid_arr, qfast
	    end
	    if ((bcs_fixext eq 0) and (not bcs_dummy_data)) then begin
		if (n_elements(bcs_buff) gt 10000) then begin
		    err_log, 'STR', 'ERR/BCS: Resetting buffer - throwing away data'
		    bcs_buff = byte(0)
		end

		if (n_elements(bcs_buff) gt 1) then begin
		    bcs_buff = [bcs_buff, bcs_mf_buff]
		end else begin
		    bcs_buff = bcs_mf_buff
		end
		ext1bcs_spec, ref, irec, rec1, lun_bcs, bytout_bcs, dp_clock_ref, bcs_clock_ref, qdiscont, bcs_buff, $
				bcs_sav_norm, bcs_sav_fast, bcs_last_mode, bcs_last_block, qeof, bcs_grpplan, modeid_arr
	    end
	end

	;-------------------- HXT Extraction --------------------

	;TODO - when to extract HXT (night?) ...
	if (qwrt_hxt and ((dpmode eq 13) or (dpmode eq 9)) and hxt_pow) then begin
	    if (lun_hxt lt 0) then open_da_file, lun_hxt, dir_out, 'HDA', fileid, ref.HXT_Fil_Rec_Siz, bytout_hxt, $
											fpoint_hxt, qs_hxt, /init_qs
	    wrt1hda, rec1, gen_index, dpmode, lun_hxt, bytout_hxt
	end

	;-------------------- WBS Extraction --------------------

	if (qwrt_wbs and wbs_pow and ((dpmode eq 13) or (dpmode eq 9)) ) then begin

	    if ((fi mod 2) eq 0) then begin
		ext1wda, 'DHK', rec1, wbs_dhk, 0
		ext1wda, 'PC', rec1, wbs_pc, 0
		ext1wda, 'PH', rec1, wbs_ph, 0

		ext1wda, 'INDEX', rec1, wbs_index0, 0, wbs_pc
		gen_index0 = gen_index
		wbs_nmf = wbs_nmf + 1
	    end else begin
		ext1wda, 'DHK', rec1, wbs_dhk, 1
		ext1wda, 'PC', rec1, wbs_pc, 1
		ext1wda, 'PH', rec1, wbs_ph, 1

		ext1wda, 'INDEX', rec1, wbs_index1, 1, wbs_pc
		gen_index1 = gen_index
		wbs_nmf = wbs_nmf + 2
	    end
	    wbs_cnt = wbs_cnt + 1

	    ;---------- Check to see if should write out buffer

	    qwrite = 0
	    if (irec gt 0) then begin
		if (dprate ne last_dprate) then qwrite = 1	;TODO
		;if (dpmode ne last_dpmode) then qwrite = 1	;TODO
	    end
	    ;if ((irec eq 0) and ((fi mod 2) eq 1)) then begin
	    if ((fi mod 2) eq 1) then begin		;write out when got to the second half
		qwrite = 1
		if (wbs_cnt eq 1) then err_log, 'WBS_ONLY_GOT1', fi
	    end
	    if (qeof) then qwrite = 1				;End of file
	    if (qdiscont_next) then qwrite = 1		;if there is a gap to the next frame, then write it out
	    if ((wbs_cnt eq 2) and (not qwrite)) then begin
		qwrite = 1
		err_log, 'STR', 'WBS/ERR: Have two major frames but write was not directed!!?!?!?!?!
	    end
	    ;print, irec, fi, fi mod 2, fmt_tim(gen_index), fmt_tim(gen_index0), fmt_tim(gen_index1), wbs_nmf, qwrite

	    if (qwrite) then begin
		if (lun_wbs lt 0) then open_da_file, lun_wbs, dir_out, 'WDA', fileid, ref.WBS_Fil_Rec_Siz, bytout_wbs, $
											fpoint_wbs, qs_wbs, /init_qs
		;
		wrt1wda, lun_wbs, bytout_wbs, wbs_nmf, $
			gen_index0, gen_index1, wbs_index0, wbs_index1, $
			wbs_dhk, wbs_pc, wbs_ph
		wbs_cnt = 0
		wbs_nmf = 0
	    end
	end

	;-------------------- Finished all Extractions --------------------

    end		;if not EOF

    irec = irec + 1
    last_sxttelem = sxttelem
    p_last_sernum = p_sernum
    f_last_sernum = f_sernum
    last_dprate = dprate
    last_dpmode = dpmode
    last_bcs_cpu = bcs_cpu

    if (keyword_set(enrec)) then if (irec gt enrec) then qeof = 1	;abort out - test cases
end
;
if (qwrt_sxt) then begin
    if (bytout_sxtf ne 0) then begin
	wrtQS, lun_sxtf, fpoint_sxtf, bytout_sxtf, qs_sxtf
	wrtSXTMap, lun_sxtf, fpoint_sxtf, bytout_sxtf, fhead, rsiz, /init_fhead
	wrt1p_fh, lun_sxtf, fpoint_sxtf, fhead, rsiz, progName, progVerNo, fileid, 'SFR', 'SXT', qs_sxtf
	close, lun_sxtf
	err_log, 'TOTALS', 'SXT', 'FFI', fhead.nDataSets, fpoint_sxtf.TotBytes
   end

    if (bytout_sxtp ne 0) then begin
	wrtQS, lun_sxtp, fpoint_sxtp, bytout_sxtp, qs_sxtp
	wrtSXTMap, lun_sxtp, fpoint_sxtp, bytout_sxtp, fhead, rsiz, /init_fhead
	wrt1p_fh, lun_sxtp, fpoint_sxtp, fhead, rsiz, progName, progVerNo, fileid, 'SPR', 'SXT', qs_sxtp
	close, lun_sxtp
	err_log, 'TOTALS', 'SXT', 'PFI', fhead.nDataSets, fpoint_sxtp.TotBytes
    end
end
if (qwrt_bcs) then begin
    if (bytout_bcs ne 0) then begin
	if (n_elements(modeid_arr) ne 0) then bcs_cpy_grpplan, unique1(modeid_arr), bcs_grpplan, qs_bcs2
	wrtQS, lun_bcs, fpoint_bcs, bytout_bcs, qs_bcs, qs_bcs2
	wrtBCSMap, lun_bcs, fpoint_bcs, bytout_bcs, fhead, rsiz, bcs_dp_sync, nbcs_dp_sync, /init_fhead, /err_log, /sub_dp
	wrt1p_fh, lun_bcs, fpoint_bcs, fhead, rsiz, progName, progVerNo, fileid, 'BDA', 'BCS', qs_bcs, qs_bcs2
	close, lun_bcs
	err_log, 'TOTALS', 'BCS', '   ', fhead.nDataSets, fpoint_bcs.TotBytes
    end
end
if (qwrt_hxt) then begin
    if (bytout_hxt ne 0) then begin
	wrtQS, lun_hxt, fpoint_hxt, bytout_hxt, qs_hxt
	wrtHXTMap, lun_hxt, fpoint_hxt, bytout_hxt, fhead, rsiz, /init_fhead
	wrt1p_fh, lun_hxt, fpoint_hxt, fhead, rsiz, progName, progVerNo, fileid, 'HDA', 'HXT', qs_hxt
	close, lun_hxt
	err_log, 'TOTALS', 'HXT', '   ', fhead.nDataSets, fpoint_hxt.TotBytes
    end
end
if (qwrt_wbs) then begin
    if (bytout_wbs ne 0) then begin
	wrtQS, lun_wbs, fpoint_wbs, bytout_wbs, qs_wbs
	wrtWBSMap, lun_wbs, fpoint_wbs, bytout_wbs, fhead, rsiz, /init_fhead
	wrt1p_fh, lun_wbs, fpoint_wbs, fhead, rsiz, progName, progVerNo, fileid, 'WDA', 'WBS', qs_wbs
	close, lun_wbs
	err_log, 'TOTALS', 'WBS', '   ', fhead.nDataSets, fpoint_wbs.TotBytes
    end
end
if (qwrt_att) then begin
    if (bytout_att ne 0) then begin
	wrtQS, lun_att, fpoint_att, bytout_att, qs_att
	wrtATTMap, lun_att, fpoint_att, bytout_att, fhead, rsiz, hxa_scan, ihxa_scan, /init_fhead
	wrt1p_fh, lun_att, fpoint_att, fhead, rsiz, progName, progVerNo, fileid, 'ADA', 'ATT', qs_att
	close, lun_att
	err_log, 'TOTALS', 'ATT', '   ', fhead.nDataSets, fpoint_att.TotBytes
    end
end
if (qwrt_cba) then begin
    if (bytout_cba ne 0) then begin
	wrtQS, lun_cba, fpoint_cba, bytout_cba, qs_cba
	wrtCBAMap, lun_cba, fpoint_cba, bytout_cba, fhead, rsiz, /init_fhead
	wrt1p_fh, lun_cba, fpoint_cba, fhead, rsiz, progName, progVerNo, fileid, 'CBA', 'CBA', qs_cba
	close, lun_cba
	err_log, 'TOTALS', 'CBA', '   ', fhead.nDataSets, fpoint_cba.TotBytes
    end
end
close, lun_in
;
end_time = systime(1)
runtime = (end_time-start_time)/60.
nrec = irec-1
err_log, 'ELAPSED', runtime, irec-1
;
close, ref.lun_err
;
if (keyword_set(qstop)) then stop
end
