From 86fde0d5cdec8570f991180edff5f0ee00c747e2 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Sun, 12 Feb 2023 12:03:43 -0500 Subject: [PATCH] C demo: Calculate true solar time from the Sun's hour angle. --- demo/c/README.md | 3 +++ demo/c/build | 2 +- demo/c/correct/solar_time.txt | 1 + demo/c/demotest | 1 + demo/c/solar_time.c | 49 +++++++++++++++++++++++++++++++++++ demo/c/test/.gitignore | 1 + 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 demo/c/correct/solar_time.txt create mode 100644 demo/c/solar_time.c diff --git a/demo/c/README.md b/demo/c/README.md index fa9747d5..e258b81c 100644 --- a/demo/c/README.md +++ b/demo/c/README.md @@ -39,6 +39,9 @@ Shows how to calculate sunrise, sunset, moonrise, and moonset times. ### [Seasons](seasons.c) Calculates the equinoxes and solstices for a given calendar year. +### [Solar Time](solar_time.c) +An example of how to use the Sun's hour angle to calculate true solar time. + ### [Triangulate](triangulate.c) Given the geographic coordinates of two observers, and angular directions they are looking in, determines geographic coordinates diff --git a/demo/c/build b/demo/c/build index c44f6358..03405589 100755 --- a/demo/c/build +++ b/demo/c/build @@ -14,7 +14,7 @@ else fi mkdir -p bin -for name in gravity galactic camera moonphase positions linux_riseset riseset seasons culminate horizon lunar_eclipse triangulate ecliptic_vector; do +for name in solar_time gravity galactic camera moonphase positions linux_riseset riseset seasons culminate horizon lunar_eclipse triangulate ecliptic_vector; do rm -f bin/${name} echo "Compiling ${name}.c" gcc ${BUILDOPT} -Wall -Werror -o bin/${name} -I../../source/c ../../source/c/astronomy.c astro_demo_common.c ${name}.c -lm || diff --git a/demo/c/correct/solar_time.txt b/demo/c/correct/solar_time.txt new file mode 100644 index 00000000..f0e2d576 --- /dev/null +++ b/demo/c/correct/solar_time.txt @@ -0,0 +1 @@ +True solar time = 11.6286 hours (11:37:42.979) diff --git a/demo/c/demotest b/demo/c/demotest index 68395427..9cf6a8c1 100755 --- a/demo/c/demotest +++ b/demo/c/demotest @@ -16,6 +16,7 @@ rm -f test/*.txt mkdir -p test [[ "$1" == "nobuild" ]] || ./build || Fail "Error building example programs." +TestDemo solar_time +38.88 -77.03 2023-02-12T17:00:00Z TestDemo camera 29 -81 2021-03-22T02:45:00Z TestDemo moonphase 2019-06-15T09:15:32.987Z TestDemo positions +45.6 -90.7 2018-11-30T17:55:07.234Z diff --git a/demo/c/solar_time.c b/demo/c/solar_time.c new file mode 100644 index 00000000..d178d11e --- /dev/null +++ b/demo/c/solar_time.c @@ -0,0 +1,49 @@ +/* + solar_time.c - by Don Cross - 2019-06-11 + + Example C program for Astronomy Engine: + https://github.com/cosinekitty/astronomy + + Given an observer's geographic latitude and longitude, + and an optional date and time, this program displays + true solar time for that observer and time. +*/ + +#include +#include +#include "astro_demo_common.h" + +int main(int argc, const char *argv[]) +{ + int error; + astro_observer_t observer; + astro_time_t time; + astro_func_result_t ha; + double solarTimeHours; + int hour, minute, second, milli; + + error = ParseArgs(argc, argv, &observer, &time); + if (error) + return error; + + ha = Astronomy_HourAngle(BODY_SUN, &time, observer); + if (ha.status != ASTRO_SUCCESS) + { + printf("ERROR %d in Astronomy_HourAngle().\n", ha.status); + return 1; + } + + solarTimeHours = fmod(ha.value + 12.0, 24.0); + + milli = (int)round(solarTimeHours * 3600000.0); + second = milli / 1000; + milli %= 1000; + minute = second / 60; + second %= 60; + hour = minute / 60; + minute %= 60; + hour %= 24; + + printf("True solar time = %0.4lf hours (%02d:%02d:%02d.%03d)\n", solarTimeHours, hour, minute, second, milli); + return 0; +} diff --git a/demo/c/test/.gitignore b/demo/c/test/.gitignore index c231da87..e6e1e6ad 100644 --- a/demo/c/test/.gitignore +++ b/demo/c/test/.gitignore @@ -12,3 +12,4 @@ galactic.txt triangulate.txt gravity.txt worldmap.txt +solar_time.txt