FUNCTION f1,name,ho
;
; Procedure Ford
; taken from Stu to make it easy to obtain CCD files of Kodak megaplus
; 8000 A during the La Palma June 1997 observations
; 
;         x = f1('filename',h)  for instance
;         then x = data array, file name is in single quotes, h = header (256
;	  bytes)
;
; Version for NON-COMPRESSED data, adapted by Michal, 25.6.1997

;check if reading on little endian machine
lendian=0
if (!version.os eq 'OSF') then lendian=1

openr,10,name 
r=assoc(10,bytarr(512))
hdr=r(0)        ;get ho (original header) to check
if (hdr(0) ne 170) or (hdr(1) ne 170) or (hdr(2) ne 85) or (hdr(3) ne 85) $
  then begin
  print,'not an F0 file'
  return,0
endif
close,10

; The F0 type found
;select various header bytes
datyp=hdr(7)
ndim=hdr(8)
subf = hdr(4)

;sflag tests if either data or machine is little endian
sflag = lendian xor (subf lt 128)

;  dflag tests if data is compressed
dflag = subf mod 128 gt 0

nhd=hdr(6)	;number of header records (make sure we get all
		;of them)
print,'original nhd =', nhd

;------------- skipped -------------------------------------------
;  if compressed data, decrunch it
;  if dflag then begin
;  str='~/decrunch/decrunch '+name+' ~/decrunch/temp/decrunch_temp'
;  spawn,str
;  str2='chmod ugo+rw  ~/decrunch/temp/decrunch_temp'
;  spawn,str2
;  openr,10,' ~/decrunch/temp/decrunch_temp'
;  re-read the header if decrunched
;  r=assoc(10,bytarr(512))
;  hdr=r(0)        ;get ho (original header) to check
;  nhd=hdr(6)	;number of header records, may have changed!
;  print,'decrunched nhd =', nhd
;  endif else begin
;----------------------------------------------------------------

openr,10,name
;  endelse

;get entire header string, even if multiple header blocks
r=assoc(10,bytarr(512*nhd))
hdr=r(0)
ho=hdr(256:*)

; Get dimensions, only need first header block here
r=assoc(10,intarr(256))
d=r(0)

;some intermediate steps for swapping bytes and words
byteorder,d
ie=indgen(128)
io=ie+1
c=intarr(256)
c(ie)=d(io)
c(io)=d(ie)
if lendian then byteorder,/lswap,c
d=long(c,0,128)
dims=d(48:48+ndim-1)
print,'dims=',dims

; Print header
p=0
while (ho(p) ne 0) do begin
    n=1
    while (ho(p+n) ne 0) and (ho(p+n) ne 13) and (n lt 80) do n=n+1
    print,"$(' * ',"+string(n)+"A)",string(ho(p:p+n-1))
    p=p+n + (ho(p+n) eq 13)
end

size=1
for i=0,ndim-1 do size=size*dims(i)

; Now case on variable data type
case datyp of
0:	begin
	nfh=512*nhd
	r=assoc(10,bytarr(size+nfh))
	a=r(0)
	a=a(nfh:*)
	end
1:	begin
	nfh=256*nhd
	r=assoc(10,intarr(size+nfh))
	a=r(0)
	a=a(nfh:*)
        if (sflag eq 1 and dflag eq 0) then byteorder,a
	end
2:	begin
	nfh=128*nhd
	r=assoc(10,lonarr(size+nfh))
	a=r(0)
	a=a(nfh:*)
	if (sflag eq 1 and dflag eq 0) then byteorder,/lswap,a
	end
3:	begin
	nfh=128*nhd
	r=assoc(10,fltarr(size+nfh))
	a=r(0)
	a=a(128:*)
        if sflag eq 1 then byteorder,/lswap,a
	end
endcase
x=reform(a,dims)
close,10
;if dflag then spawn,'rm ~/decrunch/temp/decrunch_temp'
return,x
end