pro ext1ada, rec1, gen_index, att, hxa_scan_arr, ihxa_scan
;
;+
;NAME:
;	ext1ada
;PURPOSE:
;	Extract the S/C Attitude data from one raw telemetry 
;	major frame.  Only called by REFORMAT
;INPUT:
;	rec1
;	gen_index
;OUTPUT:
;	att
;	hxa_scan_arr
;	ihxa_scan
;HISTORY:
;	Written Fall '91 by M.Morrison
;	10-Mar-92 (MDM) Made changes so that when HXA scan dump was
;			being started in the middle of a scan dump
;			because of telemetry loss, it will put the
;			time tag in the data - it was putting all
;			zero's before.
;-
;
common blk_ext1ada, att_pntg, natt_pntg
;
att_struct, att_data=att, att_pntg_save=att_pntg_save
;
fi = fix(rec1(16+3,0))/64			;0,1,2, or 3
;
;----- Extract IRU data
;
ss = indgen(2)*32
att.iru_pow_stat = rec1(16+48, ss+13)
;
ss = [0,1,2,24,25]
ss = [ss, 32+ss]
att.iru_lm	= rec1(16+112, ss)
;
if ((fi mod 2) eq 0) then begin
    att.iru_temp = rec1(16+32, 52)
end
;
ss = indgen(8)*8
att.iru(0,0:7)	= rec1(16+17,ss+5)*long(256*256.) + rec1(16+18,ss+5)*long(256) + rec1(16+19,ss+5)	;x
att.iru(1,0:7)	= rec1(16+17,ss+6)*long(256*256.) + rec1(16+18,ss+6)*long(256) + rec1(16+19,ss+6)	;y
att.iru(2,0:7)	= rec1(16+17,ss+7)*long(256*256.) + rec1(16+18,ss+7)*long(256) + rec1(16+19,ss+7)	;z
;
;
;----- Extract GAS data
;
att.gas_pow_stat= rec1(16+48, 24)
;
if ((fi mod 2) eq 0) then begin
    att.gas_e_hk	= rec1(16+32, 54)
end else begin
    att.gas_e_hk	= rec1(16+32, 36)
end
;
ss = indgen(4)*16
att.gas(0,0:3)	= rec1(16+81,ss+8)	;x
att.gas(1,0:3)	= rec1(16+82,ss+8)	;y
att.gas(2,0:3)	= rec1(16+83,ss+8)	;z
;
;
;----- Extract TFSS data
;
att.tfss_pow_stat= rec1(16+48, 24)
;
if ((fi mod 2) eq 0) then begin
    att.nsas_v		= rec1(16+32, 13:14)
    att.nsas_temp	= rec1(16+32, 55)
end
;
ss = indgen(4)*16
att.tfss(0,0:3)	= rec1(16+82,ss+12)*long(256) + rec1(16+83,ss+12)	;x
att.tfss(1,0:3)	= rec1(16+82,ss+13)*long(256) + rec1(16+83,ss+13)	;y
;
;----- Extract NSAS data
;
att.nsas_pow_stat= rec1(16+48, 24)
;
if ((fi mod 2) eq 0) then begin
    att.nsas_v		= rec1(16+32, 15:16)
    att.nsas_temp	= rec1(16+32, 56)
end
;
ss = indgen(4)*16
att.nsas(0,0:3)	= rec1(16+82,ss+14)*long(256) + rec1(16+83,ss+14)	;x
att.nsas(1,0:3)	= rec1(16+82,ss+15)*long(256) + rec1(16+83,ss+15)	;y
;
;
;----- Extract STT data
;
att.stt_pow_stat= rec1(16+48,2)
;
ss = [18,34,50]
att.stt_stat	= rec1(16+48, ss)
;
if (fi eq 0) then att.stt_v = rec1(16+32, 17)
if (fi eq 2) then att.stt_v = rec1(16+32, 17)
;
if ((fi mod 2) eq 0) then begin
    att.stt_temp	= rec1(16+32, 57:58)
end
;
ss = indgen(2)*32
att.stt_h_pos	= rec1(16+82,ss+2)*long(256) + rec1(16+83,ss+2)		;y
att.stt_v_pos	= rec1(16+82,ss+10)*long(256) + rec1(16+83,ss+10)	;y
att.stt_int	= rec1(16+82,ss+18)*long(256) + rec1(16+83,ss+18)	;intensity
att.stt_alarm	= rec1(16+82,ss+26)*long(256) + rec1(16+83,ss+26)	;alarm
;
;
;----- Extract HXA data
;
ss = indgen(2)*32
att.hxa_gain_cont = rec1(16+49, ss+25)
;
att.hxa_pow_stat= rec1(16+48, 1)
;
pow_status 	= rec1(16+48, 1)
qpow_hxa 	= btest0(pow_status, 4)
;
if (qpow_hxa) then begin
    ;
    ;----- Get HXA data -----
    ;
    ;   W33     W34     W35
    ;012345670123456701234567
    ;^				! 1 bit - says if addresses if for X or Y
    ; ^^^^^^^^^^^		!11 bits- address of fudicials
    ;            ^^^^^^		! 6 bits- X intensity (1-D 2048 point scan)
    ;                  ^^^^^^	! 6 bits- Y intensity (1-D 2048 point scan)
    ;222211111111110000000000
    ;321098765432109876543210
    ;
    ;TODO - is there a flag saying that valid HXA data is coming down??
    ;
    hxa_scan = hxa_scan_arr(ihxa_scan)	;grab current buffer
    ;
    value = rec1(16+33,*)*long(256*256.) + rec1(16+34,*)*long(256) + rec1(16+35,*)
    value = long(value,0, 64)		;turn into 1-D array
    low12 = mask(value, 0, 12)	;looking for bit pattern 111111111111
    ss = where(low12 eq 4095)
    if (n_elements(ss) gt 1) then err_log, 'STR', 'ERR/HXA: More than one 4095 value. N=' + strtrim(n_elements(ss),2)
    ;
    ist =  0
    ien = 63
    qnew_start = 0
    if (ss(0) ne -1) then begin
	qnew_start = 1
	ien = ss(0)-1
    end
    qfull = 0

    qstart_mid = ((ihxa_scan eq 0) and (hxa_scan.nPoScan eq 0))

    if (ien gt 0) then ext1hxa, hxa_scan, value(ist:ien), qfull
    ;
    if (qfull) then err_log, 'STR', 'ERR/HXA: Filled buffer with no 4095'
    ;
    ;if (qnew_start or qfull or (hxa_scan.nPoScan eq 0)) then begin
    if (qnew_start or qfull or qstart_mid) then begin
	;Initialize the time if (1) starting a new scan					- qnew_start
	;			    (2) filled buffer					- qfull
	;			    (3) starting extraction in middle of a scan		- (hxa_scan.nPoScan eq 0)
	;
	;if (hxa_scan.nPoScan ne 0) then begin		;only save buffer (increment) if there is some data in buffer
	if (not qstart_mid) then begin			;only save buffer (increment) if there is some data in buffer
	    err_log, 'HXA_SCAN', ss(0), hxa_scan.nPoScan
	    hxa_scan_arr(ihxa_scan) = hxa_scan
	    ihxa_scan = ihxa_scan + 1
	    if (ihxa_scan ge n_elements(hxa_scan_arr)) then begin
		hxa_scan_arr = [hxa_scan_arr, replicate({hxa_scan_rec}, 10)]
	    end
	end
	;
	hxa_scan = {hxa_scan_rec}	;zero out values
	;
	hxa_scan.day		= gen_index.day
	hxa_scan.time		= gen_index.time
    end
    ;
    if (ien lt 62) then ext1hxa, hxa_scan, value(ien+2:*), qfull;do a second extraction if new start in middle of major frame
    hxa_scan_arr(ihxa_scan) = hxa_scan				;save buffer into array
    ;
    for n=0,1 do begin
	ist = n*32
	ien = ist+31
	att.hxa_addr(0:31, n)	= mask(value(ist:ien), 12, 12)

	att.hxa_gain(n)		= rec1(16+49, 32*n+25)
	att.hxa_xnum(n)		= rec1(16+49, 32*n+27)
	att.hxa_ynum(n)		= rec1(16+49, 32*n+29)
    end
end
;
;----- Extract S/C Processed data
;
ss = indgen(2)*32
att.sc_pntg(0,0:1)  = rec1(16+17,ss+8)*long(256*256.) + rec1(16+18,ss+8)*long(256) + rec1(16+19,ss+8)       ;x
att.sc_pntg(1,0:1)  = rec1(16+17,ss+16)*long(256*256.) + rec1(16+18,ss+16)*long(256) + rec1(16+19,ss+16)    ;y
att.sc_pntg(2,0:1)  = rec1(16+17,ss+24)*long(256*256.) + rec1(16+18,ss+24)*long(256) + rec1(16+19,ss+24)    ;z
ss = where(btest0(att.sc_pntg, 23))
if (ss(0) ne -1) then att.sc_pntg(ss) = att.sc_pntg(ss)-2.^24		;sign bit
;
;----- Extract Ground Processed data
;
;*********************** TODO ******************
;
;--- Processed Pointing Information Solution #1 (simple copy of raw IRU data)
;
pntg_angle = lonarr(3)
pntg_angle(0) = total(att.iru(0,*))/8
pntg_angle(1) = total(att.iru(1,*))/8
pntg_angle(2) = total(att.iru(2,*))/8
pntg_trace    = 1	;
pntg_jitter   = 0		;TODO
;
att.pntg_angle	= pntg_angle
att.pntg_trace	= pntg_trace
att.pntg_jitter	= pntg_jitter
att.pntg_dev	= [0,0,0]	;TOOD
att.pntg_motion	= [0,0,0]	;TOOD
att.pntg_status	= 0		;TODO
;
;Store the processed pointing information because SXT and BCS need to access old data.
;
if (natt_pntg eq 0) then att_pntg = replicate(att_pntg_save, 500)	;create a buffer
att_pntg(natt_pntg).time	= gen_index.time
att_pntg(natt_pntg).day		= gen_index.day
att_pntg(natt_pntg).pntg_angle	= pntg_angle
att_pntg(natt_pntg).pntg_trace	= pntg_trace
att_pntg(natt_pntg).pntg_jitter	= pntg_jitter
natt_pntg = natt_pntg + 1
if (natt_pntg ge n_elements(att_pntg)) then att_pntg = [att_pntg, replicate(att_pntg_save, 500)]
;
end
