clear all 

PREFIX='Data/TS2D_noresm1-cmip6_historical_19500101_';
SUFFIX='_0000-2000_1950-2010.mat'; 
NMEM=30; 
%
if true
  clear T S; 
  for mem=1:NMEM
    FIN=sprintf('%smem%2.2i%s',PREFIX,mem,SUFFIX);
    disp(FIN)
    datain=load(FIN); 
    if mem==1 
      T=datain.T/NMEM; 
      S=datain.S/NMEM; 
    else
      T=T+datain.T/NMEM; 
      S=S+datain.S/NMEM; 
    end 
  end
  save([PREFIX 'ensave' SUFFIX],'T','S'); 
end 

PREFIX='Data/TS2D_noresm1-cmip6_analysis_19500115_';
SUFFIX='_0000-2000_1950-2010.mat';
NMEM=30;
%
if true
  clear T S;
  for mem=1:NMEM
    FIN=sprintf('%smem%2.2i%s',PREFIX,mem,SUFFIX);
    disp(FIN)
    datain=load(FIN);
    if mem==1
      T=datain.T/NMEM;
      S=datain.S/NMEM;
    else
      T=T+datain.T/NMEM;
      S=S+datain.S/NMEM;
    end
  end
  save([PREFIX 'ensave' SUFFIX],'T','S');
end


PREFIX='Data/TS2D_noresm1-cmip6_hindcast_';
DEPTHTAG='_0000-2000_'; 
STARTYEARS=1960:1:2000;
NMEM=10;
%
if true
  for startyear=STARTYEARS
    clear T S;
    for mem=1:NMEM
      FIN=sprintf('%s%4.4i1015_mem%2.2i%s%4.4i-%4.4i.mat',PREFIX,startyear,mem,DEPTHTAG,startyear+1,startyear+10);
      disp(FIN)
      datain=load(FIN);
      if mem==1  
        T=datain.T/NMEM;                                          
        S=datain.S/NMEM;
      else
        T=T+datain.T/NMEM;
        S=S+datain.S/NMEM;
      end
    end
    save([PREFIX int2str(startyear) '_ensave' DEPTHTAG int2str(startyear+1) '-' int2str(startyear+10) '.mat'],'T','S');
  end
end



