What is The Inertial Frame?
In classical mechanics, inertial reference frames are assumed to be time-independent. An Earth-fixed coordinate system can be used as an approximately stationary reference frame for many practical applications.
In my application, I use an FIS1100 MEMS sensor, which incorporates a 3-axis gyroscope and a 3-axis accelerometer, together with an external 3-axis magnetometer.
I define the inertial reference frame as follows: the x-axis points east, the y-axis points toward magnetic north, and the z-axis points downward along the direction of gravity.

Taking into account magnetic variation I had to calculate earth-fixed coordinate from inertial frames. So there are some calculation functions in javascript.
Calculation.eulerAngles = function(x, y, z) {
var r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
var za = Math.acos(z / r);
var xa = Math.atan(y / x);
if(isNaN(za)) za = 0;
if(isNaN(xa)) xa = 0;
return {
r: r,
za: za,
xa: xa
};
}
Here is more detail about Euler Angles.
