function [mean_out,data_out]=compute_all_mean(data,nino_strength,time_ann,indm,perall,perdur,strength_criteria);
   jm=length(indm); data_out=[]; time_nino=[];
   sz=length(size(data));
   if sz==2;
      data_out=nan(300,length(perall));
      for iper=1:length(perall)
          f1_all=[];
          for j=1:length(indm);
              s0=nino_strength(indm(j),:);
              if strength_criteria==3; 
                 indt=find(time_ann>=datenum(perall(iper),1,1) & time_ann<=datenum(perall(iper)+perdur(iper),1,1));
              else
                 indt=find(time_ann>=datenum(perall(iper),1,1) & time_ann<=datenum(perall(iper)+perdur(iper),1,1));
              end
              f1=squeeze(data(indm(j),indt));
              f1_all=cat(2,f1_all,f1);
          end
          data_out(1:length(f1_all),iper)=f1_all;
          mean_out(iper)=nanmean(f1_all);
      end;
   elseif sz==3;
      [mod_num,am,tm]=size(data);
      data_out=nan(am,300,length(perall));
      for iper=1:length(perall)
          f1_all=[];
          for j=1:length(indm);
              s0=nino_strength(indm(j),:);
              if strength_criteria==3;
                 indt=find(time_ann>=datenum(perall(iper),1,1) & time_ann<=datenum(perall(iper)+perdur(iper),1,1));
              else
                 indt=find(time_ann>=datenum(perall(iper),1,1) & time_ann<=datenum(perall(iper)+perdur(iper),1,1));
              end
              f1=squeeze(data(indm(j),:,indt));
              i0=length(size(f1));
              f1_all=cat(i0,f1_all,f1);
          end
          [bm2,lm2]=size(f1_all);
          data_out(:,1:lm2,iper)=f1_all;
          mean_out(:,iper)=nanmean(f1_all,i0);
      end
   elseif sz==4;
      [mod_num,am,bm,tm]=size(data);
      data_out=nan(am,bm,300,length(perall));
      for iper=1:length(perall)
          f1_all=[];
          for j=1:length(indm);
              s0=nino_strength(indm(j),:);
              if strength_criteria==3; 
                 indt=find(time_ann>=datenum(perall(iper),1,1) & time_ann<=datenum(perall(iper)+perdur(iper),1,1));
              else
                 indt=find(time_ann>=datenum(perall(iper),1,1) & time_ann<=datenum(perall(iper)+perdur(iper),1,1));
              end
              f1=squeeze(data(indm(j),:,:,indt));
              i0=length(size(f1));
              f1_all=cat(i0,f1_all,f1);
          end
          [am2,bm2,lm2]=size(f1_all);
          data_out(:,:,1:lm2,iper)=f1_all;
          mean_out(:,:,iper)=nanmean(f1_all,3);
      end
   end
end



%function [f_nino]=compute_ninomean(nino_strength2,nino_strength5,fgco22_ann,fgco25_ann,t,nino);
%if ~exist('nino','var'); nino='nino'; end
%fgco22_ann=squeeze(fgco22_ann);
%fgco25_ann=squeeze(fgco25_ann);
%
%if length(size(fgco22_ann))==2;
%for l=1:3;
%    if l==1; ind=find(t>=datenum(1901,1,1) & t<=datenum(1941,1,1)); end
%    if l==2; ind=find(t>=datenum(1980,1,1) & t<=datenum(2020,1,1)); end
%    if l==3; ind=find(t>=datenum(2061,1,1) & t<=datenum(2101,1,1)); end
%    s0=nino_strength2(ind);
%    f0=fgco22_ann(ind);
%
%    if strcmp(nino,'nino'); f1=f0(s0>=1); 
%    else; f1=f0;       end
%
%    f2_nino(1,l)=nanmean(f1);
%    f2_nino(2,l)=nanstd(f1);
%    f2_nino(3,l)=length(f1);
%
%    s0=nino_strength5(ind);
%    f0=fgco25_ann(ind);
%
%    if strcmp(nino,'nino'); f1=f0(s0>=1); 
%    else; f1=f0;       end
%
%    f5_nino(1,l)=nanmean(f1);
%    f5_nino(2,l)=nanstd(f1);
%    f5_nino(3,l)=length(f1);
%end
%f_nino(:,1:2)=f5_nino(:,1:2);
%f_nino(:,3)=f2_nino(:,3);
%f_nino(:,4)=f5_nino(:,3);
%end
%
%if length(size(fgco22_ann))==3;
%for l=1:3;
%    if l==1; ind=find(t>=datenum(1901,1,1) & t<=datenum(1941,1,1)); end
%    if l==2; ind=find(t>=datenum(1980,1,1) & t<=datenum(2020,1,1)); end
%    if l==3; ind=find(t>=datenum(2061,1,1) & t<=datenum(2101,1,1)); end
%    s0=nino_strength2(ind);
%    f0=fgco22_ann(:,:,ind);
%
%    ind2=find(s0>=1);
%    if strcmp(nino,'nino'); f1=f0(:,:,ind2); 
%    else; f1=f0;       end
%
%    f2_nino(:,:,1,l)=nanmean(f1,3);
%    f2_nino(:,:,2,l)=nanstd(f1,0,3);
%    f2_nino(:,:,3,l)=length(ind2);
%
%    s0=nino_strength5(ind);
%    f0=fgco25_ann(:,:,ind);
%    ind2=find(s0>=1);
%    if strcmp(nino,'nino'); f1=f0(:,:,ind2); 
%    else; f1=f0;       end
%
%    f5_nino(:,:,1,l)=nanmean(f1,3);
%    f5_nino(:,:,2,l)=nanstd(f1,0,3);
%    f5_nino(:,:,3,l)=length(ind2);
%end
%f_nino(:,:,:,1:2)=f5_nino(:,:,:,1:2);
%f_nino(:,:,:,3)=f2_nino(:,:,:,3);
%f_nino(:,:,:,4)=f5_nino(:,:,:,3);
%end
%
%end












