clear all;
cd /net/koko/homes/sep/jeff/
startup;
cd /net/koko/homes/sep/jeff/Research/GEO_NO/

%
dt=0.005;
ng=512;
tmax=2.25;

% . . Read in File
file_name='PSeikonal.H';
D1=load_history(file_name);
fidd1=fopen(D1.in,'r','ieee-be');
arr_size=[D1.n(1),D1.n(2)];
nelem=arr_size(1)*arr_size(2);
f(1:nelem) = fread(fidd1,nelem,'float32');
TTimes=reshape(f,[D1.n(1),D1.n(2)]);

xpts=[0:D1.n(2)-1]*D1.d(2)+D1.o(2);
zpts=[0:D1.n(1)-1]*D1.d(1)+D1.o(1);

% . . Travel Time Tables
TTimes1=TTimes-min(min(TTimes));

tline=[dt:dt:tmax];
nxline=linspace(0,1,ng);

figure(1);hold on
axis ij
for it=1:length(tline)
  disp(['WORKING ON ',num2str(it),' of ',num2str(length(tline))])
  clear xr zr iil iir ccc cccnorm xrin zrin
  figure(2); [c,h]=contour(TTimes1,[tline(it) tline(it)]);  clf;

  cc=c(:,2:end);
 
  ic=2;
  xrin(1)=cc(1,1);
  zrin(1)=cc(2,1);
  for ii=2:length(cc)
    if (cc(1,ii)==cc(1,ii-1) & cc(2,ii)==cc(2,ii-1) )
      disp('Skipped Point')
    else
      xrin(ic)=cc(1,ii);
      zrin(ic)=cc(2,ii);
      ic=ic+1;
    end
  end

  xr =(xrin -1.)*D1.d(2)+D1.o(2);
  zr =(zrin -1.)*D1.d(1)+D1.o(1);
 % [nu,iil]=min( (xlft(end) - xr).^2+(zlft(end)-zr).^2) ;
 % [nu,iir]=min( (xrgt(end) - xr).^2+(zrgt(end)-zr).^2) ;
 % lall=length(iil:iir);
  ccc=zeros(1,size(xr,2));
  ccc(2:end)=sqrt((xr(2:end)-xr(1:end-1)).^2+(zr(2:end)-zr(1:end-1)).^2);
  ccc(1)=0;
  cccnorm=cumsum(ccc)/sum(ccc);
  
  Nxbot(it,1:ng)=interp1(cccnorm,xr,nxline,'cubic');
  Nzbot(it,1:ng)=interp1(cccnorm,zr,nxline,'cubic');
%  figure(1)
%  plot(Nxbot(it,:),Nzbot(it,:)); pause(0.2)
end

meshy=cat(3,Nxbot,Nzbot);
clear D1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% . . S velocity
% . . Create D1 Data file
   eval(['D1.history_file=''PS.rays.HH'' '])
   D1.n=[size(Nxbot,1) size(Nxbot,2) 2];   D1.o=[0.0 0.0 0];
   D1.d=[10 1. 1.];    D1.esize=4;
   eval(['D1.in=''/net/koko/jeff/PS.rays@'' ']);
   D1.data_format='xdr_float';
   D1.data=meshy;

% . . Call sepwrite program ;
   eval(['sepwrite(D1,''PS.rays.HH'') '])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%








