lon=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/U10/dat/uwnd.10m.gauss.2001.nc','lon');
lat=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/U10/dat/uwnd.10m.gauss.2001.nc','lat');
year=1:14;
month=1:168;
yearreal=2001:2014;

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

for yearID=1:length(year)
    utemp=ncread(['/Users/yangyang/Aerosol_Variaiton/IRUN/U10/dat/uwnd.10m.gauss.',num2str(yearreal(yearID)),'.nc'],'uwnd');
    vtemp=ncread(['/Users/yangyang/Aerosol_Variaiton/IRUN/U10/dat/vwnd.10m.gauss.',num2str(yearreal(yearID)),'.nc'],'vwnd');
    for monthID=1:12
        U10_temp(:,:,1,yearID)=sqrt((mean(utemp(:,:,1:31),3)).^2+(mean(vtemp(:,:,1:31),3)).^2);
        U10_temp(:,:,2,yearID)=sqrt((mean(utemp(:,:,32:59),3)).^2+(mean(vtemp(:,:,32:59),3)).^2);
        U10_temp(:,:,3,yearID)=sqrt((mean(utemp(:,:,60:90),3)).^2+(mean(vtemp(:,:,60:90),3)).^2);
        U10_temp(:,:,4,yearID)=sqrt((mean(utemp(:,:,91:120),3)).^2+(mean(vtemp(:,:,91:120),3)).^2);
        U10_temp(:,:,5,yearID)=sqrt((mean(utemp(:,:,121:151),3)).^2+(mean(vtemp(:,:,121:151),3)).^2);
        U10_temp(:,:,6,yearID)=sqrt((mean(utemp(:,:,152:181),3)).^2+(mean(vtemp(:,:,152:181),3)).^2);
        U10_temp(:,:,7,yearID)=sqrt((mean(utemp(:,:,182:212),3)).^2+(mean(vtemp(:,:,182:212),3)).^2);
        U10_temp(:,:,8,yearID)=sqrt((mean(utemp(:,:,213:243),3)).^2+(mean(vtemp(:,:,213:243),3)).^2);
        U10_temp(:,:,9,yearID)=sqrt((mean(utemp(:,:,244:273),3)).^2+(mean(vtemp(:,:,244:273),3)).^2);
        U10_temp(:,:,10,yearID)=sqrt((mean(utemp(:,:,274:304),3)).^2+(mean(vtemp(:,:,274:304),3)).^2);
        U10_temp(:,:,11,yearID)=sqrt((mean(utemp(:,:,305:334),3)).^2+(mean(vtemp(:,:,305:334),3)).^2);
        U10_temp(:,:,12,yearID)=sqrt((mean(utemp(:,:,335:365),3)).^2+(mean(vtemp(:,:,335:365),3)).^2);
    end
end
    
        


% 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_NCEP_monthly.mat','U10_MONTH')

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