% Compute the range independent modal (rim) acoustic field using the 
% Legendre-Galerkin method: Edit the generic input file rimLG.dat;
% Read the imput file, run rimLG and plot transmission loss. 
% Calls read_rimLG, rimLG, and tl_plot.
%
% See rimLG_readme.txt for user instructions. See Legendre_Galerkin.pdf 
% for documentation on the computational method.
% (R. B. Evans, N. Stonington, CT, Nov. 22, 2015)
%
clear
reply_edit='Y';
display('Starts by opening rimLG.dat in the editor');
display('Press any key to continue');
pause
while ((reply_edit == 'Y')||(reply_edit == 'y'))
% Edit the data file
edit 'rimLG.dat'
display('Make changes to rimLG.dat, if any.');
display('Save rimLG.dat for the desired calculation.');
display('Press any key to continue');
pause
% Confirm that any required changes have been saved
reply_save = input('Have any required changes been saved? y/n :','s');
   if ((reply_save == 'Y')||(reply_save == 'y'))
       reply_edit='N';
   end
end
% Give option to quit
reply_run = input('Do you want to run rimLG? y/n :','s');
if ((reply_run == 'Y')||(reply_run == 'y')) % Proceed with calculation
%
% Define text data file
data_file='rimLG.dat';
% Read the data file
[case_name,FREQ,ZS,NL,NDEP,ZMIN,ZINC,RMIN,RMAX,RINC,HW,HB,...
 depw,svpw,dbpwlw,rhow,NW,depb,svpb,dbpwlb,rhob,NB]= ...
                                         read_rimLG(data_file);
% Compute complex pressure on the depth-range grid
display('Calculation started.');
[zr,rkm,cpr,NRNG]= ...
    rimLG(FREQ,ZS,NL,NDEP,ZMIN,ZINC,RMIN,RMAX,RINC, ...
    HW,HB,depw,svpw,dbpwlw,rhow,NW,depb,svpb,dbpwlb,rhob,NB);
% Define text transmission loss file
tl_file='rimLG.tl';
tl_fid = fopen(tl_file,'wt');
fprintf(tl_fid,'%s\n',case_name);
today11=date;
today=[today11(4:6) today11(1:2) '_' today11(8:11)];
NRNG0=0;
head=[FREQ ZS NRNG0 NDEP];
% Write header (spaces make spaces) and receiver depths
fprintf(tl_fid,'%10s%13.7f%13.7f%5d%5d\n',today,head); 
fprintf(tl_fid,'%13.7f',zr);
fprintf(tl_fid,'\n');
% Compute transmission loss
tl=zeros(NDEP,NRNG);
   for n=1:NRNG
      for j=1:NDEP
      tl(j,n)=-20.0*log10(abs(cpr(j,n)*(4*pi)));
      end
   rkm_tl=[rkm(n); tl(1:NDEP,n)];
   fprintf(tl_fid,'%13.7f',rkm_tl);
      if n~=NRNG
      fprintf(tl_fid,'\n');
      end
   end
fclose(tl_fid);
display('Calculation completed.');
display('Representative transmission loss plot started.');
% Plot tl at a representative reciever
tl_plot(NDEP,NRNG,tl,rkm,ZS,FREQ,zr,case_name)
%
else
display('Calculation avioded.');
end % run rimLG