% MAP_VIEWER.M
% *************************************************************************
  function map_viewer(fl,pl,wr) % fl=1, if filtered SLA field, fl=0, if not
%                               % pl=1, if plot each graph, pl=0, if do not 
%    wr=1, if write output matrix,(use pl=0 for this case),  wr=0 if do not        
% *************************************************************************
% set up filter parameters
      hh=fspecial('gaussian',5,1.5);   % filter choice 
      x3=-70.0:0.1667:-60.0;
      y3=28.25:0.1667:36.25;  
      jul=datenum(1992,10,27); % (1992,10,27)
      t=1; 
% *********LOOP   LOOP   LOOP LOOP LOOP LOOP************** 
% set up the beginning and the ending JDs you would like to display
      for i = [300:302];   % i
      j = sprintf('day%i',i)
      eval(['load ' j '.map;']);
      eval(['a = ' j ';']);
      eval(['clear ' j ' ']);
% delete header
      a(1,:)=[];
      a1 = a(:,1);           % SLA
      a2 = a(:,2);           % OA ERROR
% form "large field" arrays
      b1 = reshape(a1,60,48)';
      b2 = reshape(a2,60,48)';
% apply 2d filter (to SLA field only)
    if(fl==1)
      b1=filter2(hh,b1);
    end
% draw SLA  and SLA error fields
    if(pl==1) 
      colormap(jet(20))
      subplot(211)
      contourf(x3,y3,b1,20);
      colorbar;
      axis square
      box on
      ylabel('Latitude');
      xlabel('Longitude')
       title(['SLA (mm)       ',datestr(jul,1)]...
       ,'color','b','FontWeight','bold','FontSize',10);
%
      subplot(212)
      contourf(x3,y3,b2,10);
      caxis([0 100]);
      colorbar;
      axis square;
      box on
      ylabel('Latitude');
      xlabel('Longitude')
       title(['SLA Error (%)        ',datestr(jul,1)]...
       ,'color','b','FontWeight','bold','FontSize',10);
    end
% to store daily maps in output file (each row in output matrix):
% 
% prepare output matrix: nn x mm
      b1 = b1'; b1 = b1(:); b1 = b1';
      b2 = b2'; b2 = b2(:); b2 = b2';
     if ( t == 1)
      K1 = ones(1,2881);
      K2 = ones(1,2881);
      t=0;
    end
      K1 = [K1;i,b1];
      K2 = [K2;i,b2];

     if(pl==1)
     display('press Enter to see next map')
     input('?')
     end
     jul=jul+1;
     end
% end of the loop
% ************** LOOP LOOP LOOP LOOP LOOP ***********************
      K1(1,:) = [];
      K2(1,:) = [];
% write output matrixes into files: SLA (mm) and OA error (%)  
  if(wr==1)   
     display('wait, writing output file')
     m='%i %7.2f';
     for s=[1:2879]
     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;
   end
% complete viewing

