year=2001:2014;
lat=88.75:-2.5:-88.75;
lon=1.25:2.5:358.75;
lat_temp=-88.75:2.5:88.75;
lon_temp=-178.75:2.5:178.75;
lat_MODIS=-89.5:1:89.5;
lon_MODIS=-179.5:1:179.5;
month=1:168;

AOD_MONTH_temp=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/AOD/ncfile/AOD_MODIS.nc','AOD_MONTH');

AOD_MONTH_regrid=zeros(length(lon),length(lat),12,length(year));

[lat2,lon2]=meshgrid(lat_temp,lon_temp);
for yearID=1:length(year)
    for monthID=1:12
        AOD_MONTH_regrid(:,:,monthID,yearID)=interp2(lat_MODIS,lon_MODIS,AOD_MONTH_temp(:,:,monthID,yearID),lat2,lon2,'cubic');
    end
end

temp=AOD_MONTH_regrid(1:72,:,:,:);
AOD_MONTH_regrid(1:72,:,:,:)=AOD_MONTH_regrid(73:144,:,:,:);
AOD_MONTH_regrid(73:144,:,:,:)=temp;

AOD_MONTH=AOD_MONTH_regrid(:,72:-1:1,:,:);

AOD_MONTH(AOD_MONTH<0)=-999;
AOD_MONTH(AOD_MONTH>2)=-999;

save('AOD_MODIS_25x25.mat','AOD_MONTH')

nccreate('AOD_MODIS_25x25.nc','Year','Dimensions',{'time',Inf},'Format','classic')
ncwrite('AOD_MODIS_25x25.nc','Year', year)
nccreate('AOD_MODIS_25x25.nc','lat','Dimensions',{'lat',length(lat)},'Format','classic')
ncwrite('AOD_MODIS_25x25.nc','lat', lat)
ncwriteatt('AOD_MODIS_25x25.nc','lat','units','degrees_north')
ncwriteatt('AOD_MODIS_25x25.nc','lat','long_name','Latitude')
nccreate('AOD_MODIS_25x25.nc','lon','Dimensions',{'lon',length(lon)},'Format','classic')
ncwrite('AOD_MODIS_25x25.nc','lon', lon)
ncwriteatt('AOD_MODIS_25x25.nc','lon','units','degrees_east')
ncwriteatt('AOD_MODIS_25x25.nc','lon','long_name','Longitude')
nccreate('AOD_MODIS_25x25.nc','AOD_MONTH','Dimensions',{'lon',length(lon),'lat',length(lat),'month',12,'year',length(year)},'Format','classic')
ncwrite('AOD_MODIS_25x25.nc','AOD_MONTH', AOD_MONTH)
ncdisp('AOD_MODIS_25x25.nc')


