#！/bin/bash
stn=$1
alpha=$2
str=${stn}.g${alpha}

awk -v pai=3.141592653 '{
	if(FNR==1)
	{
		n++; lam[n]=$(NF-3); gam[n]=$(NF-2); fai[n]=$(NF-1); tau[n]=$NF;
		sinL+=sin($(NF-3)*pai/180); 
		cosL+=cos($(NF-3)*pai/180); 
		sinG+=sin($(NF-2)*pai/180); 
		cosG+=cos($(NF-2)*pai/180); 
		sinF+=sin($(NF-1)*pai/180); 
		cosF+=cos($(NF-1)*pai/180);
		tsum+=$NF; 
	}

}END{
	Lmean=atan2(sinL, cosL)*180/pai; Lmean=(Lmean<0)?(360+Lmean):Lmean
	Gmean=atan2(sinG, cosG)*180/pai
	Fmean=atan2(sinF, cosF)*180/pai; Fmean=(Fmean<0)?(360+Fmean):Fmean
	Tmean=tsum/(n-1)
	for(i=1;i<n;i++)
	{
		sinLdiff=sqrt((sin((lam[i]-Lmean)*pai/180))^2)
		degLdiff=atan2(sinLdiff, sqrt(1-sinLdiff^2))*180/pai
		Lsum += degLdiff
		sinGdiff=sqrt((sin((gam[i]-Gmean)*pai/180))^2)
		degGdiff=atan2(sinGdiff, sqrt(1-sinGdiff^2))*180/pai
		Gsum += degGdiff
		sinFdiff=sqrt((sin((fai[i]-Fmean)*pai/180))^2)
		degFdiff=atan2(sinFdiff, sqrt(1-sinFdiff^2))*180/pai
		Fsum += degFdiff
		Tsum += (tau[i]-Tmean)^2 
	}
	printf("%d\t%d\t%d\t%.1f\t%d\t%.1f\t%4.2f\t%4.2f\n", Lmean, Lsum/(n-1), Gmean, Gsum/(n-1), Fmean, Fsum/(n-1), Tmean, sqrt(Tsum/(n-1)))
}' LGFT_${str}_*.grd




