load ~/NC/grid.mat
load ~/NC/area_1.9_2.5.mat
load ~/Aerosol_Variaiton/IRUN/WIND/ncfile/WIND_monthly.mat
month=1:1800;

WIND_SD_MONTH=zeros(144,96);
WIND_RSD_MONTH=zeros(144,96);


UWND=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_UWND.nc','U');
VWND=ncread('~/Aerosol_Variaiton/IRUN/read_nc/ncfile/read_nc_VWND.nc','V');
WIND_temp=sqrt(UWND.*UWND+VWND.*VWND);
WIND_MONTH2=zeros(144,96,1800);
% Change size
for i=1:144
    for j=1:96
        n=0;
        for yearID=1:150
            for monthID=1:12
                n=n+1;
                WIND_MONTH2(i,j,n)=WIND_temp(i,j,monthID,yearID);
            end
        end
    end
end


for i=1:144
    for j=1:96
        WIND_SD_MONTH(i,j)=std(WIND_MONTH(i,j,:));
        WIND_RSD_MONTH(i,j)=WIND_SD_MONTH(i,j)./-(mean(WIND_MONTH2(i,j,:),3));
    end
end



WIND_GLOBAL_MONTH_temp=zeros(12,150);
WIND_GLOBAL_MONTH=zeros(length(month),1);
for yearID=1:150
    for monthID=1:12
        WIND_GLOBAL_MONTH_temp(monthID,yearID)=sum(sum(WIND_temp(:,:,monthID,yearID).*area,2),1)/sum(area(:));
    end
end

WIND_GLOBAL_MONTH_temp2=mean(WIND_GLOBAL_MONTH_temp(:));

for monthID=1:12
    temp=mean(WIND_GLOBAL_MONTH_temp(monthID,:),2);
	for yearID=1:150
        WIND_GLOBAL_MONTH_temp(monthID,yearID)=WIND_GLOBAL_MONTH_temp(monthID,yearID)-temp;
	end
end

n=0;
for yearID=1:150
	for monthID=1:12
        n=n+1;
        WIND_GLOBAL_MONTH(n)=WIND_GLOBAL_MONTH_temp(monthID,yearID);
	end
end

WIND_GLOBAL_SD_MONTH=std(WIND_GLOBAL_MONTH);
WIND_GLOBAL_RSD_MONTH=WIND_GLOBAL_SD_MONTH/-WIND_GLOBAL_MONTH_temp2;


save('WIND_SD_RSD_monthly.mat','WIND_SD_MONTH','WIND_RSD_MONTH')

nccreate('WIND_SD_RSD_monthly.nc','lat','Dimensions',{'lat',96},'Format','classic')
ncwrite('WIND_SD_RSD_monthly.nc','lat', lat)
ncwriteatt('WIND_SD_RSD_monthly.nc','lat','units','degrees_north')
ncwriteatt('WIND_SD_RSD_monthly.nc','lat','long_name','Latitude')
nccreate('WIND_SD_RSD_monthly.nc','lon','Dimensions',{'lon',144},'Format','classic')
ncwrite('WIND_SD_RSD_monthly.nc','lon', lon)
ncwriteatt('WIND_SD_RSD_monthly.nc','lon','units','degrees_east')
ncwriteatt('WIND_SD_RSD_monthly.nc','lon','long_name','Longitude')
nccreate('WIND_SD_RSD_monthly.nc','WIND_SD_MONTH','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('WIND_SD_RSD_monthly.nc','WIND_SD_MONTH', WIND_SD_MONTH)
nccreate('WIND_SD_RSD_monthly.nc','WIND_RSD_MONTH','Dimensions',{'lon',144,'lat',96},'Format','classic')
ncwrite('WIND_SD_RSD_monthly.nc','WIND_RSD_MONTH', WIND_RSD_MONTH)
ncdisp('WIND_SD_RSD_monthly.nc')
