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

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

UWND_temp=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_CIR.nc','UWND');

UWND_MONTH=zeros(144,30,1800);


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

% Change size
for i=1:144
    for j=1:30
        n=0;
        for yearID=1:150
            for monthID=1:12
                n=n+1;
                UWND_MONTH(i,j,n)=UWND_temp(i,j,monthID,yearID);
            end
        end
    end
end


OMEGA_temp=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_CIR.nc','OMEGA');

OMEGA_MONTH=zeros(144,30,1800);

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

% Change size
for i=1:144
    for j=1:30
        n=0;
        for yearID=1:150
            for monthID=1:12
                n=n+1;
                OMEGA_MONTH(i,j,n)=OMEGA_temp(i,j,monthID,yearID);
            end
        end
    end
end


save('CIR_monthly.mat','UWND_MONTH','OMEGA_MONTH')

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