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

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

QRS_temp=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_DT.nc','QRS');

QRS_MONTH=zeros(144,30,1800);


% Deseasonal
for i=1:144
    for j=1:30
        for monthID=1:12
            temp=mean(QRS_temp(i,j,monthID,:),4);
            for yearID=1:150
                QRS_temp(i,j,monthID,yearID)=QRS_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;
                QRS_MONTH(i,j,n)=QRS_temp(i,j,monthID,yearID);
            end
        end
    end
end


QRL_temp=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_DT.nc','QRL');

QRL_MONTH=zeros(144,30,1800);

% Deseasonal
for i=1:144
    for j=1:30
        for monthID=1:12
            temp=mean(QRL_temp(i,j,monthID,:),4);
            for yearID=1:150
                QRL_temp(i,j,monthID,yearID)=QRL_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;
                QRL_MONTH(i,j,n)=QRL_temp(i,j,monthID,yearID);
            end
        end
    end
end


DTV_temp=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_DT.nc','DTV');

DTV_MONTH=zeros(144,30,1800);

% Deseasonal
for i=1:144
    for j=1:30
        for monthID=1:12
            temp=mean(DTV_temp(i,j,monthID,:),4);
            for yearID=1:150
                DTV_temp(i,j,monthID,yearID)=DTV_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;
                DTV_MONTH(i,j,n)=DTV_temp(i,j,monthID,yearID);
            end
        end
    end
end


DTCOND_temp=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_DT.nc','DTCOND');

DTCOND_MONTH=zeros(144,30,1800);

% Deseasonal
for i=1:144
    for j=1:30
        for monthID=1:12
            temp=mean(DTCOND_temp(i,j,monthID,:),4);
            for yearID=1:150
                DTCOND_temp(i,j,monthID,yearID)=DTCOND_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;
                DTCOND_MONTH(i,j,n)=DTCOND_temp(i,j,monthID,yearID);
            end
        end
    end
end


save('DT_monthly.mat','QRS_MONTH','QRL_MONTH','DTV_MONTH','DTCOND_MONTH')

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