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.
This commit is contained in:
Don Cross
2021-06-14 12:29:33 -04:00
parent 9287fedec1
commit b9daddd04c
7 changed files with 100 additions and 10 deletions

View File

@@ -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

View File

@@ -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"

View File

@@ -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

76
demo/python/galactic.py Executable file
View File

@@ -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)

View File

@@ -9,3 +9,4 @@ horizon.txt
lunar_eclipse.txt
lunar_angles.txt
jupiter_moons.txt
galactic.txt

View File

@@ -0,0 +1 @@
altitude = 74.647, azimuth = 178.955

View File

@@ -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