figure('Color',[0.8 0.8 0.8], 'HandleVisibility','on', ...
        'Position',[427 -34 350 300], 'Tag','Medium');
 
clf reset;
orient tall
wysiwyg
SC=1; % Scale to normalize displacement plots
ux = uxtot * 1e2; % convert to cm
uy = uytot * 1e2;
uz = uztot * 1e2;

% this is for the difference (e.g., poro-elastic):
%ux1=ux; uy1=uy; uz1=uz;
%load U3.mat;
%save U3 ux uy uz;
%ux=ux-ux1; uy=uy-uy1; uz=uz-uz1;

x=x/1e3;          % convert to km
y=y/1e3;
XB=XB/1e3;
YB=YB/1e3;
dy=y(2,1)-y(1,1);

lambda=10;

nx = length(xvec);
ny = length(yvec);
dv=round(sqrt(nx*ny)/10);
xsub = x(1:dv:ny,1:dv:nx);
ysub = y(1:dv:ny,1:dv:nx);
uxsub = ux(1:dv:ny,1:dv:nx);
uysub = uy(1:dv:ny,1:dv:nx);

%unit vector towards satellite
%s_uv = [cos(azim)*sin(l_a),-sin(azim)*sin(l_a),cos(l_a)];  
s_uv = [-cos(azim)*sin(l_a),sin(azim)*sin(l_a),cos(l_a)];  
us   = reshape(([ux(:) uy(:) uz(:)] * s_uv'), size(x));
usw  = rem((us-min(us(:))),(lambda/2))/(lambda/2);    %wrapped phase, in cycles
azim*180/pi

s_uva = [cos(pi-azim)*sin(l_a),-sin(pi-azim)*sin(l_a),cos(l_a)];  
usa   = reshape(([ux(:) uy(:) uz(:)] * s_uva'), size(x));
uswa  = rem((usa-min(usa(:))),(lambda/2))/(lambda/2);    %wrapped phase, in cycles

[mgmax,ind] = max(us(:));
smx = x(ind);
smy = y(ind);
uh = (ux.^2+uy.^2).^(1/2);

subplot(2,2,1)
maxdisp = max(max(uh));
pcolor(x,y,uz), shading flat, colormap(jet), hold on
% plot projections of dislocations, if any
[n,m]=size(XB);
for j=1:m/5
 xb=XB((j-1)*5+1:(j-1)*5+5);
 yb=YB((j-1)*5+1:(j-1)*5+5);
 line(xb,yb,'Color','w','LineStyle','-','LineWidth',2),hold on
end
quiver(xsub,ysub,uxsub,uysub,'k'), hold off
xlabel('W-E distance, km')
ylabel('S-N distance, km')
title(['Max Horizontal Displacement: ' num2str(maxdisp) ' cm']);
colorbar('h')

subplot(2,2,2)
%cprof=input('Make custom profile? y/n (n is default):','s');
%if isempty(cprof)
 cprof = 'n';
%end
if cprof == 'y' | cprof == 'Y'
 fprintf('\n Please select a profile with a middle button of your mouse and press any key when ready (for zooming in/out use left/right buttons, respectively)\n');
 startpcks1
 pause
 set(gcf,'Pointer','arrow')

 fid=fopen('picks.dat');
 a = fscanf(fid,'%g %g',[2 inf]); % It has two rows now.
 fclose(fid);
 a = a';
 [a1,a2]=size(a);
 c=0;
 for l=1:a1/2
  x1=a(2*l-1,1);
  x2=a(2*l,1);
  y1=a(2*l-1,2);
  y2=a(2*l,2);
  A=(x2*y1-x1*y2)/(x2-x1);
  B=(y2-y1)/(x2-x1);
  xm=sort([x1 x2]);
  for j=1:colmax
   if x(1,j) > xm(1,1) & x(1,j) < xm(1,2)
    c=c+1;
    X(c)=x(1,j);
    Y=A+B*X(c);
    i=1;
    while y(i,1) < Y 
     i=i+1;
    end
    dU=uz(i,j)-uz(i-1,j);
    U(c)=uz(i-1,j)+dU/dy*(Y-y(i-1,j));
%   dU=uz(i,j)-uz(i-1,j);
%   U(c)=uz(i-1,j)+dU/dy*(Y-y(i-1,j));
   end
  end
 end

 maxuz = max(max(abs(uz)));
 plot(X,U/SC,'-k'), hold on
else
 yind = ceil(size(yvec,2)/2);
 plot(x(yind,:), ux(yind,:)/SC,'-k'), hold on
end
xlabel('W-E distance, km')
ylabel('displacement, cm')
title(['Central profile'])
grid, hold off
[s1,s2]=size(x);
fid = fopen('shp.dat','wt');
for i=1:s2
% fprintf(fid,'%f %f \n',x(yind,i),ux(yind,i)/SC);
end
status=fclose(fid);

%subplot(2,2,2)
%yind = ceil(size(yvec,2)/2);
%uzj=uz(:,yind);
%uhj=uy(:,yind);
%yj=y(:,yind);
%plot(x(yind,:), uz(yind,:),'-k'), hold on
%%plot(x(yind,:), ux(yind,:),'+k'), hold on
%plot(x(yind,:), us(yind,:),'--k'), hold on
%legend('vertical','LOS',0), hold off
%%xlabel('S-N distance, km')
%xlabel('W-E distance, km')
%ylabel('displacement, cm')
%title(['Look Ang=' num2str(l_a*180/pi) '   Azim=' num2str(azim*180/pi)])
%grid

subplot(2,2,3)
maxdisp = max(max(abs(us)));
pcolor(x,y,us), shading flat, colormap(jet), hold on
for j=1:m/5
 xb=XB((j-1)*5+1:(j-1)*5+5);
 yb=YB((j-1)*5+1:(j-1)*5+5);
 line(xb,yb,'Color','w','LineStyle','-','LineWidth',2),hold on
end
plot(0,0,'w+',smx,smy,'wo',smx,smy,'wx'); hold off
xlabel('W-E distance, km')
ylabel('S-N distance, km')
title(['max relative dS: ' num2str(maxdisp) ' cm']);
colorbar('h')

subplot(2,2,4)
pcolor(x,y,usw), shading flat, colormap(jet), hold on
plot(0,0,'w+',smx,smy,'wo',smx,smy,'wx'); hold off
xlabel('W-E distance, km')
ylabel('S-N distance, km')
title(['dS wrapped, #cycles, dloc=[' num2str(smx) ',' num2str(smy) ']']);
set(gca,'clim',[0 1])
colorbar('h')

%print sic_pic -depsc2
%return

fig12 = figure(12);
clf
set(fig12,'Name','Horizontal displacements',...
    'PaperPosition',[1 1 5 5],...
    'Position',[10 20 500 500],'number','off');

xlim=[min(min(x)) max(max(x))]; ylim=[min(min(y)) max(max(y))];
%save Uh2 x y uh xsub ysub uxsub uysub; 
subplot(1,1,1)
%[c,h] = contour(x,y,uh,'-k'), hold on
%clabel(c,h), hold on
%v=[0:10:50];
%[C,h]=contour(x,y,uh,v,'-k'); hold on
%clabel(C,h,'manual'), hold on
pcolor(x,y,uh), shading flat, colormap(jet), hold on
%brighten(0.5)
%set(gca,'LineWidth',4);
quiver(xsub,ysub,uxsub,uysub,'k-'), hold off
%set(gca,'LineWidth',4);
axis('equal');
set(gca,'XLim',xlim,'YLim',ylim,'FontSize',14);
%colormap(bone);
%cmap=colormap;
%cmap=flipud(cmap);
%colormap(cmap)

%print uh_disl -djpeg100
%print uh_disl -depsc2

