% dispEEOF(CHP,EXPVAR,DT,NLAG,MOD) Display few EEOFs.
%
% => DISPLAY FEW EEOFs.
% CHP contains all the EEOFs as EOF*LAG*X*Y.
% EXPVAR is a matrix with the explained variance of each
%  EEOFs in %. This is just for title.
% DT is time step between maps.
% NLAG is the number of LAG to display.
% MOD contains explicitly the EEOFs to display.
%
% Rq: it's using the plotm function
%
%================================================================

%  Guillaume MAZE - LPO/LMD - March 2004
%  gmaze@univ-brest.fr

function [] = dispEEOF_aviso(lon,lat,CHP,PC,EXPVAR,DT,NLAG,MOD)

lon(lon<0) = lon(lon<0)+360;
% Number of EEOF to display     
NMOD = length(MOD);

% Open figure and first guest
figure;
clf;hold on
set(gcf,'PaperPosition',[1 1 8 5]); 
set(gcf,'DefaultLineLineWidth',1.5)
wysiwyg;

width  = .9/NMOD;
height = .9/NLAG;
dleft = (.95-width*NMOD)/4;
left  = dleft;
%load /automount1/mgoes/matlab/mapcolor/colors/mapanom
nbcont = 10;
% Let'go :

modelcoast = load('coast.mat');
      xtrm = max(max(max(max(abs(CHP(MOD,1:NLAG,:,:))))));

  series1 = 1;
  if ~(series1)

% We choose an EEOF
figure(1)
for imod = 1 : NMOD
    mod = MOD(imod);
    
    % and plot maps
    for lag=1:NLAG
      bottom = .95-height*lag;
      subplot('Position',[left bottom width height]);
      C = squeeze(real(CHP(mod,lag,:,:)));
%      C = C./xtrm(C); % Eventually normalise field
%keyboard
       m_proj('miller','lon',[min(lon) max(lon)],'lat',[min(lat) max(lat)]);
       [cs,h]=m_contourf(lon,lat,C,nbcont);
      m_grid('ytick','','xtick','');
      %     m_grid('ytick',[-80 -60 -40 -20 0 20 40 60 80]);
      m_coast('patch',[.6 .6 .6]);

      if(lag==1),
          caxis([-abs(xtrm) abs(xtrm)]);
          cx=caxis;
%      caxis(cx);
      end;
  %    colormap(mapanom);
      if(lag==1)
         titre=strcat('EOF',num2str(mod),'(',num2str(EXPVAR(mod)),'%)');
         titre=strcat(titre,'; DT=',num2str(DT),' ');
         title(titre);
      end;
    end
    left = left + width + dleft;

end

%%%%% TIME series
figure;
clf;hold on
set(gcf,'PaperPosition',[1 1 8 5]); 
set(gcf,'DefaultLineLineWidth',1.5)
wysiwyg;
left  = dleft;
width  = .9/NMOD;
lag = 1;
for imod = 1 : NMOD
    mod = MOD(imod);
    
    % and plot maps
      bottom = .95-height*lag;
      subplot('Position',[left bottom width height]);
         plot(PC(imod,:));
         grid on;box on;
         set(gca,'XAxisLocation','top');axis tight;
         titre=strcat('EEOF',num2str(mod),'(',num2str(EXPVAR(mod)),'%)');
         titre=strcat(titre,'; DT=',num2str(DT),' ');
         title(titre);
    if (imod==1),  ax1=get(gca,'YLim');else set(gca,'YLim',ax1);end
      if(lag==1),
          caxis([-abs(xtrm) abs(xtrm)]);
      end;
      caxis(cx);
  %    colormap(mapanom);
    left = left + width + dleft;
 %      axis image, axis normal

end


  else
%      keyboard
height = .9/(NLAG+1);
% We choose an EEOF
for imod = 1 : NMOD
    mod = MOD(imod);
   % figure(imod)
    % and plot maps
    for iplot=1:NLAG%+1
      bottom = .95-height*iplot;
      subplot('Position',[left bottom width height]);
      if iplot==1 % Plot the pc's time serie
         plot(PC(imod,:));
         grid on;box on;
         set(gca,'XAxisLocation','top');axis tight;
         titre=strcat('EEOF',num2str(mod),'(',num2str(EXPVAR(mod)),'%)');
         titre=strcat(titre,'; DT=',num2str(DT),' ');
         title(titre);
         if (imod==1),ax1=get(gca,'YLim');else set(gca,'YLim',ax1);end
      else % Plot the lag maps   
         lag=iplot-1;
         C = squeeze(real(CHP(mod,lag,:,:)));
       m_proj('miller','lon',[min(lon) max(lon)],'lat',[min(lat) max(lat)]);
       [cs,h]=m_contourf(lon,lat,C,nbcont);
      m_grid('ytick','','xtick','');
      %     m_grid('ytick',[-80 -60 -40 -20 0 20 40 60 80]);
      m_coast('patch',[.6 .6 .6]);
      axis normal
      if(lag==1),
  %        caxis([-abs(xtrm) abs(xtrm)]);
          cx=caxis;end;
      caxis(cx);
  %    colormap(mapanom);
          m_text(10,-70,strcat('{\bf',num2str((lag-1)*DT),'}'));
      end %if
  %  axis image, axis normal
    end
    left = left + width + dleft;
    % Adjust plot width via the plot Aspect ratio
    ch=get(gcf,'Children');
    posiPC=get(ch(iplot),'PlotBoxAspectRatio');
    posiMAP=get(ch(iplot-1),'PlotBoxAspectRatio');
    set(ch(iplot),'PlotBoxAspectRatio',posiMAP);
end

  end %series1
