load ~/NC/grid
year=1:6;

AOD_temp=ncread('/Users/yang146/CEDS_Emission/TagSO4/read_nc/ncfile/read_nc_AOD.nc','AOD');
AOD_DJF=zeros(144,96,length(year));
AOD_DJF(:,:,1)=mean(AOD_temp(:,:,[1,2],1),3);
for yearID=2:6
    AOD_DJF(:,:,yearID)=(AOD_temp(:,:,12,yearID-1)+AOD_temp(:,:,1,yearID)+AOD_temp(:,:,2,yearID))/3;
end

AODBC_temp=ncread('/Users/yang146/CEDS_Emission/TagSO4/read_nc/ncfile/read_nc_AOD.nc','AODBC');
AODBC_DJF=zeros(144,96,length(year));
AODBC_DJF(:,:,1)=mean(AODBC_temp(:,:,[1,2],1),3);
for yearID=2:6
    AODBC_DJF(:,:,yearID)=(AODBC_temp(:,:,12,yearID-1)+AODBC_temp(:,:,1,yearID)+AODBC_temp(:,:,2,yearID))/3;
end

AODPOM_temp=ncread('/Users/yang146/CEDS_Emission/TagSO4/read_nc/ncfile/read_nc_AOD.nc','AODPOM');
AODPOM_DJF=zeros(144,96,length(year));
AODPOM_DJF(:,:,1)=mean(AODPOM_temp(:,:,[1,2],1),3);
for yearID=2:6
    AODPOM_DJF(:,:,yearID)=(AODPOM_temp(:,:,12,yearID-1)+AODPOM_temp(:,:,1,yearID)+AODPOM_temp(:,:,2,yearID))/3;
end

AODSOA_temp=ncread('/Users/yang146/CEDS_Emission/TagSO4/read_nc/ncfile/read_nc_AOD.nc','AODSOA');
AODSOA_DJF=zeros(144,96,length(year));
AODSOA_DJF(:,:,1)=mean(AODSOA_temp(:,:,[1,2],1),3);
for yearID=2:6
    AODSOA_DJF(:,:,yearID)=(AODSOA_temp(:,:,12,yearID-1)+AODSOA_temp(:,:,1,yearID)+AODSOA_temp(:,:,2,yearID))/3;
end

AODSO4_temp=ncread('/Users/yang146/CEDS_Emission/TagSO4/read_nc/ncfile/read_nc_AOD.nc','AODSO4');
AODSO4_DJF=zeros(144,96,length(year));
AODSO4_DJF(:,:,1)=mean(AODSO4_temp(:,:,[1,2],1),3);
for yearID=2:6
    AODSO4_DJF(:,:,yearID)=(AODSO4_temp(:,:,12,yearID-1)+AODSO4_temp(:,:,1,yearID)+AODSO4_temp(:,:,2,yearID))/3;
end

AODDUST_temp=ncread('/Users/yang146/CEDS_Emission/TagSO4/read_nc/ncfile/read_nc_AOD.nc','AODDUST');
AODDUST_DJF=zeros(144,96,length(year));
AODDUST_DJF(:,:,1)=mean(AODDUST_temp(:,:,[1,2],1),3);
for yearID=2:6
    AODDUST_DJF(:,:,yearID)=(AODDUST_temp(:,:,12,yearID-1)+AODDUST_temp(:,:,1,yearID)+AODDUST_temp(:,:,2,yearID))/3;
end

AODSS_temp=ncread('/Users/yang146/CEDS_Emission/TagSO4/read_nc/ncfile/read_nc_AOD.nc','AODSS');
AODSS_DJF=zeros(144,96,length(year));
AODSS_DJF(:,:,1)=mean(AODSS_temp(:,:,[1,2],1),3);
for yearID=2:6
    AODSS_DJF(:,:,yearID)=(AODSS_temp(:,:,12,yearID-1)+AODSS_temp(:,:,1,yearID)+AODSS_temp(:,:,2,yearID))/3;
end

save('AOD_CEDS.mat','AOD_DJF','AODBC_DJF','AODPOM_DJF','AODSOA_DJF','AODSO4_DJF','AODDUST_DJF','AODSS_DJF')

nccreate('AOD_CEDS.nc','Year','Dimensions',{'time',Inf},'Format','classic')
ncwrite('AOD_CEDS.nc','Year', year)
ncwriteatt('AOD_CEDS.nc','Year','long_name','Year')
nccreate('AOD_CEDS.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('AOD_CEDS.nc','lat', lat)
ncwriteatt('AOD_CEDS.nc','lat','units','degrees_north')
ncwriteatt('AOD_CEDS.nc','lat','long_name','Latitude')
nccreate('AOD_CEDS.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('AOD_CEDS.nc','lon', lon)
ncwriteatt('AOD_CEDS.nc','lon','units','degrees_east')
ncwriteatt('AOD_CEDS.nc','lon','long_name','Longitude')
nccreate('AOD_CEDS.nc','AOD_DJF','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('AOD_CEDS.nc','AOD_DJF', AOD_DJF)
nccreate('AOD_CEDS.nc','AODBC_DJF','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('AOD_CEDS.nc','AODBC_DJF', AODBC_DJF)
nccreate('AOD_CEDS.nc','AODPOM_DJF','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('AOD_CEDS.nc','AODPOM_DJF', AODPOM_DJF)
nccreate('AOD_CEDS.nc','AODSOA_DJF','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('AOD_CEDS.nc','AODSOA_DJF', AODSOA_DJF)
nccreate('AOD_CEDS.nc','AODSO4_DJF','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('AOD_CEDS.nc','AODSO4_DJF', AODSO4_DJF)
nccreate('AOD_CEDS.nc','AODDUST_DJF','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('AOD_CEDS.nc','AODDUST_DJF', AODDUST_DJF)
nccreate('AOD_CEDS.nc','AODSS_DJF','Dimensions',{'lon',144,'lat',96,'year',length(year)},'Format','classic')
ncwrite('AOD_CEDS.nc','AODSS_DJF', AODSS_DJF)
ncdisp('AOD_CEDS.nc')