From 18251d16a40aaf50c24b411ccff85fbacbfeda83 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Tue, 11 Jun 2019 20:31:22 -0400 Subject: [PATCH] Added another C example program: positions.c. This example demonstrates how to calculate equatorial coordinates and horizontal coordinates of solar system bodies. Added explanatory comments to moonphase.c. Added #defines for MIN_YEAR, MAX_YEAR in astronomy.h. Removed unnecessary code from positions.html; no longer need to calculate geocentric vector before calculating equatorial coordinates. --- demo/browser/positions.html | 1 - demo/c/README.md | 5 ++ demo/c/astro_demo_common.c | 53 +++++++++++++++++++ demo/c/astro_demo_common.h | 7 +++ demo/c/moonphase.c | 3 ++ demo/c/positions.c | 49 +++++++++++++++++ source/c/astronomy.h | 3 ++ windows/examples/positions/positions.vcxproj | 10 ++++ .../positions/positions.vcxproj.filters | 14 +++++ 9 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 demo/c/astro_demo_common.c create mode 100644 demo/c/astro_demo_common.h diff --git a/demo/browser/positions.html b/demo/browser/positions.html index 02dc1c01..1acf0c8f 100644 --- a/demo/browser/positions.html +++ b/demo/browser/positions.html @@ -206,7 +206,6 @@ for (let body of Astronomy.Bodies) { if (body !== 'Earth') { - let gv = Astronomy.GeoVector(body, date); let equ_2000 = Astronomy.Equator(body, date, observer, false, true); let equ_ofdate = Astronomy.Equator(body, date, observer, true, true); let hor = Astronomy.Horizon(date, observer, equ_ofdate.ra, equ_ofdate.dec, 'normal'); diff --git a/demo/c/README.md b/demo/c/README.md index 86bac877..480db04d 100644 --- a/demo/c/README.md +++ b/demo/c/README.md @@ -6,6 +6,11 @@ This example shows how to determine the Moon's current phase, and how to predict when the next few quarter phases will occur. +### [Body Positions](https://github.com/cosinekitty/astronomy/blob/master/demo/c/positions.c) +An example of how to calculate equatorial and horizontal coordinates for all of the major solar system bodies. + +--- + # [API Reference](../../source/c/) Complete documentation for all the functions and types available in the C version of Astronomy Engine. diff --git a/demo/c/astro_demo_common.c b/demo/c/astro_demo_common.c new file mode 100644 index 00000000..7955b400 --- /dev/null +++ b/demo/c/astro_demo_common.c @@ -0,0 +1,53 @@ +#include +#include "astronomy.h" + +int ParseArgs(int argc, const char *argv[], astro_observer_t *observer, astro_time_t *time) +{ + if (argc == 3 || argc == 4) + { + observer->height = 0.0; + + if (1 != sscanf(argv[1], "%lf", &observer->latitude) || + observer->latitude < -90.0 || + observer->latitude > +90.0) + { + fprintf(stderr, "ERROR: Invalid latitude '%s' on command line\n", argv[1]); + return 1; + } + + if (1 != sscanf(argv[2], "%lf", &observer->longitude) || + observer->longitude < -180.0 || + observer->longitude > +180.0) + { + fprintf(stderr, "ERROR: Invalid longitude '%s' on command line\n", argv[2]); + return 1; + } + + if (argc == 4) + { + /* Time is present on command line, so use it. */ + + astro_utc_t utc; + int nscanned = sscanf(argv[3], "%d-%d-%dT%d:%d:%lfZ", + &utc.year, &utc.month, &utc.day, + &utc.hour, &utc.minute, &utc.second); + + if (nscanned != 6) + { + fprintf(stderr, "ERROR: Invalid date/time format in '%s'\n", argv[3]); + return 1; + } + + *time = Astronomy_TimeFromUtc(utc); + } + else + { + /* Time is absent on command line, so use current time. */ + *time = Astronomy_CurrentTime(); + } + return 0; + } + + fprintf(stderr, "USAGE: %s latitude longitude [yyyy-mm-ddThh:mm:ssZ]\n", argv[0]); + return 1; +} diff --git a/demo/c/astro_demo_common.h b/demo/c/astro_demo_common.h new file mode 100644 index 00000000..f5ecc5e4 --- /dev/null +++ b/demo/c/astro_demo_common.h @@ -0,0 +1,7 @@ +#ifndef __ASTRONOMY_DEMO_COMMON_H +#define __ASTRONOMY_DEMO_COMMON_H +#include "astronomy.h" + +int ParseArgs(int argc, const char *argv[], astro_observer_t *observer, astro_time_t *time); + +#endif /* __ASTRONOMY_DEMO_COMMON_H */ diff --git a/demo/c/moonphase.c b/demo/c/moonphase.c index 10476da1..ab8667dc 100644 --- a/demo/c/moonphase.c +++ b/demo/c/moonphase.c @@ -3,6 +3,9 @@ Example C program for Astronomy Engine: https://cosinekitty.github.io/astronomy/ + + This program calculates the Moon's phase for the current date and time. + It also finds the dates and times of the next 10 quarter phase changes. */ #include diff --git a/demo/c/positions.c b/demo/c/positions.c index 3a3882eb..b9195e4c 100644 --- a/demo/c/positions.c +++ b/demo/c/positions.c @@ -1,7 +1,56 @@ +/* + positions.c - by Don Cross - 2019-06-11 + + Example C program for Astronomy Engine: + https://cosinekitty.github.io/astronomy/ + + Given an observer's geographic latitude and longitude, + and an optional date and time, this program displays the + equatorial and horizontal coordinates of the Sun, Moon, and planets. + If the date and time is omitted from the command line, the + program uses the computer's current date and time. +*/ + #include #include "astronomy.h" +#include "astro_demo_common.h" int main(int argc, const char *argv[]) { + int error; + astro_observer_t observer; + astro_time_t time; + astro_body_t body; + astro_equatorial_t equ_2000, equ_ofdate; + astro_horizon_t hor; + + error = ParseArgs(argc, argv, &observer, &time); + if (error) + return error; + + printf("body RA DEC azimuth altitude\n"); + for (body = MIN_BODY; body <= MAX_BODY; ++body) + { + if (body != BODY_EARTH) + { + equ_2000 = Astronomy_Equator(body, time, observer, EQUATOR_J2000, ABERRATION); + if (equ_2000.status != ASTRO_SUCCESS) + { + fprintf(stderr, "ERROR: Astronomy_Equator returned status %d trying to get J2000 coordinates.\n", equ_2000.status); + return 1; + } + + equ_ofdate = Astronomy_Equator(body, time, observer, EQUATOR_OF_DATE, ABERRATION); + if (equ_ofdate.status != ASTRO_SUCCESS) + { + fprintf(stderr, "ERROR: Astronomy_Equator returned status %d trying to get coordinates of date.\n", equ_ofdate.status); + return 1; + } + + hor = Astronomy_Horizon(time, observer, equ_ofdate.ra, equ_ofdate.dec, REFRACTION_NORMAL); + printf("%-7s %6.2lf %6.2lf %7.2lf %8.2lf\n", Astronomy_BodyName(body), equ_2000.ra, equ_2000.dec, hor.azimuth, hor.altitude); + } + } + return 0; } diff --git a/source/c/astronomy.h b/source/c/astronomy.h index aefa2cba..12a51063 100644 --- a/source/c/astronomy.h +++ b/source/c/astronomy.h @@ -193,6 +193,9 @@ astro_body_t; #define MIN_BODY BODY_MERCURY /**< Minimum valid astro_body_t value; useful for iteration. */ #define MAX_BODY BODY_MOON /**< Maximum valid astro_body_t value; useful for iteration. */ +#define MIN_YEAR 1700 /**< Minimum year value supported by Astronomy Engine. */ +#define MAX_YEAR 2200 /**< Maximum year value supported by Astronomy Engine. */ + /** * @brief The location of an observer on (or near) the surface of the Earth. * diff --git a/windows/examples/positions/positions.vcxproj b/windows/examples/positions/positions.vcxproj index d4e280eb..14739d2d 100644 --- a/windows/examples/positions/positions.vcxproj +++ b/windows/examples/positions/positions.vcxproj @@ -75,6 +75,7 @@ Disabled true C:\don\github\astronomy\source\c;%(AdditionalIncludeDirectories) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) @@ -83,6 +84,7 @@ Disabled true C:\don\github\astronomy\source\c;%(AdditionalIncludeDirectories) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) @@ -93,6 +95,7 @@ true true C:\don\github\astronomy\source\c;%(AdditionalIncludeDirectories) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true @@ -107,6 +110,7 @@ true true C:\don\github\astronomy\source\c;%(AdditionalIncludeDirectories) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true @@ -114,7 +118,13 @@ + + + + + + diff --git a/windows/examples/positions/positions.vcxproj.filters b/windows/examples/positions/positions.vcxproj.filters index eea60d9b..21a64aa9 100644 --- a/windows/examples/positions/positions.vcxproj.filters +++ b/windows/examples/positions/positions.vcxproj.filters @@ -18,5 +18,19 @@ Source Files + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + \ No newline at end of file