% Migration and Migration Velocity Lab
% Part 4: Depth Migration with Eikonal solver

% Eikonal solver requires the same horizontal and vertical spacings.
 dxx=dx;
 nxx=nx;
 dzz=dxx;
 nzz=round(nz*dz/dzz)+1;
 DX=dxx;

% Slowness Model for Traveltime Calculation
SW=Slownessmod(dxx,nxx,dzz,nzz);

%Plot Smoothed Stacking velocity
figure;
imagesc(x,dt*[1:nt],SW');
title('Slowness Model','FontSize',18);
xlabel('X (m)','FontSize',16);
ylabel('TWT (msec)','FontSize',16);
colorbar;

% % Tracveltime computation
% % uncomment if you create new slowness model.
% xx=[1:nxx]*dxx;
% zz=[1:nzz]*dzz;
% mkdir TT
% for ixsrc=1:nxx;
%     SP = [1 ixsrc];   % Shot Coordinates
%     [time]=Mray(SW',SP,DX);
%     %traveltimesrc(:,:,ixsrc)=Ttable(:,:);
%     ixsrc
%     imagesc(x,z,time);title('Traveltime Field');colorbar;text(nx*DX+5*DX,-DX,'Time (s)');
%     xlabel('X (m)');ylabel('Depth (m)');
% 
%     fn=sprintf('TT/TT_%d',ixsrc);
%     save(fn,'time');
%     clear time;
%     
%     pause(.015)
% end

% Movie of Traveltimes
for ixsrc=1:50:nx;
    fn=sprintf('TT/TT_%d',ixsrc);
    load(fn);
    imagesc(x,[1:nzz]*dzz,time);
    title(['Traveltime Field CDP=',num2str(ixsrc)]);
    colorbar;
    text(nx*DX+5*DX,-DX,'Time (s)');
    xlabel('X (m)');ylabel('Y (m)');
    pause(1)
end
    
% Migrate with tarveltimes
[MIG_depth2] = migrate_tt(stack,nx,nz,nt,dt,ap,dx,dz,TOZS,TOZE);
   
% Plot Migration Result
migdmax=0.2*max(MIG_depth2(:));
figure;
imagesc(x,z,MIG_depth2',[-migdmax migdmax]);
title('Migrated Section with Eikonal traveltime','FontSize',18);
xlabel('X (m)','FontSize',16);
ylabel('Depth (m)','FontSize',16);
colormap('gray');
