%time average of all terms 9 yrs.
%nohup matlab -nodisplay -nosplash -nodesktop -r "run('monthly_mean_balterms.m')" > nohup_mm_t3.txt 2>&1 &

outfile=char('/phodnet/share/asaha/data/Sat_combinedSMOS_SMAP_SSS_clim_365days_2010_2022_globalf.nc');
DayClim_SSS=ncread(outfile,'DayClim_SSS');
DayClim_SSSrain=ncread(outfile,'DayClim_SSSrain');

Months_365 = [31 28 31 30 31 30 31 31 30 31 30 31]';

c=0;

for i=1:12      
i

mmS=zeros(1388,584);    
mmSr=zeros(1388,584);

   for j=1:Months_365(i)
   c=c+1;
   mmS(:,:)=mmS(:,:)+DayClim_SSS(:,:,c); 
   mmSr(:,:)=mmSr(:,:)+DayClim_SSSrain(:,:,c);
   end %j day
     
climS(:,:,i)=mmS(:,:)./Months_365(i); 
climSr(:,:,i)=mmSr(:,:)./Months_365(i); 

clear mmS mmSr

end %i month
 
outfile=char('/phodnet/share/asaha/data/Sat_combinedSMOS_SMAP_SSS_monthly_clim_2010_2022_globalf.nc');
nccreate(outfile,'climS','dimensions',{'longitude' 1388 'latitude' 584 'time' 12},'Datatype','single','ChunkSize',[139 37 12],'DeflateLevel',2);
nccreate(outfile,'climSr','dimensions',{'longitude' 1388 'latitude' 584 'time' 12},'Datatype','single','ChunkSize',[139 37 12],'DeflateLevel',2);
ncwrite(outfile,'climS',climS);
ncwrite(outfile,'climSr',climSr);




