year=2001:2016;
lat=ncread('/Users/yang146/Aerosol_Variaiton/CERES/dat/CERES_EBAF-Surface_Ed2.8_Subset_200003-201602.nc','lat');
lon=ncread('/Users/yang146/Aerosol_Variaiton/CERES/dat/CERES_EBAF-Surface_Ed2.8_Subset_200003-201602.nc','lon');

temp=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/WIND/ncfile/WIND_NCEP_1949_2016.nc','WIND_DJF');
WIND=temp(:,:,2001-1949+1:2016-1949+1);
WIND_ECHINA=zeros(length(year),1);
WIND_ECHINA(:)=mean(mean(WIND(45:50,19:29,:),2),1);

FSNS=ncread('/Users/yang146/Aerosol_Variaiton/CERES/ncfile/FNS_2001_2016_DJF.nc','FSNS_DJF');

y=zeros(length(year),1);
x=zeros(length(year),1);

reg=zeros(length(lon),length(lat));
sig=zeros(length(lon),length(lat));
for i=1:length(lon)
    for j=1:length(lat)
        y(:)=FSNS(i,j,:);
        x(:)=WIND_ECHINA;
        X=[ones(length(year),1),x];
        [b,bint,r,rint,stats]=regress(y,X);
        reg(i,j)=b(2);
        sig(i,j)=double(stats(3)<0.05);        
    end
end


reg(isnan(reg))=-999;


save('reg_FSNS_WIND_EC.mat','reg')

nccreate('reg_FSNS_WIND_EC.nc','lat','Dimensions',{'lat',length(lat)},'Format','classic')
ncwrite('reg_FSNS_WIND_EC.nc','lat', lat)
ncwriteatt('reg_FSNS_WIND_EC.nc','lat','units','degrees_north')
ncwriteatt('reg_FSNS_WIND_EC.nc','lat','long_name','Latitude')
nccreate('reg_FSNS_WIND_EC.nc','lon','Dimensions',{'lon',length(lon)},'Format','classic')
ncwrite('reg_FSNS_WIND_EC.nc','lon', lon)
ncwriteatt('reg_FSNS_WIND_EC.nc','lon','units','degrees_east')
ncwriteatt('reg_FSNS_WIND_EC.nc','lon','long_name','Longitude')
nccreate('reg_FSNS_WIND_EC.nc','reg','Dimensions',{'lon',length(lon),'lat',length(lat)},'Format','classic')
ncwrite('reg_FSNS_WIND_EC.nc','reg', reg)
nccreate('reg_FSNS_WIND_EC.nc','sig','Dimensions',{'lon',length(lon),'lat',length(lat)},'Format','classic')
ncwrite('reg_FSNS_WIND_EC.nc','sig', sig)
ncdisp('reg_FSNS_WIND_EC.nc')
