lon=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/VWND/dat/vwnd.mon.mean.nc','lon');
lat=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/VWND/dat/vwnd.mon.mean.nc','lat');
year=1:14;
month=1:168;

v_temp=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/VWND/dat/vwnd.mon.mean.nc','vwnd');

VWND_temp=zeros(144,73,12,length(year));

for yearID=(1+2001-1948):(length(year)+2001-1948)
    VWND_temp(:,:,:,yearID-(2001-1948))=v_temp(:,:,3,(yearID-1)*12+1:(yearID-1)*12+12);
end


VWND_MONTH=zeros(144,73,length(month));


% Deseasonal
for i=1:144
    for j=1:73
        for monthID=1:12
            temp=mean(VWND_temp(i,j,monthID,:),4);
            for yearID=1:length(year)
                VWND_temp(i,j,monthID,yearID)=VWND_temp(i,j,monthID,yearID)-temp;
            end
        end
    end
end

% Change size
for i=1:144
    for j=1:73
        n=0;
        for yearID=1:length(year)
            for monthID=1:12
                n=n+1;
                VWND_MONTH(i,j,n)=VWND_temp(i,j,monthID,yearID);
            end
        end
    end
end



save('VWND_NCEP_monthly.mat','VWND_MONTH')

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