;+
; NAME:
; 	WBS_DB2
; PURPOSE:
;	re-reformat the data section of WBS data into (16,39) integer arrays
; INPUTS:
;	filename (e.g. /yd6/flares/wda911115.0000), time of desired record
;	  (e.g. 15-nov-91 22:37:38)
; OUTPUTS: 
;	header string
;	header integers
;	data in integer array format, (16,39,2)
; HISTORY:
; 	HSH, KO, written May 29, 1992
;	MDM (9-Jun-92) Modified to output sub-frame number (=2*dataset #)
;			Made head_str and and head_int two element arrays
;-
pro wbs_db2,infil,intime,head_str,head_int,wbs_data
;
;	first get data
;
wbs_data = intarr(16,39,2)
rd_roadmap, infil, roadmap, ndset, org
xx = tim2dset(roadmap,intime)
sub_frame = xx*2+1
rd_wda,infil,xx,index,data,roadmap
;	
;	generate header string 
;
rate_arr = ['xx ', 'LOW', 'MED', 'xxx', 'HI ']
time = anytim2ex(index)
time2ints = anytim2ints(index, off=dprate2sec(index)) 
time2 = anytim2ex( time2ints )
head_str = strarr(2)
head_str(0) = string(sub_frame,format='(i5)') + ' ' + strtrim(time(6),1) + '/' + strtrim(time(5),1) + '/' + $
  strtrim(time(4),1) + ' ' + strmid(fmt_tim(time),11,8) + $
  '.' + strmid(string(gt_time(index)), 9,3) + ' ' + gt_dp_mode(index,/short) + $
  '    ' + rate_arr( gt_dp_rate(index))
head_str(1) = string(sub_frame+1,format='(i5)') + ' ' + strtrim(time2(6),1) + '/' + strtrim(time2(5),1) + '/' + $
  strtrim(time2(4),1) + ' ' + strmid(fmt_tim(time2),11,8) + $
  '.' + strmid(string(gt_time(time2ints)),9,3) + ' ' + gt_dp_mode(index,/short) + $
  '    ' + rate_arr( gt_dp_rate(index))
head_int = [sub_frame, sub_frame+1]
;
;	extract and re-reformat data
;
wbs_data(*,0,0) = hxt_decomp(fix(data.pc.hxs_pc1(0:15)))
wbs_data(*,0,1) = hxt_decomp(fix(data.pc.hxs_pc1(16:31)))
wbs_data(*,1,0) = hxt_decomp(fix(data.pc.hxs_pc2(0:15)))
wbs_data(*,1,1) = hxt_decomp(fix(data.pc.hxs_pc2(16:31)))

for i = 2,3 do begin
  ia = 16*(i-2) & ib = ia + 15
  wbs_data(*,i,0) = hxt_decomp(fix(data.ph.hxs_ph(ia:ib,0)))
  wbs_data(*,i,1) = hxt_decomp(fix(data.ph.hxs_ph(ia:ib,2)))
endfor

for i = 4,5 do begin
  ia = 16*(i-4) & ib = ia + 15
  wbs_data(*,i,0) = hxt_decomp(fix(data.ph.hxs_ph(ia:ib,1)))
  wbs_data(*,i,1) = hxt_decomp(fix(data.ph.hxs_ph(ia:ib,3)))
endfor

for j = 0,7 do begin
  wbs_data(2*j,6,0) = data.pc.sxs_pc11(j)/256
  wbs_data(2*j+1,6,0) = data.pc.sxs_pc11(j) mod 256
  wbs_data(2*j,6,1) = data.pc.sxs_pc11(j+8)/256
  wbs_data(2*j+1,6,1) = data.pc.sxs_pc11(j+8) mod 256
endfor
    
for j = 0,7 do begin
  wbs_data(2*j,7,0) = data.pc.sxs_pc12(j)/256
  wbs_data(2*j+1,7,0) = data.pc.sxs_pc12(j) mod 256
  wbs_data(2*j,7,1) = data.pc.sxs_pc12(j+8)/256
  wbs_data(2*j+1,7,1) = data.pc.sxs_pc12(j+8) mod 256
endfor

for j = 0,7 do begin
  wbs_data(2*j,8,0) = data.pc.sxs_pc21(j)/256
  wbs_data(2*j+1,8,0) = data.pc.sxs_pc21(j) mod 256
  wbs_data(2*j,8,1) = data.pc.sxs_pc21(j+8)/256
  wbs_data(2*j+1,8,1) = data.pc.sxs_pc21(j+8) mod 256
endfor

for j = 0,7 do begin
  wbs_data(2*j,9,0) = data.pc.sxs_pc22(j)/256
  wbs_data(2*j+1,9,0) = data.pc.sxs_pc22(j) mod 256
  wbs_data(2*j,9,1) = data.pc.sxs_pc22(j+8)/256
  wbs_data(2*j+1,9,1) = data.pc.sxs_pc22(j+8) mod 256
endfor

for i = 10,17 do begin
  ia = 16*(i-10) & ib = ia + 15
  wbs_data(*,i,0) = hxt_decomp(fix(data.ph.sxs_ph1(ia:ib,0)))
  wbs_data(*,i,1) = hxt_decomp(fix(data.ph.sxs_ph1(ia:ib,1)))
endfor

for i = 18,25 do begin
  ia = 16*(i-18) & ib = ia + 15
  wbs_data(*,i,0) = hxt_decomp(fix(data.ph.sxs_ph2(ia:ib,0)))
  wbs_data(*,i,1) = hxt_decomp(fix(data.ph.sxs_ph2(ia:ib,1)))
endfor

for j = 0,7 do wbs_data(j,26,0) = hxt_decomp(data.pc.grs_pc11(j))
for j = 0,7 do wbs_data(j+8,26,0) = hxt_decomp(data.pc.grs_pc12(j))
for j = 0,7 do wbs_data(j,27,0) = hxt_decomp(data.pc.grs_pc21(j))
for j = 0,7 do wbs_data(j+8,27,0) = hxt_decomp(data.pc.grs_pc22(j))

for j = 0,7 do wbs_data(j,26,1) = hxt_decomp(data.pc.grs_pc11(j+8))
for j = 0,7 do wbs_data(j+8,26,1) = hxt_decomp(data.pc.grs_pc12(j+8))
for j = 0,7 do wbs_data(j,27,1) = hxt_decomp(data.pc.grs_pc21(j+8))
for j = 0,7 do wbs_data(j+8,27,1) = hxt_decomp(data.pc.grs_pc22(j+8))

for j = 0,3 do begin
  wbs_data(j,28,0) = hxt_decomp(data.pc.grs_pc13(j))
  wbs_data(j,28,1) = hxt_decomp(data.pc.grs_pc13(j+4))
  wbs_data(j+4,28,0) = hxt_decomp(data.pc.grs_pc14(j))
  wbs_data(j+4,28,1) = hxt_decomp(data.pc.grs_pc14(j+4))
  wbs_data(j+8,28,0) = hxt_decomp(data.pc.grs_pc15(j))
  wbs_data(j+8,28,1) = hxt_decomp(data.pc.grs_pc15(j+4))
  wbs_data(j+12,28,0) = hxt_decomp(data.pc.grs_pc16(j))
  wbs_data(j+12,28,1) = hxt_decomp(data.pc.grs_pc16(j+4))
endfor

for j = 0,3 do begin
  wbs_data(j,29,0) = hxt_decomp(data.pc.grs_pc23(j))
  wbs_data(j,29,1) = hxt_decomp(data.pc.grs_pc23(j+4))
  wbs_data(j+4,29,0) = hxt_decomp(data.pc.grs_pc24(j))
  wbs_data(j+4,29,1) = hxt_decomp(data.pc.grs_pc24(j+4))
  wbs_data(j+8,29,0) = hxt_decomp(data.pc.grs_pc25(j))
  wbs_data(j+8,29,1) = hxt_decomp(data.pc.grs_pc25(j+4))
  wbs_data(j+12,29,0) = hxt_decomp(data.pc.grs_pc26(j))
  wbs_data(j+12,29,1) = hxt_decomp(data.pc.grs_pc26(j+4))
endfor

for i = 30,33 do begin
  ia = 16*(i-30) & ib = ia + 15
  ic = ia + 64   & id = ic + 15
  wbs_data(*,i,0) = hxt_decomp(fix(data.ph.grs_phl1(ia:ib)))
  wbs_data(*,i,1) = hxt_decomp(fix(data.ph.grs_phl1(ic:id)))
endfor

for i = 34,37 do begin
  ia = 16*(i-34) & ib = ia + 15
  ic = ia + 64   & id = ic + 15
  wbs_data(*,i,0) = hxt_decomp(fix(data.ph.grs_phl2(ia:ib)))
  wbs_data(*,i,1) = hxt_decomp(fix(data.ph.grs_phl2(ic:id)))
endfor
  
wbs_data(0:7,38,0) = hxt_decomp(fix(data.ph.grs_phh1(0:7)))
wbs_data(8:15,38,0) = hxt_decomp(fix(data.ph.grs_phh2(0:7)))
wbs_data(0:7,38,1) = hxt_decomp(fix(data.ph.grs_phh1(8:15)))
wbs_data(8:15,38,1) = hxt_decomp(fix(data.ph.grs_phh2(8:15)))

end

