% dispCEOF(CEOF,EXPVAR,PHI,MOD) Display few CEOFs.
%
% => DISPLAY FEW CEOFs.
% CEOF contains all the CEOFs as nCEOF*X*Y.
% EXPVAR is a matrix with the explained variance of each
%  CEOFs in %. This is just for title.
% PHI is phase when ploting maps.
% MOD contains explicitly the CEOFs to display.
%
% Rq: it's using the plotm function
%
%================================================================

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

function [] = dispCEOF_aviso(lon,lat,ydec,CEOF,EXPVAR,PHI,MOD,PC)

% Number of EEOF to display     
NMOD = length(MOD);
NANG = size(PHI,2);

% Open figure and first guest
%figure;clf;hold on
   figure;
    clf;hold on
    set(gcf,'PaperPosition',[1 1 10 8]); 
    set(gcf,'DefaultLineLineWidth',1.5)
    wysiwyg;
typg=8;
width  = .9/NMOD;
height = .9/NANG;
dleft = (.95-width*NMOD)/4;
left  = dleft+.05;
load mapanom %2

% Let'go :

% We select a CEOF:
for imod = 1 : NMOD
    mod = MOD(imod);
    chp=squeeze(CEOF(mod,:,:));
  %  Rchp=real(chp);
  %  Ichp=-imag(chp);

    % 
    for ang=1:NANG
      %C = cos(PHI(ang))*Rchp + sin(PHI(ang))*Ichp;
      %C = cos(PHI(mod,ang))*Rchp + sin(PHI(mod,ang))*Ichp;    %MARLOS : NOW FOR EACH IMOD
      C = real(PHI(mod,ang)*conj(chp));    %MARLOS : NOW FOR EACH IMOD
      %C = abs(PHI(mod,ang)*conj(chp));
    m(ang)=abs(xtrm(C));
    end
    m=max(m);clear C
    
    % and plot maps
    for ang=1:NANG
%     phi = round(atan2(real(PHI(mod,ang)),imag(PHI(mod,ang)))*180/pi);
      %phi = round(atan2(real(PHI(mod,ang)),imag(PHI(mod,ang)))*180/pi);
      phi = angle(PHI(mod,ang))*180/pi;
   %   phi = round(atan2(imag(PHI(mod,ang)),real(PHI(mod,ang)))*180/pi);
    
     %C = cos(PHI(ang))*Rchp + sin(PHI(ang))*Ichp;     
      C = real(PHI(mod,ang)*conj(chp));    %MARLOS : NOW FOR EACH IMOD
   %   C = abs(PHI(mod,ang)*conj(chp));
%      C = cos(PHI(mod,ang))*Rchp + sin(PHI(mod,ang))*Ichp;    %MARLOS : NOW FOR EACH IMOD
      bottom = .95-height*ang;
      subplot('Position',[left bottom width height-0.01]);
     m_proj('miller','lon',[min(lon) max(lon)],'lat',[min(lat) max(lat)]);
    % m_proj('miller','lon',[min(lon) min(-20,max(lon))],'lat',[min(lat) max(lat)]);
      [cs,h]=m_contourf(lon,lat,C,10,'edgecolor','none');
     
      %set(gca,'xticklabel',[],'yticklabel',[])
      colormap(mapanom)
      %m_grid('xtick','');
      if imod>1&&ang<NANG
          m_grid('ytick','','xtick','');
      elseif imod==1&&ang<NANG
          m_grid('xtick','')
      elseif imod==1&&ang==NANG
           m_grid
      else
          m_grid('ytick','')
      end
      
      axis normal
       m_coast('patch',[.6 .6 .6]);
     %  m_text(-55,-22,strcat('\phi=',num2str(PHI(ang)*180/pi),'^o'));
%      m_text(-52,-22,sprintf('%s%7.2f%s','\phi=',(PHI(ang)*180/pi),'^o'))
      m_text(-52,-22,sprintf('%s%7.2f%s','\phi=',(phi),'^o')) %MARLOS : NOW FOR EACH IMOD

%      ylabel(strcat('\phi=',num2str(PHI(ang)*180/pi),'^o'),'Rotation',0);
      caxis([-m m]);
  %    caxis([-3 3]);
  %    caxis([-2 2]);
      if(ang==1)
         titre=strcat('CEOF',num2str(mod),'(',num2str(EXPVAR(mod)),'%)');
         title(titre);
      end;
    end
    left = left + width + dleft-0.01;

end

   figure;
    clf;hold on
    set(gcf,'PaperPosition',[1 1 10 2]); 
    set(gcf,'DefaultLineLineWidth',1.5)
    wysiwyg;
    left  = dleft+.05;
    height = .6;
for imod = 1 : NMOD
    mod = MOD(imod);
   
    % and plot maps
      bottom = .1;%.95-height;%*lag; bottom = .95-height*ang;
      subplot('Position',[left bottom width height]);
         plot(ydec,real(PC(mod,:)));   %amplitude
         %plot(ydec,variance_evolution(real(PC(mod,:)),109));   % phase
        hold on
   %      plot(ydec,variance_evolution(imag(PC(mod,:)),53));   % phase
         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);
    set(gca,'yticklabel',[])
    end
    if (imod==NMOD)
        legend('Amp','Phase','location','southeast','box','off')
    end
%      if(lag==1),
%          caxis([-abs(xtrm) abs(xtrm)]);
%      end;
  %     caxis(cx);
  %    colormap(mapanom);
    left = left + width + dleft-.01;
 %      axis image, axis normal
end
