%                togrid3.m
%               for constructing ascii flat file with lon,lat,u(ind),v(ind)
%               where each of the variables is subsampled by 2

load latlon
load ghfactor.cal
im=sqrt(-1);
ghval=[1 2 3 4 5 6 7 9];
mean=[];
um2=[];
vm2=[];
um4=[];
vm4=[];
um6=[];
vm6=[];
for k=1:8
	disp(ghval(k))
	m=[];
	eval(concat('load gh',int2str(ghval(k)),'cof.tid'))
	for i=1:7
		uarray(:,i)=subsamp(2,2,u0coef(:,i));
		varray(:,i)=subsamp(2,2,v0coef(:,i));
	end
	[i,j]=size(mean);
	[inew,j]=size(uarray);
     ind=[i+1:i+inew];
	mean(ind,3:4)=[uarray(:,1) varray(:,1)];
     um2(ind,3)=abs(uarray(:,2)+im*uarray(:,3))/mag(i);
     um2(ind,4)=180/pi*imag(log(uarray(:,2)+im*uarray(:,3)))-phase(i);
     vm2(ind,3)=abs(uarray(:,2)+im*uarray(:,3))/mag(i);
     vm2(ind,4)=180/pi*imag(log(uarray(:,2)+im*uarray(:,3)))-phase(i);
     um4(ind,3)=abs(uarray(:,2)+im*uarray(:,3))/mag(i);
     um4(ind,4)=180/pi*imag(log(uarray(:,2)+im*uarray(:,3)))-phase(i)*2;
     vm4(ind,3)=abs(uarray(:,2)+im*uarray(:,3))/mag(i);
     vm4(ind,4)=180/pi*imag(log(uarray(:,2)+im*uarray(:,3)))-phase(i)*2;
     um6(ind,3)=abs(uarray(:,2)+im*uarray(:,3))/mag(i);
     um6(ind,4)=180/pi*imag(log(uarray(:,2)+im*uarray(:,3)))-phase(i)*3;
     vm6(ind,3)=abs(uarray(:,2)+im*uarray(:,3))/mag(i);
     vm6(ind,4)=180/pi*imag(log(uarray(:,2)+im*uarray(:,3)))-phase(i)*3;
end
mean(1:2,:)=latlon;
um2(1:2,:)=latlon;
vm2(1:2,:)=latlon;
um4(1:2,:)=latlon;
vm4(1:2,:)=latlon;
um6(1:2,:)=latlon;
vm6(1:2,:)=latlon;

