pro rdhr,fnam,a,ccd_hdr,ex
;
;
;
;  This subroutine reads images from a 160k decom file and places the requested
;  images in caller's array.
;
;  Call:
;	fnam:	name of the decom file (without the .tfr extension)
;	img:	array for image data (will be dimensioned [x,y,nimg])
;       hdr:	array for headers (dimensioned [549,nimg])
;       nimg:	number of images
;       ex:	optional parameter. Used it to get a specific image
;
;  Method:
;	The routine spawn hrdbg, which returns data in a pipe.  The call
;	to hr_dbg specifies the file nam and  the image index
;	(ex in the call to this routine).  Data products are examined, by
;	data product code (DPC). Unknown DPC are discarded. Data is
;	accumulated in a one dimensional array and formatted to 3 
;	dimensions upon completion (file completed or ex acquired).
;
;  NOTE:
;	This routine as currently written is frought with potential
;	problems.  Specifically, DPC's are not required to be of the same
;	type; so, for example one could get several DPC's of one type, 
;	followed by DPC's of another. In such a case, the routine fails when
;	it attempts to reform the data.  It is the responsibility of the
;	caller to get a series of images from a file, only when all those
;	images have the same DPC; otherwise, the caller should use the ex
;	parameters to get specific images.
;
;	
;
;
; This is not a particularly sophisticated routine.  Eventually, one should
; have a repeatively callable function to return the "next" image in a file
; and/or a set of specific data products. 
;
hrdbg_prefix=getenv("MDI_TASK_DIR")
print,fnam
spawn,[hrdbg_prefix+'/hr_dbg','-p',fnam],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 }
ip_sync = 'DBE128BD'XL
dpc_128x128 = 'FFFFFF15'XL	; DPC for a 128 x 128 image
dpc5_4x4 =     'FFFFFF01'XL	; DPC for 4x4 sum
dpc_256 =     'FFFFFF03'XL	; DPC for a 256 x 256 image
dpc_4x4 =     'FFFFFF02'XL	; DPC for a 4 x 4 image
dpc_1x1 =     'FFFFFF04'XL      ; DPC for 1024X1024
dpc_vel =     'FFFFFF40'XL      ; DPC for 1024X1024 Velocity
dpc_sqc =     'FFFFFF41'XL      ; DPC for 1024X1024 SQCOMP (Continuum)
dpc_42 =     'FFFFFF42'XL       ; DPC for i1-i3 raw
dpc_43 =     'FFFFFF43'XL       ; DPC for i2=i4 raw
dpc_44 =     'FFFFFF44'XL       ; DPC for raw continuum
dpc_sqtab =   'FFFFFF70'XL      ; DPC for square table
dpc_rctab =   'FFFFFF71'XL      ; DPC for reciprocal table
dpc_vltab =   'FFFFFF72'XL      ; DPC for vel lookup table
dpc_crtab =   'FFFFFF73'XL      ; DPC for  CROPLIST
dpc_lmtab =   'FFFFFF74'XL      ; DPC for  LIMBLIST
dpc_dsos =    'FFFFFFFF'XL
imno=0
on_ioerror,io_err


nimg = 1
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
cam_temp = lonarr(10)
temp = fltarr(10)

while (imno ne ex) do begin
  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
    print,avail
    writeu,u,avail.block_addr
    ba = avail.block_addr
    if (ba ne 0) then begin
      skip = skip + 1
  
      skbuf = intarr(avail.n_words)
      readu,u,skbuf
    endif
  endwhile
  imno = imno + 1
  if (skip gt 0)  then print,skip,format='("Skipped ",i6," blocks")'
  readu,u,ccd_hdr
endwhile


ip_sync_test = long(ccd_hdr,502)
if ip_sync ne ip_sync_test then dsos_mode = 1
ip_exec_ct = fix(ccd_hdr,510)
ip_src_page = fix(ccd_hdr,512)
ip_cksum = fix(ccd_hdr,554)	; byte addr 554

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 = c_temp - c_temp(1)

extr=0

if dsos_mode eq 1 then begin
   data_product_code = dpc_dsos
   extr=743
endif $
else data_product_code = long(ccd_hdr,506)
print,data_product_code,format='("DPC = ",z10)'
DataErr=0
ItsATable = 0

case data_product_code of
dpc_sqtab: begin
  image_x = 16384
  image_y = 1
  image_words = 16384
  a = intarr(image_words)
  print,"Square Root Compression Table"
  ItsATable = 1
  end
dpc_rctab: begin
  image_x = 32768
  image_y = 1
  image_words = 32768
  a = intarr(image_words)
  ItsATable = 1
  end
dpc_vltab: begin
  image_x = 32768
  image_y = 1
  image_words = 32768
  a = intarr(image_words)
  ItsATable = 1
  end
dpc_crtab: begin
  image_x = 3013
  image_y = 1
  image_words = 3013
  a = intarr(image_words)
  ItsATable = 1
  end
dpc_4x4: begin
  image_x = 256L
  image_y = 256L
  image_words = image_x * image_y
  a = intarr(image_words)	; Buffer for displayable image
  print,'4x4',image_x,image_y  
  end
dpc5_4x4: begin
  image_x = 256L
  image_y = 256L
  image_words = image_x * image_y
  a = intarr(image_words)	; Buffer for displayable image
  end
dpc_256: begin
  image_x = 256L
  image_y = 256L
  image_words = image_x * image_y
  a = intarr(image_words)	; Buffer for displayable image
  end
dpc_vel: begin
  image_x = 1024L
  image_y = 1024L
  image_words = image_x * image_y
  a = intarr(image_words)	; Buffer for displayable image
  end
dpc_sqc: begin
  image_x = 1024L
  image_y = 1024L
  image_words = image_x * image_y
  a = intarr(image_words)	; Buffer for displayable image
  end
dpc_1x1: begin
  image_x = 1024L
  image_y = 1024L
  image_words = image_x * image_y
  a = intarr(image_words)	; Buffer for displayable image
  end
dpc_42: begin
  image_x = 1040L
  image_y = 1033L
  image_words = image_x * image_y
  a = intarr(image_words)	; Buffer for displayable image
  end
dpc_43: begin
  image_x = 1040L
  image_y = 1033L
  image_words = image_x * image_y
  a = intarr(image_words)	; Buffer for displayable image
  end
dpc_44: begin
  image_x = 1040L
  image_y = 1033L
  image_words = image_x * image_y
  a = intarr(image_words)	; Buffer for displayable image
  end
dpc_128x128: begin
  image_x = 128L
  image_y = 128L
  image_words = image_x * image_y
  a = intarr(image_words)	; Buffer for displayable image
  end
dpc_dsos: begin
  image_x = 1040L
  image_y = 1032L
  image_words = image_x * image_y
  a = intarr(image_words+extr)	; Buffer for displayable image
  end
else: begin
  print,"Unknown data product code.  Reading till end of image data or EOF."
  DataErr=1 
  image_words = 1040*1040L
  a=intarr(image_words)
  end
endcase

  repeat begin
        readu,u,avail
        print,block_addr,block_ct,avail
        if avail.block_addr eq 0 then begin
          done = 1
          if DataErr ne 1 then print,"Premature end of Data Product"
          if DataErr eq 1 then a = a(0:block_addr-1)
          goto, finish
        endif
        writeu,u,avail.block_addr	; Tell 'em that we want the block
        b=intarr(avail.n_words) ; 
        readu,u,b		; Read into the block buffer
	if block_addr+avail.n_words lt image_words then a(block_addr) = b $
	else begin
          done = 1
          stop_addr = image_words - block_addr - 1
;         print,stop_addr,image_words,block_addr
          a(block_addr) = b(0:stop_addr)
        endelse

        block_addr = block_addr + avail.n_words
        word_ct = word_ct + avail.n_words
        block_ct = block_ct + 1
  endrep until done eq 1

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

finish: 
if done eq 0 then begin
  if word_ct eq 0 then begin
     print, "No images recovered"
     return
  endif
  print,word_ct
  print,"Partial image recovered"
  return
endif
print,'Image Done'
return
io_err:
print,"IO ERROR"
a=a(0:block_addr-1)
goto, finish
end
