OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
mtoq.py
Go to the documentation of this file.
1 def mtoq(rm):
2  # Convert direction cosine matrix to equivalent quaternion
3  # input: rm
4  # output: q
5  # Ported from mtoq.pro by Fred Patt
6  # Liang Hong, 2/18/2020
7 
8  import numpy as np
9 
10  q = np.zeros(4)
11  e = np.zeros(3)
12 
13  # Compute Euler angle
14  cphi = (rm[0,0]+rm[1,1]+rm[2,2]-1.0)/2.0
15  if (np.abs(cphi) < 0.98):
16  phi = np.arccos(cphi)
17  else:
18  ssphi = ((rm[1,0]-rm[0,1])**2 + (rm[0,2]-rm[2,0])**2 + (rm[2,1]-rm[1,2])**2)/4.0
19  phi = np.arcsin(np.sqrt(ssphi))
20  if (cphi < 0):
21  phi = np.pi - phi
22 
23  # Compute Euler axis
24  e[0] = (rm[2,1]-rm[1,2])/(np.sin(phi)*2.0)
25  e[1] = (rm[0,2]-rm[2,0])/(np.sin(phi)*2.0)
26  e[2] = (rm[1,0]-rm[0,1])/(np.sin(phi)*2.0)
27  e = e/np.sqrt(np.sum(e*e))
28 
29  # Compute quaternion
30  q[0] = e[0]*np.sin(phi/2.0)
31  q[1] = e[1]*np.sin(phi/2.0)
32  q[2] = e[2]*np.sin(phi/2.0)
33  q[3] = np.cos(phi/2.0)
34 
35  return q
def mtoq(rm)
Definition: mtoq.py:1