%==================================================================
%  
%  TRACEO: Pekeris block waveguide example.
%  Faro, Mon Jan  6 15:14:15 WET 2014
%  Written by Tordar 
%  
%==================================================================

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

clear all, close all

case_title = '''Pekeris waveguide & ray coordinates''';

freq       =  100;
Rmaxboxkm  = 5.1; Rmaxbox = Rmaxboxkm*1000;
Rmaxhrykm  = 5.0; Rmaxhry = Rmaxhrykm*1000;
Dmax   =  1000;
zs = 500; rs = 0; thetamax = 20; nthetas = 301;

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

ray_step = Rmaxbox/1000; 

la = linspace(-thetamax,thetamax,nthetas);

source_data.ds       = ray_step;
source_data.position = [rs zs];
source_data.rbox     = [-Rmaxbox Rmaxbox];
source_data.f        = freq;
source_data.thetas   = la;

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

altimetry = [-Rmaxbox-1 Rmaxbox+1;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]; % [cp cs rho ap as]

%==================================================================
%  
%  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:
%  
%==================================================================

bathymetry = [-Rmaxbox-1 2000 2010 2990 3000 Rmaxbox+1;...
               Dmax      Dmax  500  500 Dmax Dmax];

bottom_data.type   = '''E''' ; 
bottom_data.ptype  = '''H''' ; % Homogeneous bottom
bottom_data.units  = '''W''' ; % Bottom attenuation units
bottom_data.itype  = '''2P'''; % 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.array_shape = '''VRY''';
output_data.ctype       = '''ARI'''; 
output_data.r           = Rmaxhry;
output_data.z           = Dmax;
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');

load ari

rayname = 'ray00000';

figure(1), hold on
plot(rs,zs,'ko',rs,zs,'m*','MarkerSize',16)
plot(bathymetry(1,:),bathymetry(2,:),'k','LineWidth',2)
for i = 1:nthetas

    si = num2str(i); lsi = length(si); 
    eval( [ 'ray = ' rayname(1:8-lsi) si ';' ] ); 
    r = ray(1,:);
    z = ray(2,:);
    plot(r,z)
    clear r z
    
end
grid on, box on
axis([0 Rmaxbox 0 Dmax])
view(0,-90)
hold off 
xlabel('Range (m)')
ylabel('Depth (m)')
title('TRACEO - Pekeris waveguide, all ray information')

disp('done.')
