lon_ECMWF=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/CIR/dat/CIR_ECMWF.nc','longitude');
lat_ECMWF=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/CIR/dat/CIR_ECMWF.nc','latitude');
lat=-89.5:2.5:89.5;
lon=lon_ECMWF;
lev=[0.2,0.3843,0.6365,0.9564,1.3448,1.8058,2.3478,2.985,3.7397,4.6462,5.7565,7.1322,8.8366,10.9483,13.5647,16.8064,20.8227,25.7989,31.9642,39.6029,49.0671,60.1802,73.0663,87.7274,104.2288,122.6137,142.9017,165.0886,189.1466,215.0251,242.6523,272.0593,303.2174,336.0439,370.4072,406.1328,443.0086,480.7907,519.2093,557.9734,596.7774,635.306,673.2403,710.2627,746.0635,780.3455,812.8303,843.2634,871.4203,897.1118,920.1893,940.5511,958.1477,972.9868,985.1399,994.7472,1002.0236,1007.2639,1010.8487,1013.25];
year=1:36;
month=1:432;


UWND_ECMWF_temp=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/CIR/dat/CIR_ECMWF.nc','u');
UWND_ECMWF=zeros(length(lon),length(lev),length(month));
UWND_ECMWF(:,:,:)=mean(UWND_ECMWF_temp(:,33:41,:,:),2);


UWND_temp=zeros(length(lon),length(lev),12,length(year));

n=0;
for yearID=1:length(year)
    for monthID=1:12
        n=n+1;
        UWND_temp(:,:,monthID,yearID)=UWND_ECMWF(:,:,n);
    end
end

UWND_MONTH=zeros(length(lon),length(lev),length(month));
% Deseasonal
for i=1:length(lon)
    for j=1:length(lev)
        for monthID=1:12
            temp=mean(UWND_temp(i,j,monthID,:),4);
            for yearID=1:length(year)
                UWND_temp(i,j,monthID,yearID)=UWND_temp(i,j,monthID,yearID)-temp;
            end
        end
    end
end

% Change size
for i=1:length(lon)
    for j=1:length(lev)
        n=0;
        for yearID=1:length(year)
            for monthID=1:12
                n=n+1;
                UWND_MONTH(i,j,n)=UWND_temp(i,j,monthID,yearID);
            end
        end
    end
end



OMEGA_ECMWF_temp=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/CIR/dat/CIR_ECMWF.nc','w');
OMEGA_ECMWF=zeros(length(lon),length(lev),length(month));
OMEGA_ECMWF(:,:,:)=mean(OMEGA_ECMWF_temp(:,33:41,:,:),2);


OMEGA_temp=zeros(length(lon),length(lev),12,length(year));

n=0;
for yearID=1:length(year)
    for monthID=1:12
        n=n+1;
        OMEGA_temp(:,:,monthID,yearID)=OMEGA_ECMWF(:,:,n);
    end
end

OMEGA_MONTH=zeros(length(lon),length(lev),length(month));
% Deseasonal
for i=1:length(lon)
    for j=1:length(lev)
        for monthID=1:12
            temp=mean(OMEGA_temp(i,j,monthID,:),4);
            for yearID=1:length(year)
                OMEGA_temp(i,j,monthID,yearID)=OMEGA_temp(i,j,monthID,yearID)-temp;
            end
        end
    end
end

% Change size
for i=1:length(lon)
    for j=1:length(lev)
        n=0;
        for yearID=1:length(year)
            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_ECMWF.mat','UWND_MONTH','OMEGA_MONTH')

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