lat=-89.5:1:89.5;
lon=-179.5:1:179.5;
year=2001:2016;

AODBD=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/AODBD/ncfile/AODBD_MODIS_2001_2016.nc','AODBD_DJF');

temp=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/WIND/ncfile/WIND_NCEP_1949_2016.nc','WIND_DJF');
WIND=temp(:,:,2001-1949+1:2016-1949+1);
lon_NCEP=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/WIND/ncfile/WIND_NCEP_1949_2016.nc','lon');
lat_NCEP=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/WIND/ncfile/WIND_NCEP_1949_2016.nc','lat');

[~,lonmin]=min(abs(lon_NCEP-110));
[~,lonmax]=min(abs(lon_NCEP-122.5));
[~,latmin]=min(abs(lat_NCEP-20));
[~,latmax]=min(abs(lat_NCEP-45));

%[~,lonmin]=min(abs(lon_NCEP-100));
%[~,lonmax]=min(abs(lon_NCEP-110));
%[~,latmin]=min(abs(lat_NCEP-35));
%[~,latmax]=min(abs(lat_NCEP-45));

WIND_Gobi=zeros(length(year),1);
WIND_Gobi(:)=mean(mean(WIND(lonmin:lonmax,latmax:latmin,:),2),1);

[~,lonmin]=min(abs(lon-100));
[~,lonmax]=min(abs(lon-110));
[~,latmin]=min(abs(lat-35));
[~,latmax]=min(abs(lat-45));

AODBD_Gobi=zeros(length(year),1);
AODBD_Gobi(:)=mean(mean(AODBD(lonmin:lonmax,latmin:latmax,:),2),1);









R=zeros(length(lon),length(lat));
P=zeros(length(lon),length(lat));
for i=1:length(lon)
    for j=1:length(lat)
        [R_temp,P_temp]=corrcoef(AODBD(i,j,:),WIND_Gobi);
        R(i,j)=R_temp(1,2);
        P(i,j)=P_temp(1,2);
    end
end
        
R(isnan(R))=0;
P(isnan(P))=-999;


save('corr_AODBD_MODIS_WINDGobi.mat','R','P')

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