lon_ECMWF=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/U10/dat/U10_ECMWF.nc','longitude');
lat_ECMWF=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/U10/dat/U10_ECMWF.nc','latitude');
lat=-89.5:1:89.5;
lon=-179.5:1:179.5;
year=1:14;
month=1:168;

U10_ECMWF_temp=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/U10/dat/U10_ECMWF.nc','si10');
U10_ECMWF_temp2=U10_ECMWF_temp(:,:,265:432);
temp=U10_ECMWF_temp2(1:180,:,:);
U10_ECMWF_temp2(1:180,:,:)=U10_ECMWF_temp2(181:360,:,:);
U10_ECMWF_temp2(181:360,:,:)=temp;
U10_ECMWF_temp2(:,1:181,:)=U10_ECMWF_temp2(:,181:-1:1,:);

U10_ECMWF=zeros(length(lon),length(lat),length(month));
for j=1:180
    U10_ECMWF(:,j,:)=(U10_ECMWF_temp2(:,j,:)+U10_ECMWF_temp2(:,j+1,:))/2;
end

U10_temp=zeros(length(lon),length(lat),12,length(year));

n=0;
for yearID=1:14
    for monthID=1:12
        n=n+1;
        U10_temp(:,:,monthID,yearID)=U10_ECMWF(:,:,n);
    end
end
        



U10_MONTH=zeros(length(lon),length(lat),length(month));

% Deseasonal
for i=1:length(lon)
    for j=1:length(lat)
        for monthID=1:12
            temp=mean(U10_temp(i,j,monthID,:),4);
            for yearID=1:length(year)
                U10_temp(i,j,monthID,yearID)=U10_temp(i,j,monthID,yearID)-temp;
            end
        end
    end
end

% Change size
for i=1:length(lon)
    for j=1:length(lat)
        n=0;
        for yearID=1:length(year)
            for monthID=1:12
                n=n+1;
                U10_MONTH(i,j,n)=U10_temp(i,j,monthID,yearID);
            end
        end
    end
end



save('U10_ECMWF_monthly.mat','U10_MONTH')

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