%==================================================================
%  
%  TRACEO: Pekeris flat waveguide example.
%  Faro, Tue Jan  7 00:42:08 WET 2014
%  Written by Tordar 
%  
%==================================================================

system('rm -rf aad.mat');

clear all, close all 

case_title = '''Pekeris waveguide & arrival calculations''';

%==================================================================
%  
%  Define source data:
%  
%==================================================================

freq   =  717;
Rmaxkm =    1; Rmax = Rmaxkm*1000;
Dmax   =  100; 

ray_step = Rmax/1000; 

zs = 25; rs = 0; thetamax = 85; nthetas = 201; thetas = linspace(-thetamax,thetamax,nthetas);

source_data.ds       =       0;
source_data.position = [rs zs];
source_data.rbox     = [rs-1 Rmax+1];
source_data.f        = freq;
source_data.thetas   = thetas;

%==================================================================
%  
%  Define altimetry data:
%  
%==================================================================

altimetry = [rs-2 Rmax+2;0 0];

surface_data.type  =   '''V'''; 
surface_data.ptype =   '''H'''; % Homogeneous
surface_data.units =   '''W'''; % (Attenuation Units) dB/Wavelenght
surface_data.itype =  '''FL'''; % Sea surface interpolation type
surface_data.x     = altimetry; % Surface coordinates
surface_data.properties = [0 0 0 0 0]; % Dummy values

%==================================================================
%  
%  Define sound speed data:
%  
%==================================================================

c0 = 1500;

ssp_data.cdist = '''c(z,z)'''; % Sound speed profile

ssp_data.cclass = '''ISOV'''; % Isovelocity profile

ssp_data.z   = [0 Dmax]';
ssp_data.r   = [];
ssp_data.c   = [c0 c0]';

%==================================================================
%  
%  Define object data:
%  
%==================================================================

object_data.nobjects = 0; % Number of objects

%==================================================================
%  
%  Define bathymetry data:
%  
%==================================================================

%disp('Defining bottom characteristics...')

bathymetry = [rs-2 Rmax+2;Dmax Dmax];

bottom_data.type   = '''E''' ;
bottom_data.ptype  = '''H''' ; % Homogeneous bottom
bottom_data.units  = '''W''' ; % Bottom attenuation units
bottom_data.itype  = '''FL'''; % Bottom interpolation type 
bottom_data.x      = bathymetry; % Bottom coordinates 
bottom_data.properties = [1700 0.0 1.7 0.7 0]; % [cp cs rho ap as]

%==================================================================
%  
%  Define output data:
%  
%==================================================================

output_data.ctype       = '''ADR''';
output_data.array_shape = '''VRY''';
output_data.r           = Rmax;
output_data.z           = Dmax/2;
output_data.miss        = 0.5;

%==================================================================
%  
%  Call the function:
%  
%==================================================================

disp('Writing TRACEO waveguide input file...')

wtraceoinfil('pekeris.in',case_title,source_data,surface_data,ssp_data,object_data,bottom_data,output_data);

disp('Calling TRACEO...')

system('runtraceo pekeris');

disp('Reading the output data...')

load aad

rayname = 'aad00000';

if neigrays >  0 

for i = 1:neigrays

    si = num2str(i); lsi = length(si); 
    eval( [ 'ray = ' rayname(1:8-lsi) si ';' ] ); 
    tau(i) = ray(3);
    amp(i) = ray(4) + 1i*ray(5);
    amp(i) = amp(i)*exp( 1i*ray(6) );
    
end
figure(1)
stem( tau, abs( amp ) )
box on, grid on
xlabel('Travel time (s)')
ylabel('Amplitude')
title('TRACEO - Pekeris waveguide, delay calculations')

else
disp('no arrivals found...')
end 

disp('done.')
