pro wrt1sda, lun_in, rec2, dp_clock_ref, strec, img, which, nx, ny, ix, iy, next, lun, bytout
;
;
;
;HISTORY:
;	Written Fall '91 by M.Morrison
;	 9-Apr-93 (MDM) - Added code to check the next major frame if the exposure
;			  duration value is zero.
;	12-Apr-93 (MDM) - Added EXPLAT to the the above 9-Apr-93 fix
;	11-Jul-93 (MDM) - Added code to flag when BLS is on (Ref Ver 1.20)
;	15-Nov-93 (MDM) - Modified 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)
;	22-Mar-95 (MDM) - Correction to make sure that PercentD does not
;			  wrap around (to handle the case where the same major
;			  frames appear twice in a telemetry file)
;
ref_struct, ref, dpos1, dpos2, sxtos1, sxtos2, sxtos3, stat1
sxt_struct, sxt_index=sxt_index		;get version number
rsiz = ref.SXT_Fil_Rec_Siz
;
if (total(img) eq 0) then begin
    err_log, 'STR', 'ERR/SXT: Image data all zeros - not saving'
    if (bytout lt 1000) then begin
	print, 'Deleting the file because there is nothing there yet'
	finfo = fstat(lun)
	err_log, 'STR', 'ERR/SXT: Deleting file: ' + finfo(0).name
	close, lun
	lun = lun * (-1)		;reset to not being opened
	bytout = 0
	file_delete, finfo(0).name
    end
    return
end
;
rd_telem, lun_in, strec, rec1
;
;TODO - resetting the dp_clock reference - needed?
dp_clock_ref, rec1, dp_clock_ref, year  ;tie time between real time and DP clock
;
get1gen, rec1, dp_clock_ref, gen_index
fi = fix(rec1(16+3,0))/64			;0,1,2, or 3
;
sxt_index.shape_sav(0)	= nx		;todo
sxt_index.shape_sav(1)	= ny
;
sxt_index.pow_stat		= rec1(16+48, 25)
sxt_index.sw_stat		= rec1(sxtos2, 12)
sxt_index.sxt_control		= rec1(sxtos2, 32)
sxt_index.sxtfmt		= rec1(sxtos3, 0)

sxt_index.temp_ccd	= rec1(sxtos1, 52)	;out of phase on major frame?

if ((fi mod 2) eq 1) then begin
    ss = [42,55,56,57,61]	;panel temperatures
    sxt_index.temp_hk = [reform(rec1(16+32, 18:26)), reform(rec1(16+32, ss))]
end

sxt_index.hw_error(0)	= rec1(sxtos1, 7)
sxt_index.hw_error(1)	= rec1(sxtos1, 23)

if (ref.log_sxt_istart) then err_log, 'SXT_IMG_SAVE', which, nx, ny

gen_index.nIndexByte	= get_nbytes(gen_index) + get_nbytes(sxt_index)
gen_index.nDataByte	= nx*ny

if (which eq 'P') then begin
    gen_index.dp_time(0)	= rec1(sxtos2, 25)		;TIMER1
    gen_index.dp_time(1)	= rec1(sxtos2, 41)		;TIMER2
    gen_index.dp_time(2)	= mask(rec1(sxtos2, 51), 0, 4)*16 + mask(rec1(dpos2,0), 0, 4)	;TIMER3	-TODO correct lower nibble?

    dp_clock, dp_clock_ref, gen_index, msod, ds79
    gen_index.time		= msod
    gen_index.day		= ds79

    sxt_index.j_register	= rec1(sxtos2, 56)		;PJREGS

    sxt_index.pfi_ffi		= 0 + (mask(rec1(sxtos2, 43), 4, 4)+1)*16
    sxt_index.periph		= rec1(sxtos2,  8)
    sxt_index.explevmode	= rec1(sxtos2,  9)
    sxt_index.imgparam		= rec1(sxtos2, 24)
    sxt_index.flush		= rec1(sxtos2, 40)
    sxt_index.explat		= rec1(sxtos2, 10)*long(256) + rec1(sxtos2, 26)
    sxt_index.expdur		= rec1(sxtos2, 42)*long(256) + rec1(sxtos2, 58)
    if (sxt_index.expdur eq 0) then begin
	print, 'WRT1SDA: Found a zero exposure duration.  Checking next major frame'
	rd_telem, lun_in, strec+1, rec10
	sernum1 = rec1(sxtos3, 18)*long(256*256.) + rec1(sxtos3, 34)*long(256) + rec1(sxtos3, 50)
	sernum2 = rec10(sxtos3, 18)*long(256*256.) + rec10(sxtos3, 34)*long(256) + rec10(sxtos3, 50)
	if (sernum1 eq sernum2) then begin
	    print, 'WRT1SDA: Serial number of next major frame matches.  Using second value
	    sxt_index.expdur		= rec10(sxtos2, 42)*long(256) + rec10(sxtos2, 58)
	    sxt_index.explat		= rec10(sxtos2, 10)*long(256) + rec10(sxtos2, 26)
	end
    end

    sxt_index.ObsRegion		= rec1(sxtos2, 50)
    sxt_index.seq_num		= rec1(sxtos2, 59)
    sxt_index.serial_num	= rec1(sxtos3, 18)*long(256*256.) + rec1(sxtos3, 34)*long(256) + rec1(sxtos3, 50)
    sxt_index.mloop		= rec1(sxtos3, 19)*long(256*256.) + rec1(sxtos3, 35)*long(256) + rec1(sxtos3, 51)
    sxt_index.loops(0)		= rec1(sxtos3,  4)
    sxt_index.loops(1)		= rec1(sxtos3, 20)
    sxt_index.loops(2)		= rec1(sxtos3, 36)
    sxt_index.loops(3)		= rec1(sxtos3, 52)	;mloop and loops was being extracted incorrectly for data reformatted
							;before 12-Nov-91

    sxt_index.shape_cmd(0)	= nx
    sxt_index.shape_cmd(1)	= (mask(rec1(sxtos2, 57), 4, 4) + 1)*64	;OR size N-S
    sxt_index.corner_cmd(0)	= rec1(sxtos2, 27)*4	;Col
    sxt_index.corner_cmd(1)	= rec1(sxtos2, 11)*4	;Line
    sxt_index.corner_sav(0)	= 0		;TODO
    sxt_index.corner_sav(1)	= 0		;TODO

    sxt_index.aec_status	= rec2(sxtos2, 44)	;need to look at first major frame AFTER completed reading out
							;TODO - should check for discontinuity
end else begin
    gen_index.dp_time(0)	= rec1(sxtos2, 19)		;TIMER1
    gen_index.dp_time(1)	= rec1(sxtos2, 35)		;TIMER2
    gen_index.dp_time(2)	= mask(rec1(sxtos2, 51), 4, 4)*16 + mask(rec1(dpos2,0), 0, 4)	;TIMER3

    dp_clock, dp_clock_ref, gen_index, msod, ds79
    gen_index.time		= msod
    gen_index.day		= ds79

    sxt_index.j_register	= rec1(sxtos2, 39)		;FJREGS

    ;sxt_index.pfi_ffi		= 1	;+TODO?? BLS
    sxt_index.pfi_ffi		= 1 + mask(rec1(sxtos2, 0), 3, 1)*8	;MDM added 11-Jul-93 Ref Ver 1.20
    sxt_index.periph		= rec1(sxtos2,  2)
    sxt_index.explevmode	= rec1(sxtos2,  3)
    sxt_index.imgparam		= rec1(sxtos2, 18)
    sxt_index.flush		= rec1(sxtos2, 34)
    sxt_index.explat		= rec1(sxtos2,  4)*long(256) + rec1(sxtos2, 20)
    sxt_index.expdur		= rec1(sxtos2, 36)*long(256) + rec1(sxtos2, 52)
    if (sxt_index.expdur eq 0) then begin
	print, 'WRT1SDA: Found a zero exposure duration.  Checking next major frame'
	rd_telem, lun_in, strec+1, rec10
	sernum1 = rec1(sxtos3, 16)*long(256*256.) + rec1(sxtos3, 32)*long(256) + rec1(sxtos3, 48)
	sernum2 = rec10(sxtos3, 16)*long(256*256.) + rec10(sxtos3, 32)*long(256) + rec10(sxtos3, 48)
	if (sernum1 eq sernum2) then begin
	    print, 'WRT1SDA: Serial number of next major frame matches.  Using second value
	    sxt_index.expdur		= rec10(sxtos2, 36)*long(256) + rec10(sxtos2, 52)
	    sxt_index.explat		= rec10(sxtos2,  4)*long(256) + rec10(sxtos2, 20)
	end
    end

    sxt_index.ObsRegion		= rec1(sxtos2, 50)	;Obs Region is not relevant, but entry table is
    sxt_index.seq_num		= rec1(sxtos2, 55)
    sxt_index.serial_num	= rec1(sxtos3, 16)*long(256*256.) + rec1(sxtos3, 32)*long(256) + rec1(sxtos3, 48)
    sxt_index.mloop		= rec1(sxtos3, 2)*long(256) + rec1(sxtos3, 3)
    sxt_index.loops(0)		= rec1(sxtos3,  1)
    sxt_index.loops(1)		= rec1(sxtos3, 17)
    sxt_index.loops(2)		= rec1(sxtos3, 33)
    sxt_index.loops(3)		= rec1(sxtos3, 49)	;mloop and loops was being extracted incorrectly for data reformatted
                                                        ;before 12-Nov-91

    sxt_index.shape_cmd(0)	= nx
    sxt_index.shape_cmd(1)	= ny
    sxt_index.corner_cmd(0)	= 0			;Col  N/A
    sxt_index.corner_cmd(1)	= rec1(sxtos2, 21) ;*4	;Line
							;TODO - ROI#
    sxt_index.corner_sav(0)	= 0			;TODO
    sxt_index.corner_sav(1)	= 0			;TODO
end
;
ext1pntg, gen_index		;get the processed pointing information
;
int2ex, msod, ds79, timarr	;take exposure time and find out what table was used
sxt_index.seq_tab_serno		= get_seq_tab(timarr)
;
img = rotate(img,5)	;rotate to put west limb on the right
			;added 21-Sep-91
img_info, img, sxt_index.imgparam, img_max, img_avg, img_dev, percent_over
sxt_index.img_max	= img_max
sxt_index.img_avg	= img_avg
sxt_index.img_dev	= img_dev
;sxt_index.percentD	= byte(next / float(nx*ny) * 255.)
sxt_index.percentD	= byte( ((next / float(nx*ny))<1.0) * 255.)
sxt_index.percentOver	= percent_over

sxt_index.fov_center	= getsxtfov(sxt_index.shape_cmd, sxt_index.corner_cmd, sxt_index.imgparam)
			;shape_cmd is the OR shape!! - need to pass nx by 64
;
rdwrt, 'W', lun, bytout, rsiz, Gen_Index, 1
rdwrt, 'W', lun, bytout, rsiz, sxt_index, 1
rdwrt, 'W', lun, bytout, rsiz, img(0:nx-1,0:ny-1), 1
;
end
