load ~/NC/grid
load ~/NC/area_1.9_2.5.mat
load ~/NC/OCNFRAC.mat

year=1:150;
month=1:1800;

DUSTSF_temp=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_DUSTSF.nc','DUSTSF');

DUSTSF_MONTH=zeros(144,96,1800);


% Deseasonal
for i=1:144
    for j=1:96
        for monthID=1:12
            temp=mean(DUSTSF_temp(i,j,monthID,:),4);
            for yearID=1:150
                DUSTSF_temp(i,j,monthID,yearID)=DUSTSF_temp(i,j,monthID,yearID)-temp;
            end
        end
    end
end

% Change size
for i=1:144
    for j=1:96
        n=0;
        for yearID=1:150
            for monthID=1:12
                n=n+1;
                DUSTSF_MONTH(i,j,n)=DUSTSF_temp(i,j,monthID,yearID);
            end
        end
    end
end

%Global mean
DUSTSF_GLOBAL_MONTH=zeros(1800,1);
for yearID=1:1800
    DUSTSF_GLOBAL_MONTH(yearID)=sum(sum(DUSTSF_MONTH(:,:,yearID).*area,2),1)/sum(area(:));
end

%TPO mean
TPO=zeros(144,96);
TPO(lon>=125&lon<=290,lat>-20&lat<20)=1;
for i=1:144
    for j=1:96
        if OCNFRAC(i,j)~=1;
            TPO(i,j)=0;
        end
    end
end

DUSTSF_TPO_MONTH=zeros(1800,1);
for yearID=1:1800
    for i=1:144
    for j=1:96
        if TPO(i,j)==1;
            DUSTSF_TPO_MONTH(yearID)=DUSTSF_TPO_MONTH(yearID)+DUSTSF_MONTH(i,j,yearID)*area(i,j);
        end
    end
    end
end
DUSTSF_TPO_MONTH=DUSTSF_TPO_MONTH/sum(sum(TPO.*area,2),1);


save('DUSTSF_monthly.mat','DUSTSF_MONTH')

nccreate('DUSTSF_monthly.nc','Month','Dimensions',{'time',Inf},'Format','classic')
ncwrite('DUSTSF_monthly.nc','Month', month)
ncwriteatt('DUSTSF_monthly.nc','Month','long_name','Month')
nccreate('DUSTSF_monthly.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('DUSTSF_monthly.nc','lat', lat)
ncwriteatt('DUSTSF_monthly.nc','lat','units','degrees_north')
ncwriteatt('DUSTSF_monthly.nc','lat','long_name','Latitude')
nccreate('DUSTSF_monthly.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('DUSTSF_monthly.nc','lon', lon)
ncwriteatt('DUSTSF_monthly.nc','lon','units','degrees_east')
ncwriteatt('DUSTSF_monthly.nc','lon','long_name','Longitude')
nccreate('DUSTSF_monthly.nc','DUSTSF_MONTH','Dimensions',{'lon',144,'lat',96,'month',length(month)},'Format','classic')
ncwrite('DUSTSF_monthly.nc','DUSTSF_MONTH', DUSTSF_MONTH)
ncdisp('DUSTSF_monthly.nc')