function [xyz,cavg,nph,iif]=arr_geom(geom_file);
%
% Call [xyz,cavg,nph,iif]=arr_geom(geom_file);
%
% Reads gamaray/orca/abx array geometry file and returns xyz positions
% (xyz), average sound speed (cavg), and number of phones (nph).
% 
iif=1;
if exist(geom_file)~=2,
	disp(['USER ERROR: array geometry file ',geom_file,' not found.']);
	xyz=[]; cavg=0; nph=0; iif=0;
	return
end
%
fid_geom=fopen(geom_file);
lgeom=length(geom_file);
%
if strcmp(geom_file(lgeom-4:lgeom),'array') | ...
	strcmp(geom_file(lgeom-3:lgeom),'geom'),
	star_seek(fid_geom);
	[iiarr,count]=fscanf(fid_geom,'%g',1);
	star_seek(fid_geom);
	if iiarr==1,
		[vals,count]=fscanf(fid_geom,'%g',7);
		zr1=vals(1); nxyz=vals(2:4); dxyz=vals(5:7);
		nrec=prod(nxyz);
		xpos=[-(nxyz(1)-1)/2:1:(nxyz(1)-1)/2]*dxyz(1);
		ypos=[-(nxyz(2)-1)/2:1:(nxyz(2)-1)/2]*dxyz(2);
		zpos=zr1 + [0:nxyz(2)-1]*dxyz(3);
		xyz=zeros(nrec,3); jrec=0;
		for jy=1,nxyz(2),
			for jx=1,nxyz(1),
				for jz=1,nxyz(3),
					jrec=jrec + 1;
					xyz(jrec,:)=[xpos(jx),ypos(jy),zpos(jz)];
				end
			end
		end
		star_seek(fid_geom);
		star_seek(fid_geom);
	else
		star_seek(fid_geom);
		[vals,count]=fscanf(fid_geom,'%g');
		nzr=abs(vals(1)); zrec=vals(2:nzr+1);
		if vals(1)<0, zrec(2:nzr)=zrec(1) + zrec(2:nzr); end
		star_seek(fid_geom);
		nrec=0; jval=0;
		for jzr=1:nzr,
			[nxy,count]=fscanf(fid_geom,'%g',[1,1]);
			[xy,count]=fscanf(fid_geom,'%g',[2,nxy]);
			if count<2*nxy, 
				disp('Trouble reading array geometry file in arr_geom.  Make sure commas not used as delimiters...');
				keyboard
			end
			xyz(nrec+1:nrec+nxy,1:3)=[xy',zrec(jzr)*ones(nxy,1)];
			nrec=nrec + nxy;
		end
	end
	star_seek(fid_geom);
	[cavg,count]=fscanf(fid_geom,'%g',1); 
elseif strcmp(geom_file(lgeom-3:lgeom),'.xyz'),
	[xyz,count]=fscanf(fid_geom,'%g',[3,inf]);
	mod3=rem(count,3); nph=floor(count/3);
	if mod3==0,
		cavg=1500; xyz=reshape(xyz,3,nph)';
	elseif mod3==1,
		cavg=xyz(count); xyz=reshape(xyz(1:count-1),3,nph)';
	else
		error(['USER ERROR: .xyz files must have xyz in three columns,', ...
			'then cavg on last line'])
	end	
else,
	disp(['USER ERROR: Array geometry file has unknown suffix: ',geom_file]);
	error('      Suffix must be _array or .xyz');
end
nph=size(xyz,1);
%
%%%
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
