; $Id: darread.pro,v 1.2 2002/01/14 20:59:26 amunter Exp $
PRO darread,filen,isnum,aux
;;
;; read the '.dar' files for area detector
;;

COMMON SEARP,sear,ddmask

;print,filen
darf=strmid(filen,strlen(filen)-12,12)
darf='/mnt/tas6/data/karen/'+darf
flen=strlen(darf)
snum=strtrim(string(fix(isnum)),2)
slen=strlen(snum)
darf=strmid(darf,0,flen-4-slen)
darf=darf+snum+'.dar'
print,'Reading file '+darf+'...'
on_ioerror,bad
openr,unit,darf,/get_lun
junk=''
fir=''
;; the headers
readf,unit,junk
fir=readword(junk,0)
while fir ne '#siz' do begin
    readf,unit,junk
    fir=readword(junk,0)
endwhile

ddx=0
ddy=0
ddx=readword(junk,1)
ddy=readword(junk,3)
    
    

dd=intarr(ddx,ddy)
readf,unit,dd
;tvscl,dd,/normal
;read,dls
dd=transpose(dd)
if (ddx ne 128) or (ddy ne 128) then begin
    print,'Ajusting mask...'
    expand,ddmask,ddx,ddy,ddma
    dd(*,*)=dd(*,*)*ddma
endif else dd(*,*)=dd(*,*)*ddmask(*,*)
aux=total(dd)


;tvscl,dd,/normal
;read,dls
goto,done

bad:
aux=0
print,"Cann't read array file: ",darf
done:
free_lun,unit
on_ioerror,null
end
