; get_image.pro
get_lun,u
cmd = '/usr/people/rogerc/dev/sccs/mdi_sci -s 4 -x -p'
spawn,cmd,unit=u
;spawn,['/usr/people/rogerc/dev/sccs/mdi_sci',' -s4',' -x',' -p'],$
;unit=u,/noshell
a = intarr(1064960)	; Buffer for displayable image
h1 = intarr(549)	; Buffer for CCD header block read
h2 = intarr(549)	; Buffer for CCD header block read
b = intarr(549)		; Buffer for non-header block reads
avail = { block_addr : 0L, n_words : 0L }
got_header = 0
done = 0
block_ct = 0L
word_ct = 0L
block_addr = 0L
stop_addr = 0L
idx = 0L
temp = 0
last_block = 1939L
raw_data = 1
ip_sync = 'DBE128BD'XL
ip_sync_test = 0L

readu,u,avail
print,"Assemblimg an image"
print,"Available Block addr read is ",avail.block_addr
print,"Available n_words read is ",avail.n_words
writeu,u,avail.block_addr	;Tell 'em that we want the block
readu,u,h1

readu,u,avail
writeu,u,avail.block_addr	; Tell 'em that we want the block
readu,u,h2

ip_sync_test = (h1(252) * 65536) + h1(253)
if ip_sync eq ip_sync_test then raw_data = 0
;print,"raw_data ",raw_data
if raw_data eq 1 then begin
  idx = 97
  for block_addr=0L,451L do begin
    a(block_addr) = h2(idx)
    idx = idx + 1
  endfor
  last_block = 1938L
end else begin
  idx = 520
  for block_addr=0L,28 do begin
    a(block_addr) = h2(idx)
    idx = idx + 1
  endfor
endelse

repeat begin
  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 last_block then begin
    writeu,u,avail.block_addr; Tell 'em that we want the block
    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
     writeu,u,avail.block_addr; Tell 'em that we want the block
     done = 1
     stop_addr = block_addr + 449 - 1
     for idx=block_addr,stop_addr do begin
       readu,u,temp		; Read into the block buffer
       a(idx) = temp
     endfor
     avail.block_addr = -1L
     writeu,u,avail.block_addr	; Tell 'em that we are done reading 
   endelse
  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
;print,"final read #: ",word_ct

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

img = reform(a,1040,1024,/overwrite)
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,1
window,title='M D I   R e a l   T i m e   1 6 0 K   I m a g e',$
xsize=1024,ysize=1024,retain=2
!order = 1	; display from top down
tvscl,img(0:1023,*)
;cursor,x,y,/normal,/down	; use mouse button to place text
;xyouts,x,y, 'File: 930330_223414_03.raw',/normal,/noclip
;xyouts, 'File: 930330_223414_03.raw'
end
