load ~/NC/grid
year=1:150;

PRECL_temp=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_PREC.nc','PRECL');

PRECL_ANN=zeros(144,96,150);
PRECL_DJF=zeros(144,96,150);
PRECL_MAM=zeros(144,96,150);
PRECL_JJA=zeros(144,96,150);
PRECL_SON=zeros(144,96,150);
PRECL_JAN=zeros(144,96,150);
PRECL_JUL=zeros(144,96,150);

PRECL_ANN(:,:,:)=mean(PRECL_temp,3);
PRECL_MAM(:,:,:)=mean(PRECL_temp(:,:,[3,4,5],:),3);
PRECL_JJA(:,:,:)=mean(PRECL_temp(:,:,[6,7,8],:),3);
PRECL_SON(:,:,:)=mean(PRECL_temp(:,:,[9,10,11],:),3);
PRECL_DJF(:,:,1)=mean(PRECL_temp(:,:,[1,2],1),3);
for yearID=2:150
    PRECL_DJF(:,:,yearID)=(PRECL_temp(:,:,12,yearID-1)+PRECL_temp(:,:,1,yearID)+PRECL_temp(:,:,2,yearID))/3;
end
PRECL_JAN(:,:,:)=PRECL_temp(:,:,1,:);
PRECL_JUL(:,:,:)=PRECL_temp(:,:,7,:);
PRECL_MONTH=PRECL_temp;

save('PRECL.mat','PRECL_ANN','PRECL_DJF','PRECL_MAM','PRECL_JJA','PRECL_SON','PRECL_JAN','PRECL_JUL','PRECL_MONTH')

nccreate('PRECL.nc','Year','Dimensions',{'time',Inf},'Format','classic')
ncwrite('PRECL.nc','Year', year)
ncwriteatt('PRECL.nc','Year','long_name','Year')
nccreate('PRECL.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('PRECL.nc','lat', lat)
ncwriteatt('PRECL.nc','lat','units','degrees_north')
ncwriteatt('PRECL.nc','lat','long_name','Latitude')
nccreate('PRECL.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('PRECL.nc','lon', lon)
ncwriteatt('PRECL.nc','lon','units','degrees_east')
ncwriteatt('PRECL.nc','lon','long_name','Longitude')
nccreate('PRECL.nc','PRECL_ANN','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('PRECL.nc','PRECL_ANN', PRECL_ANN)
nccreate('PRECL.nc','PRECL_DJF','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('PRECL.nc','PRECL_DJF', PRECL_DJF)
nccreate('PRECL.nc','PRECL_MAM','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('PRECL.nc','PRECL_MAM', PRECL_MAM)
nccreate('PRECL.nc','PRECL_JJA','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('PRECL.nc','PRECL_JJA', PRECL_JJA)
nccreate('PRECL.nc','PRECL_SON','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('PRECL.nc','PRECL_SON', PRECL_SON)
nccreate('PRECL.nc','PRECL_JAN','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('PRECL.nc','PRECL_JAN', PRECL_JAN)
nccreate('PRECL.nc','PRECL_JUL','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('PRECL.nc','PRECL_JUL', PRECL_JUL)
nccreate('PRECL.nc','PRECL_MONTH','Dimensions',{'lon',144,'lat',96,'month',12,'year',length(year)},'Format','classic')
ncwrite('PRECL.nc','PRECL_MONTH', PRECL_MONTH)
ncdisp('PRECL.nc')