% This is a script to plot TL curves using the results from calculation.

clear;
clc;

plottype = 2;    % The value of plottype should be 1, 2, or 3
                 % plottype = 1 is to plot the TL curve of the field
                 % contributed by a single image source Snb,ll
                 % plottype = 2 is to plot the TL curve of the field
                 % contributed by the four image sources coresponding to
                 % nb bottom reflections.
                 % plottype = 3 is to plot the TL curve of the field
                 % contributed by all the image sources that undergo
                 % 0~nb_max bottom reflections

nb_max = 1;     % used for plottype = 3; 
nb = 1;          % used for plottype = 2 and 1;
ll = -2;         % ll = -2, -1, 1, or 2, used for plottype =1;            
                 
                 
pr = 0;
               
switch plottype
    
    case 1
        
        jgg = load(['nb=' num2str(nb) ',' 'll=' num2str(ll) '.grid']);

        pr = jgg(:,2) + 1i*jgg(:,3);
    
    case 2
        
        ll_vals = [-2 -1 1 2]
        
        if nb == 0
              ll_vals = [-1 2]
        end
        
        for ll = ll_vals
            jgg=load(['nb=' num2str(nb) ',' 'll=' num2str(ll) '.grid']);
            pr = pr + jgg(:,2) + 1i*jgg(:,3); 
        end
        
    case 3
        
        for nb=0:nb_max
            
            ll_vals = [-2 -1 1 2]
            
            if nb == 0
              ll_vals = [-1 2]
            end
            
            for ll = ll_vals
              jgg=load(['nb=' num2str(nb) ',' 'll=' num2str(ll) '.grid']);
               pr = pr + jgg(:,2) + 1i*jgg(:,3); 
            end
            
      end
      
end

r = jgg(:,1);

TL = -20*log10(abs(pr));
figure;
plot( r ,TL,'linewidth',2);
xlabel('r (from the source), km');
ylabel('TL, dB re 1 m');
grid on;
axis ij;        
        
        switch plottype
            case 1
        title(['The image source solution for nb= ' int2str(nb) ' and ll= ' num2str(ll)]);
            case 2   
        title(['The image source solution for nb= ' int2str(nb) ]);
            case 3
        title(['The image source solution for ' int2str(nb_max) ' bottom reflections']);
            end
        