% ROT.M
  function rot(fl,pl)    % fl=1, if filter SLA field, fl=0, if do not
%                        % pl=1, if plot graph, pl=0, if do not
% *******************************************************************
% Find and extract part of the matrix  - internal box which position
% and rotation angle are coincided with the T/P diamond shaped domain. 
% x3, y3 are coordinates of the initial   SLA field
% (10 x 8 degrees) which y-size is equal to 1.0 and x-size 
% is equal to y3*cos(theta), where theta is a rotation angle (positiv cckw).
% the output consists of  2 files: sla/data & sla/error.
% ADD PATH    ADD PATH     ADD PATH      ADD PATH 
    addpath /usr/people/valery/TPD_1.0/data/maps/
% *************************************************
t = 0;                   % Loop counter
% set filter parameters
     hh=fspecial('gaussian',5,1.5);   % filter choice 
% create grid 48 x 60 (large domain)
      [x3,y3]=meshgrid(0.85:.57627:34.85,1:.68085:33);  
% now grid 25 x 31 (diamond)
      f=pi*64.4/180;
      yn=10.2728;
      xn=23.4591;
      xi=ones(25,31);
      yi=ones(25,31);
      for k=1:1:25
      for l=1:1:31
      xi(k,l)=(xn)+(-(k-1)*sin(f)+(l-1)*cos(f))*0.366;
      yi(k,l)=(yn)+((k-1)*cos(f)+(l-1)*sin(f))*0.366;
      end
      end
      xv=[xi(1,1);xi(1,31);xi(25,31);xi(25,1);xi(1,1)];
      yv=[yi(1,1);yi(1,31);yi(25,31);yi(25,1);yi(1,1)];
% *********LOOP   LOOP   LOOP LOOP LOOP LOOP************** 
      for i = [1899:2000];   % i
      j = sprintf('day%i',i)
      eval(['load ' j '.map;']);
      eval(['a = ' j ';']);
      eval(['clear ' j ' ']);
      t = t+1;
      a(1,:)=[];
      a1 = a(:,1);           % SLA
      a2 = a(:,2);           % OA ERROR
%
      b1 = reshape(a1,60,48)';
      b2 = reshape(a2,60,48)';
    if(fl==1)
      b1=filter2(hh,b1);
    end
    if(pl==1) 
      subplot(211)
      plot(xv,yv,'k-');
      hold on;
      contourf3(x3,y3,b1,10);
      plot(xv,yv,'k-');
      hold off;
      colorbar;
      title('sla (mm), av tp+ers data set (map) ');
    end
% interpolate SLA from the coarse onto a finer (model) grid)
      z1=interp2(x3,y3,b1,xi,yi,'cubic');
      z2=interp2(x3,y3,b2,xi,yi,'cubic');
% display field
    if(pl==1)  
      subplot(212)
      contourf3(xi,yi,z1,10,'r-');
%     caxis([-100 100]);
      colorbar;
%     axis square;
      title(['JD ',num2str(j),' (',num2str(t),') ']);
    end
% prepare output matrix: nn x 775
      z1 = z1'; z1 = z1(:); z1 = z1';
      z2 = z2'; z2 = z2(:); z2 = z2';
     if t == 1
     K1 = ones(1,776);
     K2 = ones(1,776);
    end
     K1 = [K1;i,z1];
     K2 = [K2;i,z2];
     end
% ************** LOOP LOOP LOOP LOOP LOOP ***********************
     K1(1,:) = [];
     K2(1,:) = [];
% write output  into files: SLA (mm) and OA error (%)     
     m='%i %7.2f';
     for s=[1:774]
     m=[m,' %7.2f'];
     end
     fid=fopen('tpersf.dat','w');
     fprintf(fid,[m,'\n'],K1');
     fclose(fid);
     fid=fopen('tpersf_err.dat','w');
     fprintf(fid,[m,'\n'],K2');
     fclose(fid);
% complete writing
