; LT_EVAL3 takes the first image to be a dark frame. This is subtracted
; all subsequent frames.

pro get_image,win_idx,xbeg,ybeg
task = getenv("MDI_TASK_DIR") + '/mdi_sci'
spawn,[task,'-s 8','-x','-p'],$
unit=u,/noshell
ccd_hdr = intarr(549)	; Buffer for CCD header block read
b = intarr(549)		; Buffer for data block read
avail = { block_addr : 0L, n_words : 0L }
string1='RT  MDI  160K  Image'
xwp = [16,16,16]
ywp = [704,384,16]
loadct,3
loops=0
!order = 1	; display from top down
ip_sync = 'DBE128BD'XL
dpc_128x128 = 'FFFFFF15'XL	; Data product code for a 128 x 128 image
dpc5_4x4 =     'FFFFFF01'XL	; Data product code for a 4 x 4 summed image
dpc_256 =     'FFFFFF03'XL	; Data product code for a 256 x 256 image
dpc_4x4 =     'FFFFFF02'XL	; Data product code for a 4 x 4 image
dpc_1x1 =     'FFFFFF04'XL      ; Data Product Code for 1024X1024

for i=0,2 do begin
  window,i,title=string1,xsize=420,ysize=256,retain=1,xpos=xwp(i),ypos=ywp(i)
  wshow,i,0
endfor
window,3,title="Limb Tracker Performance Plots",xpos=640,ypos=256
;wshow,5,0
window,6,title="Temperatures",xsize=360,ysize=110,retain=1
wshow,6

cam_temp = lonarr(10)
temp = fltarr(10)

prevdp=0
newdp=1
rptnum = 0
while ( 1 eq 1) do begin
done = 0
block_ct = 0L
word_ct = 0L
block_addr = 0L
stop_addr = 0L
idx = 0L
temp = 0
dsos_mode = 0
block_size = 549L
data_product_code = 0L
n_full_blocks = 0L
image_words = 0L
part_block_words = 0L
img_size = 0
ip_sync_test = 0L
ip_exec_ct = 0
ip_src_page = 0L
ip_cksum = 0L

ba = 999
skip = 0
while (ba ne 0) do begin
  if ((skip gt 0) and (skip lt 2)) then print,"Skipping blocks"
  readu,u,avail
  ba = avail.block_addr
  if (ba ne 0) then begin
    skip = skip + 1
    writeu,u,avail.block_addr
    skbuf = intarr(avail.n_words)
    readu,u,skbuf
  endif
endwhile
if (skip gt 0)  then print,skip,format='("Skipped ",i6," blocks")'
;print,"*********************************************************************"
print,"Assembling an image"
print,avail.block_addr,format ='("Available Block addr read is ",z10)'
;print,"Available n_words read is ",avail.n_words
writeu,u,avail.block_addr		;Tell 'em that we want the header block
print,"Reading Header..."
readu,u,ccd_hdr

ip_sync_test = long(ccd_hdr,502)
print,"ip_sync_test ",ip_sync_test
if ip_sync ne ip_sync_test then dsos_mode = 1
print,"dsos_mode ",dsos_mode
if dsos_mode eq 1 then begin
;  idx = 0
;  last_block = 118L
end
ip_exec_ct = fix(ccd_hdr,510)
ip_src_page = fix(ccd_hdr,512)
ip_cksum = fix(ccd_hdr,554)	; byte addr 554
;print,"ip_exec_ct: ",ip_exec_ct
;print,"ip_src_page: ",ip_src_page
;print,"ip_cksum: ",ip_cksum
;print,format='("ip_exec_ct: ",Z6)',ip_exec_ct
;print,format='("ip_src_page: ",Z6)',ip_src_page
;print,format='("ip_cksum: ",Z8)',ip_cksum

for i = 0,9 do begin
    cam_temp(i) = fix(ccd_hdr,514+i*4)
endfor
c_temp = ((cam_temp - 108.0)/4.0) - 150.0
;c_temp = ((cam_temp - cam_temp(1))/4.0) - 150.0
c_temp = c_temp - c_temp(1)

data_product_code = long(ccd_hdr,506)
print,data_product_code,format='("DPC = ",z10)'
DataErr=0
win_idx = loops mod 3
if (data_product_code eq prevdp) then newdp = 0 else newdp = 1
prevdp = data_product_code

case data_product_code of
dpc5_4x4: begin
  string0='single 4x4'
  image_size = 256L
  image_words = image_size * image_size
  a = intarr(65536)	; Buffer for displayable image
  imgscl = 1.00
  rpos = .35
  loops = loops + 1
  end
dpc_256: begin
  string0='256'
  image_size = 256L
  image_words = image_size * image_size
  a = intarr(65536)	; Buffer for displayable image
  rpos = .35
  imgscl = 1.00
  loops = loops + 1
  end
dpc_4x4: begin
  string0='4x4'
  image_size = 256L
  image_words = image_size * image_size
  a = intarr(65536)	; Buffer for displayable image
  rpos = .35
  imgscl = 1.00
  loops = loops + 1
  end
dpc_128x128: begin
  string0='128x128'
  image_size = 128L
  image_words = image_size * image_size
  a = intarr(16384)	; Buffer for displayable image
  rpos = .35
  imgscl = 1.00
  loops = loops + 1
  end
else: begin
  print,"Unknown data product code"
  DataErr=1 
  end
endcase
if (DataErr eq 0) then begin
  print,string0
  n_full_blocks = image_words / block_size
  part_block_words = image_words - (n_full_blocks * block_size)
  ;print,"Number of full blocks to read: ",n_full_blocks
  ;print,"Number of words to read on last block: ",part_block_words

  print,"Reading DATA BLOCKS..."
    repeat begin
    ;print,"*********************************************************************"
    readu,u,avail
    ;print,"Available Block addr read is ",avail.block_addr
    ;print,"Available n_words read is ",avail.n_words
    if block_ct lt n_full_blocks then begin
      writeu,u,avail.block_addr	; Tell 'em that we want the block
      ;print,"ACKNOWLEDGING Block addr available is ",avail.block_addr
      readu,u,b		; Read into the block buffer
   ;print,"storing at block_addr ",block_addr
      a(block_addr) = b
      block_addr = block_addr + avail.n_words
    end else begin
      if part_block_words eq 0 then begin
        done = 1
      end else begin
        writeu,u,avail.block_addr	; Tell 'em that we want the block
        ;print,"ACKNOWLEDGING Block addr available is ",avail.block_addr
        ;print,"Partial block, # of words saved: ", part_block_words
        done = 1
        stop_addr = block_addr + part_block_words - 1
        readu,u,b
          a(block_addr) = b(0:part_block_words-1)
      endelse
    endelse
    if done eq 1 then begin
      ;print,"final read #: ",word_ct
    ;  readu,u,avail
    ;  avail.block_addr = -1L
    ;  writeu,u,avail.block_addri
 		; Tell 'em that we are done reading 
    endif
    word_ct = word_ct + avail.n_words
    block_ct = block_ct + 1
    ;print,"word #: ",word_ct
    ;print,"block #: ",block_ct
  endrep until done eq 1
  print,"DONE, block_addr ",block_addr

  if dsos_mode eq 1 then begin
    for block_addr=0L,118L do begin
      a(block_addr) = b(block_addr)
    endfor
  endif

  wset, 6
  erase
  wshow, 6
  t0_str = string (c_temp(0), "(' Ref = ', f7.2)")
  t1_str = string (c_temp(1), "('  T0 = ', f7.2)")
  t2_str = string (c_temp(2), "(' ADC = ', f7.2)")
  t3_str = string (c_temp(3), "('SCBd = ', f7.2)")
  t4_str = string (c_temp(4), "('VReg = ', f7.2)")
  t5_str = string (c_temp(5), "('Drvr = ', f7.2)")
  t6_str = string (c_temp(6), "('PROM = ', f7.2)")
  t7_str = string (c_temp(7), "('CCDA = ', f7.2)")
  t8_str = string (c_temp(8), "('CCDB = ', f7.2)")
  t9_str = string (c_temp(9), "('Case = ', f7.2)")
  !fancy = 0
  xyouts,   0, 90, t0_str, /device
  xyouts, 180, 90, t1_str, /device
  xyouts,   0, 70, t2_str, /device
  xyouts, 180, 70, t3_str, /device
  xyouts,   0, 50, t4_str, /device
  xyouts, 180, 50, t5_str, /device
  xyouts,   0, 30, t6_str, /device
  xyouts, 180, 30, t9_str, /device
  xyouts,   0, 10, t7_str, /device
  xyouts, 180, 10, t8_str, /device
  !fancy = 1

  img = reform(a,image_size,image_size,/overwrite)
  if (rptnum eq 0) then begin
      drk = img
      img = img + drk
  endif
  img = img - drk
  wset,3
  wshow,3
  plotgrid,img,xbeg,ybeg,ltx,lty
  if (newdp eq 0) then sdev2str = string(stdev(previmg-img),'(f6.2)')
  previmg = img 
  string2=string(ip_exec_ct,'(Z6)')
  string3=strmid(string2,strlen(string2)-4,4)
  
  string4=string(ip_src_page,'(Z6)')
  string5=strmid(string4,strlen(string4)-4,4)
  
  string6=string(ip_cksum,'(Z8)')
  string7=strmid(string6,strlen(string6)-4,4)
  
  string11='IMAGE TYPE:  '+string0
  string8= 'EXEC COUNT: '+string3
  string9= 'SRC PAGE  : '+string5
  string10='IP_CKSUM  : '+string7
  fancy=1
  string12=string(min(img),'(i6)')
  string13=string(max(img),'(i6)')
  sdev=stdev(img,mean)
  string14=string(mean,'(f7.2)')
  string15=string(sdev,'(f6.2)')
  pos0 = .90
  delpos = .10/imgscl
  pos = pos0
  wset,win_idx
  erase
  wshow,win_idx
  xyouts,.0,pos, "Type: ",/normal,/noclip
  xyouts,rpos,pos,string0,alignment=1.0,/normal,/noclip
  pos=pos-delpos
  xyouts,0.0,pos,"Count: ",/normal,/noclip
  xyouts,rpos,pos,string3,alignment=1.0,/normal,/noclip
  pos=pos-delpos
  xyouts,0.0,pos,"Page: ",/normal,/noclip
  xyouts,rpos,pos,string5,alignment=1.0,/normal,/noclip
  pos=pos-delpos
  xyouts,0.0,pos,"Min: ",/normal,/noclip
  xyouts,rpos,pos,string12,alignment=1.0,/normal,/noclip
  pos=pos-delpos
  xyouts,.0,pos,"Max: ",/normal,/noclip
  xyouts,rpos,pos,string13,alignment=1.0,/normal,/noclip
  pos=pos-delpos
  xyouts,.0,pos,"Mean: ",/normal,/noclip
  xyouts,rpos,pos,string14,alignment=1.0,/normal,/noclip
  pos=pos-delpos
  xyouts,.0,pos,"SDev-I: ",/normal,/noclip
  xyouts,rpos,pos,string15,alignment=1.0,/normal,/noclip
  if (newdp eq 0) then begin
    pos=pos-delpos
    xyouts,.0,pos,"SDev-D: ",/normal,/noclip
    xyouts,rpos,pos,sdev2str,alignment=1.0,/normal,/noclip
  endif
  tvscl,img,164,0
  rptnum = 1
  endif
end
return
end

pro get_hr,xbeg,ybeg
print, format='("*** First Frame must be a dark Frame ***")' 
i = 0
while(1) do begin
  get_image,i,xbeg,ybeg mod 3
  i = i + 1
end
return
end
