load ~/NC/grid.mat
load /Users/yangyang/Aerosol_Variaiton/IRUN/AIE_second_efficiency/ncfile/Regions.mat
year=1:150;

RH=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/RH/ncfile/RH.nc','RH_ANN');

RH_ASI=zeros(length(year),1);
for yearID=1:length(year)
    RH_ASI(yearID)=sum(sum(RH(:,:,yearID).*ASI,2),1)/sum(ASI(:));
end
nccreate('RH_region.nc','RH_ASI','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_ASI', RH_ASI)

RH_SPO=zeros(length(year),1);
for yearID=1:length(year)
    RH_SPO(yearID)=sum(sum(RH(:,:,yearID).*SPO,2),1)/sum(SPO(:));
end
nccreate('RH_region.nc','RH_SPO','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_SPO', RH_SPO)

RH_SAO=zeros(length(year),1);
for yearID=1:length(year)
    RH_SAO(yearID)=sum(sum(RH(:,:,yearID).*SAO,2),1)/sum(SAO(:));
end
nccreate('RH_region.nc','RH_SAO','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_SAO', RH_SAO)

RH_SIO=zeros(length(year),1);
for yearID=1:length(year)
    RH_SIO(yearID)=sum(sum(RH(:,:,yearID).*SIO,2),1)/sum(SIO(:));
end
nccreate('RH_region.nc','RH_SIO','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_SIO', RH_SIO)

RH_TPO=zeros(length(year),1);
for yearID=1:length(year)
    RH_TPO(yearID)=sum(sum(RH(:,:,yearID).*TPO,2),1)/sum(TPO(:));
end
nccreate('RH_region.nc','RH_TPO','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_TPO', RH_TPO)

RH_TAO=zeros(length(year),1);
for yearID=1:length(year)
    RH_TAO(yearID)=sum(sum(RH(:,:,yearID).*TAO,2),1)/sum(TAO(:));
end
nccreate('RH_region.nc','RH_TAO','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_TAO', RH_TAO)

RH_TIO=zeros(length(year),1);
for yearID=1:length(year)
    RH_TIO(yearID)=sum(sum(RH(:,:,yearID).*TIO,2),1)/sum(TIO(:));
end
nccreate('RH_region.nc','RH_TIO','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_TIO', RH_TIO)

RH_NPO=zeros(length(year),1);
for yearID=1:length(year)
    RH_NPO(yearID)=sum(sum(RH(:,:,yearID).*NPO,2),1)/sum(NPO(:));
end
nccreate('RH_region.nc','RH_NPO','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_NPO', RH_NPO)

RH_NAO=zeros(length(year),1);
for yearID=1:length(year)
    RH_NAO(yearID)=sum(sum(RH(:,:,yearID).*NAO,2),1)/sum(NAO(:));
end
nccreate('RH_region.nc','RH_NAO','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_NAO', RH_NAO)

RH_SAM=zeros(length(year),1);
for yearID=1:length(year)
    RH_SAM(yearID)=sum(sum(RH(:,:,yearID).*SAM,2),1)/sum(SAM(:));
end
nccreate('RH_region.nc','RH_SAM','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_SAM', RH_SAM)

RH_AFR=zeros(length(year),1);
for yearID=1:length(year)
    RH_AFR(yearID)=sum(sum(RH(:,:,yearID).*AFR,2),1)/sum(AFR(:));
end
nccreate('RH_region.nc','RH_AFR','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_AFR', RH_AFR)

RH_OCE=zeros(length(year),1);
for yearID=1:length(year)
    RH_OCE(yearID)=sum(sum(RH(:,:,yearID).*OCE,2),1)/sum(OCE(:));
end
nccreate('RH_region.nc','RH_OCE','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_OCE', RH_OCE)

RH_NAM=zeros(length(year),1);
for yearID=1:length(year)
    RH_NAM(yearID)=sum(sum(RH(:,:,yearID).*NAM,2),1)/sum(NAM(:));
end
nccreate('RH_region.nc','RH_NAM','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_NAM', RH_NAM)

RH_EUR=zeros(length(year),1);
for yearID=1:length(year)
    RH_EUR(yearID)=sum(sum(RH(:,:,yearID).*EUR,2),1)/sum(EUR(:));
end
nccreate('RH_region.nc','RH_EUR','Dimensions',{'Year',length(year)},'Format','classic')
ncwrite('RH_region.nc','RH_EUR', RH_EUR)

save('RH_region.mat','RH_ASI','RH_SPO','RH_SAO','RH_SIO','RH_TPO','RH_TAO','RH_TIO','RH_NPO','RH_NAO','RH_SAM','RH_AFR','RH_OCE','RH_NAM','RH_EUR')
                      
