; get_hr
pro get_image,win_idx
;cmd = '/usr/people/rogerc/dev/sccs/mdi_sci -s 9 -x -p'
;spawn,cmd,unit=u
spawn,['/usr/people/rogerc/dev/sccs/mdi_sci','-s 4','-x','-p'],$
unit=u,/noshell
ccd_hdr = intarr(549)	; Buffer for CCD header block read
b = intarr(549)		; Buffer for data block read
a = intarr(65536)	; Buffer for displayable image
avail = { block_addr : 0L, n_words : 0L }
string1='RT  MDI  160K  Image'
xwp = [0,320,640,160,480]
ywp = [400,400,400,0,0]
for i=0,4 do begin
  window,i,title=string1,xsize=350,ysize=350,retain=1,xpos=xwp(i),ypos=ywp(i)
endfor
for loops=0,10000 do begin
win_idx = loops mod 5
wset,win_idx
erase
;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
dpc_128x128 = 'FFFFFF00'XL	; Data product code for a 128 x 128 image
dpc_4x4 =     'FFFFFF01'XL	; Data product code for a 4 x 4 image
img_size = 0
ip_sync = 'DBE128BD'XL
ip_sync_test = 0L
ip_exec_ct = 0
ip_src_page = 0L
ip_cksum = 0L

readu,u,avail
;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

data_product_code = long(ccd_hdr,506)
print,"data_product_code: ",data_product_code
case data_product_code of
dpc_4x4: begin
  string0='4x4'
  image_size = 256L
  image_words = image_size * image_size
  end
dpc_128x128: begin
  string0='128x128'
  image_size = 128L
  image_words = image_size * image_size
  end
else: begin
  print,"Unknown data product code"
  exit
  end
endcase
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_addr 		; 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

img = reform(a,image_size,image_size,/overwrite)
get_lun,iout
outname = 'img'+string(win_idx)
openw,iout,outname
writeu,iout,img
close,iout
free_lun,iout
;free_lun,u
; Color tables: 1 = BLUE/WHITE
;               2 = GRN-RED-BLU-WHT
;               3 = RED TEMPERATURE
;               4 = BLUE/GREEN/RED/YELLOW
;               5 = STD GAMMA-II
;               6 = PRISM
;               7 = RED-PURPLE
;               8 = GREEN/WHITE LINEAR
;               9 = GRN/WHT EXPONENTIAL
;              10 = GREEN-PINK
;              11 = BLUE-RED
loadct,3
!order = 1	; display from top down
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=3
string12='Extrema: '+string(min(img),'(i6)')+string(max(img),'(i6)')
xyouts,0.12,.95, string11,/normal,/noclip
xyouts,.12,.91, string8,/normal,/noclip
xyouts,.12,.87, string9,/normal,/noclip
xyouts,.12,.83, string10,/normal,/noclip
xyouts,.12,.79, string12,/normal,/noclip
tvscl,img
end
return
end

pro get_hr
i = 0
while(1) do begin
  get_image,i mod 5
  i = i + 1
end
return
end
