load ~/NC/grid.mat
load ~/Aerosol_Variaiton/IRUN/PRECT/ncfile/PRECT.mat
year=1:150;

EOF_PRECT_ANN=zeros(144,96);
EOF_PRECT_DJF=zeros(144,96);
EOF_PRECT_MAM=zeros(144,96);
EOF_PRECT_JJA=zeros(144,96);
EOF_PRECT_SON=zeros(144,96);
EOF_PRECT_JAN=zeros(144,96);
EOF_PRECT_JUL=zeros(144,96);

PRECT_ANN_ALL=zeros(150,144*96);
PRECT_DJF_ALL=zeros(150,144*96);
PRECT_MAM_ALL=zeros(150,144*96);
PRECT_JJA_ALL=zeros(150,144*96);
PRECT_SON_ALL=zeros(150,144*96);
PRECT_JAN_ALL=zeros(150,144*96);
PRECT_JUL_ALL=zeros(150,144*96);
n=1;
for i=1:144
    for j=1:96
        PRECT_ANN_ALL(:,n)=PRECT_ANN(i,j,:);
        PRECT_DJF_ALL(:,n)=PRECT_DJF(i,j,:);
        PRECT_MAM_ALL(:,n)=PRECT_MAM(i,j,:);
        PRECT_JJA_ALL(:,n)=PRECT_JJA(i,j,:);
        PRECT_SON_ALL(:,n)=PRECT_SON(i,j,:);
        PRECT_JAN_ALL(:,n)=PRECT_JAN(i,j,:);
        PRECT_JUL_ALL(:,n)=PRECT_JUL(i,j,:);
        n=n+1;
    end
end

[e,PC_PRECT_ANN,expvar_ANN] = caleof(PRECT_ANN_ALL,1,1);
n=1;
for i=1:144
    for j=1:96
        EOF_PRECT_ANN(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_PRECT_DJF,expvar_DJF] = caleof(PRECT_DJF_ALL,1,1);
n=1;
for i=1:144
    for j=1:96
        EOF_PRECT_DJF(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_PRECT_MAM,expvar_MAM] = caleof(PRECT_MAM_ALL,1,1);
n=1;
for i=1:144
    for j=1:96
        EOF_PRECT_MAM(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_PRECT_JJA,expvar_JJA] = caleof(PRECT_JJA_ALL,1,1);
n=1;
for i=1:144
    for j=1:96
        EOF_PRECT_JJA(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_PRECT_SON,expvar_SON] = caleof(PRECT_SON_ALL,1,1);
n=1;
for i=1:144
    for j=1:96
        EOF_PRECT_SON(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_PRECT_JAN,expvar_JAN] = caleof(PRECT_JAN_ALL,1,1);
n=1;
for i=1:144
    for j=1:96
        EOF_PRECT_JAN(i,j)=e(n);
        n=n+1;
    end
end

[e,PC_PRECT_JUL,expvar_JUL] = caleof(PRECT_JUL_ALL,1,1);
n=1;
for i=1:144
    for j=1:96
        EOF_PRECT_JUL(i,j)=e(n);
        n=n+1;
    end
end


save('PRECT_EOF.mat','EOF_PRECT_ANN','PC_PRECT_ANN','expvar_ANN','EOF_PRECT_DJF','PC_PRECT_DJF','expvar_DJF','EOF_PRECT_MAM','PC_PRECT_MAM','expvar_MAM','EOF_PRECT_JJA','PC_PRECT_JJA','expvar_JJA','EOF_PRECT_SON','PC_PRECT_SON','expvar_SON','EOF_PRECT_JAN','PC_PRECT_JAN','expvar_JAN','EOF_PRECT_JUL','PC_PRECT_JUL','expvar_JUL')

nccreate('PRECT_EOF.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('PRECT_EOF.nc','lat', lat)
ncwriteatt('PRECT_EOF.nc','lat','units','degrees_north')
ncwriteatt('PRECT_EOF.nc','lat','long_name','Latitude')
nccreate('PRECT_EOF.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('PRECT_EOF.nc','lon', lon)
ncwriteatt('PRECT_EOF.nc','lon','units','degrees_east')
ncwriteatt('PRECT_EOF.nc','lon','long_name','Longitude')
nccreate('PRECT_EOF.nc','EOF_PRECT_ANN','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECT_EOF.nc','EOF_PRECT_ANN', EOF_PRECT_ANN)
nccreate('PRECT_EOF.nc','PC_PRECT_ANN','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('PRECT_EOF.nc','PC_PRECT_ANN', PC_PRECT_ANN)
nccreate('PRECT_EOF.nc','EOF_PRECT_DJF','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECT_EOF.nc','EOF_PRECT_DJF', EOF_PRECT_DJF)
nccreate('PRECT_EOF.nc','PC_PRECT_DJF','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('PRECT_EOF.nc','PC_PRECT_DJF', PC_PRECT_DJF)
nccreate('PRECT_EOF.nc','EOF_PRECT_MAM','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECT_EOF.nc','EOF_PRECT_MAM', EOF_PRECT_MAM)
nccreate('PRECT_EOF.nc','PC_PRECT_MAM','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('PRECT_EOF.nc','PC_PRECT_MAM', PC_PRECT_MAM)
nccreate('PRECT_EOF.nc','EOF_PRECT_JJA','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECT_EOF.nc','EOF_PRECT_JJA', EOF_PRECT_JJA)
nccreate('PRECT_EOF.nc','PC_PRECT_JJA','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('PRECT_EOF.nc','PC_PRECT_JJA', PC_PRECT_JJA)
nccreate('PRECT_EOF.nc','EOF_PRECT_SON','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECT_EOF.nc','EOF_PRECT_SON', EOF_PRECT_SON)
nccreate('PRECT_EOF.nc','PC_PRECT_SON','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('PRECT_EOF.nc','PC_PRECT_SON', PC_PRECT_SON)
nccreate('PRECT_EOF.nc','EOF_PRECT_JAN','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECT_EOF.nc','EOF_PRECT_JAN', EOF_PRECT_JAN)
nccreate('PRECT_EOF.nc','PC_PRECT_JAN','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('PRECT_EOF.nc','PC_PRECT_JAN', PC_PRECT_JAN)
nccreate('PRECT_EOF.nc','EOF_PRECT_JUL','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('PRECT_EOF.nc','EOF_PRECT_JUL', EOF_PRECT_JUL)
nccreate('PRECT_EOF.nc','PC_PRECT_JUL','Dimensions',{'year',length(year)},'Format','classic')
ncwrite('PRECT_EOF.nc','PC_PRECT_JUL', PC_PRECT_JUL)
ncdisp('PRECT_EOF.nc')
