function adi_array_make(array_root,xyz,cavg,fid_app)
%
% Call adi_array_make(array_root,xyz,cavg,fid_app);
%
% Creates array geometry file with suffix _array using [x,y,z] positions
% in xyz and average sound speed cavg;
% fid_app = -1 to use array_root as path name, 0 to create array file in NAV directory, 
%           fid of opened output file to append to that file (without writing cavg).
%
% See also adi_array_rot to rotate or prune an array.
%
% Routines that work with array geometry files include:
% adi_array_make, adi_array_plot, adi_array_rot, adi_array_find, adi_array_geom;
%
%
% 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.

if nargin<3, cavg=1500, end
if nargin<4, fid_app=0; end
%
if fid_app<0,
	geom_file=[array_root,'_array'],
	fid=fopen(geom_file,'w'),
elseif fid_app==0, 
    nav_dir=[getevar('home'),'\inp\arrays\']; 
    if ~isdir(nav_dir), nav_dir=adi_dir_find('NAV',0); end
	geom_file=[nav_dir,array_root,'_array'],
	fid=fopen(geom_file,'w'),
else,
	fid=fid_app;
end
%
[nrec0,ncol]=size(xyz);
if ncol~=3, error('USER ERROR: xyz must have three columns'); end
%
[zrec,ii]=unique(xyz(:,3)); nrec=length(zrec); zrec_u=xyz(sort(ii),3);
fprintf(fid,'*(10) RECEIVER ARRAY SPECIFICATION: IIARR (1=uniform, 2=nonuniform)\n');
fprintf(fid,'2\n');
fprintf(fid,'!!! NOTE: POS z POINTS UP (OPPOSITE OF GAMARAY INPUT FILE) !!!\n');
fprintf(fid,'IIARR=1: uniformly spaced receiver array\n');
fprintf(fid,'*(11) zr1  nx  ny  nz   dx    dy    dz  [dx,dy,dz are element spacings]\n');
fprintf(fid,'     500.  20   1   1   10.   20.   20.\n');
fprintf(fid,'IIARR=2: receiver array depths. nzr>0 ==> give zr1,zr2,... ;\n');
fprintf(fid,'   nzr<0 ==> give zr1,dz2,dz3... (dz are element spacings, pos down)\n');
fprintf(fid,'*(12) nzr   zr1  zr2(dz2) zr3(dz3) ...\n');
fprintf(fid,'%1i\n',nrec);
fprintf(fid,'%3.3f %3.3f %3.3f %3.3f %3.3f %3.3f %3.3f %3.3f %3.3f \n',zrec_u');
fprintf(fid,'\n');
fprintf(fid,'IIARR=2: specify (x,y) receiver positions for each of the nzr depths:\n');
fprintf(fid,'*(13) nxy  x(1),y(1)  x(2),y(2) ... x(nxy),y(nxy) [nzr lines like this]\n');
for jrec=1:nrec0,
    zz=xyz(jrec,3);
    if ~any(zz==xyz(1:jrec-1,3)),
        iiz=find(xyz(:,3)==xyz(jrec,3)); nxy=length(iiz);
        fprintf(fid,'%1i\n',nxy);
        fprintf(fid,'%8.2f %8.2f\n',[xyz(iiz,1:2)]');
%        fprintf(fid,'\n');
    end
end
%
if fid_app<=0,
	fprintf(fid,'*(14) Average sound speed\n');
	fprintf(fid,sprintf('%6.2f\n',cavg));
	fclose(fid);
	disp(['Created array geometry file ',geom_file]);
    eval(['type ',geom_file]);
end
