load ~/NC/grid.mat
load ~/Aerosol_Variaiton/IRUN/SST/ncfile/ENSO_Month.mat
load ~/Aerosol_Variaiton/IRUN/DT/ncfile/DT_monthly.mat
month=1:1800;

a=sum(EN_Month);
b=sum(LL_Month);

QRS_EN_month=zeros(144,30,a);
QRS_LL_month=zeros(144,30,b);

n=0;
m=0;
for monthID=1:length(month)
    if EN_Month(monthID)==1
        n=n+1;
        QRS_EN_month(:,:,n)=QRS_MONTH(:,:,monthID);
    end
    if LL_Month(monthID)==1
        m=m+1;
        QRS_LL_month(:,:,m)=QRS_MONTH(:,:,monthID);
    end
end

QRS_EN=mean(QRS_EN_month,3);
QRS_LL=mean(QRS_LL_month,3);

QRS_ENSO=QRS_EN-QRS_LL;


QRL_EN_month=zeros(144,30,a);
QRL_LL_month=zeros(144,30,b);

n=0;
m=0;
for monthID=1:length(month)
    if EN_Month(monthID)==1
        n=n+1;
        QRL_EN_month(:,:,n)=QRL_MONTH(:,:,monthID);
    end
    if LL_Month(monthID)==1
        m=m+1;
        QRL_LL_month(:,:,m)=QRL_MONTH(:,:,monthID);
    end
end

QRL_EN=mean(QRL_EN_month,3);
QRL_LL=mean(QRL_LL_month,3);

QRL_ENSO=QRL_EN-QRL_LL;


DTV_EN_month=zeros(144,30,a);
DTV_LL_month=zeros(144,30,b);

n=0;
m=0;
for monthID=1:length(month)
    if EN_Month(monthID)==1
        n=n+1;
        DTV_EN_month(:,:,n)=DTV_MONTH(:,:,monthID);
    end
    if LL_Month(monthID)==1
        m=m+1;
        DTV_LL_month(:,:,m)=DTV_MONTH(:,:,monthID);
    end
end

DTV_EN=mean(DTV_EN_month,3);
DTV_LL=mean(DTV_LL_month,3);

DTV_ENSO=DTV_EN-DTV_LL;


DTCOND_EN_month=zeros(144,30,a);
DTCOND_LL_month=zeros(144,30,b);

n=0;
m=0;
for monthID=1:length(month)
    if EN_Month(monthID)==1
        n=n+1;
        DTCOND_EN_month(:,:,n)=DTCOND_MONTH(:,:,monthID);
    end
    if LL_Month(monthID)==1
        m=m+1;
        DTCOND_LL_month(:,:,m)=DTCOND_MONTH(:,:,monthID);
    end
end

DTCOND_EN=mean(DTCOND_EN_month,3);
DTCOND_LL=mean(DTCOND_LL_month,3);

DTCOND_ENSO=DTCOND_EN-DTCOND_LL;


save('DT_ENSO_composite_monthly.mat','QRS_ENSO','QRL_ENSO','DTV_ENSO','DTCOND_ENSO')

nccreate('DT_ENSO_composite_monthly.nc','lev','Dimensions',{'lev',30},'Format','classic')
ncwrite('DT_ENSO_composite_monthly.nc','lev', lev)
nccreate('DT_ENSO_composite_monthly.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('DT_ENSO_composite_monthly.nc','lat', lat)
ncwriteatt('DT_ENSO_composite_monthly.nc','lat','units','degrees_north')
ncwriteatt('DT_ENSO_composite_monthly.nc','lat','long_name','Latitude')
nccreate('DT_ENSO_composite_monthly.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('DT_ENSO_composite_monthly.nc','lon', lon)
ncwriteatt('DT_ENSO_composite_monthly.nc','lon','units','degrees_east')
ncwriteatt('DT_ENSO_composite_monthly.nc','lon','long_name','Longitude')
nccreate('DT_ENSO_composite_monthly.nc','QRS_ENSO','Dimensions',{'lon',144,'lev',30},'Format','classic')
ncwrite('DT_ENSO_composite_monthly.nc','QRS_ENSO', QRS_ENSO)
nccreate('DT_ENSO_composite_monthly.nc','QRL_ENSO','Dimensions',{'lon',144,'lev',30},'Format','classic')
ncwrite('DT_ENSO_composite_monthly.nc','QRL_ENSO', QRL_ENSO)
nccreate('DT_ENSO_composite_monthly.nc','DTV_ENSO','Dimensions',{'lon',144,'lev',30},'Format','classic')
ncwrite('DT_ENSO_composite_monthly.nc','DTV_ENSO', DTV_ENSO)
nccreate('DT_ENSO_composite_monthly.nc','DTCOND_ENSO','Dimensions',{'lon',144,'lev',30},'Format','classic')
ncwrite('DT_ENSO_composite_monthly.nc','DTCOND_ENSO', DTCOND_ENSO)
ncdisp('DT_ENSO_composite_monthly.nc')
