From b9daddd04cd885f09455bd98f55d38197877ecfe Mon Sep 17 00:00:00 2001 From: Don Cross Date: Mon, 14 Jun 2021 12:29:33 -0400 Subject: [PATCH] Added demo program galactic.py. This is the Python version of a demo to convert galactic coordinates to horizontal coordinates for a given time and geographic location. --- demo/c/README.md | 2 +- demo/c/galactic.c | 18 +++---- demo/python/README.md | 8 +++ demo/python/galactic.py | 76 +++++++++++++++++++++++++++ demo/python/test/.gitignore | 1 + demo/python/test/galactic_correct.txt | 1 + demo/python/test/test | 4 ++ 7 files changed, 100 insertions(+), 10 deletions(-) create mode 100755 demo/python/galactic.py create mode 100644 demo/python/test/galactic_correct.txt diff --git a/demo/c/README.md b/demo/c/README.md index 6aa22d1e..21d4a519 100644 --- a/demo/c/README.md +++ b/demo/c/README.md @@ -15,7 +15,7 @@ This could be useful for backyard radio astronomers who know the galactic coordinates of a distant radio source and want to aim a radio dish at it. Given the galactic coordinates, the geographic coordinates of the observer, and the date and time of the observation, this program shows how to -convert the altitude and azimuth to aim at the radio source. +obtain the altitude and azimuth to aim the dish at the radio source. ### [Horizon Intersection](horizon.c) This is a more advanced example. It shows how to use coordinate diff --git a/demo/c/galactic.c b/demo/c/galactic.c index 401030a3..4a14de1f 100644 --- a/demo/c/galactic.c +++ b/demo/c/galactic.c @@ -16,19 +16,19 @@ int GalaticToHorizontal( - astro_time_t time, - astro_observer_t observer, - double glat, - double glon, - double *altitude, + astro_time_t time, + astro_observer_t observer, + double glat, + double glon, + double *altitude, double *azimuth) { astro_rotation_t rot, adjust_rot; astro_spherical_t gsphere, hsphere; astro_vector_t gvec, hvec; - /* - Calculate a rotation matrix that converts + /* + Calculate a rotation matrix that converts galactic coordinates to J2000 equatorial coordinates. */ rot = Astronomy_Rotation_GAL_EQJ(); @@ -36,7 +36,7 @@ int GalaticToHorizontal( /* Adjust the rotation matrix to convert galatic to horizontal (HOR). */ - adjust_rot = Astronomy_Rotation_EQJ_HOR(time, observer); + adjust_rot = Astronomy_Rotation_EQJ_HOR(time, observer); rot = Astronomy_CombineRotation(rot, adjust_rot); /* @@ -89,7 +89,7 @@ int main(int argc, const char *argv[]) if (argc < 5 || argc > 6) { - fprintf(stderr, + fprintf(stderr, "\n" "USAGE: galactic olat olon glat glon [yyyy-mm-ddThh:mm:ssZ]\n" "\n" diff --git a/demo/python/README.md b/demo/python/README.md index 62b9f78f..03d7ee07 100644 --- a/demo/python/README.md +++ b/demo/python/README.md @@ -25,6 +25,14 @@ Culmination is also the moment a body crosses the *meridian*, the imaginary semi in the sky that passes from due north on the horizon, through the zenith (straight up), and then toward due south on the horizon. +### [Galactic to Horizontal Converter](galactic.py) +A demonstration of how to convert galactic coordinates to horizontal coordinates. +This could be useful for backyard radio astronomers who know the galactic +coordinates of a distant radio source and want to aim a radio dish at it. +Given the galactic coordinates, the geographic coordinates of the observer, +and the date and time of the observation, this program shows how to +obtain the altitude and azimuth to aim the dish at the radio source. + ### [Horizon Intersection](horizon.py) This is a more advanced example. It shows how to use coordinate transforms to find where the ecliptic intersects with an observer's diff --git a/demo/python/galactic.py b/demo/python/galactic.py new file mode 100755 index 00000000..3b84b451 --- /dev/null +++ b/demo/python/galactic.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 +# +# galactic.py - by Don Cross - 2021-06-14 +# +# Example Python program for Astronomy Engine: +# https://github.com/cosinekitty/astronomy +# +# Demo of converting galactic coordinates to horizontal coordinates. +# + +import sys +import math +from astronomy import * + +UsageText = r''' +USAGE: galactic olat olon glat glon [yyyy-mm-ddThh:mm:ssZ] + +where + + olat = observer's latitude on the Earth + olon = observer's longitude on the Earth + glat = IAU 1958 galatic latitude of the target + glon = IAU 1958 galatic longitude of the target + yyyy-mm-ddThh:mm:ssZ = optional UTC date/time + +Given the galactic coordinates of a point source in the sky, +this program calculates horizontal aiming coordinates for an +observer on or near the Earth's surface. + +If the date/time is given on the command line, it is used. +Otherwise, the computer's current date/time is used. +''' + + +def GalacticToHorizontal(time, observer, glat, glon): + # Calculate a matrix that converts galactic coordinates + # to J2000 equatorial coordinates. + rot = Rotation_GAL_EQJ() + + # Adjust the rotation matrix to convert galactic to horizontal. + adjust_rot = Rotation_EQJ_HOR(time, observer) + rot = CombineRotation(rot, adjust_rot) + + # Convert the galactic coordinates from angles to a unit vector. + gsphere = Spherical(glat, glon, 1.0) + gvec = VectorFromSphere(gsphere, time) + + # Use the rotation matrix to convert the galactic vector to a horizontal vector. + hvec = RotateVector(rot, gvec) + + # Convert the horizontal vector back to angular coordinates. + # Assume this is a radio source (not optical), do not correct for refraction. + hsphere = HorizonFromVector(hvec, Refraction.Airless) + return hsphere.lat, hsphere.lon + + +if __name__ == '__main__': + if len(sys.argv) not in [5, 6]: + print(UsageText) + sys.exit(1) + + olat = float(sys.argv[1]) + olon = float(sys.argv[2]) + observer = Observer(olat, olon) + + glat = float(sys.argv[3]) + glon = float(sys.argv[4]) + + if len(sys.argv) > 5: + time = Time.Parse(sys.argv[5]) + else: + time = Time.Now() + + altitude, azimuth = GalacticToHorizontal(time, observer, glat, glon) + print('altitude = {:10.3f}, azimuth = {:10.3f}'.format(altitude, azimuth)) + sys.exit(0) diff --git a/demo/python/test/.gitignore b/demo/python/test/.gitignore index 49df2c88..5a1c8f90 100644 --- a/demo/python/test/.gitignore +++ b/demo/python/test/.gitignore @@ -9,3 +9,4 @@ horizon.txt lunar_eclipse.txt lunar_angles.txt jupiter_moons.txt +galactic.txt diff --git a/demo/python/test/galactic_correct.txt b/demo/python/test/galactic_correct.txt new file mode 100644 index 00000000..d7b74ea3 --- /dev/null +++ b/demo/python/test/galactic_correct.txt @@ -0,0 +1 @@ +altitude = 74.647, azimuth = 178.955 diff --git a/demo/python/test/test b/demo/python/test/test index 53cec52f..87db5e7c 100755 --- a/demo/python/test/test +++ b/demo/python/test/test @@ -51,5 +51,9 @@ echo "Testing example: lunar_angles.py" ./lunar_angles.py 2021-05-15 > test/lunar_angles.txt || Fail "Error running lunar_angles.py." diff test/lunar_angles.txt test/lunar_angles_correct.txt || Fail "Error comparing lunar_angles.py output." +echo "Testing example: galactic.py" +./galactic.py 38.92056 -77.0658 22.793498 197.070510 2025-04-06T00:00:00Z > test/galactic.txt || Fail "Error running galactic.py." +diff test/galactic.txt test/galactic_correct.txt || Fail "Error comparing galactic.py output." + echo "PASS: Python examples" exit 0