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');

AI_12=ncread('/Users/yangyang/Aerosol_Variaiton/TOMS/dat/TOMS_Aerosol_Index_12_1978_1992.nc','ave_monthly_AI');
AI_1=ncread('/Users/yangyang/Aerosol_Variaiton/TOMS/dat/TOMS_Aerosol_Index_01_1979_1993.nc','ave_monthly_AI');
AI_2=ncread('/Users/yangyang/Aerosol_Variaiton/TOMS/dat/TOMS_Aerosol_Index_02_1979_1993.nc','ave_monthly_AI');

for i=1:length(lon)
    for j=1:length(lat)
        for yearID=1:length(year)
            if AI_12(i,j,yearID)==0
                AI_12(i,j,:)=-999;
            end
            if AI_1(i,j,yearID)==0
                AI_1(i,j,:)=-999;
            end
            if AI_2(i,j,yearID)==0
                AI_2(i,j,:)=-999;
            end
        end
    end
end

AI_DJF=(AI_12+AI_1+AI_2)/3;

for i=1:length(lon)
    for j=1:length(lat)
        for yearID=1:length(year)
            if AI_DJF(i,j,yearID)<0
                AI_DJF(i,j,:)=-999;
            end
        end
    end
end


save('AI.mat','AI_DJF')

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