load SST_area1;                 % load SST time serie area 1
load SST_area2;                 % load SST time serie area 2 
area(1).box=SST_area1;        
area(2).box=SST_area2;
color=['b';'r'];                % define color for AREA 1 (blue) for AREA 2 (red)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%                                          _________            %%%%%%%%%%%                                            
%%%%%%%%%%%           Monthly Mean SST (oC)  ---->   SST(month)           %%%%%%%%%%%           
%%%%%%%%%%%                                                               %%%%%%%%%%%        
%%%%%%%%%%%                                                               %%%%%%%%%%%                                                               
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for ind=1:2               % for ind=...  Put two graps in one axes: ind=1 ---> Area 1; ind=2 -->  Area 2
    area1=area(ind).box;    
    month_m=[];
    for month=min(area1(:,2)):max(area1(:,2))   % for month=...   
                                                             %   __________          ___  ___  ___                                            
        m_m=mean( area1(area1(:,2)==month,4) );              %   SST(month)   --->   Jan, Feb, Mar, ...
        month_std=std( area1(area1(:,2)==month,4) );         %        _________
        month_m=[month_m;[month m_m month_std]];             % Month, SST(month), Standard Deviation
    end        % end for month=... SST monthly mean
    subplot(322)       % graph  SST monthly mean
    hold on
    h1=errorbar(month_m(:,2),month_m(:,3),color(ind));       % SST(month), Standard Deviation
    set(h1,'Linewidth',2)
    ylabel('Monthly Mean SST ^oC');xlabel('Month')
    axis([0.5 12.5 19 28])
    set(gca,'Box','on')
    hold off
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%                                                              %%%%%%%%
    %%%%%%%%%%%%%%     SST time serie 1985 - 2008     SST(year_month)           %%%%%%%%
    %%%%%%%%%%%%%%                                                              %%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                   
    prom=[];
    for y=min(area1(:,1)):max((area1(:,1)))      % for y=... ---> Min year to Max year
       ii=find(area1(:,1)==y);                                 % Data from each year
       month_min=min(area1(ii,2));                             % First month of the complete time serie
       month_max=max(area1(ii,2));                             % Last month of the complete time serie
       for m=month_min:month_max                 % for m= ...  --->  SST(year_month)  Time serie
           ii=find(area1(:,1)==y & area1(:,2)==m);
           st_deviation=std(area1(ii,4));                                                         
           prom = [prom; [y m mean(area1(ii,4)) st_deviation]]; % year, month, SST(year_month),  std 
       end
    end
    subplot(312)        % graph Time serie  1985 - 2008
    hold on
    h2=plot(prom(:,1)+(prom(:,2)-1)/12,prom(:,3),color(ind));
    set(h2,'Linewidth',2)
    xlabel('Year');ylabel('Monthly Mean SST ^oC')
    axis([1984 2009 18 28])
    set(gca,'Box','on')
    hold off
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%                                           _________       %%%%%%%%%                                 
    %%%%%%%%%%%%%%      SSTR(year_month) = SST(year_month) - SST(month)      %%%%%%%%%    
    %%%%%%%%%%%%%%      SST residual                                         %%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    v=[];
    for y=min(prom(:,1)):max((prom(:,1)))   % for y=... --> Minimun year - Maximun year
         ii=find(prom(:,1)==y);                             % Find data for a particular year
         month_min=min(prom(ii,2));                         % Min month
         month_max=max(prom(ii,2));                         % Max month    
         for m=month_min:month_max      % for m=...                                                        _________
             val=prom(prom(:,1)==y & prom(:,2)==m,3)-month_m(m,2);  % SSTR(year_month) = SST(year_month) - SST(month)
             v=[v;[y m val]];                               % year, month, SSTR(year_month)  
         end            % end for m=...
    end      % end for y=...                          
    subplot(313)                 % Graph SST residual
    hold on
     plot(v(:,1)+(v(:,2)-1)/12,v(:,3),color(ind),'Linewidth',2)
     grid
     axis([1984 2009 -2.05 2])
     plot([1984 2009],[0 0],'black')
         set(gca,'Box','on')
     xlabel('Year');ylabel('Monthly SST Residuals ^oC')
    hold off
    if size(area(1).box)==size(area(2).box) & area(1).box==area(2).box
        break;
    end
end  % end for=ind...

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%                                                            %%%%%%%%%%%
%%%%%%%%%%%%%%%%                  World Map picture                        %%%%%%%%%%%                      
%%%%%%%%%%%%%%%%                                                            %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(321)
hold on
load coast
grid
h3=plot(long,lat,'black');
a1x=[-30 -25 -25 -30 -30];             
a1y=[25 25 30 30 25];
a2x=[-30 -25 -25 -30 -30];
a2y=[-30 -30 -25 -25 -30];
fill(a1x,a1y,'b')
set(gca,'Box','on')
if ind==2,fill(a2x,a2y,'r'),end
hold off
subplot(312)
grid
subplot(313)
grid
subplot(322)
grid
%%%%%%%%%%%%%%%%%%%  Save figure as tiff, jpg and ps format

print -djpeg90 SST_time_serieR1
print -dpsc SST_time_serieR1
print -dtiff SST_time_serieR1
