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

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

SSLTSF2_temp=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_SSLTSF.nc','SSLTSF2');

SSLTSF2_MONTH=zeros(144,96,1800);


% Deseasonal
for i=1:144
    for j=1:96
        for monthID=1:12
            temp=mean(SSLTSF2_temp(i,j,monthID,:),4);
            for yearID=1:150
                SSLTSF2_temp(i,j,monthID,yearID)=SSLTSF2_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;
                SSLTSF2_MONTH(i,j,n)=SSLTSF2_temp(i,j,monthID,yearID);
            end
        end
    end
end



save('SSLTSF2_monthly.mat','SSLTSF2_MONTH')

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