地心、日心和几何坐标转换包
ai.cs的Python项目详细描述
欢迎使用ai.cs
ai.cs是python中的坐标转换包。它提供了在几何坐标(笛卡尔坐标、球面坐标和圆柱坐标)之间以及在通常用于航天器测量的地心坐标和日心坐标系统之间转换数据的功能。该包目前还支持通过rotation matrices来旋转数据。航天器坐标系之间的转换是作为绑定到CXFORM库的python实现的。
完整的文档可在aics.rtfd.io找到。
开始
本教程将指导您了解ai.cs的基本用法。
安装
ai.cs是为Python3开发的,所以请确保您有一个有效的版本。该包与cxform库的c部分一起分发,cxform库在安装期间自动编译。因此,请确保系统中有一个运行正常的编译器,例如gcc。
如果满足上述要求,请使用python包管理器安装包:
$ pip install ai.cs
几何坐标
具有笛卡尔坐标和球面坐标之间以及笛卡尔坐标和圆柱坐标之间转换功能的ai.cs船舶:
importnumpyasnpfromaiimportcs# cartesian to sphericalr,theta,phi=cs.cart2sp(x=1,y=1,z=1)# spherical to cartesianx,y,z=cs.sp2cart(r=1,theta=np.pi/4,phi=np.pi/4)# cartesian to cylindricalr,phi,z=cs.cart2cyl(x=1,y=1,z=1)# cylindrical to cartesianx,y,z=cs.cyl2cart(r=1,phi=np.pi/2,z=1)
大多数函数都支持标量和numpy数组作为输入:
importnumpyasnpfromaiimportcs# converting spherical spiral from spherical to cartesian coordinatesx,y,z=cs.sp2cart(r=np.ones(100),theta=np.linspace(-np.pi/2,np.pi/2,100),phi=np.linspace(0,np.pi*6,100))
航天器坐标
cs提供了到cxform库的python绑定,以便在各种地心和日心笛卡尔坐标系之间进行转换。例如,下面的代码执行从GSE到HEEQ坐标系的数据转换:
fromdatetimeimportdatetimefromastropyimportunitsasufromaiimportcs# converting (0.5, 0.5, 0.5) AU location from GSE to HEEQ at current timex,y,z=cs.cxform('GSE','HEEQ',datetime.now(),x=u.au.to(u.m,0.5),y=u.au.to(u.m,0.5),z=u.au.to(u.m,0.5))
标量和numpy数组都支持作为输入:
fromdatetimeimportdatetime,timedeltafromastropyimportunitsasufromaiimportcs# converting circular orbit at 1 AU from cylindrical to cartesian coordinatesr=np.ones(365)*u.au.to(u.m,1)phi=np.linspace(0,np.pi*2,365)z=np.zeros(365)x_HEE,y_HEE,z_HEE=cs.cyl2cart(r,phi,z)# converting HEE to HEEQx_HEEQ,y_HEEQ,z_HEEQ=cs.cxform('HEE','HEEQ',[datetime(2016,1,1)+timedelta(days=d)fordinrange(365)],x=x_HEE,y=y_HEE,z=z_HEE)
几何变换
目前,ai.cs只提供一种几何变换——旋转。旋转是通过围绕x、y和z轴的右手旋转的3d变换矩阵来执行的:
importnumpyasnpfromaiimportcs# get (3x3) rotation matrix for rotation by pi/4 around X axisTx=cs.mx_rot_x(gamma=np.pi/4)# get (3x3) rotation matrix for rotation by -pi/4 around Y axisTy=cs.mx_rot_y(theta=-np.pi/4)# get (3x3) rotation matrix for rotation by pi/2 around Z axisTz=cs.mx_rot_z(phi=np.pi/2)
也可以一次性构造复合旋转的旋转矩阵:
importnumpyasnpfromaiimportcs# get matrix for right-handed rotation around X, Y and Z axes (exactly in this order)T=cs.mx_rot(theta=np.pi/4,phi=np.pi/4,gamma=np.pi/4)# get matrix for right-handed rotation around Z, Y and X axes (exactly in this order)T_reverse=cs.mx_rot_reverse(theta-np.pi/4,phi=-np.pi/4,gamma=-np.pi/4)# T_reverse effectively reverses the transformation described by T in this case
旋转矩阵可以通过以下方式应用于笛卡尔坐标系中的数据:
importnumpyasnpfromaiimportcs# a cube with the side length 2x=np.array([1,1,1,1,-1,-1,-1,-1])y=np.array([1,1,-1,-1,1,1,-1,-1])z=np.array([1,-1,1,-1,1,1,-1,-1])# rotate cube by pi/4 around each axisT=cs.mx_rot(theta=np.pi/4,phi=np.pi/4,gamma=np.pi/4)x,y,z=cs.mx_apply(T,x,y,z)