From 7b7c28bcedefd1e8e140dd6322cfb065eebf69b9 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Sat, 14 Dec 2019 17:20:32 -0500 Subject: [PATCH] C: Added Astronomy_VectorFromEquator and Astronomy_EquatorFromVector. These helper routines in the JavaScript code make sense for C also. --- generate/ctest.c | 37 +++++++--------------- generate/template/astronomy.c | 58 ++++++++++++++++++++++++++++++++++ generate/template/astronomy.js | 2 +- source/c/README.md | 45 ++++++++++++++++++++++++++ source/c/astronomy.c | 58 ++++++++++++++++++++++++++++++++++ source/c/astronomy.h | 4 ++- source/js/README.md | 2 +- source/js/astronomy.js | 2 +- 8 files changed, 179 insertions(+), 29 deletions(-) diff --git a/generate/ctest.c b/generate/ctest.c index bfa647f3..306bdbd5 100644 --- a/generate/ctest.c +++ b/generate/ctest.c @@ -2167,8 +2167,7 @@ static int Test_EQJ_EQD(astro_body_t body) { astro_time_t time; astro_observer_t observer; - astro_equatorial_t eq2000, eqdate; - astro_spherical_t sphere; + astro_equatorial_t eq2000, eqdate, eqcheck; astro_vector_t v2000, vdate, t2000; astro_rotation_t r; double ra_diff, dec_diff, dist_diff, diff; @@ -2184,12 +2183,8 @@ static int Test_EQJ_EQD(astro_body_t body) eqdate = Astronomy_Equator(body, &time, observer, EQUATOR_OF_DATE, ABERRATION); CHECK_STATUS(eqdate); - /* Convert EQJ spherical coordinates to vector. */ - sphere.status = ASTRO_SUCCESS; - sphere.lat = eq2000.dec; - sphere.lon = 15.0 * eq2000.ra; - sphere.dist = eq2000.dist; - v2000 = Astronomy_VectorFromSphere(sphere, time); + /* Convert EQJ angular coordinates to vector. */ + v2000 = Astronomy_VectorFromEquator(eq2000, time); CHECK_STATUS(v2000); /* Find rotation matrix. */ @@ -2200,14 +2195,14 @@ static int Test_EQJ_EQD(astro_body_t body) vdate = Astronomy_RotateVector(r, v2000); CHECK_STATUS(vdate); - /* Convert vector back to spherical coordinates. */ - sphere = Astronomy_SphereFromVector(vdate); - CHECK_STATUS(sphere); + /* Convert vector back to angular coordinates. */ + eqcheck = Astronomy_EquatorFromVector(vdate); + CHECK_STATUS(eqcheck); /* Compare the result with the eqdate. */ - ra_diff = fabs((sphere.lon / 15.0) - eqdate.ra); - dec_diff = fabs(sphere.lat - eqdate.dec); - dist_diff = fabs(sphere.dist - eqdate.dist); + ra_diff = fabs(eqcheck.ra - eqdate.ra); + dec_diff = fabs(eqcheck.dec - eqdate.dec); + dist_diff = fabs(eqcheck.dist - eqdate.dist); printf("Test_EQJ_EQD: %s ra=%0.3lf, dec=%0.3lf, dist=%0.3lf, ra_diff=%lg, dec_diff=%lg, dist_diff=%lg\n", Astronomy_BodyName(body), eqdate.ra, eqdate.dec, eqdate.dist, ra_diff, dec_diff, dist_diff); if (ra_diff > 1.0e-14 || dec_diff > 1.0e-14 || dist_diff > 4.0e-15) { @@ -2240,8 +2235,8 @@ static int Test_EQD_HOR(astro_body_t body) astro_equatorial_t eqd, eqj; astro_horizon_t hor; astro_rotation_t rot; - astro_vector_t vec_eqd, vec_eqj, vec_hor, check_eqd, check_eqj, check_hor; astro_spherical_t sphere; + astro_vector_t vec_eqd, vec_eqj, vec_hor, check_eqd, check_eqj, check_hor; double diff_az, diff_alt, diff; int error; @@ -2253,11 +2248,7 @@ static int Test_EQD_HOR(astro_body_t body) hor = Astronomy_Horizon(&time, observer, eqd.ra, eqd.dec, REFRACTION_NORMAL); /* Calculate the position of the body as an equatorial vector of date. */ - sphere.status = ASTRO_SUCCESS; - sphere.dist = eqd.dist; - sphere.lat = eqd.dec; - sphere.lon = 15.0 * eqd.ra; - CHECK_VECTOR(vec_eqd, Astronomy_VectorFromSphere(sphere, time)); + CHECK_VECTOR(vec_eqd, Astronomy_VectorFromEquator(eqd, time)); /* Calculate rotation matrix to convert equatorial J2000 vector to horizontal vector. */ rot = Astronomy_Rotation_EQD_HOR(time, observer); @@ -2306,11 +2297,7 @@ static int Test_EQD_HOR(astro_body_t body) /* Exercise HOR to EQJ translation. */ CHECK_EQU(eqj, Astronomy_Equator(body, &time, observer, EQUATOR_J2000, ABERRATION)); - sphere.status = ASTRO_SUCCESS; - sphere.dist = eqj.dist; - sphere.lat = eqj.dec; - sphere.lon = 15.0 * eqj.ra; - CHECK_VECTOR(vec_eqj, Astronomy_VectorFromSphere(sphere, time)); + CHECK_VECTOR(vec_eqj, Astronomy_VectorFromEquator(eqj, time)); rot = Astronomy_Rotation_HOR_EQJ(time, observer); CHECK_ROTMAT(rot); diff --git a/generate/template/astronomy.c b/generate/template/astronomy.c index 55698cd5..51bedcfe 100644 --- a/generate/template/astronomy.c +++ b/generate/template/astronomy.c @@ -4210,6 +4210,64 @@ astro_spherical_t Astronomy_SphereFromVector(astro_vector_t vector) return sphere; } + +/** + * @brief + * Given angular equatorial coordinates in `equ`, calculates equatorial vector. + * + * @param equ + * An object that contains angular equatorial coordinates to be converted to a vector. + * + * @param 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. + * + * @return + * A vector in the equatorial system. + */ +astro_vector_t Astronomy_VectorFromEquator(astro_equatorial_t equ, astro_time_t time) +{ + astro_spherical_t sphere; + + if (equ.status != ASTRO_SUCCESS) + return VecError(ASTRO_INVALID_PARAMETER, time); + + sphere.status = ASTRO_SUCCESS; + sphere.lat = equ.dec; + sphere.lon = 15.0 * equ.ra; /* convert sidereal hours to degrees */ + sphere.dist = equ.dist; + + return Astronomy_VectorFromSphere(sphere, time); +} + + +/** + * Given an equatorial vector, calculates equatorial angular coordinates. + * + * @param vector + * A vector in an equatorial coordinate system. + * + * @return + * Angular coordinates expressed in the same equatorial system as `vec`. + */ +astro_equatorial_t Astronomy_EquatorFromVector(astro_vector_t vector) +{ + astro_equatorial_t equ; + astro_spherical_t sphere; + + sphere = Astronomy_SphereFromVector(vector); + if (sphere.status != ASTRO_SUCCESS) + return EquError(sphere.status); + + equ.status = ASTRO_SUCCESS; + equ.dec = sphere.lat; + equ.ra = sphere.lon / 15.0; /* convert degrees to sidereal hours */ + equ.dist = sphere.dist; + + return equ; +} + + static double ToggleAzimuthDirection(double az) { az = 360.0 - az; diff --git a/generate/template/astronomy.js b/generate/template/astronomy.js index c8ec3e6d..21e492a7 100644 --- a/generate/template/astronomy.js +++ b/generate/template/astronomy.js @@ -3354,7 +3354,7 @@ Astronomy.VectorFromSphere = function(sphere, time) { } /** - * Given angular equatorial coordinates in `sphere`, calculates equatorial vector. + * Given angular equatorial coordinates in `equ`, calculates equatorial vector. * * @param {Astronomy.EquatorialCoordinates} equ * An object that contains angular equatorial coordinates to be converted to a vector. diff --git a/source/c/README.md b/source/c/README.md index 82298307..a749cea7 100644 --- a/source/c/README.md +++ b/source/c/README.md @@ -354,6 +354,28 @@ Correction for aberration is optional, using the `aberration` parameter. +--- + + +### Astronomy_EquatorFromVector(vector) ⇒ [`astro_equatorial_t`](#astro_equatorial_t) + + + +Given an equatorial vector, calculates equatorial angular coordinates. + + + +**Returns:** Angular coordinates expressed in the same equatorial system as `vec`. + + + +| Type | Parameter | Description | +| --- | --- | --- | +| [`astro_vector_t`](#astro_vector_t) | `vector` | A vector in an equatorial coordinate system. | + + + + --- @@ -1558,6 +1580,29 @@ After calculating the date and time of an astronomical event in the form of an [ +--- + + +### Astronomy_VectorFromEquator(equ, time) ⇒ [`astro_vector_t`](#astro_vector_t) + +**Given angular equatorial coordinates in `equ`, calculates equatorial vector.** + + + + + +**Returns:** A vector in the equatorial system. + + + +| Type | Parameter | Description | +| --- | --- | --- | +| [`astro_equatorial_t`](#astro_equatorial_t) | `equ` | An object that contains angular equatorial coordinates to be converted to a vector. | +| [`astro_time_t`](#astro_time_t) | `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. | + + + + --- diff --git a/source/c/astronomy.c b/source/c/astronomy.c index feaa6125..5b9e3b19 100644 --- a/source/c/astronomy.c +++ b/source/c/astronomy.c @@ -5449,6 +5449,64 @@ astro_spherical_t Astronomy_SphereFromVector(astro_vector_t vector) return sphere; } + +/** + * @brief + * Given angular equatorial coordinates in `equ`, calculates equatorial vector. + * + * @param equ + * An object that contains angular equatorial coordinates to be converted to a vector. + * + * @param 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. + * + * @return + * A vector in the equatorial system. + */ +astro_vector_t Astronomy_VectorFromEquator(astro_equatorial_t equ, astro_time_t time) +{ + astro_spherical_t sphere; + + if (equ.status != ASTRO_SUCCESS) + return VecError(ASTRO_INVALID_PARAMETER, time); + + sphere.status = ASTRO_SUCCESS; + sphere.lat = equ.dec; + sphere.lon = 15.0 * equ.ra; /* convert sidereal hours to degrees */ + sphere.dist = equ.dist; + + return Astronomy_VectorFromSphere(sphere, time); +} + + +/** + * Given an equatorial vector, calculates equatorial angular coordinates. + * + * @param vector + * A vector in an equatorial coordinate system. + * + * @return + * Angular coordinates expressed in the same equatorial system as `vec`. + */ +astro_equatorial_t Astronomy_EquatorFromVector(astro_vector_t vector) +{ + astro_equatorial_t equ; + astro_spherical_t sphere; + + sphere = Astronomy_SphereFromVector(vector); + if (sphere.status != ASTRO_SUCCESS) + return EquError(sphere.status); + + equ.status = ASTRO_SUCCESS; + equ.dec = sphere.lat; + equ.ra = sphere.lon / 15.0; /* convert degrees to sidereal hours */ + equ.dist = sphere.dist; + + return equ; +} + + static double ToggleAzimuthDirection(double az) { az = 360.0 - az; diff --git a/source/c/astronomy.h b/source/c/astronomy.h index 78be064d..24435b5d 100644 --- a/source/c/astronomy.h +++ b/source/c/astronomy.h @@ -616,8 +616,10 @@ astro_rotation_t Astronomy_InverseRotation(astro_rotation_t rotation); astro_rotation_t Astronomy_CombineRotation(astro_rotation_t a, astro_rotation_t b); astro_vector_t Astronomy_VectorFromSphere(astro_spherical_t sphere, astro_time_t time); astro_spherical_t Astronomy_SphereFromVector(astro_vector_t vector); -astro_spherical_t Astronomy_HorizonFromVector(astro_vector_t vector, astro_refraction_t refraction); +astro_vector_t Astronomy_VectorFromEquator(astro_equatorial_t equ, astro_time_t time); +astro_equatorial_t Astronomy_EquatorFromVector(astro_vector_t vector); astro_vector_t Astronomy_VectorFromHorizon(astro_spherical_t sphere, astro_time_t time, astro_refraction_t refraction); +astro_spherical_t Astronomy_HorizonFromVector(astro_vector_t vector, astro_refraction_t refraction); astro_vector_t Astronomy_RotateVector(astro_rotation_t rotation, astro_vector_t vector); astro_rotation_t Astronomy_Rotation_EQD_EQJ(astro_time_t time); diff --git a/source/js/README.md b/source/js/README.md index aaebb73e..db041305 100644 --- a/source/js/README.md +++ b/source/js/README.md @@ -1149,7 +1149,7 @@ includes the time, as required by AstroTime. ### Astronomy.VectorFromEquator(equ, time) ⇒ [Vector](#Astronomy.Vector) -Given angular equatorial coordinates in `sphere`, calculates equatorial vector. +Given angular equatorial coordinates in `equ`, calculates equatorial vector. **Kind**: static method of [Astronomy](#Astronomy) **Returns**: [Vector](#Astronomy.Vector) - A vector in the equatorial system. diff --git a/source/js/astronomy.js b/source/js/astronomy.js index dc96b7bc..2a8c2819 100644 --- a/source/js/astronomy.js +++ b/source/js/astronomy.js @@ -4375,7 +4375,7 @@ Astronomy.VectorFromSphere = function(sphere, time) { } /** - * Given angular equatorial coordinates in `sphere`, calculates equatorial vector. + * Given angular equatorial coordinates in `equ`, calculates equatorial vector. * * @param {Astronomy.EquatorialCoordinates} equ * An object that contains angular equatorial coordinates to be converted to a vector.