FUNCTION generate_r_matrix,filename,nx,ny,ctrx,ctry
; 
; SCCS variables for IDL use
; 
; @(#)generate_r_matrix.pro	1.1 05/14/97 :NRL Solar Physics
;

nx_file=0
ny_file=0
ctrx_file=0
ctry_file=0

junk=findfile(filename,count=count) 	;Determine if the file exists
if (count gt 0) then begin	;The file exists
	r=readfits(filename)
	h=headfits(filename)
	nx_file=sxpar(h,'NX')	;Get the characteristics of the array
	ny_file=sxpar(h,'NY')
	ctrx_file=sxpar(h,'CTRX')
	ctry_file=sxpar(h,'CTRY')
	endif
if ((count eq 0) OR $		;If file doesn't exist
	(nx_file ne nx) OR $	;Or there are differences
	(ny_file ne ny) OR $
	(ctrx_file ne ctrx) OR $
	(ctry_file ne ctry)) then begin 	;Make a new array and file
		print,'generating r matrix...'
		r=fltarr(nx,ny)
		help,r
		for i=0,nx-1 do begin
			for j=0,ny-1 do begin
				r(i,j)=sqrt((float(i-ctrx))^2+(float(j-ctry))^2)
				endfor;j
			endfor;i
		print,'min(r)=',min(r)
		print,'max(r)=',max(r)
	;###CREATE A NEW HEADER###
	sxaddpar,hnew,'SIMPLE','T'
	sxaddpar,hnew,'NAXIS',2
	sxaddpar,hnew,'NX',nx
	sxaddpar,hnew,'NY',ny
	sxaddpar,hnew,'CTRX',ctrx
	sxaddpar,hnew,'CTRY',ctry

	;###WRITE A NEW FILE###
	writefits,filename,r,hnew

	endif $
	
else print,'R matrix already exists so skipping generation step...'
return,r
END; FUNCTION
