year=1978:1992;

lon=ncread('/Users/yangyang/Aerosol_Variaiton/TOMS/dat/TOMS_Aerosol_Index_12_1978_1992.nc','lon');
lat=ncread('/Users/yangyang/Aerosol_Variaiton/TOMS/dat/TOMS_Aerosol_Index_12_1978_1992.nc','lat');

WIND_year=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/WIND/ncfile/WIND_NCEP_10th_1978_1992.nc','WIND_year');
AI=ncread('/Users/yangyang/Aerosol_Variaiton/TOMS/ncfile/AI.nc','AI_DJF');

AI_ECHINA=zeros(length(year),1);
AI_ECHINA(:)=mean(mean(AI(223:242,70:46,:),2),1);

AI_weak_temp=zeros(length(lon),length(lat));
AI_ECHINA_weak=0;
n=0;
for yearID=1:length(year)
    if WIND_year(yearID)==1
        n=n+1;
        AI_weak_temp(:,:,n)=AI(:,:,yearID);
        AI_ECHINA_weak=AI_ECHINA_weak+AI_ECHINA(yearID)/sum(WIND_year);
    end
end
AI_weak=mean(AI_weak_temp,3);

     


AI_aveg=mean(AI,3);
AI_diff=AI_weak-AI_aveg;

AI_ECHINA_aveg=mean(AI_ECHINA);
AI_ECHINA_diff=AI_ECHINA_weak-AI_ECHINA_aveg;

for i=1:length(lon)
    for j=1:length(lat)
        if AI_weak(i,j)<0
            AI_diff(i,j)=-999;
        end
    end
end



h=zeros(length(lon),length(lat));
temp1=zeros(sum(WIND_year),1);
temp2=zeros(length(year),1);
for i=1:length(lon)
    for j=1:length(lat)
        temp1(:)=AI_weak_temp(i,j,:);
        temp2(:)=AI(i,j,:);
        temp1(temp1<0)=nan;
        temp2(temp2<0)=nan;
        h(i,j)=ttest2(temp1,temp2);
    end
end

h(isnan(h))=-999;


save('AI_10th.mat','AI_weak','AI_aveg','AI_diff','h')

nccreate('AI_10th.nc','lat','Dimensions',{'lat',length(lat)},'Format','classic')
ncwrite('AI_10th.nc','lat', lat)
ncwriteatt('AI_10th.nc','lat','units','degrees_north')
ncwriteatt('AI_10th.nc','lat','long_name','Latitude')
nccreate('AI_10th.nc','lon','Dimensions',{'lon',length(lon)},'Format','classic')
ncwrite('AI_10th.nc','lon', lon)
ncwriteatt('AI_10th.nc','lon','units','degrees_east')
ncwriteatt('AI_10th.nc','lon','long_name','Longitude')
nccreate('AI_10th.nc','AI_weak','Dimensions',{'lon',length(lon),'lat',length(lat)},'Format','classic')
ncwrite('AI_10th.nc','AI_weak', AI_weak)
nccreate('AI_10th.nc','AI_aveg','Dimensions',{'lon',length(lon),'lat',length(lat)},'Format','classic')
ncwrite('AI_10th.nc','AI_aveg', AI_aveg)
nccreate('AI_10th.nc','AI_diff','Dimensions',{'lon',length(lon),'lat',length(lat)},'Format','classic')
ncwrite('AI_10th.nc','AI_diff', AI_diff)
nccreate('AI_10th.nc','h','Dimensions',{'lon',length(lon),'lat',length(lat)},'Format','classic')
ncwrite('AI_10th.nc','h', h)
ncdisp('AI_10th.nc')