lon_ECMWF=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/U10/dat/U10_ECMWF_25x25.nc','longitude');
lat_ECMWF=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/U10/dat/U10_ECMWF_25x25.nc','latitude');
lat=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/PRECT/dat/precip.mon.mean.nc','lat');
lon=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/PRECT/dat/precip.mon.mean.nc','lon');
year=1:14;
month=1:168;

U10_ECMWF_25x25_temp=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/U10/dat/U10_ECMWF_25x25.nc','si10');
U10_ECMWF_25x25_temp2=U10_ECMWF_25x25_temp(:,:,265:432);

U10_ECMWF_25x25_temp3=zeros(length(lon),length(lat)+1,length(month));
for i=1:143
    U10_ECMWF_25x25_temp3(i,:,:)=(U10_ECMWF_25x25_temp2(i,:,:)+U10_ECMWF_25x25_temp2(i+1,:,:)/2);
end
 U10_ECMWF_25x25_temp3(144,:,:)=(U10_ECMWF_25x25_temp2(144,:,:)+U10_ECMWF_25x25_temp2(1,:,:)/2);


U10_ECMWF_25x25=zeros(length(lon),length(lat),length(month));
for j=1:length(lat)
    U10_ECMWF_25x25(:,j,:)=(U10_ECMWF_25x25_temp3(:,j,:)+U10_ECMWF_25x25_temp3(:,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_25x25(:,:,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_25x25_monthly.mat','U10_MONTH')

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