load ~/NC/grid
load ~/NC/area_1.9_2.5.mat
year=1:150;

TS_temp=ncread('~/Aerosol_Variaiton/DRUN/read_nc/ncfile/read_nc_TS.nc','TS');

TS_ANN=zeros(144,96,150);

TS_ANN(:,:,:)=mean(TS_temp,3);


TS_DJF=zeros(144,96,150);

TS_DJF(:,:,1)=mean(TS_temp(:,:,[1,2],1),3);
for yearID=2:150
    TS_DJF(:,:,yearID)=(TS_temp(:,:,12,yearID-1)+TS_temp(:,:,1,yearID)+TS_temp(:,:,2,yearID))/3;
end



TS_GLOBAL_ANN=zeros(150,1);
for yearID=1:150
    TS_GLOBAL_ANN(yearID)=sum(sum(TS_ANN(:,:,yearID).*area,2),1)/sum(area(:));
end


save('TS.mat','TS_ANN','TS_DJF','TS_GLOBAL_ANN')

nccreate('TS.nc','Year','Dimensions',{'time',Inf},'Format','classic')
ncwrite('TS.nc','Year', year)
ncwriteatt('TS.nc','Year','long_name','Year')
nccreate('TS.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('TS.nc','lat', lat)
ncwriteatt('TS.nc','lat','units','degrees_north')
ncwriteatt('TS.nc','lat','long_name','Latitude')
nccreate('TS.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('TS.nc','lon', lon)
ncwriteatt('TS.nc','lon','units','degrees_east')
ncwriteatt('TS.nc','lon','long_name','Longitude')
nccreate('TS.nc','TS_ANN','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('TS.nc','TS_ANN', TS_ANN)
nccreate('TS.nc','TS_DJF','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('TS.nc','TS_DJF', TS_DJF)
nccreate('TS.nc','TS_GLOBAL_ANN','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('TS.nc','TS_GLOBAL_ANN', TS_GLOBAL_ANN)
ncdisp('TS.nc')