Python: Added more rotation functions and unit tests.

This commit is contained in:
Don Cross
2019-12-15 21:24:32 -05:00
parent d572edb10f
commit 85d9113f77
4 changed files with 441 additions and 0 deletions

View File

@@ -626,6 +626,20 @@ Equatorial coordinates in the specified frame of reference.
---
<a name="EquatorFromVector"></a>
### EquatorFromVector(vec)
**Given an equatorial vector, calculates equatorial angular coordinates.**
| Type | Parameter | Description |
| --- | --- | --- |
| [`Vector`](#Vector) | `vec` | A vector in an equatorial coordinate system. |
### Returns: Equatorial
Angular coordinates expressed in the same equatorial system as `vec`.
---
<a name="GeoMoon"></a>
### GeoMoon(time)
@@ -962,6 +976,25 @@ A rotation matrix that converts ECL to EQJ.
---
<a name="Rotation_EQD_EQJ"></a>
### Rotation_EQD_EQJ(time)
**Calculates a rotation matrix from equatorial of-date (EQD) to equatorial J2000 (EQJ).**
This is one of the family of functions that returns a rotation matrix
for converting from one orientation to another.
Source: EQD = equatorial system, using equator of the specified date/time.
Target: EQJ = equatorial system, using equator at J2000 epoch.
| Type | Parameter | Description |
| --- | --- | --- |
| [`Time`](#Time) | `time` | The date and time at which the Earth's equator defines the source orientation. |
### Returns: RotationMatrix
A rotation matrix that converts EQD at `time` to EQJ.
---
<a name="Rotation_EQJ_ECL"></a>
### Rotation_EQJ_ECL()
@@ -977,6 +1010,25 @@ A rotation matrix that converts EQJ to ECL.
---
<a name="Rotation_EQJ_EQD"></a>
### Rotation_EQJ_EQD(time)
**Calculates a rotation matrix from equatorial J2000 (EQJ) to equatorial of-date (EQD).**
This is one of the family of functions that returns a rotation matrix
for converting from one orientation to another.
Source: EQJ = equatorial system, using equator at J2000 epoch.
Target: EQD = equatorial system, using equator of the specified date/time.
| Type | Parameter | Description |
| --- | --- | --- |
| [`Time`](#Time) | `time` | The date and time at which the Earth's equator defines the target orientation. |
### Returns: RotationMatrix
A rotation matrix that converts EQJ to EQD at `time`.
---
<a name="Search"></a>
### Search(func, context, t1, t2, dt_tolerance_seconds)
@@ -1334,6 +1386,22 @@ of winter in the southern hemisphere.
---
<a name="SphereFromVector"></a>
### SphereFromVector(vector)
**Converts Cartesian coordinates to spherical coordinates.**
Given a Cartesian vector, returns latitude, longitude, and distance.
| Type | Parameter | Description |
| --- | --- | --- |
| [`Vector`](#Vector) | `vector` | Cartesian vector to be converted to spherical coordinates. |
### Returns: Spherical
Spherical coordinates that are equivalent to the given vector.
---
<a name="SunPosition"></a>
### SunPosition(time)
@@ -1359,3 +1427,37 @@ In fact, the function #Seasons does use this function for that purpose.
### Returns: #EclipticCoordinates
The ecliptic coordinates of the Sun using the Earth's true equator of date.
---
<a name="VectorFromEquator"></a>
### VectorFromEquator(equ, time)
**Given angular equatorial coordinates in `equ`, calculates equatorial vector.**
| Type | Parameter | Description |
| --- | --- | --- |
| [`Equatorial`](#Equatorial) | `equ` | Angular equatorial coordinates to be converted to a vector. |
| [`Time`](#Time) | `time` | The date and time of the observation. This is needed because the returned vector object requires a valid time value when passed to certain other functions. |
### Returns: Vector
A vector in the equatorial system.
---
<a name="VectorFromSphere"></a>
### VectorFromSphere(sphere, time)
**Converts spherical coordinates to Cartesian coordinates.**
Given spherical coordinates and a time at which they are valid,
returns a vector of Cartesian coordinates. The returned value
includes the time, as required by all `Time` objects.
| Type | Parameter | Description |
| --- | --- | --- |
| [`Spherical`](#Spherical) | `sphere` | Spherical coordinates to be converted. |
| [`Time`](#Time) | `time` | The time that should be included in the returned vector. |
### Returns: Vector
The vector form of the supplied spherical coordinates.

View File

@@ -5083,6 +5083,107 @@ def NextLunarApsis(apsis):
raise InternalError()
return next
def VectorFromSphere(sphere, time):
"""Converts spherical coordinates to Cartesian coordinates.
Given spherical coordinates and a time at which they are valid,
returns a vector of Cartesian coordinates. The returned value
includes the time, as required by all `Time` objects.
Parameters
----------
sphere : Spherical
Spherical coordinates to be converted.
time : Time
The time that should be included in the returned vector.
Returns
-------
Vector
The vector form of the supplied spherical coordinates.
"""
radlat = math.radians(sphere.lat)
radlon = math.radians(sphere.lon)
rcoslat = sphere.dist * math.cos(radlat)
return Vector(
rcoslat * math.cos(radlon),
rcoslat * math.sin(radlon),
sphere.dist * math.sin(radlat),
time
)
def VectorFromEquator(equ, time):
"""Given angular equatorial coordinates in `equ`, calculates equatorial vector.
Parameters
----------
equ : Equatorial
Angular equatorial coordinates to be converted to a vector.
time : Time
The date and time of the observation. This is needed because the returned
vector object requires a valid time value when passed to certain other functions.
Returns
-------
Vector
A vector in the equatorial system.
"""
return VectorFromSphere(Spherical(equ.dec, 15.0 * equ.ra, equ.dist), time)
def EquatorFromVector(vec):
"""Given an equatorial vector, calculates equatorial angular coordinates.
Parameters
----------
vec : Vector
A vector in an equatorial coordinate system.
Returns
-------
Equatorial
Angular coordinates expressed in the same equatorial system as `vec`.
"""
sphere = SphereFromVector(vec)
return Equatorial(sphere.lon / 15.0, sphere.lat, sphere.dist)
def SphereFromVector(vector):
"""Converts Cartesian coordinates to spherical coordinates.
Given a Cartesian vector, returns latitude, longitude, and distance.
Parameters
----------
vector : Vector
Cartesian vector to be converted to spherical coordinates.
Returns
-------
Spherical
Spherical coordinates that are equivalent to the given vector.
"""
xyproj = vector.x*vector.x + vector.y*vector.y
dist = math.sqrt(xyproj + vector.z*vector.z)
if xyproj == 0.0:
if vector.z == 0.0:
raise Exception('Zero-length vector not allowed.')
lon = 0.0
if vector.z < 0.0:
lat = -90.0
else:
lat = +90.0
else:
lon = math.degrees(math.atan2(vector.y, vector.x))
if lon < 0.0:
lon += 360.0
lat = math.degrees(math.atan2(vector.z, math.sqrt(xyproj)))
return Spherical(lat, lon, dist)
def InverseRotation(rotation):
"""Calculates the inverse of a rotation matrix.
@@ -5220,3 +5321,48 @@ def Rotation_ECL_EQJ():
[ 0, +c, +s],
[ 0, -s, +c]
])
def Rotation_EQJ_EQD(time):
"""Calculates a rotation matrix from equatorial J2000 (EQJ) to equatorial of-date (EQD).
This is one of the family of functions that returns a rotation matrix
for converting from one orientation to another.
Source: EQJ = equatorial system, using equator at J2000 epoch.
Target: EQD = equatorial system, using equator of the specified date/time.
Parameters
----------
time : Time
The date and time at which the Earth's equator defines the target orientation.
Returns
-------
RotationMatrix
A rotation matrix that converts EQJ to EQD at `time`.
"""
prec = _precession_rot(0.0, time.tt)
nut = _nutation_rot(time, 0)
return CombineRotation(prec, nut)
def Rotation_EQD_EQJ(time):
"""Calculates a rotation matrix from equatorial of-date (EQD) to equatorial J2000 (EQJ).
This is one of the family of functions that returns a rotation matrix
for converting from one orientation to another.
Source: EQD = equatorial system, using equator of the specified date/time.
Target: EQJ = equatorial system, using equator at J2000 epoch.
Parameters
----------
time : Time
The date and time at which the Earth's equator defines the source orientation.
Returns
-------
RotationMatrix
A rotation matrix that converts EQD at `time` to EQJ.
"""
nut = _nutation_rot(time, 1)
prec = _precession_rot(time.tt, 0.0)
return CombineRotation(nut, prec)